]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks.pm
merge get_smart_data/health
[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 => {
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 },
136 code => sub {
137 my ($param) = @_;
138
139 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
140
141 my $result = {};
142
143 my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
144
145 $result->{health} = 'UNKNOWN' if !defined $result->{health};
146 $result = { health => $result->{health} } if $param->{healthonly};
147
148 return $result;
149 }});
150
151 __PACKAGE__->register_method ({
152 name => 'initgpt',
153 path => 'initgpt',
154 method => 'POST',
155 description => "Initialize Disk with GPT",
156 protected => 1,
157 proxyto => "node",
158 permissions => {
159 check => ['perm', '/', ['Sys.Modify']],
160 },
161 parameters => {
162 additionalProperties => 0,
163 properties => {
164 node => get_standard_option('pve-node'),
165 disk => {
166 type => 'string',
167 description => "Block device name",
168 pattern => '^/dev/[a-zA-Z0-9\/]+$',
169 },
170 uuid => {
171 type => 'string',
172 description => 'UUID for the GPT table',
173 pattern => '[a-fA-F0-9\-]+',
174 maxLength => 36,
175 optional => 1,
176 },
177 },
178 },
179 returns => { type => 'string' },
180 code => sub {
181 my ($param) = @_;
182
183 my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
184
185 my $rpcenv = PVE::RPCEnvironment::get();
186
187 my $authuser = $rpcenv->get_user();
188
189 die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk);
190 my $worker = sub {
191 PVE::Diskmanage::init_disk($disk, $param->{uuid});
192 };
193
194 my $diskid = $disk;
195 $diskid =~ s|^.*/||; # remove all up to the last slash
196 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
197 }});
198
199 1;