]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks.pm
api: storage/content: add ctime to return schema
[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 11use PVE::API2::Disks::LVM;
0ea9f384 12use PVE::API2::Disks::LVMThin;
793d720c 13use PVE::API2::Disks::Directory;
c84106ed 14use PVE::API2::Disks::ZFS;
8b6842ca 15
409f8203
DC
16use PVE::RESTHandler;
17
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' },
409f8203
DC
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'),
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;
558d412d 140 next if !($entry->{gpt} || !$entry->{used} || $entry->{used} eq 'LVM');
83bbd6f5
DC
141 } elsif ($type eq 'unused') {
142 next if $entry->{used};
143 } elsif ($type ne '') {
144 die "internal error"; # should not happen
145 }
409f8203
DC
146 push @$result, $entry;
147 }
148 return $result;
149 }});
150
151__PACKAGE__->register_method ({
152 name => 'smart',
153 path => 'smart',
154 method => 'GET',
155 description => "Get SMART Health of a disk.",
156 protected => 1,
157 proxyto => "node",
158 permissions => {
159 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
160 },
161 parameters => {
162 additionalProperties => 0,
163 properties => {
164 node => get_standard_option('pve-node'),
165 disk => {
166 type => 'string',
167 pattern => '^/dev/[a-zA-Z0-9\/]+$',
168 description => "Block device name",
169 },
170 healthonly => {
171 type => 'boolean',
172 description => "If true returns only the health status",
173 optional => 1,
174 },
175 },
176 },
16bf963b
FG
177 returns => {
178 type => 'object',
179 properties => {
180 health => { type => 'string' },
181 type => { type => 'string', optional => 1 },
182 attributes => { type => 'array', optional => 1},
183 text => { type => 'string', optional => 1 },
184 },
185 },
409f8203
DC
186 code => sub {
187 my ($param) = @_;
188
189 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
190
dd902da7 191 my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
409f8203 192
e3b02ffe 193 $result->{health} = 'UNKNOWN' if !defined $result->{health};
dd902da7 194 $result = { health => $result->{health} } if $param->{healthonly};
acd3d916 195
409f8203
DC
196 return $result;
197 }});
198
199__PACKAGE__->register_method ({
200 name => 'initgpt',
201 path => 'initgpt',
202 method => 'POST',
203 description => "Initialize Disk with GPT",
204 protected => 1,
205 proxyto => "node",
206 permissions => {
207 check => ['perm', '/', ['Sys.Modify']],
208 },
209 parameters => {
210 additionalProperties => 0,
211 properties => {
212 node => get_standard_option('pve-node'),
213 disk => {
214 type => 'string',
215 description => "Block device name",
216 pattern => '^/dev/[a-zA-Z0-9\/]+$',
217 },
218 uuid => {
219 type => 'string',
220 description => 'UUID for the GPT table',
221 pattern => '[a-fA-F0-9\-]+',
222 maxLength => 36,
223 optional => 1,
224 },
225 },
226 },
227 returns => { type => 'string' },
228 code => sub {
229 my ($param) = @_;
230
231 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
232
233 my $rpcenv = PVE::RPCEnvironment::get();
234
235 my $authuser = $rpcenv->get_user();
236
237 die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk);
238 my $worker = sub {
239 PVE::Diskmanage::init_disk($disk, $param->{uuid});
240 };
241
242 my $diskid = $disk;
243 $diskid =~ s|^.*/||; # remove all up to the last slash
244 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
245 }});
246
2471;