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