]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage::ZFSPoolPlugin; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use IO::File; | |
7 | use Net::IP; | |
8 | use POSIX; | |
9 | ||
10 | use PVE::ProcFSTools; | |
11 | use PVE::RPCEnvironment; | |
12 | use PVE::Storage::Plugin; | |
13 | use PVE::Tools qw(run_command); | |
14 | ||
15 | use base qw(PVE::Storage::Plugin); | |
16 | ||
17 | sub type { | |
18 | return 'zfspool'; | |
19 | } | |
20 | ||
21 | sub plugindata { | |
22 | return { | |
23 | content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}], | |
24 | format => [ { raw => 1, subvol => 1 } , 'raw' ], | |
25 | }; | |
26 | } | |
27 | ||
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 | }, | |
38 | mountpoint => { | |
39 | description => "mount point", | |
40 | type => 'string', format => 'pve-storage-path', | |
41 | }, | |
42 | }; | |
43 | } | |
44 | ||
45 | sub options { | |
46 | return { | |
47 | pool => { fixed => 1 }, | |
48 | blocksize => { optional => 1 }, | |
49 | sparse => { optional => 1 }, | |
50 | nodes => { optional => 1 }, | |
51 | disable => { optional => 1 }, | |
52 | content => { optional => 1 }, | |
53 | bwlimit => { optional => 1 }, | |
54 | mountpoint => { optional => 1 }, | |
55 | }; | |
56 | } | |
57 | ||
58 | # static zfs helper methods | |
59 | ||
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) { | |
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; | |
74 | next if scalar(@parts) < 2; # we need pool/name | |
75 | my $name = pop @parts; | |
76 | my $pool = join('/', @parts); | |
77 | ||
78 | next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!; | |
79 | $zvol->{owner} = $2; | |
80 | ||
81 | $zvol->{pool} = $pool; | |
82 | $zvol->{name} = $name; | |
83 | if ($type eq 'filesystem') { | |
84 | if ($refquota eq 'none') { | |
85 | $zvol->{size} = 0; | |
86 | } else { | |
87 | $zvol->{size} = $refquota + 0; | |
88 | } | |
89 | $zvol->{format} = 'subvol'; | |
90 | } else { | |
91 | $zvol->{size} = $size + 0; | |
92 | $zvol->{format} = 'raw'; | |
93 | } | |
94 | if ($origin !~ /^-$/) { | |
95 | $zvol->{origin} = $origin; | |
96 | } | |
97 | push @$list, $zvol; | |
98 | } | |
99 | ||
100 | return $list; | |
101 | } | |
102 | ||
103 | sub parse_volname { | |
104 | my ($class, $volname) = @_; | |
105 | ||
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); | |
110 | } | |
111 | ||
112 | die "unable to parse zfs volume name '$volname'\n"; | |
113 | } | |
114 | ||
115 | # virtual zfs methods (subclass can overwrite them) | |
116 | ||
117 | sub on_add_hook { | |
118 | my ($class, $storeid, $scfg, %param) = @_; | |
119 | ||
120 | my $cfg_mountpoint = $scfg->{mountpoint}; | |
121 | ||
122 | # ignore failure, pool might currently not be imported | |
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); | |
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 | } | |
137 | ||
138 | return; | |
139 | } | |
140 | ||
141 | sub path { | |
142 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; | |
143 | ||
144 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
145 | ||
146 | my $path = ''; | |
147 | my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}"; | |
148 | ||
149 | if ($vtype eq "images") { | |
150 | if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { | |
151 | $path = "$mountpoint/$name"; | |
152 | } else { | |
153 | $path = "/dev/zvol/$scfg->{pool}/$name"; | |
154 | } | |
155 | $path .= "\@$snapname" if defined($snapname); | |
156 | } else { | |
157 | die "$vtype is not allowed in ZFSPool!"; | |
158 | } | |
159 | ||
160 | return ($path, $vmid, $vtype); | |
161 | } | |
162 | ||
163 | sub zfs_request { | |
164 | my ($class, $scfg, $timeout, $method, @params) = @_; | |
165 | ||
166 | my $cmd = []; | |
167 | ||
168 | if ($method eq 'zpool_list') { | |
169 | push @$cmd, 'zpool', 'list'; | |
170 | } elsif ($method eq 'zpool_import') { | |
171 | push @$cmd, 'zpool', 'import'; | |
172 | $timeout = 15 if !$timeout || $timeout < 15; | |
173 | } else { | |
174 | push @$cmd, 'zfs', $method; | |
175 | } | |
176 | push @$cmd, @params; | |
177 | ||
178 | my $msg = ''; | |
179 | my $output = sub { $msg .= "$_[0]\n" }; | |
180 | ||
181 | $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout; | |
182 | ||
183 | run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); | |
184 | ||
185 | return $msg; | |
186 | } | |
187 | ||
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 | ||
205 | sub alloc_image { | |
206 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
207 | ||
208 | my $volname = $name; | |
209 | ||
210 | if ($fmt eq 'raw') { | |
211 | ||
212 | die "illegal name '$volname' - should be 'vm-$vmid-*'\n" | |
213 | if $volname && $volname !~ m/^vm-$vmid-/; | |
214 | $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) | |
215 | if !$volname; | |
216 | ||
217 | $class->zfs_create_zvol($scfg, $volname, $size); | |
218 | $class->zfs_wait_for_zvol_link($scfg, $volname); | |
219 | ||
220 | } elsif ( $fmt eq 'subvol') { | |
221 | ||
222 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" | |
223 | if $volname && $volname !~ m/^subvol-$vmid-/; | |
224 | $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) | |
225 | if !$volname; | |
226 | ||
227 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" | |
228 | if $volname !~ m/^subvol-$vmid-/; | |
229 | ||
230 | $class->zfs_create_subvol($scfg, $volname, $size); | |
231 | ||
232 | } else { | |
233 | die "unsupported format '$fmt'"; | |
234 | } | |
235 | ||
236 | return $volname; | |
237 | } | |
238 | ||
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 | ||
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 | ||
260 | my $info = $dat->{$image}; | |
261 | ||
262 | my $volname = $info->{name}; | |
263 | my $parent = $info->{parent}; | |
264 | my $owner = $info->{vmid}; | |
265 | ||
266 | if ($parent && $parent =~ m/^(\S+)\@__base__$/) { | |
267 | my ($basename) = ($1); | |
268 | $info->{volid} = "$storeid:$basename/$volname"; | |
269 | } else { | |
270 | $info->{volid} = "$storeid:$volname"; | |
271 | } | |
272 | ||
273 | if ($vollist) { | |
274 | my $found = grep { $_ eq $info->{volid} } @$vollist; | |
275 | next if !$found; | |
276 | } else { | |
277 | next if defined ($vmid) && ($owner ne $vmid); | |
278 | } | |
279 | ||
280 | push @$res, $info; | |
281 | } | |
282 | } | |
283 | return $res; | |
284 | } | |
285 | ||
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 | ||
295 | sub zfs_get_pool_stats { | |
296 | my ($class, $scfg) = @_; | |
297 | ||
298 | my $available = 0; | |
299 | my $used = 0; | |
300 | ||
301 | my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool}); | |
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 | ||
314 | sub zfs_create_zvol { | |
315 | my ($class, $scfg, $zvol, $size) = @_; | |
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 | ||
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 | ||
330 | $class->zfs_request($scfg, undef, @$cmd); | |
331 | } | |
332 | ||
333 | sub zfs_create_subvol { | |
334 | my ($class, $scfg, $volname, $size) = @_; | |
335 | ||
336 | my $dataset = "$scfg->{pool}/$volname"; | |
337 | my $quota = $size ? "${size}k" : "none"; | |
338 | ||
339 | my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', | |
340 | '-o', "refquota=${quota}", $dataset]; | |
341 | ||
342 | $class->zfs_request($scfg, undef, @$cmd); | |
343 | } | |
344 | ||
345 | sub zfs_delete_zvol { | |
346 | my ($class, $scfg, $zvol) = @_; | |
347 | ||
348 | my $err; | |
349 | ||
350 | for (my $i = 0; $i < 6; $i++) { | |
351 | ||
352 | eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); }; | |
353 | if ($err = $@) { | |
354 | if ($err =~ m/^zfs error:(.*): dataset is busy.*/) { | |
355 | sleep(1); | |
356 | } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) { | |
357 | $err = undef; | |
358 | last; | |
359 | } else { | |
360 | die $err; | |
361 | } | |
362 | } else { | |
363 | last; | |
364 | } | |
365 | } | |
366 | ||
367 | die $err if $err; | |
368 | } | |
369 | ||
370 | sub zfs_list_zvol { | |
371 | my ($class, $scfg) = @_; | |
372 | ||
373 | my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp'); | |
374 | my $zvols = zfs_parse_zvol_list($text); | |
375 | return undef if !$zvols; | |
376 | ||
377 | my $list = (); | |
378 | foreach my $zvol (@$zvols) { | |
379 | my $pool = $zvol->{pool}; | |
380 | my $name = $zvol->{name}; | |
381 | my $parent = $zvol->{origin}; | |
382 | if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){ | |
383 | $parent = $1; | |
384 | } | |
385 | ||
386 | $list->{$pool}->{$name} = { | |
387 | name => $name, | |
388 | size => $zvol->{size}, | |
389 | parent => $parent, | |
390 | format => $zvol->{format}, | |
391 | vmid => $zvol->{owner}, | |
392 | }; | |
393 | } | |
394 | ||
395 | return $list; | |
396 | } | |
397 | ||
398 | sub zfs_get_latest_snapshot { | |
399 | my ($class, $scfg, $volname) = @_; | |
400 | ||
401 | my $vname = ($class->parse_volname($volname))[1]; | |
402 | ||
403 | # abort rollback if snapshot is not the latest | |
404 | my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation'); | |
405 | my $text = $class->zfs_request($scfg, undef, 'list', @params); | |
406 | my @snapshots = split(/\n/, $text); | |
407 | ||
408 | my $recentsnap; | |
409 | foreach (@snapshots) { | |
410 | if (/$scfg->{pool}\/$vname/) { | |
411 | s/^.*@//; | |
412 | $recentsnap = $_; | |
413 | } | |
414 | } | |
415 | ||
416 | return $recentsnap; | |
417 | } | |
418 | ||
419 | sub status { | |
420 | my ($class, $storeid, $scfg, $cache) = @_; | |
421 | ||
422 | my $total = 0; | |
423 | my $free = 0; | |
424 | my $used = 0; | |
425 | my $active = 0; | |
426 | ||
427 | eval { | |
428 | ($free, $used) = $class->zfs_get_pool_stats($scfg); | |
429 | $active = 1; | |
430 | $total = $free + $used; | |
431 | }; | |
432 | warn $@ if $@; | |
433 | ||
434 | return ($total, $free, $used, $active); | |
435 | } | |
436 | ||
437 | sub volume_size_info { | |
438 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
439 | ||
440 | my (undef, $vname, undef, undef, undef, undef, $format) = | |
441 | $class->parse_volname($volname); | |
442 | ||
443 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
444 | my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname"); | |
445 | if ($value =~ /^(\d+)$/) { | |
446 | return $1; | |
447 | } | |
448 | ||
449 | die "Could not get zfs volume size\n"; | |
450 | } | |
451 | ||
452 | sub volume_snapshot { | |
453 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
454 | ||
455 | my $vname = ($class->parse_volname($volname))[1]; | |
456 | ||
457 | $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap"); | |
458 | } | |
459 | ||
460 | sub volume_snapshot_delete { | |
461 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
462 | ||
463 | my $vname = ($class->parse_volname($volname))[1]; | |
464 | ||
465 | $class->deactivate_volume($storeid, $scfg, $vname, $snap, {}); | |
466 | $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap"); | |
467 | } | |
468 | ||
469 | sub volume_snapshot_rollback { | |
470 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
471 | ||
472 | my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname); | |
473 | ||
474 | my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap"); | |
475 | ||
476 | # we have to unmount rollbacked subvols, to invalidate wrong kernel | |
477 | # caches, they get mounted in activate volume again | |
478 | # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931 | |
479 | if ($format eq 'subvol') { | |
480 | $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); | |
481 | } | |
482 | ||
483 | return $msg; | |
484 | } | |
485 | ||
486 | sub volume_rollback_is_possible { | |
487 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
488 | ||
489 | my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); | |
490 | ||
491 | die "can't rollback, no snapshots exist at all\n" | |
492 | if !defined($recentsnap); | |
493 | ||
494 | die "can't rollback, '$snap' is not most recent snapshot\n" | |
495 | if $snap ne $recentsnap; | |
496 | ||
497 | return 1; | |
498 | } | |
499 | ||
500 | sub volume_snapshot_list { | |
501 | my ($class, $scfg, $storeid, $volname) = @_; | |
502 | ||
503 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
504 | ||
505 | my $zpath = "$scfg->{pool}/$name"; | |
506 | ||
507 | my $snaps = []; | |
508 | ||
509 | my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o', | |
510 | 'name', $zpath]; | |
511 | ||
512 | my $outfunc = sub { | |
513 | my $line = shift; | |
514 | ||
515 | if ($line =~ m/^\Q$zpath\E@(.*)$/) { | |
516 | push @$snaps, $1; | |
517 | } | |
518 | }; | |
519 | ||
520 | eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); }; | |
521 | ||
522 | # return an empty array if dataset does not exist. | |
523 | return $snaps; | |
524 | } | |
525 | ||
526 | my sub dataset_mounted_heuristic { | |
527 | my ($dataset) = @_; | |
528 | ||
529 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
530 | for my $mp (@$mounts) { | |
531 | my ($what, $dir, $fs) = $mp->@*; | |
532 | next if $fs ne 'zfs'; | |
533 | # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off') | |
534 | # If any child is mounted heuristically assume that `zfs mount -a` was successful | |
535 | next if $what !~ m!^$dataset(?:/|$)!; | |
536 | return 1; | |
537 | } | |
538 | return 0; | |
539 | } | |
540 | ||
541 | sub activate_storage { | |
542 | my ($class, $storeid, $scfg, $cache) = @_; | |
543 | ||
544 | # Note: $scfg->{pool} can include dataset <pool>/<dataset> | |
545 | my $dataset = $scfg->{pool}; | |
546 | my $pool = ($dataset =~ s!/.*$!!r); | |
547 | ||
548 | return 1 if dataset_mounted_heuristic($dataset); # early return | |
549 | ||
550 | my $pool_imported = sub { | |
551 | my @param = ('-o', 'name', '-H', $pool); | |
552 | my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) }; | |
553 | warn "$@\n" if $@; | |
554 | ||
555 | return defined($res) && $res =~ m/$pool/; | |
556 | }; | |
557 | ||
558 | if (!$pool_imported->()) { | |
559 | # import can only be done if not yet imported! | |
560 | my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool); | |
561 | eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) }; | |
562 | if (my $err = $@) { | |
563 | # just could've raced with another import, so recheck if it is imported | |
564 | die "could not activate storage '$storeid', $err\n" if !$pool_imported->(); | |
565 | } | |
566 | } | |
567 | eval { $class->zfs_request($scfg, undef, 'mount', '-a') }; | |
568 | die "could not activate storage '$storeid', $@\n" if $@; | |
569 | return 1; | |
570 | } | |
571 | ||
572 | sub deactivate_storage { | |
573 | my ($class, $storeid, $scfg, $cache) = @_; | |
574 | return 1; | |
575 | } | |
576 | ||
577 | sub activate_volume { | |
578 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
579 | ||
580 | return 1 if defined($snapname); | |
581 | ||
582 | my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname); | |
583 | ||
584 | if ($format eq 'raw') { | |
585 | $class->zfs_wait_for_zvol_link($scfg, $volname); | |
586 | } elsif ($format eq 'subvol') { | |
587 | my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset"); | |
588 | if ($mounted !~ m/^yes$/) { | |
589 | $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset"); | |
590 | } | |
591 | } | |
592 | ||
593 | return 1; | |
594 | } | |
595 | ||
596 | sub deactivate_volume { | |
597 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
598 | return 1; | |
599 | } | |
600 | ||
601 | sub clone_image { | |
602 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
603 | ||
604 | $snap ||= '__base__'; | |
605 | ||
606 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = | |
607 | $class->parse_volname($volname); | |
608 | ||
609 | die "clone_image only works on base images\n" if !$isBase; | |
610 | ||
611 | my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format); | |
612 | ||
613 | if ($format eq 'subvol') { | |
614 | my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename"); | |
615 | chomp($size); | |
616 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size"); | |
617 | } else { | |
618 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); | |
619 | } | |
620 | ||
621 | return "$basename/$name"; | |
622 | } | |
623 | ||
624 | sub create_base { | |
625 | my ($class, $storeid, $scfg, $volname) = @_; | |
626 | ||
627 | my $snap = '__base__'; | |
628 | ||
629 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = | |
630 | $class->parse_volname($volname); | |
631 | ||
632 | die "create_base not possible with base image\n" if $isBase; | |
633 | ||
634 | my $newname = $name; | |
635 | if ( $format eq 'subvol' ) { | |
636 | $newname =~ s/^subvol-/basevol-/; | |
637 | } else { | |
638 | $newname =~ s/^vm-/base-/; | |
639 | } | |
640 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; | |
641 | ||
642 | $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); | |
643 | ||
644 | my $running = undef; #fixme : is create_base always offline ? | |
645 | ||
646 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
647 | ||
648 | return $newvolname; | |
649 | } | |
650 | ||
651 | sub volume_resize { | |
652 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
653 | ||
654 | my $new_size = int($size/1024); | |
655 | ||
656 | my (undef, $vname, undef, undef, undef, undef, $format) = | |
657 | $class->parse_volname($volname); | |
658 | ||
659 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
660 | ||
661 | # align size to 1M so we always have a valid multiple of the volume block size | |
662 | if ($format eq 'raw') { | |
663 | my $padding = (1024 - $new_size % 1024) % 1024; | |
664 | $new_size = $new_size + $padding; | |
665 | } | |
666 | ||
667 | $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); | |
668 | ||
669 | return $new_size; | |
670 | } | |
671 | ||
672 | sub storage_can_replicate { | |
673 | my ($class, $scfg, $storeid, $format) = @_; | |
674 | ||
675 | return 1 if $format eq 'raw' || $format eq 'subvol'; | |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
680 | sub volume_has_feature { | |
681 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
682 | ||
683 | my $features = { | |
684 | snapshot => { current => 1, snap => 1}, | |
685 | clone => { base => 1}, | |
686 | template => { current => 1}, | |
687 | copy => { base => 1, current => 1}, | |
688 | sparseinit => { base => 1, current => 1}, | |
689 | replicate => { base => 1, current => 1}, | |
690 | }; | |
691 | ||
692 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
693 | $class->parse_volname($volname); | |
694 | ||
695 | my $key = undef; | |
696 | ||
697 | if ($snapname) { | |
698 | $key = 'snap'; | |
699 | } else { | |
700 | $key = $isBase ? 'base' : 'current'; | |
701 | } | |
702 | ||
703 | return 1 if $features->{$feature}->{$key}; | |
704 | ||
705 | return undef; | |
706 | } | |
707 | ||
708 | sub volume_export { | |
709 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
710 | ||
711 | die "unsupported export stream format for $class: $format\n" | |
712 | if $format ne 'zfs'; | |
713 | ||
714 | die "$class storage can only export snapshots\n" | |
715 | if !defined($snapshot); | |
716 | ||
717 | my $dataset = ($class->parse_volname($volname))[1]; | |
718 | ||
719 | my $fd = fileno($fh); | |
720 | die "internal error: invalid file handle for volume_export\n" | |
721 | if !defined($fd); | |
722 | $fd = ">&$fd"; | |
723 | ||
724 | # For zfs we always create a replication stream (-R) which means the remote | |
725 | # side will always delete non-existing source snapshots. This should work | |
726 | # for all our use cases. | |
727 | my $cmd = ['zfs', 'send', '-Rpv']; | |
728 | if (defined($base_snapshot)) { | |
729 | my $arg = $with_snapshots ? '-I' : '-i'; | |
730 | push @$cmd, $arg, $base_snapshot; | |
731 | } | |
732 | push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot"; | |
733 | ||
734 | run_command($cmd, output => $fd); | |
735 | ||
736 | return; | |
737 | } | |
738 | ||
739 | sub volume_export_formats { | |
740 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
741 | ||
742 | my @formats = ('zfs'); | |
743 | # TODOs: | |
744 | # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/; | |
745 | # push @formats, 'raw' if !$base_snapshot && !$with_snapshots; | |
746 | return @formats; | |
747 | } | |
748 | ||
749 | sub volume_import { | |
750 | my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_; | |
751 | ||
752 | die "unsupported import stream format for $class: $format\n" | |
753 | if $format ne 'zfs'; | |
754 | ||
755 | my $fd = fileno($fh); | |
756 | die "internal error: invalid file handle for volume_import\n" | |
757 | if !defined($fd); | |
758 | ||
759 | my (undef, $dataset, $vmid) = $class->parse_volname($volname); | |
760 | my $zfspath = "$scfg->{pool}/$dataset"; | |
761 | my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : ''; | |
762 | my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix], | |
763 | noerr => 1, quiet => 1); | |
764 | if (defined($base_snapshot)) { | |
765 | die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists; | |
766 | } elsif ($exists) { | |
767 | die "volume '$zfspath' already exists\n" if !$allow_rename; | |
768 | warn "volume '$zfspath' already exists - importing with a different name\n"; | |
769 | $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format); | |
770 | $zfspath = "$scfg->{pool}/$dataset"; | |
771 | } | |
772 | ||
773 | eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") }; | |
774 | if (my $err = $@) { | |
775 | if (defined($base_snapshot)) { | |
776 | eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) }; | |
777 | } else { | |
778 | eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) }; | |
779 | } | |
780 | die $err; | |
781 | } | |
782 | ||
783 | return "$storeid:$dataset"; | |
784 | } | |
785 | ||
786 | sub volume_import_formats { | |
787 | my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_; | |
788 | ||
789 | return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots); | |
790 | } | |
791 | ||
792 | 1; |