]>
Commit | Line | Data |
---|---|---|
409f8203 DC |
1 | package PVE::API2::Disks; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
409f8203 | 6 | use HTTP::Status qw(:constants); |
d5c80a5b TL |
7 | |
8 | use PVE::Diskmanage; | |
409f8203 | 9 | use PVE::JSONSchema qw(get_standard_option); |
d5c80a5b | 10 | use PVE::SafeSyslog; |
409f8203 | 11 | |
d5c80a5b | 12 | use PVE::API2::Disks::Directory; |
8b6842ca | 13 | use PVE::API2::Disks::LVM; |
0ea9f384 | 14 | use PVE::API2::Disks::LVMThin; |
c84106ed | 15 | use PVE::API2::Disks::ZFS; |
8b6842ca | 16 | |
409f8203 | 17 | use PVE::RESTHandler; |
409f8203 DC |
18 | use base qw(PVE::RESTHandler); |
19 | ||
8b6842ca DC |
20 | __PACKAGE__->register_method ({ |
21 | subclass => "PVE::API2::Disks::LVM", | |
22 | path => 'lvm', | |
23 | }); | |
409f8203 | 24 | |
0ea9f384 DC |
25 | __PACKAGE__->register_method ({ |
26 | subclass => "PVE::API2::Disks::LVMThin", | |
27 | path => 'lvmthin', | |
28 | }); | |
29 | ||
793d720c DC |
30 | __PACKAGE__->register_method ({ |
31 | subclass => "PVE::API2::Disks::Directory", | |
32 | path => 'directory', | |
33 | }); | |
34 | ||
c84106ed DC |
35 | __PACKAGE__->register_method ({ |
36 | subclass => "PVE::API2::Disks::ZFS", | |
37 | path => 'zfs', | |
38 | }); | |
39 | ||
409f8203 DC |
40 | __PACKAGE__->register_method ({ |
41 | name => 'index', | |
42 | path => '', | |
43 | method => 'GET', | |
44 | proxyto => 'node', | |
45 | permissions => { user => 'all' }, | |
46 | description => "Node index.", | |
47 | parameters => { | |
48 | additionalProperties => 0, | |
49 | properties => { | |
50 | node => get_standard_option('pve-node'), | |
51 | }, | |
52 | }, | |
53 | returns => { | |
54 | type => 'array', | |
55 | items => { | |
56 | type => "object", | |
57 | properties => {}, | |
58 | }, | |
59 | links => [ { rel => 'child', href => "{name}" } ], | |
60 | }, | |
61 | code => sub { | |
62 | my ($param) = @_; | |
63 | ||
64 | my $result = [ | |
65 | { name => 'list' }, | |
66 | { name => 'initgpt' }, | |
67 | { name => 'smart' }, | |
8b6842ca | 68 | { name => 'lvm' }, |
0ea9f384 | 69 | { name => 'lvmthin' }, |
793d720c | 70 | { name => 'directory' }, |
c84106ed | 71 | { name => 'zfs' }, |
d5c80a5b | 72 | ]; |
409f8203 DC |
73 | |
74 | return $result; | |
75 | }}); | |
76 | ||
77 | __PACKAGE__->register_method ({ | |
78 | name => 'list', | |
79 | path => 'list', | |
80 | method => 'GET', | |
81 | description => "List local disks.", | |
82 | protected => 1, | |
83 | proxyto => 'node', | |
84 | permissions => { | |
1d6c5488 FE |
85 | check => ['or', |
86 | ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1], | |
87 | ['perm', '/nodes/{node}', ['Sys.Audit', 'Datastore.Audit'], any => 1], | |
88 | ], | |
409f8203 DC |
89 | }, |
90 | parameters => { | |
91 | additionalProperties => 0, | |
92 | properties => { | |
93 | node => get_standard_option('pve-node'), | |
2949c537 FE |
94 | 'include-partitions' => { |
95 | description => "Also include partitions.", | |
96 | type => 'boolean', | |
97 | optional => 1, | |
98 | default => 0, | |
99 | }, | |
83bbd6f5 DC |
100 | skipsmart => { |
101 | description => "Skip smart checks.", | |
102 | type => 'boolean', | |
103 | optional => 1, | |
104 | default => 0, | |
105 | }, | |
106 | type => { | |
107 | description => "Only list specific types of disks.", | |
108 | type => 'string', | |
109 | enum => ['unused', 'journal_disks'], | |
110 | optional => 1, | |
111 | }, | |
409f8203 DC |
112 | }, |
113 | }, | |
114 | returns => { | |
115 | type => 'array', | |
116 | items => { | |
117 | type => 'object', | |
118 | properties => { | |
119 | devpath => { | |
120 | type => 'string', | |
121 | description => 'The device path', | |
122 | }, | |
123 | used => { type => 'string', optional => 1 }, | |
124 | gpt => { type => 'boolean' }, | |
125 | size => { type => 'integer'}, | |
126 | osdid => { type => 'integer'}, | |
127 | vendor => { type => 'string', optional => 1 }, | |
128 | model => { type => 'string', optional => 1 }, | |
129 | serial => { type => 'string', optional => 1 }, | |
130 | wwn => { type => 'string', optional => 1}, | |
131 | health => { type => 'string', optional => 1}, | |
2949c537 FE |
132 | parent => { |
133 | type => 'string', | |
134 | description => 'For partitions only. The device path of ' . | |
135 | 'the disk the partition resides on.', | |
136 | optional => 1 | |
137 | }, | |
409f8203 DC |
138 | }, |
139 | }, | |
140 | }, | |
141 | code => sub { | |
142 | my ($param) = @_; | |
143 | ||
83bbd6f5 | 144 | my $skipsmart = $param->{skipsmart} // 0; |
2949c537 | 145 | my $include_partitions = $param->{'include-partitions'} // 0; |
83bbd6f5 | 146 | |
2949c537 FE |
147 | my $disks = PVE::Diskmanage::get_disks( |
148 | undef, | |
149 | $skipsmart, | |
150 | $include_partitions | |
151 | ); | |
409f8203 | 152 | |
83bbd6f5 | 153 | my $type = $param->{type} // ''; |
409f8203 DC |
154 | my $result = []; |
155 | ||
156 | foreach my $disk (sort keys %$disks) { | |
157 | my $entry = $disks->{$disk}; | |
83bbd6f5 DC |
158 | if ($type eq 'journal_disks') { |
159 | next if $entry->{osdid} >= 0; | |
b2843044 FE |
160 | if (my $usage = $entry->{used}) { |
161 | next if !($usage eq 'partitions' && $entry->{gpt} | |
162 | || $usage eq 'LVM'); | |
163 | } | |
83bbd6f5 DC |
164 | } elsif ($type eq 'unused') { |
165 | next if $entry->{used}; | |
166 | } elsif ($type ne '') { | |
167 | die "internal error"; # should not happen | |
168 | } | |
409f8203 DC |
169 | push @$result, $entry; |
170 | } | |
171 | return $result; | |
172 | }}); | |
173 | ||
174 | __PACKAGE__->register_method ({ | |
175 | name => 'smart', | |
176 | path => 'smart', | |
177 | method => 'GET', | |
178 | description => "Get SMART Health of a disk.", | |
179 | protected => 1, | |
180 | proxyto => "node", | |
181 | permissions => { | |
182 | check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1], | |
183 | }, | |
184 | parameters => { | |
185 | additionalProperties => 0, | |
186 | properties => { | |
187 | node => get_standard_option('pve-node'), | |
188 | disk => { | |
189 | type => 'string', | |
190 | pattern => '^/dev/[a-zA-Z0-9\/]+$', | |
191 | description => "Block device name", | |
192 | }, | |
193 | healthonly => { | |
194 | type => 'boolean', | |
195 | description => "If true returns only the health status", | |
196 | optional => 1, | |
197 | }, | |
198 | }, | |
199 | }, | |
16bf963b FG |
200 | returns => { |
201 | type => 'object', | |
202 | properties => { | |
203 | health => { type => 'string' }, | |
204 | type => { type => 'string', optional => 1 }, | |
205 | attributes => { type => 'array', optional => 1}, | |
206 | text => { type => 'string', optional => 1 }, | |
207 | }, | |
208 | }, | |
409f8203 DC |
209 | code => sub { |
210 | my ($param) = @_; | |
211 | ||
212 | my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk}); | |
213 | ||
dd902da7 | 214 | my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly}); |
409f8203 | 215 | |
e3b02ffe | 216 | $result->{health} = 'UNKNOWN' if !defined $result->{health}; |
dd902da7 | 217 | $result = { health => $result->{health} } if $param->{healthonly}; |
acd3d916 | 218 | |
409f8203 DC |
219 | return $result; |
220 | }}); | |
221 | ||
222 | __PACKAGE__->register_method ({ | |
223 | name => 'initgpt', | |
224 | path => 'initgpt', | |
225 | method => 'POST', | |
226 | description => "Initialize Disk with GPT", | |
227 | protected => 1, | |
228 | proxyto => "node", | |
229 | permissions => { | |
230 | check => ['perm', '/', ['Sys.Modify']], | |
231 | }, | |
232 | parameters => { | |
233 | additionalProperties => 0, | |
234 | properties => { | |
235 | node => get_standard_option('pve-node'), | |
236 | disk => { | |
237 | type => 'string', | |
238 | description => "Block device name", | |
239 | pattern => '^/dev/[a-zA-Z0-9\/]+$', | |
240 | }, | |
241 | uuid => { | |
242 | type => 'string', | |
243 | description => 'UUID for the GPT table', | |
244 | pattern => '[a-fA-F0-9\-]+', | |
245 | maxLength => 36, | |
246 | optional => 1, | |
247 | }, | |
248 | }, | |
249 | }, | |
250 | returns => { type => 'string' }, | |
251 | code => sub { | |
252 | my ($param) = @_; | |
253 | ||
254 | my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk}); | |
255 | ||
256 | my $rpcenv = PVE::RPCEnvironment::get(); | |
257 | ||
258 | my $authuser = $rpcenv->get_user(); | |
259 | ||
260 | die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk); | |
261 | my $worker = sub { | |
262 | PVE::Diskmanage::init_disk($disk, $param->{uuid}); | |
263 | }; | |
264 | ||
265 | my $diskid = $disk; | |
266 | $diskid =~ s|^.*/||; # remove all up to the last slash | |
267 | return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker); | |
268 | }}); | |
269 | ||
270 | 1; |