]>
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';
21 __PACKAGE__-
>register_method ({
28 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
30 description
=> "List Zpools.",
32 additionalProperties
=> 0,
34 node
=> get_standard_option
('pve-node'),
72 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
78 die "zfsutils-linux not installed\n";
81 my $propnames = [qw(name size alloc free frag dedup health)];
90 my $cmd = [$ZPOOL,'list', '-HpPLo', join(',', @$propnames)];
94 run_command
($cmd, outfunc
=> sub {
97 my @props = split('\s+', trim
($line));
99 for (my $i = 0; $i < scalar(@$propnames); $i++) {
100 if ($numbers->{$propnames->[$i]}) {
101 $pool->{$propnames->[$i]} = $props[$i] + 0;
103 $pool->{$propnames->[$i]} = $props[$i];
116 if ($el->{children
} && scalar(@{$el->{children
}})) {
118 foreach my $child (@{$el->{children
}}) {
127 __PACKAGE__-
>register_method ({
134 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
136 description
=> "Get details about a zpool.",
138 additionalProperties
=> 0,
140 node
=> get_standard_option
('pve-node'),
141 name
=> get_standard_option
('pve-storage-id'),
149 description
=> 'The name of the zpool.',
153 description
=> 'The state of the zpool.',
158 description
=> 'Information about the state of the zpool.',
163 description
=> 'Information about the recommended action to fix the state.',
168 description
=> 'Information about the last/current scrub.',
172 description
=> 'Information about the errors on the zpool.',
176 description
=> "The pool configuration information, including the vdevs for each section (e.g. spares, cache), may be nested.",
182 description
=> 'The name of the vdev or section.',
187 description
=> 'The state of the vdev.',
203 description
=> 'An optional message about the vdev.'
214 die "zfsutils-linux not installed\n";
217 my $cmd = [$ZPOOL, 'status', '-P', $param->{name
}];
229 run_command
($cmd, outfunc
=> sub {
232 if ($line =~ m/^\s*(\S+): (\S+.*)$/) {
234 $pool->{$curfield} = $2;
236 $config = 0 if $curfield eq 'errors';
237 } elsif (!$config && $line =~ m/^\s+(\S+.*)$/) {
238 $pool->{$curfield} .= " " . $1;
239 } elsif (!$config && $line =~ m/^\s*config:/) {
241 } elsif ($config && $line =~ m/^(\s+)(\S+)\s*(\S+)?(?:\s+(\S+)\s+(\S+)\s+(\S+))?\s*(.*)$/) {
242 my ($space, $name, $state, $read, $write, $cksum, $msg) = ($1, $2, $3, $4, $5, $6, $7);
243 if ($name ne "NAME") {
244 my $lvl = int(length($space) / 2) + 1; # two spaces per level
251 $vdev->{state} = $state if defined($state);
252 $vdev->{read} = $read + 0 if defined($read);
253 $vdev->{write} = $write + 0 if defined($write);
254 $vdev->{cksum
} = $cksum + 0 if defined($cksum);
256 my $cur = pop @$stack;
258 if ($lvl > $curlvl) {
259 $cur->{children
} = [ $vdev ];
260 } elsif ($lvl == $curlvl) {
262 push @{$cur->{children
}}, $vdev;
264 while ($lvl <= $cur->{lvl
} && $cur->{lvl
} != 0) {
267 push @{$cur->{children
}}, $vdev;
277 # change treenodes for extjs tree
278 $pool->{name
} = delete $pool->{pool
};
284 __PACKAGE__-
>register_method ({
291 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
293 description
=> "Create a ZFS pool.",
295 additionalProperties
=> 0,
297 node
=> get_standard_option
('pve-node'),
298 name
=> get_standard_option
('pve-storage-id'),
301 description
=> 'The RAID level to use.',
302 enum
=> ['single', 'mirror', 'raid10', 'raidz', 'raidz2', 'raidz3'],
305 type
=> 'string', format
=> 'string-list',
306 description
=> 'The block devices you want to create the zpool on.',
314 description
=> 'Pool sector size exponent.',
318 description
=> 'The compression algorithm to use.',
319 enum
=> ['on', 'off', 'gzip', 'lz4', 'lzjb', 'zle', 'zstd'],
324 description
=> "Configure storage using the zpool.",
331 returns
=> { type
=> 'string' },
335 my $rpcenv = PVE
::RPCEnvironment
::get
();
336 my $user = $rpcenv->get_user();
338 my $name = $param->{name
};
339 my $devs = [PVE
::Tools
::split_list
($param->{devices
})];
340 my $raidlevel = $param->{raidlevel
};
341 my $node = $param->{node
};
342 my $ashift = $param->{ashift
} // 12;
343 my $compression = $param->{compression
} // 'on';
345 foreach my $dev (@$devs) {
346 $dev = PVE
::Diskmanage
::verify_blockdev_path
($dev);
347 PVE
::Diskmanage
::assert_disk_unused
($dev);
350 PVE
::Storage
::assert_sid_unused
($name) if $param->{add_storage
};
352 my $numdisks = scalar(@$devs);
363 die "raid10 needs an even number of disks\n"
364 if $raidlevel eq 'raid10' && $numdisks % 2 != 0;
366 die "please give only one disk for single disk mode\n"
367 if $raidlevel eq 'single' && $numdisks > 1;
369 die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n"
370 if $numdisks < $mindisks->{$raidlevel};
373 PVE
::Diskmanage
::locked_disk_action
(sub {
374 for my $dev (@$devs) {
375 PVE
::Diskmanage
::assert_disk_unused
($dev);
377 my $is_partition = PVE
::Diskmanage
::is_partition
($dev);
381 PVE
::Diskmanage
::change_parttype
(
383 '6a898cc3-1dd2-11b2-99a6-080020736631',
389 my $sysfsdev = $is_partition ? PVE
::Diskmanage
::get_blockdev
($dev) : $dev;
391 $sysfsdev =~ s!^/dev/!/sys/block/!;
393 my $part = $dev =~ s!^/dev/!!r;
394 $sysfsdev .= "/${part}";
397 my $udevinfo = PVE
::Diskmanage
::get_udev_info
($sysfsdev);
398 $dev = $udevinfo->{by_id_link
} if defined($udevinfo->{by_id_link
});
401 # create zpool with desired raidlevel
403 my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
405 if ($raidlevel eq 'raid10') {
406 for (my $i = 0; $i < @$devs; $i+=2) {
407 push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1];
409 } elsif ($raidlevel eq 'single') {
410 push @$cmd, $devs->[0];
412 push @$cmd, $raidlevel, @$devs;
415 print "# ", join(' ', @$cmd), "\n";
418 $cmd = [$ZFS, 'set', "compression=$compression", $name];
419 print "# ", join(' ', @$cmd), "\n";
422 if (-e
'/lib/systemd/system/zfs-import@.service') {
423 my $importunit = 'zfs-import@'. PVE
::Systemd
::escape_unit
($name, undef) . '.service';
424 $cmd = ['systemctl', 'enable', $importunit];
425 print "# ", join(' ', @$cmd), "\n";
429 PVE
::Diskmanage
::udevadm_trigger
($devs->@*);
431 if ($param->{add_storage
}) {
432 my $storage_params = {
436 content
=> 'rootdir,images',
440 PVE
::API2
::Storage
::Config-
>create($storage_params);
445 return $rpcenv->fork_worker('zfscreate', $name, $user, $worker);
448 __PACKAGE__-
>register_method ({
455 check
=> ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
457 description
=> "Destroy a ZFS pool.",
459 additionalProperties
=> 0,
461 node
=> get_standard_option
('pve-node'),
462 name
=> get_standard_option
('pve-storage-id'),
463 'cleanup-config' => {
464 description
=> "Marks associated storage(s) as not available on this node anymore ".
465 "or removes them from the configuration (if configured for this node only).",
471 description
=> "Also wipe disks so they can be repurposed afterwards.",
478 returns
=> { type
=> 'string' },
482 my $rpcenv = PVE
::RPCEnvironment
::get
();
483 my $user = $rpcenv->get_user();
485 my $name = $param->{name
};
486 my $node = $param->{node
};
489 PVE
::Diskmanage
::locked_disk_action
(sub {
491 if ($param->{'cleanup-disks'}) {
492 # Using -o name does not only output the name in combination with -v.
493 run_command
(['zpool', 'list', '-vHPL', $name], outfunc
=> sub {
496 my ($name) = PVE
::Tools
::split_list
($line);
497 return if $name !~ m
|^/dev/.+|;
499 my $dev = PVE
::Diskmanage
::verify_blockdev_path
($name);
503 my $info = PVE
::Diskmanage
::get_disks
($dev, 1, 1);
504 die "unable to obtain information for disk '$dev'\n" if !$info->{$dev};
506 # Wipe whole disk if usual ZFS layout with partition 9 as ZFS reserved.
507 my $parent = $info->{$dev}->{parent
};
508 if ($parent && scalar(keys $info->%*) == 3) {
509 $parent =~ s
|^/dev/||;
510 my $info9 = $info->{"${parent}9"};
512 $wipe = $info->{$dev}->{parent
} # need leading /dev/
513 if $info9 && $info9->{used
} && $info9->{used
} =~ m/^ZFS reserved/;
516 push $to_wipe->@*, $wipe;
520 if (-e
'/lib/systemd/system/zfs-import@.service') {
521 my $importunit = 'zfs-import@' . PVE
::Systemd
::escape_unit
($name) . '.service';
522 run_command
(['systemctl', 'disable', $importunit]);
525 run_command
(['zpool', 'destroy', $name]);
528 if ($param->{'cleanup-config'}) {
531 return $scfg->{type
} eq 'zfspool' && $scfg->{pool
} eq $name;
533 eval { PVE
::API2
::Storage
::Config-
>cleanup_storages_for_node($match, $node); };
534 warn $config_err = $@ if $@;
537 eval { PVE
::Diskmanage
::wipe_blockdev
($_) for $to_wipe->@*; };
539 PVE
::Diskmanage
::udevadm_trigger
($to_wipe->@*);
540 die "cleanup failed - $err" if $err;
542 die "config cleanup failed - $config_err" if $config_err;
546 return $rpcenv->fork_worker('zfsremove', $name, $user, $worker);