]>
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);
376 my $sysfsdev = $dev =~ s!^/dev/!/sys/block/!r;
377 my $udevinfo = PVE
::Diskmanage
::get_udev_info
($sysfsdev);
378 $dev = $udevinfo->{by_id_link
} if defined($udevinfo->{by_id_link
});
381 # create zpool with desired raidlevel
383 my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
385 if ($raidlevel eq 'raid10') {
386 for (my $i = 0; $i < @$devs; $i+=2) {
387 push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1];
389 } elsif ($raidlevel eq 'single') {
390 push @$cmd, $devs->[0];
392 push @$cmd, $raidlevel, @$devs;
395 print "# ", join(' ', @$cmd), "\n";
398 $cmd = [$ZFS, 'set', "compression=$compression", $name];
399 print "# ", join(' ', @$cmd), "\n";
402 if (-e
'/lib/systemd/system/zfs-import@.service') {
403 my $importunit = 'zfs-import@'. PVE
::Systemd
::escape_unit
($name, undef) . '.service';
404 $cmd = ['systemctl', 'enable', $importunit];
405 print "# ", join(' ', @$cmd), "\n";
409 if ($param->{add_storage
}) {
410 my $storage_params = {
414 content
=> 'rootdir,images',
418 PVE
::API2
::Storage
::Config-
>create($storage_params);
423 return $rpcenv->fork_worker('zfscreate', $name, $user, $worker);