aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/disk_backend/Plugin/Btrfs.pm
blob: 0683f669eaef90cd2a3cc75ed5d3534c5eaeb48b (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# vim: set et ts=4 sw=4:
package ManaTools::Shared::disk_backend::Plugin::Btrfs;

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

=head1 NAME

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

=head1 SYNOPSIS

    use ManaTools::Shared::disk_backend::Plugin::Btrfs;

    my $db_man = ManaTools::Shared::disk_backend::Plugin::Btrfs->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::Btrfs


=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';

with 'ManaTools::Shared::disk_backend::FileSystem';

has '+fstypes' => (
    default => sub {
        return {
            'btrfs' => 'Btrfs',
        };
    },
);

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

has '+tools' => (
    default => sub {
        return {'btrfs' => '/usr/sbin/btrfs'};
    }
);

has 'filesystems' => (
    is => 'ro',
    isa => 'HashRef[ManaTools::Shared::disk_backend::Part::Btrfs]',
    init_arg => undef,
    default => sub { return {};}
);

sub create_subvolume {
    my $self = shift;
    my $part = shift;
    my $partstate = shift;
    my $fields = shift;
    my $quotas = shift;
    my $subvolumes = shift;

    # look or create part for btrfsvol
    my $p = $part->trychild($partstate, sub {
        my $self = shift;
        my $parameters = shift;
        return ($self->uuid() eq $parameters->{uuid});
    }, 'BtrfsVol', {plugin => $self, fs => $part, mountsourcepath => $fields->{path} =~ s'<FS_TREE>''r, uuid => $fields->{uuid}, loaded => undef, saved => undef});

    # set properties
    $p->prop('label', $fields->{path} =~ s'<FS_TREE>/?''r);
    $p->prop('uuid', $fields->{uuid});
    $p->prop('parent_uuid', $fields->{parent_uuid});
    $p->prop('subvolid', $fields->{ID});
    $p->prop('gen', $fields->{gen});
    $p->prop('cgen', $fields->{cgen});
    $p->prop('parent', $fields->{parent});
    $p->prop('top_level', $fields->{top_level});

    # set quota information
    if (defined($quotas->{'0/'. $fields->{ID}})) {
        my $item = $quotas->{'0/'. $fields->{ID}};
        $p->prop('referred', $item->{rfer});
        $p->prop('exclusive', $item->{excl});
        $p->prop('quota_referred', $item->{max_rfer});
        $p->prop('quota_exclusive', $item->{max_excl});
    }

    # trace parenting and fill in subvolumes in all the BtrfsVol Parts
    # create missing parents too!

    $self->D("$self: trigger changepart for BTRFS Volume $p: ". $p->mountsourcepath());
    $p->changedpart($partstate);

    # if it has a mount point, get readonly state
    # find Mount child (for subvolumes, might need to check the parent and base the path from there)
    my $path = $p->find_path($partstate);
    if (defined($path)) {
        # if it's mounted, we can get readonly status with properties
        my %fields = $self->tool_fields('btrfs', '=', 'property', 'get', "'$path'");
        $p->prop('readonly', $fields{ro} eq 'true');
    }

    # set up children subvolumes
    for my $id (keys %{$fields->{subvolumes}}) {
        if (defined($subvolumes->{$id})) {
            # TODO: add childvol tag for this one
            $p->add_taglink($subvolumes->{$id}, 'childsubvol');
        }
    }

    if (defined($subvolumes->{$p->prop('parent')})) {
        # add a parent link
        $p->add_taglink($subvolumes->{$p->prop('parent')}, 'parentsubvol');
    }

    $subvolumes->{$p->prop('subvolid')} = $p;

    return $p;
}

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

=head2 probe

=head3 OUTPUT

    0 if failed, 1 if success

=head3 DESCRIPTION

    this method will probe the active btrfs filesystems and volumes

=cut

#=============================================================
override ('probe', sub {
    my $self = shift;
    my $fss = $self->filesystems();
    # check in sysfs and create a Btrfs for each one
    for my $fs (glob("/sys/fs/btrfs/*")) {
        next if ($fs !~ m'/[-0-9a-f]+$'i);
        my $part = $self->parent->mkpart('Btrfs', {uuid => $fs =~ s'^.+/''r, plugin => $self});
        $part->prop_from_file('label', "$fs/label");
        $part->prop('features', join(',', map {$_ =~ s'^.+/''r} glob("$fs/features/*")));
        $part->prop_from_file('used', "$fs/allocation/data/disk_used");
        $part->prop_from_file('total', "$fs/allocation/data/disk_total");
        $part->prop_from_file('flags', "$fs/allocation/data/flags");

        # TODO: find base mount point in order to find volumes
        # TODO: quotas ...? pathbased?
    }
    1;
});

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


package ManaTools::Shared::disk_backend::Part::Btrfs;

use Moose;

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

use MooseX::ClassAttribute;

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

has 'uuid' => (
    is => 'rw',
    isa => 'Str',
    required => 1,
    trigger => sub {
        my $self = shift;
        my $value = shift;
        $self->prop('uuid', $value);
    }
);

has 'subvolumes' => (
    is => 'rw',
    isa => 'ArrayRef[ManaTools::Shared::disk_backend::Part::BtrfsVol]',
    init_arg => undef,
    default => sub { return [];},
);

package ManaTools::Shared::disk_backend::Part::BtrfsVol;

use Moose;

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

use MooseX::ClassAttribute;

with 'ManaTools::Shared::disk_backend::Mountable';

sub _get_mount_source {
    my $self = shift;
    my $fs = $self->fs();

    # TODO: multiple parents
    # get parent partlink (which should be a blockdevice anyway)
    my $parent = $fs->find_part(undef, 'parent');
    return undef if (!defined $parent);

    # return parent's devicepath
    return $parent->devicepath();
}

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

has 'uuid' => (
    is => 'rw',
    isa => 'Str',
    required => 1,
    trigger => sub {
        my $self = shift;
        my $value = shift;
        $self->prop('uuid', $value);
    }
);

has 'fs' => (
    is => 'rw',
    isa => 'ManaTools::Shared::disk_backend::Part::Btrfs',
    required => 1,
);

has 'subvolumes' => (
    is => 'rw',
    isa => 'ArrayRef[ManaTools::Shared::disk_backend::Part::BtrfsVol]',
    init_arg => undef,
    default => sub { return [];},
);

class_has '+restrictions' => (
    default => sub {
        return {
            sibling => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::BtrfsVol');;
            },
            parentsubvol => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::BtrfsVol');
            },
            childsubvol => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::BtrfsVol');
            },
            parent => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::Btrfs');
            },
            child => sub {
                my $self = shift;
                my $part = shift;
                return $part->isa('ManaTools::Shared::disk_backend::Part::Mount');
            },
        }
    }
);

augment('_reverse_tag', sub {
    my $self = shift;
    my $tag = shift;
    my $rtag = inner($tag);
    return $rtag if (defined($rtag) && $tag ne $rtag);
    return 'childsubvol' if ($tag eq 'parentsubvol');
    return 'parentsubvol' if ($tag eq 'childsubvol');
    return $tag;
});

around('find_path', sub {
    my $orig = shift;
    my $self = shift;
    my $partstate = shift;

    # first try the standard method
    my $path = $self->$orig($partstate);
    return $path if (defined $path);

    # subvolumes can check parent subvolumes and add the relative path
    my @parents = $self->find_parts($partstate, 'parent');
    for my $parent (@parents) {
        if ($parent->isa('ManaTools::Shared::disk_backend::Part::BtrfsVol')) {
            $path = $parent->find_path($partstate);
            return $path . substr($self->prop('label'), length($parent->prop('label'))) if defined($path);
        }
    }
    return undef;
});

1;