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