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