-
Notifications
You must be signed in to change notification settings - Fork 2
/
MooseFSPlugin.pm
294 lines (214 loc) · 7.71 KB
/
MooseFSPlugin.pm
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
package PVE::Storage::Custom::MooseFSPlugin;
use strict;
use warnings;
use IO::File;
use File::Path;
use PVE::Storage::Plugin;
use PVE::Tools qw(run_command);
use PVE::ProcFSTools;
use base qw(PVE::Storage::Plugin);
# MooseFS helper functions
sub moosefs_is_mounted {
my ($mfsmaster, $mfsport, $mountpoint, $mountdata) = @_;
$mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
# Check that we return something like mfs#10.1.1.201:9421
# on a fuse filesystem with the correct mountpoint
return $mountpoint if grep {
$_->[2] eq 'fuse' &&
$_->[0] =~ /^mfs(#|\\043)\Q$mfsmaster\E\Q:\E\Q$mfsport\E$/ &&
$_->[1] eq $mountpoint
} @$mountdata;
return undef;
}
sub moosefs_mount {
my ($scfg) = @_;
my $mfsmaster = $scfg->{mfsmaster} // 'mfsmaster';
my $mfspassword = $scfg->{mfspassword};
my $mfsport = $scfg->{mfsport};
my $mfssubfolder = $scfg->{mfssubfolder};
my $cmd = ['/usr/bin/mfsmount'];
if (defined $mfsmaster) {
push @$cmd, '-o', "mfsmaster=$mfsmaster";
}
if (defined $mfsport) {
push @$cmd, '-o', "mfsport=$mfsport";
}
if (defined $mfspassword) {
push @$cmd, '-o', "mfspassword=$mfspassword";
}
if (defined $mfssubfolder) {
push @$cmd, '-o', "mfssubfolder=$mfssubfolder";
}
push @$cmd, $scfg->{path};
run_command($cmd, errmsg => "mount error");
}
sub moosefs_unmount {
my ($scfg) = @_;
my $mountdata = PVE::ProcFSTools::parse_proc_mounts();
my $path = $scfg->{path};
my $mfsmaster = $scfg->{mfsmaster} // 'mfsmaster';
my $mfsport = $scfg->{mfsport} // '9421';
if (moosefs_is_mounted($mfsmaster, $mfsport, $path, $mountdata)) {
my $cmd = ['/bin/umount', $path];
run_command($cmd, errmsg => 'umount error');
}
}
sub api {
return 10;
}
sub type {
return 'moosefs';
}
sub plugindata {
return {
content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1},
{ images => 1 } ],
format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
shared => 1,
};
}
sub properties {
return {
mfsmaster => {
description => "MooseFS master to use for connection (default 'mfsmaster').",
type => 'string',
},
mfsport => {
description => "Port with which to connect to the MooseFS master (default 9421)",
type => 'string',
},
mfspassword => {
description => "Password with which to connect to the MooseFS master (default none)",
type => 'string',
},
mfssubfolder => {
description => "Define subfolder to mount as root (default: /)",
type => 'string',
}
};
}
sub options {
return {
path => { fixed => 1 },
mfsmaster => { optional => 1 },
mfsport => { optional => 1 },
mfspassword => { optional => 1 },
mfssubfolder => { optional => 1 },
disable => { optional => 1 },
shared => { optional => 1 },
content => { optional => 1 },
};
}
sub volume_has_feature {
my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
my $features = {
snapshot => {
current => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
snap => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 }
},
clone => {
base => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
current => { raw => 1 },
snap => { raw => 1 },
},
template => {
current => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
},
copy => {
base => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
current => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
snap => { qcow2 => 1, raw => 1 },
},
sparseinit => {
base => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
current => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 },
},
};
my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
my $key = undef;
if ($snapname) {
$key = 'snap';
} else {
$key = $isBase ? 'base' : 'current';
}
if (defined($features->{$feature}->{$key}->{$format})) {
return 1;
}
return undef;
}
sub parse_name_dir {
my $name = shift;
return PVE::Storage::Plugin::parse_name_dir($name);
}
sub parse_volname {
my ($class, $volname) = @_;
return PVE::Storage::Plugin::parse_volname(@_);
}
sub volume_snapshot {
my ($class, $scfg, $storeid, $volname, $snap) = @_;
my ($storageType, $name, $vmid, $basename, $basedvmid, $isBase, $format) = $class->parse_volname($volname);
if ($format ne 'raw') {
return PVE::Storage::Plugin::volume_snapshot(@_);
}
die "snapshots not supported for this storage type" if $storageType ne 'images';
my $mountpoint = $scfg->{path};
my $snapdir = "$mountpoint/images/$vmid/snaps/$snap";
File::Path::make_path($snapdir);
print "running '/usr/bin/mfsmakesnapshot $mountpoint/images/$vmid/$name $snapdir/$name'\n";
my $cmd = ['/usr/bin/mfsmakesnapshot', "$mountpoint/images/$vmid/$name", "$snapdir/$name"];
run_command($cmd, errmsg => 'An error occurred while making the snapshot');
return undef;
}
sub volume_snapshot_delete {
my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
my ($storageType, $name, $vmid, $basename, $basedvmid, $isBase, $format) = $class->parse_volname($volname);
if ($format ne 'raw') {
return PVE::Storage::Plugin::volume_snapshot_delete(@_);
}
my $mountpoint = $scfg->{path};
my $cmd = ['/usr/bin/rm', '-rf', "$mountpoint/images/$vmid/snaps/$snap/$basename"];
run_command($cmd, errmsg => 'An error occurred while deleting the snapshot');
return undef;
}
sub volume_snapshot_rollback {
my ($class, $scfg, $storeid, $volname, $snap) = @_;
my ($storageType, $name, $vmid, $basename, $basedvmid, $isBase, $format) = $class->parse_volname($volname);
if ($format ne 'raw') {
return PVE::Storage::Plugin::volume_snapshot_rollback(@_);
}
my $mountpoint = $scfg->{path};
my $snapdir = "$mountpoint/images/$vmid/snaps/$snap";
my $cmd = ['/usr/bin/mfsmakesnapshot', '-o', "$snapdir/$name", "$mountpoint/images/$vmid/$name"];
run_command($cmd, errmsg => 'An error occurred while restoring the snapshot');
return undef;
}
sub status {
my ($class, $storeid, $scfg, $cache) = @_;
$cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
if !$cache->{mountdata};
my $path = $scfg->{path};
my $mfsmaster = $scfg->{mfsmaster} // 'mfsmaster';
my $mfsport = $scfg->{mfsport} // '9421';
return undef if !moosefs_is_mounted($mfsmaster, $mfsport, $path, $cache->{mountdata});
return $class->SUPER::status($storeid, $scfg, $cache);
}
sub activate_storage {
my ($class, $storeid, $scfg, $cache) = @_;
$cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
if !$cache->{mountdata};
my $path = $scfg->{path};
my $mfsmaster = $scfg->{mfsmaster} // 'mfsmaster';
my $mfsport = $scfg->{mfsport} // '9421';
if (!moosefs_is_mounted($mfsmaster, $mfsport, $path, $cache->{mountdata})) {
mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
die "unable to activate storage '$storeid' - " .
"directory '$path' does not exist\n" if ! -d $path;
moosefs_mount($scfg);
}
$class->SUPER::activate_storage($storeid, $scfg, $cache);
}
sub deactivate_storage {
my ($class, $storeid, $scfg, $cache) = @_;
moosefs_unmount($scfg);
}
1;