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