]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/ZFS.pm
Disks: instantiate import unit for created zpool
[pve-storage.git] / PVE / API2 / Disks / ZFS.pm
1 package PVE::API2::Disks::ZFS;
2
3 use strict;
4 use warnings;
5
6 use PVE::Diskmanage;
7 use PVE::JSONSchema qw(get_standard_option);
8 use PVE::Systemd;
9 use PVE::API2::Storage::Config;
10 use PVE::Storage;
11 use PVE::Tools qw(run_command lock_file trim);
12
13 use PVE::RPCEnvironment;
14 use PVE::RESTHandler;
15
16 use base qw(PVE::RESTHandler);
17
18 my $ZPOOL = '/sbin/zpool';
19 my $ZFS = '/sbin/zfs';
20
21 __PACKAGE__->register_method ({
22 name => 'index',
23 path => '',
24 method => 'GET',
25 proxyto => 'node',
26 protected => 1,
27 permissions => {
28 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
29 },
30 description => "List Zpools.",
31 parameters => {
32 additionalProperties => 0,
33 properties => {
34 node => get_standard_option('pve-node'),
35 },
36 },
37 returns => {
38 type => 'array',
39 items => {
40 type => 'object',
41 properties => {
42 name => {
43 type => 'string',
44 description => "",
45 },
46 size => {
47 type => 'integer',
48 description => "",
49 },
50 alloc => {
51 type => 'integer',
52 description => "",
53 },
54 free => {
55 type => 'integer',
56 description => "",
57 },
58 frag => {
59 type => 'integer',
60 description => "",
61 },
62 dedup => {
63 type => 'number',
64 description => "",
65 },
66 health => {
67 type => 'string',
68 description => "",
69 },
70 },
71 },
72 links => [ { rel => 'child', href => "{name}" } ],
73 },
74 code => sub {
75 my ($param) = @_;
76
77 if (!-f $ZPOOL) {
78 die "zfsutils-linux not installed\n";
79 }
80
81 my $propnames = [qw(name size alloc free frag dedup health)];
82 my $numbers = {
83 size => 1,
84 alloc => 1,
85 free => 1,
86 frag => 1,
87 dedup => 1,
88 };
89
90 my $cmd = [$ZPOOL,'list', '-HpPLo', join(',', @$propnames)];
91
92 my $pools = [];
93
94 run_command($cmd, outfunc => sub {
95 my ($line) = @_;
96
97 my @props = split('\s+', trim($line));
98 my $pool = {};
99 for (my $i = 0; $i < scalar(@$propnames); $i++) {
100 if ($numbers->{$propnames->[$i]}) {
101 $pool->{$propnames->[$i]} = $props[$i] + 0;
102 } else {
103 $pool->{$propnames->[$i]} = $props[$i];
104 }
105 }
106
107 push @$pools, $pool;
108 });
109
110 return $pools;
111 }});
112
113 sub preparetree {
114 my ($el) = @_;
115 delete $el->{lvl};
116 if ($el->{children} && scalar(@{$el->{children}})) {
117 $el->{leaf} = 0;
118 foreach my $child (@{$el->{children}}) {
119 preparetree($child);
120 }
121 } else {
122 $el->{leaf} = 1;
123 }
124 }
125
126
127 __PACKAGE__->register_method ({
128 name => 'detail',
129 path => '{name}',
130 method => 'GET',
131 proxyto => 'node',
132 protected => 1,
133 permissions => {
134 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
135 },
136 description => "Get details about a zpool.",
137 parameters => {
138 additionalProperties => 0,
139 properties => {
140 node => get_standard_option('pve-node'),
141 name => get_standard_option('pve-storage-id'),
142 },
143 },
144 returns => {
145 type => 'object',
146 properties => {
147 name => {
148 type => 'string',
149 description => 'The name of the zpool.',
150 },
151 state => {
152 type => 'string',
153 description => 'The state of the zpool.',
154 },
155 status => {
156 optional => 1,
157 type => 'string',
158 description => 'Information about the state of the zpool.',
159 },
160 action => {
161 optional => 1,
162 type => 'string',
163 description => 'Information about the recommended action to fix the state.',
164 },
165 scan => {
166 type => 'string',
167 description => 'Information about the last/current scrub.',
168 },
169 errors => {
170 type => 'string',
171 description => 'Information about the errors on the zpool.',
172 },
173 children => {
174 type => 'array',
175 description => "The pool configuration information, including the vdevs for each section (e.g. spares, cache), may be nested.",
176 items => {
177 type => 'object',
178 properties => {
179 name => {
180 type => 'string',
181 description => 'The name of the vdev or section.',
182 },
183 state => {
184 optional => 1,
185 type => 'string',
186 description => 'The state of the vdev.',
187 },
188 read => {
189 optional => 1,
190 type => 'number',
191 },
192 write => {
193 optional => 1,
194 type => 'number',
195 },
196 cksum => {
197 optional => 1,
198 type => 'number',
199 },
200 msg => {
201 type => 'string',
202 description => 'An optional message about the vdev.'
203 }
204 },
205 },
206 },
207 },
208 },
209 code => sub {
210 my ($param) = @_;
211
212 if (!-f $ZPOOL) {
213 die "zfsutils-linux not installed\n";
214 }
215
216 my $cmd = [$ZPOOL, 'status', '-P', $param->{name}];
217
218 my $pool = {
219 lvl => 0,
220 };
221
222 my $curfield;
223 my $config = 0;
224
225 my $stack = [$pool];
226 my $curlvl = 0;
227
228 run_command($cmd, outfunc => sub {
229 my ($line) = @_;
230
231 if ($line =~ m/^\s*(\S+): (\S+.*)$/) {
232 $curfield = $1;
233 $pool->{$curfield} = $2;
234
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:/) {
239 $config = 1;
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" and $name ne $param->{name}) {
243 my $lvl= int(length($space)/2); # two spaces per level
244 my $vdev = {
245 name => $name,
246 msg => $msg,
247 lvl => $lvl,
248 };
249
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);
254
255 my $cur = pop @$stack;
256
257 if ($lvl > $curlvl) {
258 $cur->{children} = [ $vdev ];
259 } elsif ($lvl == $curlvl) {
260 $cur = pop @$stack;
261 push @{$cur->{children}}, $vdev;
262 } else {
263 while ($lvl <= $cur->{lvl} && $cur->{lvl} != 0) {
264 $cur = pop @$stack;
265 }
266 push @{$cur->{children}}, $vdev;
267 }
268
269 push @$stack, $cur;
270 push @$stack, $vdev;
271 $curlvl = $lvl;
272 }
273 }
274 });
275
276 # change treenodes for extjs tree
277 $pool->{name} = delete $pool->{pool};
278 preparetree($pool);
279
280 return $pool;
281 }});
282
283 __PACKAGE__->register_method ({
284 name => 'create',
285 path => '',
286 method => 'POST',
287 proxyto => 'node',
288 protected => 1,
289 permissions => {
290 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
291 },
292 description => "Create a ZFS pool.",
293 parameters => {
294 additionalProperties => 0,
295 properties => {
296 node => get_standard_option('pve-node'),
297 name => get_standard_option('pve-storage-id'),
298 raidlevel => {
299 type => 'string',
300 description => 'The RAID level to use.',
301 enum => ['single', 'mirror', 'raid10', 'raidz', 'raidz2', 'raidz3'],
302 },
303 devices => {
304 type => 'string', format => 'string-list',
305 description => 'The block devices you want to create the zpool on.',
306 },
307 ashift => {
308 type => 'integer',
309 minimum => 9,
310 maximum => 16,
311 optional => 1,
312 default => 12,
313 description => 'Pool sector size exponent.',
314 },
315 compression => {
316 type => 'string',
317 description => 'The compression algorithm to use.',
318 enum => ['on', 'off', 'gzip', 'lz4', 'lzjb', 'zle'],
319 optional => 1,
320 default => 'on',
321 },
322 add_storage => {
323 description => "Configure storage using the zpool.",
324 type => 'boolean',
325 optional => 1,
326 default => 0,
327 },
328 },
329 },
330 returns => { type => 'string' },
331 code => sub {
332 my ($param) = @_;
333
334 my $rpcenv = PVE::RPCEnvironment::get();
335 my $user = $rpcenv->get_user();
336
337 my $name = $param->{name};
338 my $devs = [PVE::Tools::split_list($param->{devices})];
339 my $raidlevel = $param->{raidlevel};
340 my $node = $param->{node};
341 my $ashift = $param->{ashift} // 12;
342 my $compression = $param->{compression} // 'on';
343
344 foreach my $dev (@$devs) {
345 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
346 PVE::Diskmanage::assert_disk_unused($dev);
347 my $sysfsdev = $dev =~ s!^/dev/!/sys/block/!r;
348 my $udevinfo = PVE::Diskmanage::get_udev_info($sysfsdev);
349 $dev = $udevinfo->{by_id_link} if defined($udevinfo->{by_id_link});
350 }
351
352 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
353
354 my $numdisks = scalar(@$devs);
355 my $mindisks = {
356 single => 1,
357 mirror => 2,
358 raid10 => 4,
359 raidz => 3,
360 raidz2 => 4,
361 raidz3 => 5,
362 };
363
364 # sanity checks
365 die "raid10 needs an even number of disks\n"
366 if $raidlevel eq 'raid10' && $numdisks % 2 != 0;
367
368 die "please give only one disk for single disk mode\n"
369 if $raidlevel eq 'single' && $numdisks > 1;
370
371 die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n"
372 if $numdisks < $mindisks->{$raidlevel};
373
374 my $worker = sub {
375 PVE::Diskmanage::locked_disk_action(sub {
376 # create zpool with desired raidlevel
377
378 my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
379
380 if ($raidlevel eq 'raid10') {
381 for (my $i = 0; $i < @$devs; $i+=2) {
382 push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1];
383 }
384 } elsif ($raidlevel eq 'single') {
385 push @$cmd, $devs->[0];
386 } else {
387 push @$cmd, $raidlevel, @$devs;
388 }
389
390 print "# ", join(' ', @$cmd), "\n";
391 run_command($cmd);
392
393 $cmd = [$ZFS, 'set', "compression=$compression", $name];
394 print "# ", join(' ', @$cmd), "\n";
395 run_command($cmd);
396
397 my $importunit = 'zfs-import@'. PVE::Systemd::escape_unit($name, undef) . '.service';
398 $cmd = ['systemctl', 'enable', $importunit];
399 print "# ", join(' ', @$cmd), "\n";
400 run_command($cmd);
401
402 if ($param->{add_storage}) {
403 my $storage_params = {
404 type => 'zfspool',
405 pool => $name,
406 storage => $name,
407 content => 'rootdir,images',
408 nodes => $node,
409 };
410
411 PVE::API2::Storage::Config->create($storage_params);
412 }
413 });
414 };
415
416 return $rpcenv->fork_worker('zfscreate', $name, $user, $worker);
417 }});
418
419 1;