]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks.pm
add FileRestore API for PBS
[pve-storage.git] / PVE / API2 / Disks.pm
1 package PVE::API2::Disks;
2
3 use strict;
4 use warnings;
5
6 use HTTP::Status qw(:constants);
7
8 use PVE::Diskmanage;
9 use PVE::JSONSchema qw(get_standard_option);
10 use PVE::SafeSyslog;
11
12 use PVE::API2::Disks::Directory;
13 use PVE::API2::Disks::LVM;
14 use PVE::API2::Disks::LVMThin;
15 use PVE::API2::Disks::ZFS;
16
17 use PVE::RESTHandler;
18 use base qw(PVE::RESTHandler);
19
20 __PACKAGE__->register_method ({
21 subclass => "PVE::API2::Disks::LVM",
22 path => 'lvm',
23 });
24
25 __PACKAGE__->register_method ({
26 subclass => "PVE::API2::Disks::LVMThin",
27 path => 'lvmthin',
28 });
29
30 __PACKAGE__->register_method ({
31 subclass => "PVE::API2::Disks::Directory",
32 path => 'directory',
33 });
34
35 __PACKAGE__->register_method ({
36 subclass => "PVE::API2::Disks::ZFS",
37 path => 'zfs',
38 });
39
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' },
68 { name => 'lvm' },
69 { name => 'lvmthin' },
70 { name => 'directory' },
71 { name => 'zfs' },
72 ];
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 => {
85 check => ['or',
86 ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
87 ['perm', '/nodes/{node}', ['Sys.Audit', 'Datastore.Audit'], any => 1],
88 ],
89 },
90 parameters => {
91 additionalProperties => 0,
92 properties => {
93 node => get_standard_option('pve-node'),
94 'include-partitions' => {
95 description => "Also include partitions.",
96 type => 'boolean',
97 optional => 1,
98 default => 0,
99 },
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 },
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},
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 },
138 },
139 },
140 },
141 code => sub {
142 my ($param) = @_;
143
144 my $skipsmart = $param->{skipsmart} // 0;
145 my $include_partitions = $param->{'include-partitions'} // 0;
146
147 my $disks = PVE::Diskmanage::get_disks(
148 undef,
149 $skipsmart,
150 $include_partitions
151 );
152
153 my $type = $param->{type} // '';
154 my $result = [];
155
156 foreach my $disk (sort keys %$disks) {
157 my $entry = $disks->{$disk};
158 if ($type eq 'journal_disks') {
159 next if $entry->{osdid} >= 0;
160 if (my $usage = $entry->{used}) {
161 next if !($usage eq 'partitions' && $entry->{gpt}
162 || $usage eq 'LVM');
163 }
164 } elsif ($type eq 'unused') {
165 next if $entry->{used};
166 } elsif ($type ne '') {
167 die "internal error"; # should not happen
168 }
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 },
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 },
209 code => sub {
210 my ($param) = @_;
211
212 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
213
214 my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
215
216 $result->{health} = 'UNKNOWN' if !defined $result->{health};
217 $result = { health => $result->{health} } if $param->{healthonly};
218
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;