]>
Commit | Line | Data |
---|---|---|
85fda4dd | 1 | package PVE::Storage::ZFSPoolPlugin; |
5bb8e010 DM |
2 | |
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use POSIX; | |
7 | use PVE::Tools qw(run_command); | |
8 | use PVE::Storage::Plugin; | |
21430e50 | 9 | use PVE::RPCEnvironment; |
5bb8e010 DM |
10 | |
11 | use base qw(PVE::Storage::Plugin); | |
12 | ||
5bb8e010 | 13 | sub type { |
85fda4dd | 14 | return 'zfspool'; |
5bb8e010 DM |
15 | } |
16 | ||
17 | sub plugindata { | |
18 | return { | |
1ccae449 DM |
19 | content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}], |
20 | format => [ { raw => 1, subvol => 1 } , 'raw' ], | |
5bb8e010 | 21 | }; |
85fda4dd | 22 | } |
5bb8e010 | 23 | |
7730694e DM |
24 | sub properties { |
25 | return { | |
26 | blocksize => { | |
27 | description => "block size", | |
28 | type => 'string', | |
29 | }, | |
30 | sparse => { | |
31 | description => "use sparse volumes", | |
32 | type => 'boolean', | |
33 | }, | |
34 | }; | |
35 | } | |
36 | ||
5bb8e010 DM |
37 | sub options { |
38 | return { | |
7730694e DM |
39 | pool => { fixed => 1 }, |
40 | blocksize => { optional => 1 }, | |
41 | sparse => { optional => 1 }, | |
42 | nodes => { optional => 1 }, | |
5bb8e010 | 43 | disable => { optional => 1 }, |
5bb8e010 DM |
44 | content => { optional => 1 }, |
45 | }; | |
46 | } | |
47 | ||
7730694e DM |
48 | # static zfs helper methods |
49 | ||
060ef890 DM |
50 | sub zfs_parse_size { |
51 | my ($text) = @_; | |
52 | ||
53 | return 0 if !$text; | |
54 | ||
55 | if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) { | |
56 | ||
57 | my ($size, $reminder, $unit) = ($1, $2, $3); | |
58 | ||
59 | if ($unit) { | |
60 | if ($unit eq 'K') { | |
61 | $size *= 1024; | |
62 | } elsif ($unit eq 'M') { | |
63 | $size *= 1024*1024; | |
64 | } elsif ($unit eq 'G') { | |
65 | $size *= 1024*1024*1024; | |
66 | } elsif ($unit eq 'T') { | |
67 | $size *= 1024*1024*1024*1024; | |
68 | } else { | |
69 | die "got unknown zfs size unit '$unit'\n"; | |
70 | } | |
71 | } | |
72 | ||
73 | if ($reminder) { | |
74 | $size = ceil($size); | |
75 | } | |
76 | ||
77 | return $size; | |
78 | ||
79 | } | |
80 | ||
81 | warn "unable to parse zfs size '$text'\n"; | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
7730694e DM |
86 | sub zfs_parse_zvol_list { |
87 | my ($text) = @_; | |
88 | ||
89 | my $list = (); | |
90 | ||
91 | return $list if !$text; | |
92 | ||
93 | my @lines = split /\n/, $text; | |
94 | foreach my $line (@lines) { | |
1ccae449 DM |
95 | my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line); |
96 | next if !($type eq 'volume' || $type eq 'filesystem'); | |
97 | ||
98 | my $zvol = {}; | |
99 | my @parts = split /\//, $dataset; | |
dec97937 | 100 | next if scalar(@parts) < 2; # we need pool/name |
1ccae449 DM |
101 | my $name = pop @parts; |
102 | my $pool = join('/', @parts); | |
103 | ||
851658c3 | 104 | next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!; |
1ccae449 DM |
105 | $zvol->{owner} = $2; |
106 | ||
1ccae449 DM |
107 | $zvol->{pool} = $pool; |
108 | $zvol->{name} = $name; | |
109 | if ($type eq 'filesystem') { | |
110 | if ($refquota eq 'none') { | |
111 | $zvol->{size} = 0; | |
112 | } else { | |
113 | $zvol->{size} = zfs_parse_size($refquota); | |
7730694e | 114 | } |
1ccae449 DM |
115 | $zvol->{format} = 'subvol'; |
116 | } else { | |
117 | $zvol->{size} = zfs_parse_size($size); | |
118 | $zvol->{format} = 'raw'; | |
7730694e | 119 | } |
1ccae449 DM |
120 | if ($origin !~ /^-$/) { |
121 | $zvol->{origin} = $origin; | |
122 | } | |
123 | push @$list, $zvol; | |
7730694e DM |
124 | } |
125 | ||
126 | return $list; | |
127 | } | |
128 | ||
cc80ed9c WL |
129 | sub parse_volname { |
130 | my ($class, $volname) = @_; | |
131 | ||
8e5b96ca DM |
132 | if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) { |
133 | my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw'; | |
134 | my $isBase = ($6 eq 'base' || $6 eq 'basevol'); | |
135 | return ('images', $5, $7, $2, $4, $isBase, $format); | |
cc80ed9c WL |
136 | } |
137 | ||
138 | die "unable to parse zfs volume name '$volname'\n"; | |
139 | } | |
140 | ||
7730694e DM |
141 | # virtual zfs methods (subclass can overwrite them) |
142 | ||
f3e632d0 | 143 | sub path { |
e67069eb | 144 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
f3e632d0 WL |
145 | |
146 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
147 | ||
148 | my $path = ''; | |
149 | ||
1ccae449 | 150 | if ($vtype eq "images") { |
74b724a6 | 151 | if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { |
f482231e | 152 | # fixme: we currently assume standard mount point?! |
851658c3 | 153 | $path = "/$scfg->{pool}/$name"; |
1ccae449 | 154 | } else { |
851658c3 | 155 | $path = "/dev/zvol/$scfg->{pool}/$name"; |
1ccae449 | 156 | } |
f482231e | 157 | $path .= "\@$snapname" if defined($snapname); |
f3e632d0 | 158 | } else { |
85fda4dd | 159 | die "$vtype is not allowed in ZFSPool!"; |
f3e632d0 WL |
160 | } |
161 | ||
162 | return ($path, $vmid, $vtype); | |
163 | } | |
164 | ||
7730694e DM |
165 | sub zfs_request { |
166 | my ($class, $scfg, $timeout, $method, @params) = @_; | |
167 | ||
72bdeea1 | 168 | my $default_timeout = PVE::RPCEnvironment::is_worker() ? 60*60 : 5; |
7730694e DM |
169 | |
170 | my $cmd = []; | |
171 | ||
172 | if ($method eq 'zpool_list') { | |
86d47239 | 173 | push @$cmd, 'zpool', 'list'; |
e2e63801 FG |
174 | } elsif ($method eq 'zpool_import') { |
175 | push @$cmd, 'zpool', 'import'; | |
72bdeea1 | 176 | $default_timeout = 15 if $default_timeout < 15; |
7730694e DM |
177 | } else { |
178 | push @$cmd, 'zfs', $method; | |
179 | } | |
180 | ||
181 | push @$cmd, @params; | |
182 | ||
183 | my $msg = ''; | |
184 | ||
185 | my $output = sub { | |
186 | my $line = shift; | |
187 | $msg .= "$line\n"; | |
188 | }; | |
189 | ||
72bdeea1 FG |
190 | $timeout = $default_timeout if !$timeout; |
191 | ||
1f390a30 | 192 | run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); |
7730694e DM |
193 | |
194 | return $msg; | |
195 | } | |
196 | ||
b3ba95e4 WL |
197 | sub alloc_image { |
198 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
199 | ||
1ccae449 DM |
200 | my $volname = $name; |
201 | ||
202 | if ($fmt eq 'raw') { | |
b3ba95e4 | 203 | |
1ccae449 DM |
204 | die "illegal name '$volname' - sould be 'vm-$vmid-*'\n" |
205 | if $volname && $volname !~ m/^vm-$vmid-/; | |
55525ad2 | 206 | $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) |
1ccae449 | 207 | if !$volname; |
b3ba95e4 | 208 | |
1ccae449 DM |
209 | $class->zfs_create_zvol($scfg, $volname, $size); |
210 | my $devname = "/dev/zvol/$scfg->{pool}/$volname"; | |
82e08809 | 211 | |
1ccae449 | 212 | run_command("udevadm trigger --subsystem-match block"); |
602eacfe | 213 | system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}"); |
76fd7dc7 | 214 | |
1ccae449 | 215 | } elsif ( $fmt eq 'subvol') { |
55525ad2 DM |
216 | |
217 | die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n" | |
218 | if $volname && $volname !~ m/^subvol-$vmid-/; | |
219 | $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) | |
220 | if !$volname; | |
221 | ||
1ccae449 DM |
222 | die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n" |
223 | if $volname !~ m/^subvol-$vmid-/; | |
76fd7dc7 | 224 | |
1ccae449 DM |
225 | $class->zfs_create_subvol($scfg, $volname, $size); |
226 | ||
227 | } else { | |
228 | die "unsupported format '$fmt'"; | |
229 | } | |
b3ba95e4 | 230 | |
82e08809 | 231 | return $volname; |
b3ba95e4 WL |
232 | } |
233 | ||
e9565df5 WL |
234 | sub free_image { |
235 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
236 | ||
237 | my (undef, $name, undef) = $class->parse_volname($volname); | |
238 | ||
239 | $class->zfs_delete_zvol($scfg, $name); | |
240 | ||
241 | return undef; | |
242 | } | |
243 | ||
ca04180f WL |
244 | sub list_images { |
245 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
246 | ||
247 | $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs}; | |
248 | my $zfspool = $scfg->{pool}; | |
249 | my $res = []; | |
250 | ||
251 | if (my $dat = $cache->{zfs}->{$zfspool}) { | |
252 | ||
253 | foreach my $image (keys %$dat) { | |
254 | ||
1b83c3d9 | 255 | my $info = $dat->{$image}; |
ca04180f | 256 | |
1b83c3d9 FG |
257 | my $volname = $info->{name}; |
258 | my $parent = $info->{parent}; | |
259 | my $owner = $info->{vmid}; | |
260 | ||
261 | if ($parent && $parent =~ m/^(\S+)\@__base__$/) { | |
ca04180f | 262 | my ($basename) = ($1); |
1b83c3d9 | 263 | $info->{volid} = "$storeid:$basename/$volname"; |
ca04180f | 264 | } else { |
1b83c3d9 | 265 | $info->{volid} = "$storeid:$volname"; |
ca04180f WL |
266 | } |
267 | ||
ca04180f | 268 | if ($vollist) { |
1b83c3d9 | 269 | my $found = grep { $_ eq $info->{volid} } @$vollist; |
ca04180f WL |
270 | next if !$found; |
271 | } else { | |
272 | next if defined ($vmid) && ($owner ne $vmid); | |
273 | } | |
274 | ||
ca04180f WL |
275 | push @$res, $info; |
276 | } | |
277 | } | |
ca04180f WL |
278 | return $res; |
279 | } | |
280 | ||
7730694e DM |
281 | sub zfs_get_pool_stats { |
282 | my ($class, $scfg) = @_; | |
283 | ||
284 | my $available = 0; | |
285 | my $used = 0; | |
286 | ||
287 | my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp', | |
288 | 'available,used', $scfg->{pool}); | |
289 | ||
290 | my @lines = split /\n/, $text; | |
291 | ||
292 | if($lines[0] =~ /^(\d+)$/) { | |
293 | $available = $1; | |
294 | } | |
295 | ||
296 | if($lines[1] =~ /^(\d+)$/) { | |
297 | $used = $1; | |
298 | } | |
299 | ||
300 | return ($available, $used); | |
301 | } | |
302 | ||
7730694e DM |
303 | sub zfs_create_zvol { |
304 | my ($class, $scfg, $zvol, $size) = @_; | |
305 | ||
306 | my $cmd = ['create']; | |
307 | ||
308 | push @$cmd, '-s' if $scfg->{sparse}; | |
309 | ||
310 | push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize}; | |
311 | ||
312 | push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol"; | |
313 | ||
314 | $class->zfs_request($scfg, undef, @$cmd); | |
315 | } | |
316 | ||
1ccae449 DM |
317 | sub zfs_create_subvol { |
318 | my ($class, $scfg, $volname, $size) = @_; | |
319 | ||
320 | my $dataset = "$scfg->{pool}/$volname"; | |
321 | ||
efaf4017 DM |
322 | my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', |
323 | '-o', "refquota=${size}k", $dataset]; | |
1ccae449 DM |
324 | |
325 | $class->zfs_request($scfg, undef, @$cmd); | |
326 | } | |
327 | ||
7730694e DM |
328 | sub zfs_delete_zvol { |
329 | my ($class, $scfg, $zvol) = @_; | |
330 | ||
1f390a30 WL |
331 | my $err; |
332 | ||
333 | for (my $i = 0; $i < 6; $i++) { | |
334 | ||
335 | eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); }; | |
336 | if ($err = $@) { | |
337 | if ($err =~ m/^zfs error:(.*): dataset is busy.*/) { | |
338 | sleep(1); | |
27ff0e99 WL |
339 | } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) { |
340 | $err = undef; | |
341 | last; | |
1f390a30 WL |
342 | } else { |
343 | die $err; | |
344 | } | |
345 | } else { | |
346 | last; | |
347 | } | |
348 | } | |
349 | ||
350 | die $err if $err; | |
7730694e DM |
351 | } |
352 | ||
353 | sub zfs_list_zvol { | |
354 | my ($class, $scfg) = @_; | |
355 | ||
1ccae449 | 356 | my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr'); |
7730694e DM |
357 | my $zvols = zfs_parse_zvol_list($text); |
358 | return undef if !$zvols; | |
359 | ||
360 | my $list = (); | |
361 | foreach my $zvol (@$zvols) { | |
1ccae449 DM |
362 | my $pool = $zvol->{pool}; |
363 | my $name = $zvol->{name}; | |
7730694e DM |
364 | my $parent = $zvol->{origin}; |
365 | if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){ | |
366 | $parent = $1; | |
367 | } | |
368 | ||
1ccae449 DM |
369 | $list->{$pool}->{$name} = { |
370 | name => $name, | |
7730694e DM |
371 | size => $zvol->{size}, |
372 | parent => $parent, | |
1ccae449 DM |
373 | format => $zvol->{format}, |
374 | vmid => $zvol->{owner}, | |
7730694e DM |
375 | }; |
376 | } | |
377 | ||
378 | return $list; | |
379 | } | |
380 | ||
381 | sub zfs_find_free_diskname { | |
55525ad2 | 382 | my ($class, $storeid, $scfg, $vmid, $format) = @_; |
7730694e DM |
383 | |
384 | my $name = undef; | |
385 | my $volumes = $class->zfs_list_zvol($scfg); | |
386 | ||
387 | my $disk_ids = {}; | |
388 | my $dat = $volumes->{$scfg->{pool}}; | |
389 | ||
390 | foreach my $image (keys %$dat) { | |
391 | my $volname = $dat->{$image}->{name}; | |
851658c3 | 392 | if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){ |
7730694e DM |
393 | $disk_ids->{$2} = 1; |
394 | } | |
395 | } | |
396 | ||
397 | for (my $i = 1; $i < 100; $i++) { | |
398 | if (!$disk_ids->{$i}) { | |
55525ad2 | 399 | return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i"; |
7730694e DM |
400 | } |
401 | } | |
402 | ||
403 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"; | |
404 | } | |
405 | ||
2fc59177 DM |
406 | sub zfs_get_latest_snapshot { |
407 | my ($class, $scfg, $volname) = @_; | |
408 | ||
851658c3 WL |
409 | my $vname = ($class->parse_volname($volname))[1]; |
410 | ||
2fc59177 DM |
411 | # abort rollback if snapshot is not the latest |
412 | my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation'); | |
281f9587 | 413 | my $text = $class->zfs_request($scfg, undef, 'list', @params); |
2fc59177 DM |
414 | my @snapshots = split(/\n/, $text); |
415 | ||
416 | my $recentsnap; | |
417 | foreach (@snapshots) { | |
851658c3 | 418 | if (/$scfg->{pool}\/$vname/) { |
2fc59177 DM |
419 | s/^.*@//; |
420 | $recentsnap = $_; | |
421 | } | |
422 | } | |
423 | ||
424 | return $recentsnap; | |
425 | } | |
426 | ||
b5e5f7e3 DM |
427 | sub status { |
428 | my ($class, $storeid, $scfg, $cache) = @_; | |
429 | ||
430 | my $total = 0; | |
431 | my $free = 0; | |
432 | my $used = 0; | |
433 | my $active = 0; | |
434 | ||
435 | eval { | |
436 | ($free, $used) = $class->zfs_get_pool_stats($scfg); | |
437 | $active = 1; | |
438 | $total = $free + $used; | |
439 | }; | |
440 | warn $@ if $@; | |
441 | ||
442 | return ($total, $free, $used, $active); | |
443 | } | |
444 | ||
445 | sub volume_size_info { | |
446 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
447 | ||
851658c3 | 448 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
449 | $class->parse_volname($volname); |
450 | ||
451 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
851658c3 | 452 | my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname"); |
79f2b938 DM |
453 | if ($text =~ /\s$attr\s(\d+)\s/) { |
454 | return $1; | |
455 | } | |
456 | ||
457 | die "Could not get zfs volume size\n"; | |
b5e5f7e3 DM |
458 | } |
459 | ||
460 | sub volume_snapshot { | |
f5640e7d | 461 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
b5e5f7e3 | 462 | |
851658c3 WL |
463 | my $vname = ($class->parse_volname($volname))[1]; |
464 | ||
465 | $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap"); | |
b5e5f7e3 DM |
466 | } |
467 | ||
468 | sub volume_snapshot_delete { | |
469 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
470 | ||
851658c3 WL |
471 | my $vname = ($class->parse_volname($volname))[1]; |
472 | ||
473 | $class->deactivate_volume($storeid, $scfg, $vname, $snap, {}); | |
474 | $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap"); | |
b5e5f7e3 DM |
475 | } |
476 | ||
2b40ffae WL |
477 | sub volume_snapshot_rollback { |
478 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
479 | ||
851658c3 WL |
480 | my $vname = ($class->parse_volname($volname))[1]; |
481 | ||
482 | $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap"); | |
1597f1f9 WL |
483 | } |
484 | ||
485 | sub volume_rollback_is_possible { | |
486 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
487 | ||
2fc59177 | 488 | my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); |
2b40ffae | 489 | if ($snap ne $recentsnap) { |
1597f1f9 | 490 | die "can't rollback, more recent snapshots exist\n"; |
2b40ffae WL |
491 | } |
492 | ||
1597f1f9 | 493 | return 1; |
2b40ffae WL |
494 | } |
495 | ||
0a3d992f DM |
496 | sub activate_storage { |
497 | my ($class, $storeid, $scfg, $cache) = @_; | |
86d47239 | 498 | |
93124ef4 DM |
499 | # Note: $scfg->{pool} can include dataset <pool>/<dataset> |
500 | my $pool = $scfg->{pool}; | |
501 | $pool =~ s!/.*$!!; | |
502 | ||
e2e63801 FG |
503 | my @param = ('-o', 'name', '-H', "$pool"); |
504 | my $res; | |
505 | eval { | |
506 | $res = $class->zfs_request($scfg, undef, 'zpool_list', @param); | |
507 | }; | |
508 | ||
509 | if ($@ || !defined($res) || $res !~ $pool) { | |
510 | eval { | |
511 | @param = ('-d', '/dev/disk/by-id/', "$pool"); | |
512 | $class->zfs_request($scfg, undef, 'zpool_import', @param); | |
513 | }; | |
514 | die "could not activate storage '$storeid', $@\n" if $@; | |
86d47239 | 515 | } |
0a3d992f DM |
516 | return 1; |
517 | } | |
518 | ||
519 | sub deactivate_storage { | |
520 | my ($class, $storeid, $scfg, $cache) = @_; | |
521 | return 1; | |
522 | } | |
523 | ||
d4c63dc1 | 524 | sub activate_volume { |
02e797b8 | 525 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
d4c63dc1 WL |
526 | return 1; |
527 | } | |
528 | ||
529 | sub deactivate_volume { | |
02e797b8 | 530 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
d4c63dc1 WL |
531 | return 1; |
532 | } | |
5bb8e010 | 533 | |
d3a282e8 WL |
534 | sub clone_image { |
535 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
536 | ||
537 | $snap ||= '__base__'; | |
538 | ||
55525ad2 | 539 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = |
d3a282e8 WL |
540 | $class->parse_volname($volname); |
541 | ||
542 | die "clone_image only works on base images\n" if !$isBase; | |
543 | ||
55525ad2 | 544 | my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format); |
d3a282e8 | 545 | |
851658c3 WL |
546 | if ($format eq 'subvol') { |
547 | my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename"); | |
548 | chomp($size); | |
549 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size"); | |
550 | } else { | |
551 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); | |
552 | } | |
d3a282e8 | 553 | |
851658c3 | 554 | return "$basename/$name"; |
d3a282e8 WL |
555 | } |
556 | ||
557 | sub create_base { | |
558 | my ($class, $storeid, $scfg, $volname) = @_; | |
559 | ||
560 | my $snap = '__base__'; | |
561 | ||
851658c3 | 562 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
d3a282e8 WL |
563 | $class->parse_volname($volname); |
564 | ||
565 | die "create_base not possible with base image\n" if $isBase; | |
566 | ||
567 | my $newname = $name; | |
851658c3 WL |
568 | if ( $format eq 'subvol' ) { |
569 | $newname =~ s/^subvol-/basevol-/; | |
570 | } else { | |
571 | $newname =~ s/^vm-/base-/; | |
572 | } | |
d3a282e8 WL |
573 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; |
574 | ||
575 | $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); | |
576 | ||
577 | my $running = undef; #fixme : is create_base always offline ? | |
578 | ||
579 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
580 | ||
581 | return $newvolname; | |
582 | } | |
583 | ||
a4034b9f WL |
584 | sub volume_resize { |
585 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
586 | ||
79f2b938 DM |
587 | my $new_size = int($size/1024); |
588 | ||
851658c3 | 589 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
590 | $class->parse_volname($volname); |
591 | ||
592 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
a4034b9f | 593 | |
851658c3 | 594 | $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); |
a4034b9f WL |
595 | |
596 | return $new_size; | |
597 | } | |
598 | ||
2b40ffae WL |
599 | sub volume_has_feature { |
600 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
601 | ||
602 | my $features = { | |
603 | snapshot => { current => 1, snap => 1}, | |
604 | clone => { base => 1}, | |
605 | template => { current => 1}, | |
606 | copy => { base => 1, current => 1}, | |
baafddbd | 607 | sparseinit => { base => 1, current => 1}, |
2b40ffae WL |
608 | }; |
609 | ||
610 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
611 | $class->parse_volname($volname); | |
612 | ||
613 | my $key = undef; | |
614 | ||
615 | if ($snapname) { | |
616 | $key = 'snap'; | |
617 | } else { | |
618 | $key = $isBase ? 'base' : 'current'; | |
619 | } | |
620 | ||
621 | return 1 if $features->{$feature}->{$key}; | |
622 | ||
623 | return undef; | |
624 | } | |
625 | ||
5bb8e010 | 626 | 1; |