]>
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', | |
174 | description => "The tree of the vdevs. Depending on the configuration of the zpool, they can be nested.", | |
175 | items => { | |
176 | type => 'object', | |
177 | properties => { | |
178 | name => { | |
179 | type => 'string', | |
180 | description => 'The name of the vdev.', | |
181 | }, | |
182 | state => { | |
183 | type => 'string', | |
184 | description => 'The state of the vdev.', | |
185 | }, | |
186 | read => { | |
187 | type => 'number', | |
188 | }, | |
189 | write => { | |
190 | type => 'number', | |
191 | }, | |
192 | cksum => { | |
193 | type => 'number', | |
194 | }, | |
195 | msg => { | |
196 | type => 'string', | |
197 | description => 'An optional message about the vdev.' | |
198 | } | |
199 | }, | |
200 | }, | |
201 | }, | |
202 | }, | |
c84106ed DC |
203 | }, |
204 | code => sub { | |
205 | my ($param) = @_; | |
206 | ||
207 | if (!-f $ZPOOL) { | |
208 | die "zfsutils-linux not installed\n"; | |
209 | } | |
210 | ||
211 | my $cmd = [$ZPOOL, 'status', '-P', $param->{name}]; | |
212 | ||
213 | my $pool = { | |
214 | lvl => 0, | |
215 | }; | |
216 | my $vdevs = []; | |
217 | ||
218 | my $curfield; | |
219 | my $config = 0; | |
220 | ||
221 | my $stack = [$pool]; | |
222 | my $curlvl = 0; | |
223 | ||
224 | run_command($cmd, outfunc => sub { | |
225 | my ($line) = @_; | |
226 | ||
227 | if ($line =~ m/^\s*(\S+): (\S+.*)$/) { | |
228 | $curfield = $1; | |
229 | $pool->{$curfield} = $2; | |
230 | ||
231 | $config = 0 if $curfield eq 'errors'; | |
232 | } elsif (!$config && $line =~ m/^\s+(\S+.*)$/) { | |
233 | $pool->{$curfield} .= " " . $1; | |
234 | } elsif (!$config && $line =~ m/^\s*config:/) { | |
235 | $config = 1; | |
a49fc735 | 236 | } elsif ($config && $line =~ m/^(\s+)(\S+)\s*(\S+)?(?:\s+(\S+)\s+(\S+)\s+(\S+))?\s*(.*)$/) { |
c84106ed | 237 | my ($space, $name, $state, $read, $write, $cksum, $msg) = ($1, $2, $3, $4, $5, $6, $7); |
a49fc735 TM |
238 | if ($name ne "NAME" and $name ne $param->{name}) { |
239 | my $lvl= int(length($space)/2); # two spaces per level | |
c84106ed DC |
240 | my $vdev = { |
241 | name => $name, | |
c84106ed DC |
242 | msg => $msg, |
243 | lvl => $lvl, | |
244 | }; | |
a49fc735 TM |
245 | |
246 | $vdev->{state} = $state if defined($state); | |
247 | $vdev->{read} = $read + 0 if defined($read); | |
248 | $vdev->{write} = $write + 0 if defined($write); | |
249 | $vdev->{cksum} = $cksum + 0 if defined($cksum); | |
250 | ||
c84106ed DC |
251 | my $cur = pop @$stack; |
252 | ||
253 | if ($lvl > $curlvl) { | |
254 | $cur->{children} = [ $vdev ]; | |
c84106ed DC |
255 | } elsif ($lvl == $curlvl) { |
256 | $cur = pop @$stack; | |
257 | push @{$cur->{children}}, $vdev; | |
c84106ed | 258 | } else { |
a49fc735 | 259 | while ($lvl <= $cur->{lvl} and $cur->{lvl} != 0) { |
c84106ed DC |
260 | $cur = pop @$stack; |
261 | } | |
262 | push @{$cur->{children}}, $vdev; | |
c84106ed | 263 | } |
a49fc735 TM |
264 | |
265 | push @$stack, $cur; | |
266 | push @$stack, $vdev; | |
c84106ed DC |
267 | $curlvl = $lvl; |
268 | } | |
269 | } | |
270 | }); | |
271 | ||
272 | # change treenodes for extjs tree | |
273 | $pool->{name} = delete $pool->{pool}; | |
274 | preparetree($pool); | |
275 | ||
276 | return $pool; | |
277 | }}); | |
278 | ||
279 | __PACKAGE__->register_method ({ | |
280 | name => 'create', | |
281 | path => '', | |
282 | method => 'POST', | |
283 | proxyto => 'node', | |
284 | protected => 1, | |
285 | permissions => { | |
286 | check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']], | |
287 | }, | |
fdc863c7 | 288 | description => "Create a ZFS pool.", |
c84106ed DC |
289 | parameters => { |
290 | additionalProperties => 0, | |
291 | properties => { | |
292 | node => get_standard_option('pve-node'), | |
293 | name => get_standard_option('pve-storage-id'), | |
294 | raidlevel => { | |
295 | type => 'string', | |
7058abe2 DC |
296 | description => 'The RAID level to use.', |
297 | enum => ['single', 'mirror', 'raid10', 'raidz', 'raidz2', 'raidz3'], | |
c84106ed DC |
298 | }, |
299 | devices => { | |
5be1a092 | 300 | type => 'string', format => 'string-list', |
fdc863c7 | 301 | description => 'The block devices you want to create the zpool on.', |
c84106ed DC |
302 | }, |
303 | ashift => { | |
304 | type => 'integer', | |
305 | minimum => 9, | |
306 | maximum => 16, | |
307 | optional => 1, | |
308 | default => 12, | |
7d597888 | 309 | description => 'Pool sector size exponent.', |
c84106ed DC |
310 | }, |
311 | compression => { | |
312 | type => 'string', | |
313 | description => 'The compression algorithm to use.', | |
314 | enum => ['on', 'off', 'gzip', 'lz4', 'lzjb', 'zle'], | |
315 | optional => 1, | |
316 | default => 'on', | |
317 | }, | |
318 | add_storage => { | |
fdc863c7 | 319 | description => "Configure storage using the zpool.", |
c84106ed DC |
320 | type => 'boolean', |
321 | optional => 1, | |
322 | default => 0, | |
323 | }, | |
324 | }, | |
325 | }, | |
326 | returns => { type => 'string' }, | |
327 | code => sub { | |
328 | my ($param) = @_; | |
329 | ||
330 | my $rpcenv = PVE::RPCEnvironment::get(); | |
331 | my $user = $rpcenv->get_user(); | |
332 | ||
333 | my $name = $param->{name}; | |
334 | my $devs = [PVE::Tools::split_list($param->{devices})]; | |
38572a8f | 335 | my $raidlevel = $param->{raidlevel}; |
c84106ed DC |
336 | my $node = $param->{node}; |
337 | my $ashift = $param->{ashift} // 12; | |
338 | my $compression = $param->{compression} // 'on'; | |
339 | ||
340 | foreach my $dev (@$devs) { | |
341 | $dev = PVE::Diskmanage::verify_blockdev_path($dev); | |
0370861c | 342 | PVE::Diskmanage::assert_disk_unused($dev); |
c84106ed DC |
343 | } |
344 | ||
9280153e | 345 | PVE::Storage::assert_sid_unused($name) if $param->{add_storage}; |
c84106ed DC |
346 | |
347 | my $numdisks = scalar(@$devs); | |
348 | my $mindisks = { | |
7058abe2 DC |
349 | single => 1, |
350 | mirror => 2, | |
c84106ed DC |
351 | raid10 => 4, |
352 | raidz => 3, | |
353 | raidz2 => 4, | |
354 | raidz3 => 5, | |
355 | }; | |
356 | ||
357 | # sanity checks | |
358 | die "raid10 needs an even number of disks\n" | |
38572a8f | 359 | if $raidlevel eq 'raid10' && $numdisks % 2 != 0; |
c84106ed | 360 | |
7058abe2 DC |
361 | die "please give only one disk for single disk mode\n" |
362 | if $raidlevel eq 'single' && $numdisks > 1; | |
363 | ||
38572a8f DC |
364 | die "$raidlevel needs at least $mindisks->{$raidlevel} disks\n" |
365 | if $numdisks < $mindisks->{$raidlevel}; | |
c84106ed DC |
366 | |
367 | my $worker = sub { | |
e39e8ee2 | 368 | PVE::Diskmanage::locked_disk_action(sub { |
c84106ed DC |
369 | # create zpool with desired raidlevel |
370 | ||
371 | my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name]; | |
372 | ||
38572a8f | 373 | if ($raidlevel eq 'raid10') { |
c84106ed DC |
374 | for (my $i = 0; $i < @$devs; $i+=2) { |
375 | push @$cmd, 'mirror', $devs->[$i], $devs->[$i+1]; | |
376 | } | |
7058abe2 DC |
377 | } elsif ($raidlevel eq 'single') { |
378 | push @$cmd, $devs->[0]; | |
c84106ed | 379 | } else { |
38572a8f | 380 | push @$cmd, $raidlevel, @$devs; |
c84106ed DC |
381 | } |
382 | ||
383 | print "# ", join(' ', @$cmd), "\n"; | |
384 | run_command($cmd); | |
385 | ||
386 | $cmd = [$ZFS, 'set', "compression=$compression", $name]; | |
387 | print "# ", join(' ', @$cmd), "\n"; | |
388 | run_command($cmd); | |
389 | ||
390 | if ($param->{add_storage}) { | |
391 | my $storage_params = { | |
392 | type => 'zfspool', | |
393 | pool => $name, | |
394 | storage => $name, | |
395 | content => 'rootdir,images', | |
396 | nodes => $node, | |
397 | }; | |
398 | ||
399 | PVE::API2::Storage::Config->create($storage_params); | |
400 | } | |
401 | }); | |
c84106ed DC |
402 | }; |
403 | ||
c84106ed DC |
404 | return $rpcenv->fork_worker('zfscreate', $name, $user, $worker); |
405 | }}); | |
406 | ||
407 | 1; |