]>
Commit | Line | Data |
---|---|---|
85fda4dd | 1 | package PVE::Storage::ZFSPoolPlugin; |
5bb8e010 DM |
2 | |
3 | use strict; | |
4 | use warnings; | |
e3eb131e | 5 | |
5bb8e010 | 6 | use IO::File; |
e3eb131e | 7 | use Net::IP; |
5bb8e010 | 8 | use POSIX; |
e3eb131e | 9 | |
56a7637a | 10 | use PVE::ProcFSTools; |
21430e50 | 11 | use PVE::RPCEnvironment; |
e3eb131e TL |
12 | use PVE::Storage::Plugin; |
13 | use PVE::Tools qw(run_command); | |
5bb8e010 DM |
14 | |
15 | use base qw(PVE::Storage::Plugin); | |
16 | ||
5bb8e010 | 17 | sub type { |
85fda4dd | 18 | return 'zfspool'; |
5bb8e010 DM |
19 | } |
20 | ||
21 | sub plugindata { | |
22 | return { | |
1ccae449 DM |
23 | content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}], |
24 | format => [ { raw => 1, subvol => 1 } , 'raw' ], | |
5bb8e010 | 25 | }; |
85fda4dd | 26 | } |
5bb8e010 | 27 | |
7730694e DM |
28 | sub properties { |
29 | return { | |
30 | blocksize => { | |
31 | description => "block size", | |
32 | type => 'string', | |
33 | }, | |
34 | sparse => { | |
35 | description => "use sparse volumes", | |
36 | type => 'boolean', | |
37 | }, | |
dcefd9dd FE |
38 | mountpoint => { |
39 | description => "mount point", | |
40 | type => 'string', format => 'pve-storage-path', | |
41 | }, | |
7730694e DM |
42 | }; |
43 | } | |
44 | ||
5bb8e010 DM |
45 | sub options { |
46 | return { | |
7730694e DM |
47 | pool => { fixed => 1 }, |
48 | blocksize => { optional => 1 }, | |
49 | sparse => { optional => 1 }, | |
50 | nodes => { optional => 1 }, | |
5bb8e010 | 51 | disable => { optional => 1 }, |
5bb8e010 | 52 | content => { optional => 1 }, |
9edb99a5 | 53 | bwlimit => { optional => 1 }, |
dcefd9dd | 54 | mountpoint => { optional => 1 }, |
5bb8e010 DM |
55 | }; |
56 | } | |
57 | ||
7730694e DM |
58 | # static zfs helper methods |
59 | ||
7730694e DM |
60 | sub zfs_parse_zvol_list { |
61 | my ($text) = @_; | |
62 | ||
63 | my $list = (); | |
64 | ||
65 | return $list if !$text; | |
66 | ||
67 | my @lines = split /\n/, $text; | |
68 | foreach my $line (@lines) { | |
1ccae449 DM |
69 | my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line); |
70 | next if !($type eq 'volume' || $type eq 'filesystem'); | |
71 | ||
72 | my $zvol = {}; | |
73 | my @parts = split /\//, $dataset; | |
dec97937 | 74 | next if scalar(@parts) < 2; # we need pool/name |
1ccae449 DM |
75 | my $name = pop @parts; |
76 | my $pool = join('/', @parts); | |
77 | ||
851658c3 | 78 | next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!; |
1ccae449 DM |
79 | $zvol->{owner} = $2; |
80 | ||
1ccae449 DM |
81 | $zvol->{pool} = $pool; |
82 | $zvol->{name} = $name; | |
83 | if ($type eq 'filesystem') { | |
84 | if ($refquota eq 'none') { | |
85 | $zvol->{size} = 0; | |
86 | } else { | |
3881e680 | 87 | $zvol->{size} = $refquota + 0; |
7730694e | 88 | } |
1ccae449 DM |
89 | $zvol->{format} = 'subvol'; |
90 | } else { | |
3881e680 | 91 | $zvol->{size} = $size + 0; |
1ccae449 | 92 | $zvol->{format} = 'raw'; |
7730694e | 93 | } |
1ccae449 DM |
94 | if ($origin !~ /^-$/) { |
95 | $zvol->{origin} = $origin; | |
96 | } | |
97 | push @$list, $zvol; | |
7730694e DM |
98 | } |
99 | ||
100 | return $list; | |
101 | } | |
102 | ||
cc80ed9c WL |
103 | sub parse_volname { |
104 | my ($class, $volname) = @_; | |
105 | ||
8e5b96ca DM |
106 | if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) { |
107 | my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw'; | |
108 | my $isBase = ($6 eq 'base' || $6 eq 'basevol'); | |
109 | return ('images', $5, $7, $2, $4, $isBase, $format); | |
cc80ed9c WL |
110 | } |
111 | ||
112 | die "unable to parse zfs volume name '$volname'\n"; | |
113 | } | |
114 | ||
7730694e DM |
115 | # virtual zfs methods (subclass can overwrite them) |
116 | ||
dcefd9dd FE |
117 | sub on_add_hook { |
118 | my ($class, $storeid, $scfg, %param) = @_; | |
119 | ||
120 | my $cfg_mountpoint = $scfg->{mountpoint}; | |
dcefd9dd FE |
121 | |
122 | # ignore failure, pool might currently not be imported | |
75815bf5 FE |
123 | my $mountpoint; |
124 | eval { | |
125 | my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1); | |
126 | $mountpoint = PVE::Storage::Plugin::verify_path($res, 1) if defined($res); | |
dcefd9dd FE |
127 | }; |
128 | ||
129 | if (defined($cfg_mountpoint)) { | |
130 | if (defined($mountpoint) && !($cfg_mountpoint =~ m|^\Q$mountpoint\E/?$|)) { | |
131 | warn "warning for $storeid - mountpoint: $cfg_mountpoint " . | |
132 | "does not match current mount point: $mountpoint\n"; | |
133 | } | |
134 | } else { | |
135 | $scfg->{mountpoint} = $mountpoint; | |
136 | } | |
f3ccd0ef FE |
137 | |
138 | return; | |
dcefd9dd FE |
139 | } |
140 | ||
f3e632d0 | 141 | sub path { |
e67069eb | 142 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
f3e632d0 WL |
143 | |
144 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
145 | ||
146 | my $path = ''; | |
dcefd9dd | 147 | my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}"; |
f3e632d0 | 148 | |
1ccae449 | 149 | if ($vtype eq "images") { |
74b724a6 | 150 | if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { |
dcefd9dd | 151 | $path = "$mountpoint/$name"; |
1ccae449 | 152 | } else { |
851658c3 | 153 | $path = "/dev/zvol/$scfg->{pool}/$name"; |
1ccae449 | 154 | } |
f482231e | 155 | $path .= "\@$snapname" if defined($snapname); |
f3e632d0 | 156 | } else { |
85fda4dd | 157 | die "$vtype is not allowed in ZFSPool!"; |
f3e632d0 WL |
158 | } |
159 | ||
160 | return ($path, $vmid, $vtype); | |
161 | } | |
162 | ||
7730694e | 163 | sub zfs_request { |
44257d2e | 164 | my ($class, $scfg, $timeout, $method, @params) = @_; |
7730694e | 165 | |
7730694e DM |
166 | my $cmd = []; |
167 | ||
168 | if ($method eq 'zpool_list') { | |
86d47239 | 169 | push @$cmd, 'zpool', 'list'; |
e2e63801 FG |
170 | } elsif ($method eq 'zpool_import') { |
171 | push @$cmd, 'zpool', 'import'; | |
e9ab8ea3 | 172 | $timeout = 15 if !$timeout || $timeout < 15; |
7730694e DM |
173 | } else { |
174 | push @$cmd, 'zfs', $method; | |
175 | } | |
7730694e | 176 | push @$cmd, @params; |
7730694e | 177 | |
a10695b4 TL |
178 | my $msg = ''; |
179 | my $output = sub { $msg .= "$_[0]\n" }; | |
7730694e | 180 | |
a10695b4 | 181 | $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout; |
72bdeea1 | 182 | |
1f390a30 | 183 | run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); |
7730694e DM |
184 | |
185 | return $msg; | |
186 | } | |
187 | ||
56362cfb FG |
188 | sub zfs_wait_for_zvol_link { |
189 | my ($class, $scfg, $volname, $timeout) = @_; | |
190 | ||
191 | my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10; | |
192 | $timeout = $default_timeout if !defined($timeout); | |
193 | ||
194 | my ($devname, undef, undef) = $class->path($scfg, $volname); | |
195 | ||
196 | for (my $i = 1; $i <= $timeout; $i++) { | |
197 | last if -b $devname; | |
198 | die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n" | |
199 | if $i == $timeout; | |
200 | ||
201 | sleep(1); | |
202 | } | |
203 | } | |
204 | ||
b3ba95e4 WL |
205 | sub alloc_image { |
206 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
207 | ||
1ccae449 | 208 | my $volname = $name; |
3824ba88 | 209 | |
1ccae449 | 210 | if ($fmt eq 'raw') { |
b3ba95e4 | 211 | |
fc05c9a0 | 212 | die "illegal name '$volname' - should be 'vm-$vmid-*'\n" |
1ccae449 | 213 | if $volname && $volname !~ m/^vm-$vmid-/; |
a44c0147 | 214 | $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) |
1ccae449 | 215 | if !$volname; |
b3ba95e4 | 216 | |
1ccae449 | 217 | $class->zfs_create_zvol($scfg, $volname, $size); |
56362cfb | 218 | $class->zfs_wait_for_zvol_link($scfg, $volname); |
82e08809 | 219 | |
1ccae449 | 220 | } elsif ( $fmt eq 'subvol') { |
55525ad2 | 221 | |
fc05c9a0 | 222 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" |
55525ad2 | 223 | if $volname && $volname !~ m/^subvol-$vmid-/; |
a44c0147 | 224 | $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) |
55525ad2 DM |
225 | if !$volname; |
226 | ||
fc05c9a0 | 227 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" |
1ccae449 | 228 | if $volname !~ m/^subvol-$vmid-/; |
76fd7dc7 | 229 | |
3824ba88 FE |
230 | $class->zfs_create_subvol($scfg, $volname, $size); |
231 | ||
1ccae449 DM |
232 | } else { |
233 | die "unsupported format '$fmt'"; | |
234 | } | |
b3ba95e4 | 235 | |
82e08809 | 236 | return $volname; |
b3ba95e4 WL |
237 | } |
238 | ||
e9565df5 WL |
239 | sub free_image { |
240 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
241 | ||
242 | my (undef, $name, undef) = $class->parse_volname($volname); | |
243 | ||
244 | $class->zfs_delete_zvol($scfg, $name); | |
245 | ||
246 | return undef; | |
247 | } | |
248 | ||
ca04180f WL |
249 | sub list_images { |
250 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
251 | ||
252 | $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs}; | |
253 | my $zfspool = $scfg->{pool}; | |
254 | my $res = []; | |
255 | ||
256 | if (my $dat = $cache->{zfs}->{$zfspool}) { | |
257 | ||
258 | foreach my $image (keys %$dat) { | |
259 | ||
1b83c3d9 | 260 | my $info = $dat->{$image}; |
ca04180f | 261 | |
1b83c3d9 FG |
262 | my $volname = $info->{name}; |
263 | my $parent = $info->{parent}; | |
264 | my $owner = $info->{vmid}; | |
265 | ||
266 | if ($parent && $parent =~ m/^(\S+)\@__base__$/) { | |
ca04180f | 267 | my ($basename) = ($1); |
1b83c3d9 | 268 | $info->{volid} = "$storeid:$basename/$volname"; |
ca04180f | 269 | } else { |
1b83c3d9 | 270 | $info->{volid} = "$storeid:$volname"; |
ca04180f WL |
271 | } |
272 | ||
ca04180f | 273 | if ($vollist) { |
1b83c3d9 | 274 | my $found = grep { $_ eq $info->{volid} } @$vollist; |
ca04180f WL |
275 | next if !$found; |
276 | } else { | |
277 | next if defined ($vmid) && ($owner ne $vmid); | |
278 | } | |
279 | ||
ca04180f WL |
280 | push @$res, $info; |
281 | } | |
282 | } | |
ca04180f WL |
283 | return $res; |
284 | } | |
285 | ||
4966c886 FE |
286 | sub zfs_get_properties { |
287 | my ($class, $scfg, $properties, $dataset, $timeout) = @_; | |
288 | ||
289 | my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value', | |
290 | '-Hp', $properties, $dataset); | |
291 | my @values = split /\n/, $result; | |
292 | return wantarray ? @values : $values[0]; | |
293 | } | |
294 | ||
7730694e DM |
295 | sub zfs_get_pool_stats { |
296 | my ($class, $scfg) = @_; | |
297 | ||
298 | my $available = 0; | |
299 | my $used = 0; | |
300 | ||
4966c886 | 301 | my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool}); |
7730694e DM |
302 | |
303 | if($lines[0] =~ /^(\d+)$/) { | |
304 | $available = $1; | |
305 | } | |
306 | ||
307 | if($lines[1] =~ /^(\d+)$/) { | |
308 | $used = $1; | |
309 | } | |
310 | ||
311 | return ($available, $used); | |
312 | } | |
313 | ||
7730694e DM |
314 | sub zfs_create_zvol { |
315 | my ($class, $scfg, $zvol, $size) = @_; | |
cdef3abb ML |
316 | |
317 | # always align size to 1M as workaround until | |
318 | # https://github.com/zfsonlinux/zfs/issues/8541 is solved | |
319 | my $padding = (1024 - $size % 1024) % 1024; | |
320 | $size = $size + $padding; | |
321 | ||
7730694e DM |
322 | my $cmd = ['create']; |
323 | ||
324 | push @$cmd, '-s' if $scfg->{sparse}; | |
325 | ||
326 | push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize}; | |
327 | ||
328 | push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol"; | |
329 | ||
44257d2e | 330 | $class->zfs_request($scfg, undef, @$cmd); |
7730694e DM |
331 | } |
332 | ||
1ccae449 DM |
333 | sub zfs_create_subvol { |
334 | my ($class, $scfg, $volname, $size) = @_; | |
335 | ||
336 | my $dataset = "$scfg->{pool}/$volname"; | |
8c858f7e | 337 | my $quota = $size ? "${size}k" : "none"; |
3824ba88 | 338 | |
efaf4017 | 339 | my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', |
8c858f7e | 340 | '-o', "refquota=${quota}", $dataset]; |
1ccae449 | 341 | |
44257d2e | 342 | $class->zfs_request($scfg, undef, @$cmd); |
1ccae449 DM |
343 | } |
344 | ||
7730694e DM |
345 | sub zfs_delete_zvol { |
346 | my ($class, $scfg, $zvol) = @_; | |
347 | ||
1f390a30 WL |
348 | my $err; |
349 | ||
350 | for (my $i = 0; $i < 6; $i++) { | |
351 | ||
44257d2e | 352 | eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); }; |
1f390a30 WL |
353 | if ($err = $@) { |
354 | if ($err =~ m/^zfs error:(.*): dataset is busy.*/) { | |
355 | sleep(1); | |
27ff0e99 WL |
356 | } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) { |
357 | $err = undef; | |
358 | last; | |
1f390a30 WL |
359 | } else { |
360 | die $err; | |
361 | } | |
362 | } else { | |
363 | last; | |
364 | } | |
365 | } | |
366 | ||
367 | die $err if $err; | |
7730694e DM |
368 | } |
369 | ||
370 | sub zfs_list_zvol { | |
371 | my ($class, $scfg) = @_; | |
372 | ||
d99de0f8 | 373 | my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp'); |
7730694e DM |
374 | my $zvols = zfs_parse_zvol_list($text); |
375 | return undef if !$zvols; | |
376 | ||
377 | my $list = (); | |
378 | foreach my $zvol (@$zvols) { | |
1ccae449 DM |
379 | my $pool = $zvol->{pool}; |
380 | my $name = $zvol->{name}; | |
7730694e DM |
381 | my $parent = $zvol->{origin}; |
382 | if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){ | |
383 | $parent = $1; | |
384 | } | |
385 | ||
1ccae449 DM |
386 | $list->{$pool}->{$name} = { |
387 | name => $name, | |
7730694e DM |
388 | size => $zvol->{size}, |
389 | parent => $parent, | |
1ccae449 DM |
390 | format => $zvol->{format}, |
391 | vmid => $zvol->{owner}, | |
7730694e DM |
392 | }; |
393 | } | |
394 | ||
395 | return $list; | |
396 | } | |
397 | ||
ac5c1af5 FE |
398 | sub zfs_get_sorted_snapshot_list { |
399 | my ($class, $scfg, $volname, $sort_params) = @_; | |
2fc59177 | 400 | |
73dfe360 FE |
401 | my @params = ('-H', '-r', '-t', 'snapshot', '-o', 'name', $sort_params->@*); |
402 | ||
851658c3 | 403 | my $vname = ($class->parse_volname($volname))[1]; |
73dfe360 | 404 | push @params, "$scfg->{pool}\/$vname"; |
851658c3 | 405 | |
44257d2e | 406 | my $text = $class->zfs_request($scfg, undef, 'list', @params); |
2fc59177 DM |
407 | my @snapshots = split(/\n/, $text); |
408 | ||
ac5c1af5 FE |
409 | my $snap_names = []; |
410 | for my $snapshot (@snapshots) { | |
411 | (my $snap_name = $snapshot) =~ s/^.*@//; | |
412 | push $snap_names->@*, $snap_name; | |
2fc59177 | 413 | } |
ac5c1af5 | 414 | return $snap_names; |
2fc59177 DM |
415 | } |
416 | ||
b5e5f7e3 DM |
417 | sub status { |
418 | my ($class, $storeid, $scfg, $cache) = @_; | |
419 | ||
420 | my $total = 0; | |
421 | my $free = 0; | |
422 | my $used = 0; | |
423 | my $active = 0; | |
424 | ||
425 | eval { | |
426 | ($free, $used) = $class->zfs_get_pool_stats($scfg); | |
427 | $active = 1; | |
428 | $total = $free + $used; | |
429 | }; | |
430 | warn $@ if $@; | |
431 | ||
432 | return ($total, $free, $used, $active); | |
433 | } | |
434 | ||
435 | sub volume_size_info { | |
436 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
437 | ||
851658c3 | 438 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
439 | $class->parse_volname($volname); |
440 | ||
441 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
4966c886 FE |
442 | my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname"); |
443 | if ($value =~ /^(\d+)$/) { | |
79f2b938 DM |
444 | return $1; |
445 | } | |
446 | ||
447 | die "Could not get zfs volume size\n"; | |
b5e5f7e3 DM |
448 | } |
449 | ||
450 | sub volume_snapshot { | |
f5640e7d | 451 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
b5e5f7e3 | 452 | |
851658c3 WL |
453 | my $vname = ($class->parse_volname($volname))[1]; |
454 | ||
44257d2e | 455 | $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap"); |
b5e5f7e3 DM |
456 | } |
457 | ||
458 | sub volume_snapshot_delete { | |
459 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
460 | ||
851658c3 WL |
461 | my $vname = ($class->parse_volname($volname))[1]; |
462 | ||
463 | $class->deactivate_volume($storeid, $scfg, $vname, $snap, {}); | |
44257d2e | 464 | $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap"); |
b5e5f7e3 DM |
465 | } |
466 | ||
2b40ffae WL |
467 | sub volume_snapshot_rollback { |
468 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
469 | ||
26a8f21a DC |
470 | my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname); |
471 | ||
472 | my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap"); | |
851658c3 | 473 | |
26a8f21a DC |
474 | # we have to unmount rollbacked subvols, to invalidate wrong kernel |
475 | # caches, they get mounted in activate volume again | |
476 | # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931 | |
477 | if ($format eq 'subvol') { | |
ab3516a6 FE |
478 | eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); }; |
479 | if (my $err = $@) { | |
480 | die $err if $err !~ m/not currently mounted$/; | |
481 | } | |
26a8f21a DC |
482 | } |
483 | ||
484 | return $msg; | |
1597f1f9 WL |
485 | } |
486 | ||
487 | sub volume_rollback_is_possible { | |
9a5d5095 | 488 | my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_; |
3824ba88 | 489 | |
ac5c1af5 FE |
490 | # can't use '-S creation', because zfs list won't reverse the order when the |
491 | # creation time is the same second, breaking at least our tests. | |
492 | my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']); | |
c8eb0178 | 493 | |
9a5d5095 FE |
494 | my $found; |
495 | $blockers //= []; # not guaranteed to be set by caller | |
496 | for my $snapshot ($snapshots->@*) { | |
497 | if ($snapshot eq $snap) { | |
498 | $found = 1; | |
499 | } elsif ($found) { | |
500 | push $blockers->@*, $snapshot; | |
501 | } | |
502 | } | |
503 | ||
504 | my $volid = "${storeid}:${volname}"; | |
505 | ||
506 | die "can't rollback, snapshot '$snap' does not exist on '$volid'\n" | |
507 | if !$found; | |
c8eb0178 | 508 | |
9a5d5095 FE |
509 | die "can't rollback, '$snap' is not most recent snapshot on '$volid'\n" |
510 | if scalar($blockers->@*) > 0; | |
2b40ffae | 511 | |
3824ba88 | 512 | return 1; |
2b40ffae WL |
513 | } |
514 | ||
8c20d8af FE |
515 | sub volume_snapshot_info { |
516 | my ($class, $scfg, $storeid, $volname) = @_; | |
517 | ||
73dfe360 FE |
518 | my @params = ('-Hp', '-r', '-t', 'snapshot', '-o', 'name,guid,creation'); |
519 | ||
8c20d8af | 520 | my $vname = ($class->parse_volname($volname))[1]; |
73dfe360 | 521 | push @params, "$scfg->{pool}\/$vname"; |
8c20d8af | 522 | |
8c20d8af FE |
523 | my $text = $class->zfs_request($scfg, undef, 'list', @params); |
524 | my @lines = split(/\n/, $text); | |
525 | ||
526 | my $info = {}; | |
527 | for my $line (@lines) { | |
528 | my ($snapshot, $guid, $creation) = split(/\s+/, $line); | |
529 | (my $snap_name = $snapshot) =~ s/^.*@//; | |
530 | ||
531 | $info->{$snap_name} = { | |
532 | id => $guid, | |
533 | timestamp => $creation, | |
534 | }; | |
535 | } | |
536 | return $info; | |
537 | } | |
538 | ||
26de022b TL |
539 | my sub dataset_mounted_heuristic { |
540 | my ($dataset) = @_; | |
541 | ||
542 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
543 | for my $mp (@$mounts) { | |
544 | my ($what, $dir, $fs) = $mp->@*; | |
545 | next if $fs ne 'zfs'; | |
546 | # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off') | |
547 | # If any child is mounted heuristically assume that `zfs mount -a` was successful | |
548 | next if $what !~ m!^$dataset(?:/|$)!; | |
549 | return 1; | |
550 | } | |
551 | return 0; | |
552 | } | |
553 | ||
0a3d992f DM |
554 | sub activate_storage { |
555 | my ($class, $storeid, $scfg, $cache) = @_; | |
86d47239 | 556 | |
93124ef4 | 557 | # Note: $scfg->{pool} can include dataset <pool>/<dataset> |
56a7637a SI |
558 | my $dataset = $scfg->{pool}; |
559 | my $pool = ($dataset =~ s!/.*$!!r); | |
560 | ||
26de022b | 561 | return 1 if dataset_mounted_heuristic($dataset); # early return |
93124ef4 | 562 | |
b5c8278a | 563 | my $pool_imported = sub { |
a2d747a1 | 564 | my @param = ('-o', 'name', '-H', $pool); |
b5c8278a | 565 | my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) }; |
a2d747a1 SI |
566 | warn "$@\n" if $@; |
567 | ||
b5c8278a TL |
568 | return defined($res) && $res =~ m/$pool/; |
569 | }; | |
570 | ||
26de022b TL |
571 | if (!$pool_imported->()) { |
572 | # import can only be done if not yet imported! | |
573 | my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool); | |
574 | eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) }; | |
575 | if (my $err = $@) { | |
576 | # just could've raced with another import, so recheck if it is imported | |
577 | die "could not activate storage '$storeid', $err\n" if !$pool_imported->(); | |
dc18abe0 | 578 | } |
86d47239 | 579 | } |
26de022b TL |
580 | eval { $class->zfs_request($scfg, undef, 'mount', '-a') }; |
581 | die "could not activate storage '$storeid', $@\n" if $@; | |
0a3d992f DM |
582 | return 1; |
583 | } | |
584 | ||
585 | sub deactivate_storage { | |
586 | my ($class, $storeid, $scfg, $cache) = @_; | |
587 | return 1; | |
588 | } | |
589 | ||
d4c63dc1 | 590 | sub activate_volume { |
02e797b8 | 591 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
56362cfb FG |
592 | |
593 | return 1 if defined($snapname); | |
594 | ||
3893d275 | 595 | my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname); |
56362cfb | 596 | |
815df2dd FE |
597 | if ($format eq 'raw') { |
598 | $class->zfs_wait_for_zvol_link($scfg, $volname); | |
599 | } elsif ($format eq 'subvol') { | |
3893d275 | 600 | my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset"); |
815df2dd | 601 | if ($mounted !~ m/^yes$/) { |
3893d275 | 602 | $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset"); |
815df2dd FE |
603 | } |
604 | } | |
56362cfb | 605 | |
d4c63dc1 WL |
606 | return 1; |
607 | } | |
608 | ||
609 | sub deactivate_volume { | |
02e797b8 | 610 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
d4c63dc1 WL |
611 | return 1; |
612 | } | |
5bb8e010 | 613 | |
d3a282e8 WL |
614 | sub clone_image { |
615 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
616 | ||
617 | $snap ||= '__base__'; | |
618 | ||
55525ad2 | 619 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = |
d3a282e8 WL |
620 | $class->parse_volname($volname); |
621 | ||
622 | die "clone_image only works on base images\n" if !$isBase; | |
623 | ||
a44c0147 | 624 | my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format); |
d3a282e8 | 625 | |
851658c3 | 626 | if ($format eq 'subvol') { |
e05113fb | 627 | my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename"); |
851658c3 | 628 | chomp($size); |
44257d2e | 629 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size"); |
851658c3 | 630 | } else { |
44257d2e | 631 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); |
851658c3 | 632 | } |
d3a282e8 | 633 | |
851658c3 | 634 | return "$basename/$name"; |
d3a282e8 WL |
635 | } |
636 | ||
637 | sub create_base { | |
638 | my ($class, $storeid, $scfg, $volname) = @_; | |
639 | ||
640 | my $snap = '__base__'; | |
641 | ||
851658c3 | 642 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
d3a282e8 WL |
643 | $class->parse_volname($volname); |
644 | ||
645 | die "create_base not possible with base image\n" if $isBase; | |
646 | ||
647 | my $newname = $name; | |
851658c3 WL |
648 | if ( $format eq 'subvol' ) { |
649 | $newname =~ s/^subvol-/basevol-/; | |
650 | } else { | |
651 | $newname =~ s/^vm-/base-/; | |
652 | } | |
d3a282e8 WL |
653 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; |
654 | ||
44257d2e | 655 | $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); |
d3a282e8 WL |
656 | |
657 | my $running = undef; #fixme : is create_base always offline ? | |
658 | ||
659 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
660 | ||
661 | return $newvolname; | |
662 | } | |
663 | ||
a4034b9f WL |
664 | sub volume_resize { |
665 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
666 | ||
79f2b938 DM |
667 | my $new_size = int($size/1024); |
668 | ||
851658c3 | 669 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
670 | $class->parse_volname($volname); |
671 | ||
672 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
a4034b9f | 673 | |
d3e3e5d6 FE |
674 | # align size to 1M so we always have a valid multiple of the volume block size |
675 | if ($format eq 'raw') { | |
676 | my $padding = (1024 - $new_size % 1024) % 1024; | |
677 | $new_size = $new_size + $padding; | |
678 | } | |
679 | ||
44257d2e | 680 | $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); |
a4034b9f WL |
681 | |
682 | return $new_size; | |
683 | } | |
684 | ||
7118dd91 DM |
685 | sub storage_can_replicate { |
686 | my ($class, $scfg, $storeid, $format) = @_; | |
687 | ||
688 | return 1 if $format eq 'raw' || $format eq 'subvol'; | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
2b40ffae WL |
693 | sub volume_has_feature { |
694 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
695 | ||
696 | my $features = { | |
697 | snapshot => { current => 1, snap => 1}, | |
698 | clone => { base => 1}, | |
699 | template => { current => 1}, | |
700 | copy => { base => 1, current => 1}, | |
baafddbd | 701 | sparseinit => { base => 1, current => 1}, |
f189504c | 702 | replicate => { base => 1, current => 1}, |
95dfa44c | 703 | rename => {current => 1}, |
2b40ffae WL |
704 | }; |
705 | ||
706 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
707 | $class->parse_volname($volname); | |
708 | ||
709 | my $key = undef; | |
710 | ||
711 | if ($snapname) { | |
712 | $key = 'snap'; | |
713 | } else { | |
714 | $key = $isBase ? 'base' : 'current'; | |
715 | } | |
716 | ||
717 | return 1 if $features->{$feature}->{$key}; | |
718 | ||
719 | return undef; | |
720 | } | |
721 | ||
47f37b53 WB |
722 | sub volume_export { |
723 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
724 | ||
725 | die "unsupported export stream format for $class: $format\n" | |
726 | if $format ne 'zfs'; | |
727 | ||
728 | die "$class storage can only export snapshots\n" | |
729 | if !defined($snapshot); | |
730 | ||
9a75947b WL |
731 | my $dataset = ($class->parse_volname($volname))[1]; |
732 | ||
47f37b53 WB |
733 | my $fd = fileno($fh); |
734 | die "internal error: invalid file handle for volume_export\n" | |
735 | if !defined($fd); | |
736 | $fd = ">&$fd"; | |
737 | ||
738 | # For zfs we always create a replication stream (-R) which means the remote | |
739 | # side will always delete non-existing source snapshots. This should work | |
740 | # for all our use cases. | |
741 | my $cmd = ['zfs', 'send', '-Rpv']; | |
742 | if (defined($base_snapshot)) { | |
743 | my $arg = $with_snapshots ? '-I' : '-i'; | |
744 | push @$cmd, $arg, $base_snapshot; | |
745 | } | |
9a75947b | 746 | push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot"; |
47f37b53 WB |
747 | |
748 | run_command($cmd, output => $fd); | |
749 | ||
750 | return; | |
751 | } | |
752 | ||
d390328b WB |
753 | sub volume_export_formats { |
754 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
755 | ||
756 | my @formats = ('zfs'); | |
757 | # TODOs: | |
758 | # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/; | |
759 | # push @formats, 'raw' if !$base_snapshot && !$with_snapshots; | |
760 | return @formats; | |
761 | } | |
762 | ||
47f37b53 | 763 | sub volume_import { |
3cc29a04 | 764 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_; |
47f37b53 WB |
765 | |
766 | die "unsupported import stream format for $class: $format\n" | |
767 | if $format ne 'zfs'; | |
768 | ||
769 | my $fd = fileno($fh); | |
770 | die "internal error: invalid file handle for volume_import\n" | |
771 | if !defined($fd); | |
772 | ||
a97d3ee4 | 773 | my (undef, $dataset, $vmid) = $class->parse_volname($volname); |
9a75947b | 774 | my $zfspath = "$scfg->{pool}/$dataset"; |
47f37b53 WB |
775 | my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : ''; |
776 | my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix], | |
0fd0a627 | 777 | noerr => 1, quiet => 1); |
47f37b53 WB |
778 | if (defined($base_snapshot)) { |
779 | die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists; | |
a97d3ee4 FE |
780 | } elsif ($exists) { |
781 | die "volume '$zfspath' already exists\n" if !$allow_rename; | |
782 | warn "volume '$zfspath' already exists - importing with a different name\n"; | |
783 | $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format); | |
784 | $zfspath = "$scfg->{pool}/$dataset"; | |
47f37b53 WB |
785 | } |
786 | ||
787 | eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") }; | |
788 | if (my $err = $@) { | |
789 | if (defined($base_snapshot)) { | |
790 | eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) }; | |
791 | } else { | |
792 | eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) }; | |
793 | } | |
794 | die $err; | |
795 | } | |
796 | ||
a97d3ee4 | 797 | return "$storeid:$dataset"; |
47f37b53 WB |
798 | } |
799 | ||
d390328b | 800 | sub volume_import_formats { |
3cc29a04 | 801 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; |
d390328b | 802 | |
3cc29a04 | 803 | return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots); |
d390328b WB |
804 | } |
805 | ||
95dfa44c AL |
806 | sub rename_volume { |
807 | my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_; | |
808 | ||
809 | my ( | |
810 | undef, | |
811 | $source_image, | |
812 | $source_vmid, | |
813 | $base_name, | |
814 | $base_vmid, | |
815 | undef, | |
816 | $format | |
817 | ) = $class->parse_volname($source_volname); | |
818 | $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format) | |
819 | if !$target_volname; | |
820 | ||
821 | my $pool = $scfg->{pool}; | |
822 | my $source_zfspath = "${pool}/${source_image}"; | |
823 | my $target_zfspath = "${pool}/${target_volname}"; | |
824 | ||
825 | my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $target_zfspath], | |
826 | noerr => 1, quiet => 1); | |
827 | die "target volume '${target_volname}' already exists\n" if $exists; | |
828 | ||
829 | $class->zfs_request($scfg, 5, 'rename', ${source_zfspath}, ${target_zfspath}); | |
830 | ||
831 | $base_name = $base_name ? "${base_name}/" : ''; | |
832 | ||
833 | return "${storeid}:${base_name}${target_volname}"; | |
834 | } | |
835 | ||
5bb8e010 | 836 | 1; |