]>
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);
12 use PVE
::Tools
qw(run_command);
14 use PVE
::API2
::Disks
::Directory
;
15 use PVE
::API2
::Disks
::LVM
;
16 use PVE
::API2
::Disks
::LVMThin
;
17 use PVE
::API2
::Disks
::ZFS
;
20 use base
qw(PVE::RESTHandler);
22 __PACKAGE__-
>register_method ({
23 subclass
=> "PVE::API2::Disks::LVM",
27 __PACKAGE__-
>register_method ({
28 subclass
=> "PVE::API2::Disks::LVMThin",
32 __PACKAGE__-
>register_method ({
33 subclass
=> "PVE::API2::Disks::Directory",
37 __PACKAGE__-
>register_method ({
38 subclass
=> "PVE::API2::Disks::ZFS",
42 __PACKAGE__-
>register_method ({
47 permissions
=> { user
=> 'all' },
48 description
=> "Node index.",
50 additionalProperties
=> 0,
52 node
=> get_standard_option
('pve-node'),
61 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
68 { name
=> 'initgpt' },
71 { name
=> 'lvmthin' },
72 { name
=> 'directory' },
73 { name
=> 'wipedisk' },
80 __PACKAGE__-
>register_method ({
84 description
=> "List local disks.",
89 ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
90 ['perm', '/nodes/{node}', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
94 additionalProperties
=> 0,
96 node
=> get_standard_option
('pve-node'),
97 'include-partitions' => {
98 description
=> "Also include partitions.",
104 description
=> "Skip smart checks.",
110 description
=> "Only list specific types of disks.",
112 enum
=> ['unused', 'journal_disks'],
124 description
=> 'The device path',
126 used
=> { type
=> 'string', optional
=> 1 },
127 gpt
=> { type
=> 'boolean' },
128 size
=> { type
=> 'integer'},
129 osdid
=> { type
=> 'integer'},
130 vendor
=> { type
=> 'string', optional
=> 1 },
131 model
=> { type
=> 'string', optional
=> 1 },
132 serial
=> { type
=> 'string', optional
=> 1 },
133 wwn
=> { type
=> 'string', optional
=> 1},
134 health
=> { type
=> 'string', optional
=> 1},
137 description
=> 'For partitions only. The device path of ' .
138 'the disk the partition resides on.',
147 my $skipsmart = $param->{skipsmart
} // 0;
148 my $include_partitions = $param->{'include-partitions'} // 0;
150 my $disks = PVE
::Diskmanage
::get_disks
(
156 my $type = $param->{type
} // '';
159 foreach my $disk (sort keys %$disks) {
160 my $entry = $disks->{$disk};
161 if ($type eq 'journal_disks') {
162 next if $entry->{osdid
} >= 0;
163 if (my $usage = $entry->{used
}) {
164 next if !($usage eq 'partitions' && $entry->{gpt
}
167 } elsif ($type eq 'unused') {
168 next if $entry->{used
};
169 } elsif ($type ne '') {
170 die "internal error"; # should not happen
172 push @$result, $entry;
177 __PACKAGE__-
>register_method ({
181 description
=> "Get SMART Health of a disk.",
185 check
=> ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any
=> 1],
188 additionalProperties
=> 0,
190 node
=> get_standard_option
('pve-node'),
193 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
194 description
=> "Block device name",
198 description
=> "If true returns only the health status",
206 health
=> { type
=> 'string' },
207 type
=> { type
=> 'string', optional
=> 1 },
208 attributes
=> { type
=> 'array', optional
=> 1},
209 text
=> { type
=> 'string', optional
=> 1 },
215 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
217 my $result = PVE
::Diskmanage
::get_smart_data
($disk, $param->{healthonly
});
219 $result->{health
} = 'UNKNOWN' if !defined $result->{health
};
220 $result = { health
=> $result->{health
} } if $param->{healthonly
};
225 __PACKAGE__-
>register_method ({
229 description
=> "Initialize Disk with GPT",
233 check
=> ['perm', '/', ['Sys.Modify']],
236 additionalProperties
=> 0,
238 node
=> get_standard_option
('pve-node'),
241 description
=> "Block device name",
242 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
246 description
=> 'UUID for the GPT table',
247 pattern
=> '[a-fA-F0-9\-]+',
253 returns
=> { type
=> 'string' },
257 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
259 my $rpcenv = PVE
::RPCEnvironment
::get
();
261 my $authuser = $rpcenv->get_user();
263 die "$disk is a partition\n" if PVE
::Diskmanage
::is_partition
($disk);
264 die "disk $disk already in use\n" if PVE
::Diskmanage
::disk_is_used
($disk);
266 PVE
::Diskmanage
::init_disk
($disk, $param->{uuid
});
270 $diskid =~ s
|^.*/||; # remove all up to the last slash
271 return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
274 __PACKAGE__-
>register_method ({
278 description
=> "Wipe a disk or partition.",
282 additionalProperties
=> 0,
284 node
=> get_standard_option
('pve-node'),
287 description
=> "Block device name",
288 pattern
=> '^/dev/[a-zA-Z0-9\/]+$',
292 returns
=> { type
=> 'string' },
296 my $disk = PVE
::Diskmanage
::verify_blockdev_path
($param->{disk
});
298 my $mounted = PVE
::Diskmanage
::is_mounted
($disk);
299 die "disk/partition '${mounted}' is mounted\n" if $mounted;
301 my $held = PVE
::Diskmanage
::has_holder
($disk);
302 die "disk/partition '${held}' has a holder\n" if $held;
304 my $rpcenv = PVE
::RPCEnvironment
::get
();
305 my $authuser = $rpcenv->get_user();
308 PVE
::Diskmanage
::wipe_blockdev
($disk);
309 PVE
::Diskmanage
::udevadm_trigger
($disk);
312 my $basename = basename
($disk); # avoid '/' in the ID
314 return $rpcenv->fork_worker('wipedisk', $basename, $authuser, $worker);