]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/ZFS.pm
1 package PVE
::API2
::Disks
::ZFS
;
7 use PVE
::JSONSchema
qw(get_standard_option);
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';
23 die "zfsutils-linux not installed\n";
26 my $propnames = [qw(name size alloc free frag dedup health)];
35 my $cmd = [$ZPOOL,'list', '-HpPLo', join(',', @$propnames)];
39 run_command
($cmd, outfunc
=> sub {
42 my @props = split('\s+', trim
($line));
44 for (my $i = 0; $i < scalar(@$propnames); $i++) {
45 if ($numbers->{$propnames->[$i]}) {
46 $pool->{$propnames->[$i]} = $props[$i] + 0;
48 $pool->{$propnames->[$i]} = $props[$i];
58 __PACKAGE__-
>register_method ({
65 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
67 description
=> "List Zpools.",
69 additionalProperties
=> 0,
71 node
=> get_standard_option
('pve-node'),
109 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
114 return get_pool_data
();
120 if ($el->{children
} && scalar(@{$el->{children
}})) {
122 foreach my $child (@{$el->{children
}}) {
131 __PACKAGE__-
>register_method ({
138 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
140 description
=> "Get details about a zpool.",
142 additionalProperties
=> 0,
144 node
=> get_standard_option
('pve-node'),
145 name
=> get_standard_option
('pve-storage-id'),
153 description
=> 'The name of the zpool.',
157 description
=> 'The state of the zpool.',
162 description
=> 'Information about the state of the zpool.',
167 description
=> 'Information about the recommended action to fix the state.',
172 description
=> 'Information about the last/current scrub.',
176 description
=> 'Information about the errors on the zpool.',
180 description
=> "The pool configuration information, including the vdevs for each section (e.g. spares, cache), may be nested.",
186 description
=> 'The name of the vdev or section.',
191 description
=> 'The state of the vdev.',
207 description
=> 'An optional message about the vdev.'
218 die "zfsutils-linux not installed\n";
221 my $cmd = [$ZPOOL, 'status', '-P', $param->{name
}];
233 run_command
($cmd, outfunc
=> sub {
236 if ($line =~ m/^\s*(\S+): (\S+.*)$/) {
238 $pool->{$curfield} = $2;
240 $config = 0 if $curfield eq 'errors';
241 } elsif (!$config && $line =~ m/^\s+(\S+.*)$/) {
242 $pool->{$curfield} .= " " . $1;
243 } elsif (!$config && $line =~ m/^\s*config:/) {
245 } elsif ($config && $line =~ m/^(\s+)(\S+)\s*(\S+)?(?:\s+(\S+)\s+(\S+)\s+(\S+))?\s*(.*)$/) {
246 my ($space, $name, $state, $read, $write, $cksum, $msg) = ($1, $2, $3, $4, $5, $6, $7);
247 if ($name ne "NAME") {
248 my $lvl = int(length($space) / 2) + 1; # two spaces per level
255 $vdev->{state} = $state if defined($state);
256 $vdev->{read} = $read + 0 if defined($read);
257 $vdev->{write} = $write + 0 if defined($write);
258 $vdev->{cksum
} = $cksum + 0 if defined($cksum);
260 my $cur = pop @$stack;
262 if ($lvl > $curlvl) {
263 $cur->{children
} = [ $vdev ];
264 } elsif ($lvl == $curlvl) {
266 push @{$cur->{children
}}, $vdev;
268 while ($lvl <= $cur->{lvl
} && $cur->{lvl
} != 0) {
271 push @{$cur->{children
}}, $vdev;
281 # change treenodes for extjs tree
282 $pool->{name
} = delete $pool->{pool
};
288 __PACKAGE__-
>register_method ({
295 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
297 description
=> "Create a ZFS pool.",
299 additionalProperties
=> 0,
301 node
=> get_standard_option
('pve-node'),
302 name
=> get_standard_option
('pve-storage-id'),
305 description
=> 'The RAID level to use.',
306 enum
=> ['single', 'mirror', 'raid10', 'raidz', 'raidz2', 'raidz3'],
309 type
=> 'string', format
=> 'string-list',
310 description
=> 'The block devices you want to create the zpool on.',
318 description
=> 'Pool sector size exponent.',
322 description
=> 'The compression algorithm to use.',
323 enum
=> ['on', 'off', 'gzip', 'lz4', 'lzjb', 'zle', 'zstd'],
328 description
=> "Configure storage using the zpool.",
335 returns
=> { type
=> 'string' },
339 my $rpcenv = PVE
::RPCEnvironment
::get
();
340 my $user = $rpcenv->get_user();
342 my $name = $param->{name
};
343 my $node = $param->{node
};
344 my $devs = [PVE
::Tools
::split_list
($param->{devices
})];
345 my $node = $param->{node
};
346 my $raidlevel = $param->{raidlevel
};
347 my $compression = $param->{compression
} // 'on';
349 for my $dev (@$devs) {
350 $dev = PVE
::Diskmanage
::verify_blockdev_path
($dev);
351 PVE
::Diskmanage
::assert_disk_unused
($dev);
354 my $storage_params = {
358 content
=> 'rootdir,images',
361 my $verify_params = [qw(pool)];
363 if ($param->{add_storage
}) {
364 PVE
::API2
::Storage
::Config-
>create_or_update(
373 my $pools = get_pool_data
();
374 die "pool '${name}' already exists on node '${node}'\n"
375 if grep { $_->{name
} eq $name } @{$pools};
377 my $numdisks = scalar(@$devs);
388 die "raid10 needs an even number of disks\n"
389 if $raidlevel eq 'raid10' && $numdisks % 2 != 0;
391 die "please give only one disk for single disk mode\n"
392 if $raidlevel eq 'single' && $numdisks > 1;
394 die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n"
395 if $numdisks < $mindisks->{$raidlevel};
398 for my $dev (@$devs) {
399 PVE
::Diskmanage
::assert_disk_unused
($dev);
401 my $is_partition = PVE
::Diskmanage
::is_partition
($dev);
405 PVE
::Diskmanage
::change_parttype
($dev, '6a898cc3-1dd2-11b2-99a6-080020736631');
410 my $sysfsdev = $is_partition ? PVE
::Diskmanage
::get_blockdev
($dev) : $dev;
412 $sysfsdev =~ s!^/dev/!/sys/block/!;
414 my $part = $dev =~ s!^/dev/!!r;
415 $sysfsdev .= "/${part}";
418 my $udevinfo = PVE
::Diskmanage
::get_udev_info
($sysfsdev);
419 $dev = $udevinfo->{by_id_link
} if defined($udevinfo->{by_id_link
});
422 # create zpool with desired raidlevel
423 my $ashift = $param->{ashift
} // 12;
425 my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
427 if ($raidlevel eq 'raid10') {
428 for (my $i = 0; $i < @$devs; $i+=2) {
429 push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1];
431 } elsif ($raidlevel eq 'single') {
432 push @$cmd, $devs->[0];
434 push @$cmd, $raidlevel, @$devs;
437 print "# ", join(' ', @$cmd), "\n";
440 $cmd = [$ZFS, 'set', "compression=$compression", $name];
441 print "# ", join(' ', @$cmd), "\n";
444 if (-e
'/lib/systemd/system/zfs-import@.service') {
445 my $importunit = 'zfs-import@'. PVE
::Systemd
::escape_unit
($name, undef) . '.service';
446 $cmd = ['systemctl', 'enable', $importunit];
447 print "# ", join(' ', @$cmd), "\n";
451 PVE
::Diskmanage
::udevadm_trigger
($devs->@*);
453 if ($param->{add_storage
}) {
454 PVE
::API2
::Storage
::Config-
>create_or_update(
463 return $rpcenv->fork_worker('zfscreate', $name, $user, sub {
464 PVE
::Diskmanage
::locked_disk_action
($code);
468 __PACKAGE__-
>register_method ({
475 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
477 description
=> "Destroy a ZFS pool.",
479 additionalProperties
=> 0,
481 node
=> get_standard_option
('pve-node'),
482 name
=> get_standard_option
('pve-storage-id'),
483 'cleanup-config' => {
484 description
=> "Marks associated storage(s) as not available on this node anymore ".
485 "or removes them from the configuration (if configured for this node only).",
491 description
=> "Also wipe disks so they can be repurposed afterwards.",
498 returns
=> { type
=> 'string' },
502 my $rpcenv = PVE
::RPCEnvironment
::get
();
503 my $user = $rpcenv->get_user();
505 my $name = $param->{name
};
506 my $node = $param->{node
};
509 PVE
::Diskmanage
::locked_disk_action
(sub {
511 if ($param->{'cleanup-disks'}) {
512 # Using -o name does not only output the name in combination with -v.
513 run_command
(['zpool', 'list', '-vHPL', $name], outfunc
=> sub {
516 my ($name) = PVE
::Tools
::split_list
($line);
517 return if $name !~ m
|^/dev/.+|;
519 my $dev = PVE
::Diskmanage
::verify_blockdev_path
($name);
523 my $info = PVE
::Diskmanage
::get_disks
($dev, 1, 1);
524 die "unable to obtain information for disk '$dev'\n" if !$info->{$dev};
526 # Wipe whole disk if usual ZFS layout with partition 9 as ZFS reserved.
527 my $parent = $info->{$dev}->{parent
};
528 if ($parent && scalar(keys $info->%*) == 3) {
529 $parent =~ s
|^/dev/||;
530 my $info9 = $info->{"${parent}9"};
532 $wipe = $info->{$dev}->{parent
} # need leading /dev/
533 if $info9 && $info9->{used
} && $info9->{used
} =~ m/^ZFS reserved/;
536 push $to_wipe->@*, $wipe;
540 if (-e
'/lib/systemd/system/zfs-import@.service') {
541 my $importunit = 'zfs-import@' . PVE
::Systemd
::escape_unit
($name) . '.service';
542 run_command
(['systemctl', 'disable', $importunit]);
545 run_command
(['zpool', 'destroy', $name]);
548 if ($param->{'cleanup-config'}) {
551 return $scfg->{type
} eq 'zfspool' && $scfg->{pool
} eq $name;
553 eval { PVE
::API2
::Storage
::Config-
>cleanup_storages_for_node($match, $node); };
554 warn $config_err = $@ if $@;
557 eval { PVE
::Diskmanage
::wipe_blockdev
($_) for $to_wipe->@*; };
559 PVE
::Diskmanage
::udevadm_trigger
($to_wipe->@*);
560 die "cleanup failed - $err" if $err;
562 die "config cleanup failed - $config_err" if $config_err;
566 return $rpcenv->fork_worker('zfsremove', $name, $user, $worker);