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