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