]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/ZFS.pm
a9dc3a7cf5e299317c30058735698e6a4f820162
1 package PVE
::API2
::Disks
::ZFS
;
7 use PVE
::JSONSchema
qw(get_standard_option parse_property_string);
9 use PVE
::API2
::Storage
::Config
;
11 use PVE
::Tools
qw(run_command lock_file trim);
13 use PVE
::RPCEnvironment
;
16 use base
qw(PVE::RESTHandler);
18 my $ZPOOL = '/sbin/zpool';
19 my $ZFS = '/sbin/zfs';
22 die "zfsutils-linux not installed\n" if ! -f
$ZPOOL;
24 my $propnames = [qw(name size alloc free frag dedup health)];
34 run_command
([$ZPOOL, 'list', '-HpPLo', join(',', @$propnames)], outfunc
=> sub {
37 my @props = split('\s+', trim
($line));
39 for (my $i = 0; $i < scalar(@$propnames); $i++) {
40 if ($numbers->{$propnames->[$i]}) {
41 $pool->{$propnames->[$i]} = $props[$i] + 0;
43 $pool->{$propnames->[$i]} = $props[$i];
53 __PACKAGE__-
>register_method ({
60 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
62 description
=> "List Zpools.",
64 additionalProperties
=> 0,
66 node
=> get_standard_option
('pve-node'),
104 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
109 return get_pool_data
();
115 if ($el->{children
} && scalar(@{$el->{children
}})) {
117 foreach my $child (@{$el->{children
}}) {
126 __PACKAGE__-
>register_method ({
133 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
135 description
=> "Get details about a zpool.",
137 additionalProperties
=> 0,
139 node
=> get_standard_option
('pve-node'),
140 name
=> get_standard_option
('pve-storage-id'),
148 description
=> 'The name of the zpool.',
152 description
=> 'The state of the zpool.',
157 description
=> 'Information about the state of the zpool.',
162 description
=> 'Information about the recommended action to fix the state.',
167 description
=> 'Information about the last/current scrub.',
171 description
=> 'Information about the errors on the zpool.',
175 description
=> "The pool configuration information, including the vdevs for each section (e.g. spares, cache), may be nested.",
181 description
=> 'The name of the vdev or section.',
186 description
=> 'The state of the vdev.',
202 description
=> 'An optional message about the vdev.'
213 die "zfsutils-linux not installed\n";
216 my $cmd = [$ZPOOL, 'status', '-P', $param->{name
}];
228 run_command
($cmd, outfunc
=> sub {
231 if ($line =~ m/^\s*(\S+): (\S+.*)$/) {
233 $pool->{$curfield} = $2;
235 $config = 0 if $curfield eq 'errors';
236 } elsif (!$config && $line =~ m/^\s+(\S+.*)$/) {
237 $pool->{$curfield} .= " " . $1;
238 } elsif (!$config && $line =~ m/^\s*config:/) {
240 } elsif ($config && $line =~ m/^(\s+)(\S+)\s*(\S+)?(?:\s+(\S+)\s+(\S+)\s+(\S+))?\s*(.*)$/) {
241 my ($space, $name, $state, $read, $write, $cksum, $msg) = ($1, $2, $3, $4, $5, $6, $7);
242 if ($name ne "NAME") {
243 my $lvl = int(length($space) / 2) + 1; # two spaces per level
250 $vdev->{state} = $state if defined($state);
251 $vdev->{read} = $read + 0 if defined($read);
252 $vdev->{write} = $write + 0 if defined($write);
253 $vdev->{cksum
} = $cksum + 0 if defined($cksum);
255 my $cur = pop @$stack;
257 if ($lvl > $curlvl) {
258 $cur->{children
} = [ $vdev ];
259 } elsif ($lvl == $curlvl) {
261 push @{$cur->{children
}}, $vdev;
263 while ($lvl <= $cur->{lvl
} && $cur->{lvl
} != 0) {
266 push @{$cur->{children
}}, $vdev;
276 # change treenodes for extjs tree
277 $pool->{name
} = delete $pool->{pool
};
283 my $draid_config_format = {
287 description
=> 'Number of dRAID spares.',
292 description
=> 'The number of data devices per redundancy group. (dRAID)',
296 __PACKAGE__-
>register_method ({
303 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
305 description
=> "Create a ZFS pool.",
307 additionalProperties
=> 0,
309 node
=> get_standard_option
('pve-node'),
310 name
=> get_standard_option
('pve-storage-id'),
313 description
=> 'The RAID level to use.',
316 'raid10', 'raidz', 'raidz2', 'raidz3',
317 'draid', 'draid2', 'draid3',
321 type
=> 'string', format
=> 'string-list',
322 description
=> 'The block devices you want to create the zpool on.',
326 format
=> $draid_config_format,
335 description
=> 'Pool sector size exponent.',
339 description
=> 'The compression algorithm to use.',
340 enum
=> ['on', 'off', 'gzip', 'lz4', 'lzjb', 'zle', 'zstd'],
345 description
=> "Configure storage using the zpool.",
352 returns
=> { type
=> 'string' },
356 my $rpcenv = PVE
::RPCEnvironment
::get
();
357 my $user = $rpcenv->get_user();
359 my $name = $param->{name
};
360 my $node = $param->{node
};
361 my $devs = [PVE
::Tools
::split_list
($param->{devices
})];
362 my $raidlevel = $param->{raidlevel
};
363 my $compression = $param->{compression
} // 'on';
366 if (exists $param->{'draid-config'}) {
367 die "draid-config set without using dRAID level\n" if $raidlevel !~ m/^draid/;
368 $draid_config = parse_property_string
($draid_config_format, $param->{'draid-config'});
371 for my $dev (@$devs) {
372 $dev = PVE
::Diskmanage
::verify_blockdev_path
($dev);
373 PVE
::Diskmanage
::assert_disk_unused
($dev);
376 my $storage_params = {
380 content
=> 'rootdir,images',
383 my $verify_params = [qw(pool)];
385 if ($param->{add_storage
}) {
386 PVE
::API2
::Storage
::Config-
>create_or_update(
395 my $pools = get_pool_data
();
396 die "pool '${name}' already exists on node '${node}'\n"
397 if grep { $_->{name
} eq $name } @{$pools};
399 my $numdisks = scalar(@$devs);
413 die "raid10 needs an even number of disks\n"
414 if $raidlevel eq 'raid10' && $numdisks % 2 != 0;
416 die "please give only one disk for single disk mode\n"
417 if $raidlevel eq 'single' && $numdisks > 1;
419 die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n"
420 if $numdisks < $mindisks->{$raidlevel};
423 if ($raidlevel =~ m/^draid/) {
424 # bare minimum would be two drives: one for parity & one for data, but forbid that
425 # because it makes no sense in practice, at least one spare disk should be used
426 my $draid_min = $mindisks->{$raidlevel} - 2;
428 $draid_min += $draid_config->{data
} || 0;
429 $draid_min += $draid_config->{spares
} || 0;
431 die "At least $draid_min disks needed for current dRAID config\n"
432 if $numdisks < $draid_min;
436 for my $dev (@$devs) {
437 PVE
::Diskmanage
::assert_disk_unused
($dev);
439 my $is_partition = PVE
::Diskmanage
::is_partition
($dev);
443 PVE
::Diskmanage
::change_parttype
($dev, '6a898cc3-1dd2-11b2-99a6-080020736631');
448 my $sysfsdev = $is_partition ? PVE
::Diskmanage
::get_blockdev
($dev) : $dev;
450 $sysfsdev =~ s!^/dev/!/sys/block/!;
452 my $part = $dev =~ s!^/dev/!!r;
453 $sysfsdev .= "/${part}";
456 my $udevinfo = PVE
::Diskmanage
::get_udev_info
($sysfsdev);
457 $dev = $udevinfo->{by_id_link
} if defined($udevinfo->{by_id_link
});
460 # create zpool with desired raidlevel
461 my $ashift = $param->{ashift
} // 12;
463 my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
465 if ($raidlevel eq 'raid10') {
466 for (my $i = 0; $i < @$devs; $i+=2) {
467 push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1];
469 } elsif ($raidlevel eq 'single') {
470 push @$cmd, $devs->[0];
471 } elsif ($raidlevel =~ m/^draid/) {
472 my $draid_cmd = $raidlevel;
473 $draid_cmd .= ":$draid_config->{data}d" if $draid_config->{data
};
474 $draid_cmd .= ":$draid_config->{spares}s" if $draid_config->{spares
};
475 push @$cmd, $draid_cmd, @$devs;
477 push @$cmd, $raidlevel, @$devs;
480 print "# ", join(' ', @$cmd), "\n";
483 $cmd = [$ZFS, 'set', "compression=$compression", $name];
484 print "# ", join(' ', @$cmd), "\n";
487 if (-e
'/lib/systemd/system/zfs-import@.service') {
488 my $importunit = 'zfs-import@'. PVE
::Systemd
::escape_unit
($name, undef) . '.service';
489 $cmd = ['systemctl', 'enable', $importunit];
490 print "# ", join(' ', @$cmd), "\n";
494 PVE
::Diskmanage
::udevadm_trigger
($devs->@*);
496 if ($param->{add_storage
}) {
497 PVE
::API2
::Storage
::Config-
>create_or_update(
506 return $rpcenv->fork_worker('zfscreate', $name, $user, sub {
507 PVE
::Diskmanage
::locked_disk_action
($code);
511 __PACKAGE__-
>register_method ({
518 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
520 description
=> "Destroy a ZFS pool.",
522 additionalProperties
=> 0,
524 node
=> get_standard_option
('pve-node'),
525 name
=> get_standard_option
('pve-storage-id'),
526 'cleanup-config' => {
527 description
=> "Marks associated storage(s) as not available on this node anymore ".
528 "or removes them from the configuration (if configured for this node only).",
534 description
=> "Also wipe disks so they can be repurposed afterwards.",
541 returns
=> { type
=> 'string' },
545 my $rpcenv = PVE
::RPCEnvironment
::get
();
546 my $user = $rpcenv->get_user();
548 my $name = $param->{name
};
549 my $node = $param->{node
};
552 PVE
::Diskmanage
::locked_disk_action
(sub {
554 if ($param->{'cleanup-disks'}) {
555 # Using -o name does not only output the name in combination with -v.
556 run_command
(['zpool', 'list', '-vHPL', $name], outfunc
=> sub {
559 my ($name) = PVE
::Tools
::split_list
($line);
560 return if $name !~ m
|^/dev/.+|;
562 my $dev = PVE
::Diskmanage
::verify_blockdev_path
($name);
566 my $info = PVE
::Diskmanage
::get_disks
($dev, 1, 1);
567 die "unable to obtain information for disk '$dev'\n" if !$info->{$dev};
569 # Wipe whole disk if usual ZFS layout with partition 9 as ZFS reserved.
570 my $parent = $info->{$dev}->{parent
};
571 if ($parent && scalar(keys $info->%*) == 3) {
572 $parent =~ s
|^/dev/||;
573 my $info9 = $info->{"${parent}9"};
575 $wipe = $info->{$dev}->{parent
} # need leading /dev/
576 if $info9 && $info9->{used
} && $info9->{used
} =~ m/^ZFS reserved/;
579 push $to_wipe->@*, $wipe;
583 if (-e
'/lib/systemd/system/zfs-import@.service') {
584 my $importunit = 'zfs-import@' . PVE
::Systemd
::escape_unit
($name) . '.service';
585 run_command
(['systemctl', 'disable', $importunit]);
588 run_command
(['zpool', 'destroy', $name]);
591 if ($param->{'cleanup-config'}) {
594 return $scfg->{type
} eq 'zfspool' && $scfg->{pool
} eq $name;
596 eval { PVE
::API2
::Storage
::Config-
>cleanup_storages_for_node($match, $node); };
597 warn $config_err = $@ if $@;
600 eval { PVE
::Diskmanage
::wipe_blockdev
($_) for $to_wipe->@*; };
602 PVE
::Diskmanage
::udevadm_trigger
($to_wipe->@*);
603 die "cleanup failed - $err" if $err;
605 die "config cleanup failed - $config_err" if $config_err;
609 return $rpcenv->fork_worker('zfsremove', $name, $user, $worker);