]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks.pm
1 package PVE
::API2
::Disks
;
7 use HTTP
::Status
qw(:constants);
10 use PVE
::JSONSchema
qw(get_standard_option);
13 use PVE
::API2
::Disks
::Directory
;
14 use PVE
::API2
::Disks
::LVM
;
15 use PVE
::API2
::Disks
::LVMThin
;
16 use PVE
::API2
::Disks
::ZFS
;
19 use base
qw(PVE::RESTHandler);
21 __PACKAGE__-
>register_method ({
22 subclass
=> "PVE::API2::Disks::LVM",
26 __PACKAGE__-
>register_method ({
27 subclass
=> "PVE::API2::Disks::LVMThin",
31 __PACKAGE__-
>register_method ({
32 subclass
=> "PVE::API2::Disks::Directory",
36 __PACKAGE__-
>register_method ({
37 subclass
=> "PVE::API2::Disks::ZFS",
41 __PACKAGE__-
>register_method ({
46 permissions
=> { user
=> 'all' },
47 description
=> "Node index.",
49 additionalProperties
=> 0,
51 node
=> get_standard_option
('pve-node'),
60 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
67 { name
=> 'initgpt' },
70 { name
=> 'lvmthin' },
71 { name
=> 'directory' },
72 { name
=> 'wipedisk' },
79 __PACKAGE__-
>register_method ({
83 description
=> "List local disks.",
88 ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
89 ['perm', '/nodes/{node}', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
93 additionalProperties
=> 0,
95 node
=> get_standard_option
('pve-node'),
96 'include-partitions' => {
97 description
=> "Also include partitions.",
103 description
=> "Skip smart checks.",
109 description
=> "Only list specific types of disks.",
111 enum
=> ['unused', 'journal_disks'],
123 description
=> 'The device path',
125 used
=> { type
=> 'string', optional
=> 1 },
126 gpt
=> { type
=> 'boolean' },
127 size
=> { type
=> 'integer'},
128 osdid
=> { type
=> 'integer'},
129 vendor
=> { type
=> 'string', optional
=> 1 },
130 model
=> { type
=> 'string', optional
=> 1 },
131 serial
=> { type
=> 'string', optional
=> 1 },
132 wwn
=> { type
=> 'string', optional
=> 1},
133 health
=> { type
=> 'string', optional
=> 1},
136 description
=> 'For partitions only. The device path of ' .
137 'the disk the partition resides on.',
146 my $skipsmart = $param->{skipsmart
} // 0;
147 my $include_partitions = $param->{'include-partitions'} // 0;
149 my $disks = PVE
::Diskmanage
::get_disks
(
155 my $type = $param->{type
} // '';
158 foreach my $disk (sort keys %$disks) {
159 my $entry = $disks->{$disk};
160 if ($type eq 'journal_disks') {
161 next if $entry->{osdid
} >= 0;
162 if (my $usage = $entry->{used
}) {
163 next if !($usage eq 'partitions' && $entry->{gpt
}
166 } elsif ($type eq 'unused') {
167 next if $entry->{used
};
168 } elsif ($type ne '') {
169 die "internal error"; # should not happen
171 push @$result, $entry;
176 __PACKAGE__-
>register_method ({
180 description
=> "Get SMART Health of a disk.",
184 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
187 additionalProperties
=> 0,
189 node
=> get_standard_option
('pve-node'),
192 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
193 description
=> "Block device name",
197 description
=> "If true returns only the health status",
205 health
=> { type
=> 'string' },
206 type
=> { type
=> 'string', optional
=> 1 },
207 attributes
=> { type
=> 'array', optional
=> 1},
208 text
=> { type
=> 'string', optional
=> 1 },
214 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
216 my $result = PVE
::Diskmanage
::get_smart_data
($disk, $param->{healthonly
});
218 $result->{health
} = 'UNKNOWN' if !defined $result->{health
};
219 $result = { health
=> $result->{health
} } if $param->{healthonly
};
224 __PACKAGE__-
>register_method ({
228 description
=> "Initialize Disk with GPT",
232 check
=> ['perm', '/', ['Sys.Modify']],
235 additionalProperties
=> 0,
237 node
=> get_standard_option
('pve-node'),
240 description
=> "Block device name",
241 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
245 description
=> 'UUID for the GPT table',
246 pattern
=> '[a-fA-F0-9\-]+',
252 returns
=> { type
=> 'string' },
256 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
258 my $rpcenv = PVE
::RPCEnvironment
::get
();
260 my $authuser = $rpcenv->get_user();
262 die "disk $disk already in use\n" if PVE
::Diskmanage
::disk_is_used
($disk);
264 PVE
::Diskmanage
::init_disk
($disk, $param->{uuid
});
268 $diskid =~ s
|^.*/||; # remove all up to the last slash
269 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
272 __PACKAGE__-
>register_method ({
276 description
=> "Wipe a disk or partition.",
280 additionalProperties
=> 0,
282 node
=> get_standard_option
('pve-node'),
285 description
=> "Block device name",
286 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
290 returns
=> { type
=> 'string' },
294 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
296 my $mounted = PVE
::Diskmanage
::is_mounted
($disk);
297 die "disk/partition '${mounted}' is mounted\n" if $mounted;
299 my $held = PVE
::Diskmanage
::has_holder
($disk);
300 die "disk/partition '${held}' has a holder\n" if $held;
302 my $rpcenv = PVE
::RPCEnvironment
::get
();
303 my $authuser = $rpcenv->get_user();
305 my $worker = sub { PVE
::Diskmanage
::wipe_blockdev
($disk); };
307 my $basename = basename
($disk); # avoid '/' in the ID
309 return $rpcenv->fork_worker('wipedisk', $basename, $authuser, $worker);