]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks.pm
move SMART error handling into get_disks
[pve-storage.git] / PVE / API2 / Disks.pm
1 package PVE::API2::Disks;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Diskmanage;
8 use HTTP::Status qw(:constants);
9 use PVE::JSONSchema qw(get_standard_option);
10
11 use PVE::RESTHandler;
12
13 use base qw(PVE::RESTHandler);
14
15 use 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 },
127 returns => { type => 'object' },
128 code => sub {
129 my ($param) = @_;
130
131 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
132
133 my $result = {};
134
135 if ($param->{healthonly}) {
136 $result = { health => PVE::Diskmanage::get_smart_health($disk) };
137 } else {
138 $result = PVE::Diskmanage::get_smart_data($disk);
139 }
140
141 $result->{health} = 'UNKOWN' if !defined $result->{health};
142
143 return $result;
144 }});
145
146 __PACKAGE__->register_method ({
147 name => 'initgpt',
148 path => 'initgpt',
149 method => 'POST',
150 description => "Initialize Disk with GPT",
151 protected => 1,
152 proxyto => "node",
153 permissions => {
154 check => ['perm', '/', ['Sys.Modify']],
155 },
156 parameters => {
157 additionalProperties => 0,
158 properties => {
159 node => get_standard_option('pve-node'),
160 disk => {
161 type => 'string',
162 description => "Block device name",
163 pattern => '^/dev/[a-zA-Z0-9\/]+$',
164 },
165 uuid => {
166 type => 'string',
167 description => 'UUID for the GPT table',
168 pattern => '[a-fA-F0-9\-]+',
169 maxLength => 36,
170 optional => 1,
171 },
172 },
173 },
174 returns => { type => 'string' },
175 code => sub {
176 my ($param) = @_;
177
178 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
179
180 my $rpcenv = PVE::RPCEnvironment::get();
181
182 my $authuser = $rpcenv->get_user();
183
184 die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk);
185 my $worker = sub {
186 PVE::Diskmanage::init_disk($disk, $param->{uuid});
187 };
188
189 my $diskid = $disk;
190 $diskid =~ s|^.*/||; # remove all up to the last slash
191 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
192 }});
193
194 1;