]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
1 package PVE
::Diskmanage
;
9 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
11 my $SMARTCTL = "/usr/sbin/smartctl";
12 my $ZPOOL = "/sbin/zpool";
13 my $SGDISK = "/sbin/sgdisk";
14 my $PVS = "/sbin/pvs";
15 my $UDEVADM = "/bin/udevadm";
17 sub verify_blockdev_path
{
20 die "missing path" if !$rel_path;
21 my $path = abs_path
($rel_path);
22 die "failed to get absolute path to $rel_path\n" if !$path;
24 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
26 $path = "/dev/$1"; # untaint
28 assert_blockdev
($path);
34 my ($dev, $noerr) = @_;
36 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
37 return undef if $noerr;
38 die "not a valid block device\n";
45 my ($disk, $uuid) = @_;
47 assert_blockdev
($disk);
49 # we should already have checked if it is in use in the api call
50 # but we check again for safety
51 die "disk $disk is already in use\n" if disk_is_used
($disk);
53 my $id = $uuid || 'R';
54 run_command
([$SGDISK, $disk, '-U', $id]);
64 my $disklist = get_disks
($dev);
66 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
67 return 1 if $disklist->{$dev}->{used
};
75 assert_blockdev
($disk);
81 $returncode = run_command
([$SMARTCTL, '-a', '-f', 'brief', $disk], noerr
=> 1, outfunc
=> sub{
84 if ($datastarted && $line =~ m/^[ \d]{2}\d/) {
86 my @data = split /\s+/, $line;
88 $entry->{name
} = $data[1];
89 $entry->{flags
} = $data[2];
90 # the +0 makes a number out of the strings
91 $entry->{value
} = $data[3] + 0;
92 $entry->{worst
} = $data[4] + 0;
93 $entry->{threshold
} = $data[5] + 0;
94 $entry->{fail
} = $data[6];
95 $entry->{raw
} = $data[7];
96 $entry->{id
} = $data[0];
97 push @{$smartdata->{attributes
}}, $entry;
98 } elsif ($line =~ m/self\-assessment test result: (.*)$/) {
99 $smartdata->{health
} = $1;
100 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
107 # bit 0 and 1 mark an severe smartctl error
108 # all others are for disk status, so ignore them
110 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
111 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
116 sub get_smart_health
{
119 return "NOT A DEVICE" if !assert_blockdev
($disk, 1);
123 run_command
([$SMARTCTL, '-H', $disk], noerr
=> 1, outfunc
=> sub {
126 if ($line =~ m/test result: (.*)$/) {
128 } elsif ($line =~ m/open device: (.*) failed: (.*)$/) {
129 $message = "FAILED TO OPEN";
130 } elsif ($line =~ m/^SMART Disabled/) {
131 $message = "SMART DISABLED";
138 sub get_zfs_devices
{
141 # use zpool and parttype uuid,
142 # because log and cache do not have
145 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
148 if ($line =~ m
|^\t([^\t]+)\t|) {
155 # because maybe zfs tools are not installed
158 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
159 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
161 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
163 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
164 $list->{$real_dev} = 1;
170 sub get_lvm_devices
{
173 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
176 if ($line =~ m
|^/dev/|) {
182 # if something goes wrong, we do not want
183 # to give up, but indicate an error has occured
186 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
188 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
190 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
191 $list->{$real_dev} = 1;
197 sub get_ceph_journals
{
198 my $journalhash = {};
200 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
202 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
204 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
205 $journalhash->{$real_dev} = 1;
217 run_command
([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc
=> sub {
223 return undef if !$info;
225 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
226 return undef if $info =~ m/^E: ID_CDROM/m;
228 # we use this, because some disks are not simply in /dev
229 # e.g. /dev/cciss/c0d0
230 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
231 $data->{devpath
} = $1;
233 return if !defined($data->{devpath
});
235 $data->{serial
} = 'unknown';
236 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
237 $data->{serial
} = $1;
241 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
247 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
251 if ($info =~ m/^E: ID_BUS=usb$/m) {
255 $data->{wwn
} = 'unknown';
256 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
263 sub get_sysdir_info
{
268 my $size = file_read_firstline
("$sysdir/size");
269 return undef if !$size;
271 # linux always considers sectors to be 512 bytes,
272 # independently of real block size
273 $data->{size
} = $size * 512;
275 # dir/queue/rotational should be 1 for hdd, 0 for ssd
276 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational");
278 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
279 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
290 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
292 foreach my $mount (@$mounts) {
293 next if $mount->[0] !~ m
|^/dev/|;
294 $mounted->{abs_path
($mount->[0])} = $mount->[1];
297 my $dev_is_mounted = sub {
299 return $mounted->{$dev};
302 my $dir_is_empty = sub {
305 my $dh = IO
::Dir-
>new ($dir);
308 while (defined(my $tmp = $dh->read)) {
309 next if $tmp eq '.' || $tmp eq '..';
317 my $journalhash = get_ceph_journals
();
319 my $zfslist = get_zfs_devices
();
321 my $lvmlist = get_lvm_devices
();
323 dir_glob_foreach
('/sys/block', '.*', sub {
325 return if defined($disk) && $disk ne $dev;
326 # whitelisting following devices
327 # hdX: ide block device
328 # sdX: sd block device
329 # vdX: virtual block device
330 # xvdX: xen virtual block device
331 # nvmeXnY: nvme devices
332 # cXnY: cciss devices
333 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
334 $dev !~ m/^nvme\d+n\d+$/ &&
335 $dev !~ m/^c\d+d\d+$/;
337 my $data = get_udev_info
($dev);
338 return if !defined($data);
339 my $devpath = $data->{devpath
};
341 my $sysdir = "/sys/block/$dev";
343 return if ! -d
"$sysdir/device";
345 # we do not want iscsi devices
346 return if readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|;
348 my $sysdata = get_sysdir_info
($sysdir);
349 return if !defined($sysdata);
351 my $type = 'unknown';
353 if ($sysdata->{rotational
} == 0) {
356 } elsif ($sysdata->{rotational
} == 1) {
357 if ($data->{rpm
} != -1) {
359 } elsif ($data->{usb
}) {
365 my $health = 'UNKNOWN';
368 if ($type eq 'ssd' && !defined($disk)) {
369 # if we have an ssd we try to get the wearout indicator
371 my $smartdata = get_smart_data
($devpath);
372 $health = $smartdata->{health
};
373 foreach my $attr (@{$smartdata->{attributes
}}) {
374 # ID 233 is media wearout indicator on intel and sandisk
375 # ID 177 is media wearout indicator on samsung
376 next if ($attr->{id
} != 233 && $attr->{id
} != 177);
377 next if ($attr->{name
} !~ m/wear/i);
378 $wearout = $attr->{value
};
380 # prefer the 233 value
381 last if ($attr->{id
} == 233);
383 } elsif (!defined($disk)) {
384 # we do not need smart data if we check a single disk
385 # because this functionality is only for disk_is_used
386 $health = get_smart_health
($devpath) if !defined($disk);
392 $used = 'LVM' if $lvmlist->{$devpath};
394 $used = 'mounted' if &$dev_is_mounted($devpath);
396 $used = 'ZFS' if $zfslist->{$devpath};
398 $disklist->{$dev} = {
399 vendor
=> $sysdata->{vendor
},
400 model
=> $sysdata->{model
},
401 size
=> $sysdata->{size
},
402 serial
=> $data->{serial
},
414 my $journal_count = 0;
416 my $found_partitions;
418 my $found_mountpoints;
421 my $partpath = $devpath;
423 # remove part after last / to
424 # get the base path for the partitions
425 # e.g. from /dev/cciss/c0d0 get /dev/cciss
426 $partpath =~ s/\/[^\/]+$//;
428 dir_glob_foreach
("$sysdir", "$dev.+", sub {
431 $found_partitions = 1;
433 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
434 $found_mountpoints = 1;
435 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
440 if ($lvmlist->{"$partpath/$part"}) {
444 if ($zfslist->{"$partpath/$part"}) {
448 $journal_count++ if $journalhash->{"$partpath/$part"};
450 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
455 $used = 'mounted' if $found_mountpoints && !$used;
456 $used = 'LVM' if $found_lvm && !$used;
457 $used = 'ZFS' if $found_zfs && !$used;
458 $used = 'Device Mapper' if $found_dm && !$used;
459 $used = 'partitions' if $found_partitions && !$used;
461 # multipath, software raid, etc.
462 # this check comes in last, to show more specific info
464 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
466 $disklist->{$dev}->{used
} = $used if $used;
467 $disklist->{$dev}->{osdid
} = $osdid;
468 $disklist->{$dev}->{journals
} = $journal_count;