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