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