]>
Commit | Line | Data |
---|---|---|
c84106ed DC |
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 => { | |
4d12dbff DC |
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 | }, | |
b005f2f4 | 168 | errors => { |
4d12dbff DC |
169 | type => 'string', |
170 | description => 'Information about the errors on the zpool.', | |
171 | }, | |
172 | children => { | |
173 | type => 'array', | |
32f749b8 | 174 | description => "The pool configuration information, including the vdevs for each section (e.g. spares, cache), may be nested.", |
4d12dbff DC |
175 | items => { |
176 | type => 'object', | |
177 | properties => { | |
178 | name => { | |
179 | type => 'string', | |
32f749b8 | 180 | description => 'The name of the vdev or section.', |
4d12dbff DC |
181 | }, |
182 | state => { | |
32f749b8 | 183 | optional => 1, |
4d12dbff DC |
184 | type => 'string', |
185 | description => 'The state of the vdev.', | |
186 | }, | |
187 | read => { | |
32f749b8 | 188 | optional => 1, |
4d12dbff DC |
189 | type => 'number', |
190 | }, | |
191 | write => { | |
32f749b8 | 192 | optional => 1, |
4d12dbff DC |
193 | type => 'number', |
194 | }, | |
195 | cksum => { | |
32f749b8 | 196 | optional => 1, |
4d12dbff DC |
197 | type => 'number', |
198 | }, | |
199 | msg => { | |
200 | type => 'string', | |
201 | description => 'An optional message about the vdev.' | |
202 | } | |
203 | }, | |
204 | }, | |
205 | }, | |
206 | }, | |
c84106ed DC |
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 | my $vdevs = []; | |
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; | |
a49fc735 | 240 | } elsif ($config && $line =~ m/^(\s+)(\S+)\s*(\S+)?(?:\s+(\S+)\s+(\S+)\s+(\S+))?\s*(.*)$/) { |
c84106ed | 241 | my ($space, $name, $state, $read, $write, $cksum, $msg) = ($1, $2, $3, $4, $5, $6, $7); |
a49fc735 TM |
242 | if ($name ne "NAME" and $name ne $param->{name}) { |
243 | my $lvl= int(length($space)/2); # two spaces per level | |
c84106ed DC |
244 | my $vdev = { |
245 | name => $name, | |
c84106ed DC |
246 | msg => $msg, |
247 | lvl => $lvl, | |
248 | }; | |
a49fc735 TM |
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 | ||
c84106ed DC |
255 | my $cur = pop @$stack; |
256 | ||
257 | if ($lvl > $curlvl) { | |
258 | $cur->{children} = [ $vdev ]; | |
c84106ed DC |
259 | } elsif ($lvl == $curlvl) { |
260 | $cur = pop @$stack; | |
261 | push @{$cur->{children}}, $vdev; | |
c84106ed | 262 | } else { |
a49fc735 | 263 | while ($lvl <= $cur->{lvl} and $cur->{lvl} != 0) { |
c84106ed DC |
264 | $cur = pop @$stack; |
265 | } | |
266 | push @{$cur->{children}}, $vdev; | |
c84106ed | 267 | } |
a49fc735 TM |
268 | |
269 | push @$stack, $cur; | |
270 | push @$stack, $vdev; | |
c84106ed DC |
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 | }, | |
fdc863c7 | 292 | description => "Create a ZFS pool.", |
c84106ed DC |
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', | |
7058abe2 DC |
300 | description => 'The RAID level to use.', |
301 | enum => ['single', 'mirror', 'raid10', 'raidz', 'raidz2', 'raidz3'], | |
c84106ed DC |
302 | }, |
303 | devices => { | |
5be1a092 | 304 | type => 'string', format => 'string-list', |
fdc863c7 | 305 | description => 'The block devices you want to create the zpool on.', |
c84106ed DC |
306 | }, |
307 | ashift => { | |
308 | type => 'integer', | |
309 | minimum => 9, | |
310 | maximum => 16, | |
311 | optional => 1, | |
312 | default => 12, | |
7d597888 | 313 | description => 'Pool sector size exponent.', |
c84106ed DC |
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 => { | |
fdc863c7 | 323 | description => "Configure storage using the zpool.", |
c84106ed DC |
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})]; | |
38572a8f | 339 | my $raidlevel = $param->{raidlevel}; |
c84106ed DC |
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); | |
0370861c | 346 | PVE::Diskmanage::assert_disk_unused($dev); |
c84106ed DC |
347 | } |
348 | ||
9280153e | 349 | PVE::Storage::assert_sid_unused($name) if $param->{add_storage}; |
c84106ed DC |
350 | |
351 | my $numdisks = scalar(@$devs); | |
352 | my $mindisks = { | |
7058abe2 DC |
353 | single => 1, |
354 | mirror => 2, | |
c84106ed DC |
355 | raid10 => 4, |
356 | raidz => 3, | |
357 | raidz2 => 4, | |
358 | raidz3 => 5, | |
359 | }; | |
360 | ||
361 | # sanity checks | |
362 | die "raid10 needs an even number of disks\n" | |
38572a8f | 363 | if $raidlevel eq 'raid10' && $numdisks % 2 != 0; |
c84106ed | 364 | |
7058abe2 DC |
365 | die "please give only one disk for single disk mode\n" |
366 | if $raidlevel eq 'single' && $numdisks > 1; | |
367 | ||
38572a8f DC |
368 | die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n" |
369 | if $numdisks < $mindisks->{$raidlevel}; | |
c84106ed DC |
370 | |
371 | my $worker = sub { | |
e39e8ee2 | 372 | PVE::Diskmanage::locked_disk_action(sub { |
c84106ed DC |
373 | # create zpool with desired raidlevel |
374 | ||
375 | my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name]; | |
376 | ||
38572a8f | 377 | if ($raidlevel eq 'raid10') { |
c84106ed DC |
378 | for (my $i = 0; $i < @$devs; $i+=2) { |
379 | push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1]; | |
380 | } | |
7058abe2 DC |
381 | } elsif ($raidlevel eq 'single') { |
382 | push @$cmd, $devs->[0]; | |
c84106ed | 383 | } else { |
38572a8f | 384 | push @$cmd, $raidlevel, @$devs; |
c84106ed DC |
385 | } |
386 | ||
387 | print "# ", join(' ', @$cmd), "\n"; | |
388 | run_command($cmd); | |
389 | ||
390 | $cmd = [$ZFS, 'set', "compression=$compression", $name]; | |
391 | print "# ", join(' ', @$cmd), "\n"; | |
392 | run_command($cmd); | |
393 | ||
394 | if ($param->{add_storage}) { | |
395 | my $storage_params = { | |
396 | type => 'zfspool', | |
397 | pool => $name, | |
398 | storage => $name, | |
399 | content => 'rootdir,images', | |
400 | nodes => $node, | |
401 | }; | |
402 | ||
403 | PVE::API2::Storage::Config->create($storage_params); | |
404 | } | |
405 | }); | |
c84106ed DC |
406 | }; |
407 | ||
c84106ed DC |
408 | return $rpcenv->fork_worker('zfscreate', $name, $user, $worker); |
409 | }}); | |
410 | ||
411 | 1; |