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