aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/disk_backend/Plugin/Swap.pm
blob: 3953dd443808b3a455bfa59e41dd66cf66eb3d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# vim: set et ts=4 sw=4:
package ManaTools::Shared::disk_backend::Plugin::Swap;

#============================================================= -*-perl-*-

=head1 NAME

    ManaTools::Shared::disk_backend::Plugin::Swap - disks object

=head1 SYNOPSIS

    use ManaTools::Shared::disk_backend::Plugin::Swap;

    my $db_man = ManaTools::Shared::disk_backend::Plugin::Swap->new($parent);
    ...


=head1 DESCRIPTION

    This plugin is a disk plugin for the backend to manadisk

=head1 SUPPORT

    You can find documentation for this plugin with the perldoc command:

    perldoc ManaTools::Shared::disk_backend::Plugin::Swap


=head1 AUTHOR

    Maarten Vanraes <alien@rmail.be>

=head1 COPYRIGHT and LICENSE

Copyright (c) 2015 Maarten Vanraes <alien@rmail.be>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=head1 METHODS

=cut

use Moose;

use File::Basename;

extends 'ManaTools::Shared::disk_backend::Plugin';


has '+dependencies' => (
    default => sub {
        return ['Partition'];
    }
);

has '+tools' => (
    default => sub {
        return {
            'swaplabel' => '/usr/sbin/swaplabel',
            'swapon' => '/usr/sbin/swapon',
            'swapoff' => '/usr/sbin/swapoff',
        };
    }
);

#=============================================================

=head2 probe

=head3 OUTPUT

    0 if failed, 1 if success

=head3 DESCRIPTION

    this method will probe the current swaps

=cut

#=============================================================
override ('probe', sub {
    my $self = shift;
    # check current swaps and create a Swap Part for each one
    # TODO: find the in device (create if needed?)
    open F, '</proc/swaps' or return 0;
    # skip columns line
    <F>;
    while (my $line = <F>) {
        my @fields = split(/[ \t\r\n]+/, $line);

        # look or create the part
        my $part = $self->parent->trypart(ManaTools::Shared::disk_backend::Part->LoadedState, sub {
            my $part = shift;
            my $parameters = shift;
            return ($part->path() eq $parameters->{path});
        }, 'Swap', {path => $fields[0], plugin => $self, loaded => undef, saved => undef});

        # look for the parent part if not set
        if (!$part->has_link(undef, 'parent')) {
            my @stat = stat($fields[0]);
            if (($stat[2] >> 13) == 3) {
                my $dev = $stat[6];
                my $minor = $dev % 256;
                my $major = int (($dev - $minor) / 256);
                my @parents = $self->parent->findpartprop(undef, 'dev', $major .':'. $minor);
                $part->add_taglink($parents[0], 'parent') if (scalar(@parents) > 0);
            }
        }

        $part->prop('filename', $fields[0]);
        $part->prop('swaptype', $fields[1]);
        $part->prop('size', $fields[2]);
        $part->prop('used', $fields[3]);
        $part->prop('priority', $fields[4]);
        $part->prop('active', 1);

        # add a swapoff action
        $part->add_action('swapoff', 'Turn off swap', $part, sub {
            my $self = shift;
            my $part = $self->item();
            my $plugin = $part->plugin();
            print STDERR "Dangerous actions are disabled: '". $self->label() ."'\n";
            return 1;
            if ($plugin->tool_exec('swapoff', $part->prop('filename')) == 0) {
                $part->prop('active', 0);
                $part->prop('priority', 0);
            }
            return 1;
        }, sub {
            my $self = shift;
            my $part = $self->item();
            return $part->prop('active') == 1;
        });

        # use swaplabel to get label and uuid
        my %labelfields = $self->tool_fields('swaplabel', ':', $fields[0]);
        $part->prop('uuid', defined($labelfields{'UUID'}) ? $labelfields{'UUID'} : '');
        $part->prop('label', defined($labelfields{'LABEL'}) ? $labelfields{'LABEL'} : '');

        # check first if it's a device, then find the define
        my @stat = stat($fields[0]);
        # if device: then...
        if ($stat[2] >> 12 == 6) {
            my $dev = $stat[6];
            my $minor = $dev % 256;
            my $major = int (($dev - $minor) / 256);
            my @ios = $self->parent->findioprop('dev', $major .':'. $minor);
            if (scalar(@ios) > 0) {
                $part->in_add($ios[0]);
            }
            else {
                # TODO: create the IO ? try to probe parent? or ???
                # think of XEN where you may have device partition files without an actual disk?
            }
        }
        else {
            # TODO the in should be the mount point containing the filename
        }
    }
# /proc/swaps:
#
# Filename				Type		Size	Used	Priority
# /dev/sda3                    partition	8388604	2739584	-1

    1;
});


package ManaTools::Shared::disk_backend::Part::Swap;

use Moose;

extends 'ManaTools::Shared::disk_backend::Part';

use MooseX::ClassAttribute;

class_has '+type' => (
    default => 'Swap'
);

has 'path' => (
    is => 'rw',
    isa => 'Str',
    required => 1
);

class_has '+in_restriction' => (
    default => sub {
        return sub {
            my $self = shift;
            my $io = shift;
            my $del = shift;
            if (defined $del && !$del) {
                return ($self->in_length() > 0);
            }
            # only 1 device allowed
            return $self->in_length() < 1 && ($io->does('ManaTools::Shared::disk_backend::BlockDevice') || $io->does('ManaTools::Shared::disk_backend::FileRole'));
        };
    }
);

class_has '+out_restriction' => (
    default => sub {
        return sub {return 0;};
    }
);

class_has '+order' => (
    default => sub {
        sub {
            my $self = shift;
            my $part = shift;
            return ($self->prop('priority') <=> $part->prop('priority'));
        }
    }
);

class_has '+restrictions' => (
    default => sub {
        return {
            parent => sub {
                my $self = shift;
                my $part = shift;
                return $part->does('ManaTools::Shared::disk_backend::BlockDevice') || $part->does('ManaTools::Shared::disk_backend::FileRole');
            },
            # TODO: memory Part?
            child => sub {
                my $self = shift;
                my $part = shift;
                return 0;
            },
            sibling => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::Swap');
            },
            previous => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::Swap');
            },
            next => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::Swap');
            },
        }
    }
);

1;