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