]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks.pm
Disks: return correct journal disk candidates
[pve-storage.git] / PVE / API2 / Disks.pm
CommitLineData
409f8203
DC
1package PVE::API2::Disks;
2
3use strict;
4use warnings;
5
409f8203 6use HTTP::Status qw(:constants);
d5c80a5b
TL
7
8use PVE::Diskmanage;
409f8203 9use PVE::JSONSchema qw(get_standard_option);
d5c80a5b 10use PVE::SafeSyslog;
409f8203 11
d5c80a5b 12use PVE::API2::Disks::Directory;
8b6842ca 13use PVE::API2::Disks::LVM;
0ea9f384 14use PVE::API2::Disks::LVMThin;
c84106ed 15use PVE::API2::Disks::ZFS;
8b6842ca 16
409f8203 17use PVE::RESTHandler;
409f8203
DC
18use 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 => {
85 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
86 },
87 parameters => {
88 additionalProperties => 0,
89 properties => {
90 node => get_standard_option('pve-node'),
83bbd6f5
DC
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 },
409f8203
DC
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
83bbd6f5
DC
129 my $skipsmart = $param->{skipsmart} // 0;
130
131 my $disks = PVE::Diskmanage::get_disks(undef, $skipsmart);
409f8203 132
83bbd6f5 133 my $type = $param->{type} // '';
409f8203
DC
134 my $result = [];
135
136 foreach my $disk (sort keys %$disks) {
137 my $entry = $disks->{$disk};
83bbd6f5
DC
138 if ($type eq 'journal_disks') {
139 next if $entry->{osdid} >= 0;
b2843044
FE
140 if (my $usage = $entry->{used}) {
141 next if !($usage eq 'partitions' && $entry->{gpt}
142 || $usage eq 'LVM');
143 }
83bbd6f5
DC
144 } elsif ($type eq 'unused') {
145 next if $entry->{used};
146 } elsif ($type ne '') {
147 die "internal error"; # should not happen
148 }
409f8203
DC
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 },
16bf963b
FG
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 },
409f8203
DC
189 code => sub {
190 my ($param) = @_;
191
192 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
193
dd902da7 194 my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
409f8203 195
e3b02ffe 196 $result->{health} = 'UNKNOWN' if !defined $result->{health};
dd902da7 197 $result = { health => $result->{health} } if $param->{healthonly};
acd3d916 198
409f8203
DC
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
2501;