]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks.pm
Disks: return correct journal disk candidates
[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 => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
86 },
87 parameters => {
88 additionalProperties => 0,
89 properties => {
90 node => get_standard_option('pve-node'),
91 skipsmart => {
92 description => "Skip smart checks.",
93 type => 'boolean',
94 optional => 1,
95 default => 0,
96 },
97 type => {
98 description => "Only list specific types of disks.",
99 type => 'string',
100 enum => ['unused', 'journal_disks'],
101 optional => 1,
102 },
103 },
104 },
105 returns => {
106 type => 'array',
107 items => {
108 type => 'object',
109 properties => {
110 devpath => {
111 type => 'string',
112 description => 'The device path',
113 },
114 used => { type => 'string', optional => 1 },
115 gpt => { type => 'boolean' },
116 size => { type => 'integer'},
117 osdid => { type => 'integer'},
118 vendor => { type => 'string', optional => 1 },
119 model => { type => 'string', optional => 1 },
120 serial => { type => 'string', optional => 1 },
121 wwn => { type => 'string', optional => 1},
122 health => { type => 'string', optional => 1},
123 },
124 },
125 },
126 code => sub {
127 my ($param) = @_;
128
129 my $skipsmart = $param->{skipsmart} // 0;
130
131 my $disks = PVE::Diskmanage::get_disks(undef, $skipsmart);
132
133 my $type = $param->{type} // '';
134 my $result = [];
135
136 foreach my $disk (sort keys %$disks) {
137 my $entry = $disks->{$disk};
138 if ($type eq 'journal_disks') {
139 next if $entry->{osdid} >= 0;
140 if (my $usage = $entry->{used}) {
141 next if !($usage eq 'partitions' && $entry->{gpt}
142 || $usage eq 'LVM');
143 }
144 } elsif ($type eq 'unused') {
145 next if $entry->{used};
146 } elsif ($type ne '') {
147 die "internal error"; # should not happen
148 }
149 push @$result, $entry;
150 }
151 return $result;
152 }});
153
154 __PACKAGE__->register_method ({
155 name => 'smart',
156 path => 'smart',
157 method => 'GET',
158 description => "Get SMART Health of a disk.",
159 protected => 1,
160 proxyto => "node",
161 permissions => {
162 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
163 },
164 parameters => {
165 additionalProperties => 0,
166 properties => {
167 node => get_standard_option('pve-node'),
168 disk => {
169 type => 'string',
170 pattern => '^/dev/[a-zA-Z0-9\/]+$',
171 description => "Block device name",
172 },
173 healthonly => {
174 type => 'boolean',
175 description => "If true returns only the health status",
176 optional => 1,
177 },
178 },
179 },
180 returns => {
181 type => 'object',
182 properties => {
183 health => { type => 'string' },
184 type => { type => 'string', optional => 1 },
185 attributes => { type => 'array', optional => 1},
186 text => { type => 'string', optional => 1 },
187 },
188 },
189 code => sub {
190 my ($param) = @_;
191
192 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
193
194 my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
195
196 $result->{health} = 'UNKNOWN' if !defined $result->{health};
197 $result = { health => $result->{health} } if $param->{healthonly};
198
199 return $result;
200 }});
201
202 __PACKAGE__->register_method ({
203 name => 'initgpt',
204 path => 'initgpt',
205 method => 'POST',
206 description => "Initialize Disk with GPT",
207 protected => 1,
208 proxyto => "node",
209 permissions => {
210 check => ['perm', '/', ['Sys.Modify']],
211 },
212 parameters => {
213 additionalProperties => 0,
214 properties => {
215 node => get_standard_option('pve-node'),
216 disk => {
217 type => 'string',
218 description => "Block device name",
219 pattern => '^/dev/[a-zA-Z0-9\/]+$',
220 },
221 uuid => {
222 type => 'string',
223 description => 'UUID for the GPT table',
224 pattern => '[a-fA-F0-9\-]+',
225 maxLength => 36,
226 optional => 1,
227 },
228 },
229 },
230 returns => { type => 'string' },
231 code => sub {
232 my ($param) = @_;
233
234 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
235
236 my $rpcenv = PVE::RPCEnvironment::get();
237
238 my $authuser = $rpcenv->get_user();
239
240 die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk);
241 my $worker = sub {
242 PVE::Diskmanage::init_disk($disk, $param->{uuid});
243 };
244
245 my $diskid = $disk;
246 $diskid =~ s|^.*/||; # remove all up to the last slash
247 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
248 }});
249
250 1;