]>
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; |
aefe82ea | 10 | use Net::IP; |
5bb8e010 DM |
11 | |
12 | use base qw(PVE::Storage::Plugin); | |
13 | ||
5bb8e010 | 14 | sub type { |
85fda4dd | 15 | return 'zfspool'; |
5bb8e010 DM |
16 | } |
17 | ||
18 | sub plugindata { | |
19 | return { | |
1ccae449 DM |
20 | content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}], |
21 | format => [ { raw => 1, subvol => 1 } , 'raw' ], | |
5bb8e010 | 22 | }; |
85fda4dd | 23 | } |
5bb8e010 | 24 | |
7730694e DM |
25 | sub properties { |
26 | return { | |
27 | blocksize => { | |
28 | description => "block size", | |
29 | type => 'string', | |
30 | }, | |
31 | sparse => { | |
32 | description => "use sparse volumes", | |
33 | type => 'boolean', | |
34 | }, | |
35 | }; | |
36 | } | |
37 | ||
5bb8e010 DM |
38 | sub options { |
39 | return { | |
7730694e DM |
40 | pool => { fixed => 1 }, |
41 | blocksize => { optional => 1 }, | |
42 | sparse => { optional => 1 }, | |
43 | nodes => { optional => 1 }, | |
5bb8e010 | 44 | disable => { optional => 1 }, |
5bb8e010 | 45 | content => { optional => 1 }, |
9edb99a5 | 46 | bwlimit => { optional => 1 }, |
5bb8e010 DM |
47 | }; |
48 | } | |
49 | ||
7730694e DM |
50 | # static zfs helper methods |
51 | ||
060ef890 DM |
52 | sub zfs_parse_size { |
53 | my ($text) = @_; | |
54 | ||
55 | return 0 if !$text; | |
56 | ||
57 | if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) { | |
58 | ||
59 | my ($size, $reminder, $unit) = ($1, $2, $3); | |
60 | ||
61 | if ($unit) { | |
62 | if ($unit eq 'K') { | |
63 | $size *= 1024; | |
64 | } elsif ($unit eq 'M') { | |
65 | $size *= 1024*1024; | |
66 | } elsif ($unit eq 'G') { | |
67 | $size *= 1024*1024*1024; | |
68 | } elsif ($unit eq 'T') { | |
69 | $size *= 1024*1024*1024*1024; | |
70 | } else { | |
71 | die "got unknown zfs size unit '$unit'\n"; | |
72 | } | |
73 | } | |
74 | ||
75 | if ($reminder) { | |
76 | $size = ceil($size); | |
77 | } | |
78 | ||
79 | return $size; | |
80 | ||
81 | } | |
82 | ||
83 | warn "unable to parse zfs size '$text'\n"; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
7730694e DM |
88 | sub zfs_parse_zvol_list { |
89 | my ($text) = @_; | |
90 | ||
91 | my $list = (); | |
92 | ||
93 | return $list if !$text; | |
94 | ||
95 | my @lines = split /\n/, $text; | |
96 | foreach my $line (@lines) { | |
1ccae449 DM |
97 | my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line); |
98 | next if !($type eq 'volume' || $type eq 'filesystem'); | |
99 | ||
100 | my $zvol = {}; | |
101 | my @parts = split /\//, $dataset; | |
dec97937 | 102 | next if scalar(@parts) < 2; # we need pool/name |
1ccae449 DM |
103 | my $name = pop @parts; |
104 | my $pool = join('/', @parts); | |
105 | ||
851658c3 | 106 | next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!; |
1ccae449 DM |
107 | $zvol->{owner} = $2; |
108 | ||
1ccae449 DM |
109 | $zvol->{pool} = $pool; |
110 | $zvol->{name} = $name; | |
111 | if ($type eq 'filesystem') { | |
112 | if ($refquota eq 'none') { | |
113 | $zvol->{size} = 0; | |
114 | } else { | |
115 | $zvol->{size} = zfs_parse_size($refquota); | |
7730694e | 116 | } |
1ccae449 DM |
117 | $zvol->{format} = 'subvol'; |
118 | } else { | |
119 | $zvol->{size} = zfs_parse_size($size); | |
120 | $zvol->{format} = 'raw'; | |
7730694e | 121 | } |
1ccae449 DM |
122 | if ($origin !~ /^-$/) { |
123 | $zvol->{origin} = $origin; | |
124 | } | |
125 | push @$list, $zvol; | |
7730694e DM |
126 | } |
127 | ||
128 | return $list; | |
129 | } | |
130 | ||
cc80ed9c WL |
131 | sub parse_volname { |
132 | my ($class, $volname) = @_; | |
133 | ||
8e5b96ca DM |
134 | if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) { |
135 | my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw'; | |
136 | my $isBase = ($6 eq 'base' || $6 eq 'basevol'); | |
137 | return ('images', $5, $7, $2, $4, $isBase, $format); | |
cc80ed9c WL |
138 | } |
139 | ||
140 | die "unable to parse zfs volume name '$volname'\n"; | |
141 | } | |
142 | ||
7730694e DM |
143 | # virtual zfs methods (subclass can overwrite them) |
144 | ||
f3e632d0 | 145 | sub path { |
e67069eb | 146 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
f3e632d0 WL |
147 | |
148 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
149 | ||
150 | my $path = ''; | |
151 | ||
1ccae449 | 152 | if ($vtype eq "images") { |
74b724a6 | 153 | if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { |
f482231e | 154 | # fixme: we currently assume standard mount point?! |
851658c3 | 155 | $path = "/$scfg->{pool}/$name"; |
1ccae449 | 156 | } else { |
851658c3 | 157 | $path = "/dev/zvol/$scfg->{pool}/$name"; |
1ccae449 | 158 | } |
f482231e | 159 | $path .= "\@$snapname" if defined($snapname); |
f3e632d0 | 160 | } else { |
85fda4dd | 161 | die "$vtype is not allowed in ZFSPool!"; |
f3e632d0 WL |
162 | } |
163 | ||
164 | return ($path, $vmid, $vtype); | |
165 | } | |
166 | ||
7730694e | 167 | sub zfs_request { |
44257d2e | 168 | my ($class, $scfg, $timeout, $method, @params) = @_; |
7730694e | 169 | |
7730694e DM |
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'; | |
e9ab8ea3 | 176 | $timeout = 15 if !$timeout || $timeout < 15; |
7730694e DM |
177 | } else { |
178 | push @$cmd, 'zfs', $method; | |
179 | } | |
7730694e | 180 | push @$cmd, @params; |
7730694e | 181 | |
a10695b4 TL |
182 | my $msg = ''; |
183 | my $output = sub { $msg .= "$_[0]\n" }; | |
7730694e | 184 | |
a10695b4 | 185 | $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout; |
72bdeea1 | 186 | |
1f390a30 | 187 | run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); |
7730694e DM |
188 | |
189 | return $msg; | |
190 | } | |
191 | ||
b3ba95e4 WL |
192 | sub alloc_image { |
193 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
194 | ||
1ccae449 DM |
195 | my $volname = $name; |
196 | ||
197 | if ($fmt eq 'raw') { | |
b3ba95e4 | 198 | |
fc05c9a0 | 199 | die "illegal name '$volname' - should be 'vm-$vmid-*'\n" |
1ccae449 | 200 | if $volname && $volname !~ m/^vm-$vmid-/; |
55525ad2 | 201 | $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) |
1ccae449 | 202 | if !$volname; |
b3ba95e4 | 203 | |
1ccae449 DM |
204 | $class->zfs_create_zvol($scfg, $volname, $size); |
205 | my $devname = "/dev/zvol/$scfg->{pool}/$volname"; | |
82e08809 | 206 | |
cf8ded6a | 207 | my $timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10; |
894b9ecc WL |
208 | for (my $i = 1; $i <= $timeout; $i++) { |
209 | last if -b $devname; | |
210 | die "Timeout: no zvol after $timeout sec found.\n" | |
211 | if $i == $timeout; | |
76fd7dc7 | 212 | |
894b9ecc WL |
213 | sleep(1); |
214 | } | |
1ccae449 | 215 | } elsif ( $fmt eq 'subvol') { |
55525ad2 | 216 | |
fc05c9a0 | 217 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" |
55525ad2 DM |
218 | if $volname && $volname !~ m/^subvol-$vmid-/; |
219 | $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) | |
220 | if !$volname; | |
221 | ||
fc05c9a0 | 222 | die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" |
1ccae449 | 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 | ||
44257d2e | 287 | my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp', |
7730694e DM |
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) = @_; | |
cdef3abb ML |
305 | |
306 | # always align size to 1M as workaround until | |
307 | # https://github.com/zfsonlinux/zfs/issues/8541 is solved | |
308 | my $padding = (1024 - $size % 1024) % 1024; | |
309 | $size = $size + $padding; | |
310 | ||
7730694e DM |
311 | my $cmd = ['create']; |
312 | ||
313 | push @$cmd, '-s' if $scfg->{sparse}; | |
314 | ||
315 | push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize}; | |
316 | ||
317 | push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol"; | |
318 | ||
44257d2e | 319 | $class->zfs_request($scfg, undef, @$cmd); |
7730694e DM |
320 | } |
321 | ||
1ccae449 DM |
322 | sub zfs_create_subvol { |
323 | my ($class, $scfg, $volname, $size) = @_; | |
324 | ||
325 | my $dataset = "$scfg->{pool}/$volname"; | |
326 | ||
efaf4017 DM |
327 | my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', |
328 | '-o', "refquota=${size}k", $dataset]; | |
1ccae449 | 329 | |
44257d2e | 330 | $class->zfs_request($scfg, undef, @$cmd); |
1ccae449 DM |
331 | } |
332 | ||
7730694e DM |
333 | sub zfs_delete_zvol { |
334 | my ($class, $scfg, $zvol) = @_; | |
335 | ||
1f390a30 WL |
336 | my $err; |
337 | ||
338 | for (my $i = 0; $i < 6; $i++) { | |
339 | ||
44257d2e | 340 | eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); }; |
1f390a30 WL |
341 | if ($err = $@) { |
342 | if ($err =~ m/^zfs error:(.*): dataset is busy.*/) { | |
343 | sleep(1); | |
27ff0e99 WL |
344 | } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) { |
345 | $err = undef; | |
346 | last; | |
1f390a30 WL |
347 | } else { |
348 | die $err; | |
349 | } | |
350 | } else { | |
351 | last; | |
352 | } | |
353 | } | |
354 | ||
355 | die $err if $err; | |
7730694e DM |
356 | } |
357 | ||
358 | sub zfs_list_zvol { | |
359 | my ($class, $scfg) = @_; | |
360 | ||
44257d2e | 361 | my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr'); |
7730694e DM |
362 | my $zvols = zfs_parse_zvol_list($text); |
363 | return undef if !$zvols; | |
364 | ||
365 | my $list = (); | |
366 | foreach my $zvol (@$zvols) { | |
1ccae449 DM |
367 | my $pool = $zvol->{pool}; |
368 | my $name = $zvol->{name}; | |
7730694e DM |
369 | my $parent = $zvol->{origin}; |
370 | if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){ | |
371 | $parent = $1; | |
372 | } | |
373 | ||
1ccae449 DM |
374 | $list->{$pool}->{$name} = { |
375 | name => $name, | |
7730694e DM |
376 | size => $zvol->{size}, |
377 | parent => $parent, | |
1ccae449 DM |
378 | format => $zvol->{format}, |
379 | vmid => $zvol->{owner}, | |
7730694e DM |
380 | }; |
381 | } | |
382 | ||
383 | return $list; | |
384 | } | |
385 | ||
386 | sub zfs_find_free_diskname { | |
55525ad2 | 387 | my ($class, $storeid, $scfg, $vmid, $format) = @_; |
7730694e | 388 | |
7730694e | 389 | my $volumes = $class->zfs_list_zvol($scfg); |
7730694e DM |
390 | my $dat = $volumes->{$scfg->{pool}}; |
391 | ||
c4a29df4 SI |
392 | my $disk_list = [ keys %$dat ]; |
393 | return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg); | |
7730694e DM |
394 | } |
395 | ||
2fc59177 DM |
396 | sub zfs_get_latest_snapshot { |
397 | my ($class, $scfg, $volname) = @_; | |
398 | ||
851658c3 WL |
399 | my $vname = ($class->parse_volname($volname))[1]; |
400 | ||
2fc59177 DM |
401 | # abort rollback if snapshot is not the latest |
402 | my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation'); | |
44257d2e | 403 | my $text = $class->zfs_request($scfg, undef, 'list', @params); |
2fc59177 DM |
404 | my @snapshots = split(/\n/, $text); |
405 | ||
406 | my $recentsnap; | |
407 | foreach (@snapshots) { | |
851658c3 | 408 | if (/$scfg->{pool}\/$vname/) { |
2fc59177 DM |
409 | s/^.*@//; |
410 | $recentsnap = $_; | |
411 | } | |
412 | } | |
413 | ||
414 | return $recentsnap; | |
415 | } | |
416 | ||
b5e5f7e3 DM |
417 | sub status { |
418 | my ($class, $storeid, $scfg, $cache) = @_; | |
419 | ||
420 | my $total = 0; | |
421 | my $free = 0; | |
422 | my $used = 0; | |
423 | my $active = 0; | |
424 | ||
425 | eval { | |
426 | ($free, $used) = $class->zfs_get_pool_stats($scfg); | |
427 | $active = 1; | |
428 | $total = $free + $used; | |
429 | }; | |
430 | warn $@ if $@; | |
431 | ||
432 | return ($total, $free, $used, $active); | |
433 | } | |
434 | ||
435 | sub volume_size_info { | |
436 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
437 | ||
851658c3 | 438 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
439 | $class->parse_volname($volname); |
440 | ||
441 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
44257d2e | 442 | my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname"); |
79f2b938 DM |
443 | if ($text =~ /\s$attr\s(\d+)\s/) { |
444 | return $1; | |
445 | } | |
446 | ||
447 | die "Could not get zfs volume size\n"; | |
b5e5f7e3 DM |
448 | } |
449 | ||
450 | sub volume_snapshot { | |
f5640e7d | 451 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
b5e5f7e3 | 452 | |
851658c3 WL |
453 | my $vname = ($class->parse_volname($volname))[1]; |
454 | ||
44257d2e | 455 | $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap"); |
b5e5f7e3 DM |
456 | } |
457 | ||
458 | sub volume_snapshot_delete { | |
459 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
460 | ||
851658c3 WL |
461 | my $vname = ($class->parse_volname($volname))[1]; |
462 | ||
463 | $class->deactivate_volume($storeid, $scfg, $vname, $snap, {}); | |
44257d2e | 464 | $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap"); |
b5e5f7e3 DM |
465 | } |
466 | ||
2b40ffae WL |
467 | sub volume_snapshot_rollback { |
468 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
469 | ||
851658c3 WL |
470 | my $vname = ($class->parse_volname($volname))[1]; |
471 | ||
44257d2e | 472 | $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap"); |
1597f1f9 WL |
473 | } |
474 | ||
475 | sub volume_rollback_is_possible { | |
476 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
477 | ||
2fc59177 | 478 | my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); |
2b40ffae | 479 | if ($snap ne $recentsnap) { |
1597f1f9 | 480 | die "can't rollback, more recent snapshots exist\n"; |
2b40ffae WL |
481 | } |
482 | ||
1597f1f9 | 483 | return 1; |
2b40ffae WL |
484 | } |
485 | ||
aefe82ea | 486 | sub volume_snapshot_list { |
8b622c2d | 487 | my ($class, $scfg, $storeid, $volname) = @_; |
aefe82ea WL |
488 | |
489 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
490 | ||
491 | my $zpath = "$scfg->{pool}/$name"; | |
492 | ||
aefe82ea WL |
493 | my $snaps = []; |
494 | ||
495 | my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o', | |
496 | 'name', $zpath]; | |
497 | ||
aefe82ea WL |
498 | my $outfunc = sub { |
499 | my $line = shift; | |
500 | ||
8b622c2d | 501 | if ($line =~ m/^\Q$zpath\E@(.*)$/) { |
aefe82ea WL |
502 | push @$snaps, $1; |
503 | } | |
504 | }; | |
505 | ||
506 | eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); }; | |
507 | ||
508 | # return an empty array if dataset does not exist. | |
509 | return $snaps; | |
510 | } | |
511 | ||
0a3d992f DM |
512 | sub activate_storage { |
513 | my ($class, $storeid, $scfg, $cache) = @_; | |
86d47239 | 514 | |
93124ef4 DM |
515 | # Note: $scfg->{pool} can include dataset <pool>/<dataset> |
516 | my $pool = $scfg->{pool}; | |
517 | $pool =~ s!/.*$!!; | |
518 | ||
e2e63801 FG |
519 | my @param = ('-o', 'name', '-H', "$pool"); |
520 | my $res; | |
521 | eval { | |
44257d2e | 522 | $res = $class->zfs_request($scfg, undef, 'zpool_list', @param); |
e2e63801 | 523 | }; |
dc18abe0 WL |
524 | my $err = $@; |
525 | if ($err || !defined($res) || $res !~ $pool) { | |
e2e63801 | 526 | eval { |
c6f13155 | 527 | @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool"); |
44257d2e | 528 | $class->zfs_request($scfg, undef, 'zpool_import', @param); |
e2e63801 | 529 | }; |
dc18abe0 WL |
530 | if ($@) { |
531 | warn "$err\n"; | |
532 | die "could not activate storage '$storeid', $@\n"; | |
533 | } | |
86d47239 | 534 | } |
0a3d992f DM |
535 | return 1; |
536 | } | |
537 | ||
538 | sub deactivate_storage { | |
539 | my ($class, $storeid, $scfg, $cache) = @_; | |
540 | return 1; | |
541 | } | |
542 | ||
d4c63dc1 | 543 | sub activate_volume { |
02e797b8 | 544 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
d4c63dc1 WL |
545 | return 1; |
546 | } | |
547 | ||
548 | sub deactivate_volume { | |
02e797b8 | 549 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
d4c63dc1 WL |
550 | return 1; |
551 | } | |
5bb8e010 | 552 | |
d3a282e8 WL |
553 | sub clone_image { |
554 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
555 | ||
556 | $snap ||= '__base__'; | |
557 | ||
55525ad2 | 558 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = |
d3a282e8 WL |
559 | $class->parse_volname($volname); |
560 | ||
561 | die "clone_image only works on base images\n" if !$isBase; | |
562 | ||
55525ad2 | 563 | my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format); |
d3a282e8 | 564 | |
851658c3 | 565 | if ($format eq 'subvol') { |
44257d2e | 566 | my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename"); |
851658c3 | 567 | chomp($size); |
44257d2e | 568 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size"); |
851658c3 | 569 | } else { |
44257d2e | 570 | $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); |
851658c3 | 571 | } |
d3a282e8 | 572 | |
851658c3 | 573 | return "$basename/$name"; |
d3a282e8 WL |
574 | } |
575 | ||
576 | sub create_base { | |
577 | my ($class, $storeid, $scfg, $volname) = @_; | |
578 | ||
579 | my $snap = '__base__'; | |
580 | ||
851658c3 | 581 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
d3a282e8 WL |
582 | $class->parse_volname($volname); |
583 | ||
584 | die "create_base not possible with base image\n" if $isBase; | |
585 | ||
586 | my $newname = $name; | |
851658c3 WL |
587 | if ( $format eq 'subvol' ) { |
588 | $newname =~ s/^subvol-/basevol-/; | |
589 | } else { | |
590 | $newname =~ s/^vm-/base-/; | |
591 | } | |
d3a282e8 WL |
592 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; |
593 | ||
44257d2e | 594 | $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); |
d3a282e8 WL |
595 | |
596 | my $running = undef; #fixme : is create_base always offline ? | |
597 | ||
598 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
599 | ||
600 | return $newvolname; | |
601 | } | |
602 | ||
a4034b9f WL |
603 | sub volume_resize { |
604 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
605 | ||
79f2b938 DM |
606 | my $new_size = int($size/1024); |
607 | ||
851658c3 | 608 | my (undef, $vname, undef, undef, undef, undef, $format) = |
79f2b938 DM |
609 | $class->parse_volname($volname); |
610 | ||
611 | my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; | |
a4034b9f | 612 | |
44257d2e | 613 | $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); |
a4034b9f WL |
614 | |
615 | return $new_size; | |
616 | } | |
617 | ||
7118dd91 DM |
618 | sub storage_can_replicate { |
619 | my ($class, $scfg, $storeid, $format) = @_; | |
620 | ||
621 | return 1 if $format eq 'raw' || $format eq 'subvol'; | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
2b40ffae WL |
626 | sub volume_has_feature { |
627 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
628 | ||
629 | my $features = { | |
630 | snapshot => { current => 1, snap => 1}, | |
631 | clone => { base => 1}, | |
632 | template => { current => 1}, | |
633 | copy => { base => 1, current => 1}, | |
baafddbd | 634 | sparseinit => { base => 1, current => 1}, |
f189504c | 635 | replicate => { base => 1, current => 1}, |
2b40ffae WL |
636 | }; |
637 | ||
638 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
639 | $class->parse_volname($volname); | |
640 | ||
641 | my $key = undef; | |
642 | ||
643 | if ($snapname) { | |
644 | $key = 'snap'; | |
645 | } else { | |
646 | $key = $isBase ? 'base' : 'current'; | |
647 | } | |
648 | ||
649 | return 1 if $features->{$feature}->{$key}; | |
650 | ||
651 | return undef; | |
652 | } | |
653 | ||
47f37b53 WB |
654 | sub volume_export { |
655 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
656 | ||
657 | die "unsupported export stream format for $class: $format\n" | |
658 | if $format ne 'zfs'; | |
659 | ||
660 | die "$class storage can only export snapshots\n" | |
661 | if !defined($snapshot); | |
662 | ||
9a75947b WL |
663 | my $dataset = ($class->parse_volname($volname))[1]; |
664 | ||
47f37b53 WB |
665 | my $fd = fileno($fh); |
666 | die "internal error: invalid file handle for volume_export\n" | |
667 | if !defined($fd); | |
668 | $fd = ">&$fd"; | |
669 | ||
670 | # For zfs we always create a replication stream (-R) which means the remote | |
671 | # side will always delete non-existing source snapshots. This should work | |
672 | # for all our use cases. | |
673 | my $cmd = ['zfs', 'send', '-Rpv']; | |
674 | if (defined($base_snapshot)) { | |
675 | my $arg = $with_snapshots ? '-I' : '-i'; | |
676 | push @$cmd, $arg, $base_snapshot; | |
677 | } | |
9a75947b | 678 | push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot"; |
47f37b53 WB |
679 | |
680 | run_command($cmd, output => $fd); | |
681 | ||
682 | return; | |
683 | } | |
684 | ||
d390328b WB |
685 | sub volume_export_formats { |
686 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
687 | ||
688 | my @formats = ('zfs'); | |
689 | # TODOs: | |
690 | # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/; | |
691 | # push @formats, 'raw' if !$base_snapshot && !$with_snapshots; | |
692 | return @formats; | |
693 | } | |
694 | ||
47f37b53 WB |
695 | sub volume_import { |
696 | my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_; | |
697 | ||
698 | die "unsupported import stream format for $class: $format\n" | |
699 | if $format ne 'zfs'; | |
700 | ||
701 | my $fd = fileno($fh); | |
702 | die "internal error: invalid file handle for volume_import\n" | |
703 | if !defined($fd); | |
704 | ||
9a75947b WL |
705 | my $dataset = ($class->parse_volname($volname))[1]; |
706 | my $zfspath = "$scfg->{pool}/$dataset"; | |
47f37b53 WB |
707 | my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : ''; |
708 | my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix], | |
709 | noerr => 1, errfunc => sub {}); | |
710 | if (defined($base_snapshot)) { | |
711 | die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists; | |
712 | } else { | |
713 | die "volume '$zfspath' already exists\n" if $exists; | |
714 | } | |
715 | ||
716 | eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") }; | |
717 | if (my $err = $@) { | |
718 | if (defined($base_snapshot)) { | |
719 | eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) }; | |
720 | } else { | |
721 | eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) }; | |
722 | } | |
723 | die $err; | |
724 | } | |
725 | ||
726 | return; | |
727 | } | |
728 | ||
d390328b WB |
729 | sub volume_import_formats { |
730 | my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_; | |
731 | ||
732 | return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots); | |
733 | } | |
734 | ||
5bb8e010 | 735 | 1; |