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