]>
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, 1);
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, '-H', '-A', '-f', 'brief', $disk], noerr
=> 1, outfunc
=> sub{
84 # ATA SMART attributes, e.g.:
85 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
86 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
88 # SAS and NVME disks, e.g.:
89 # Data Units Written: 5,584,952 [2.85 TB]
90 # Accumulated start-stop cycles: 34
92 if (defined($type) && $type eq 'ata' && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/) {
94 $entry->{name
} = $2 if defined $2;
95 $entry->{flags
} = $3 if defined $3;
96 # the +0 makes a number out of the strings
97 $entry->{value
} = $4+0 if defined $4;
98 $entry->{worst
} = $5+0 if defined $5;
99 $entry->{threshold
} = $6+0 if defined $6;
100 $entry->{fail
} = $7 if defined $7;
101 $entry->{raw
} = $8 if defined $8;
102 $entry->{id
} = $1 if defined $1;
103 push @{$smartdata->{attributes
}}, $entry;
104 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
105 $smartdata->{health
} = $1;
106 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
108 delete $smartdata->{text
};
109 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
111 } elsif (defined($type) && $type eq 'text') {
112 $smartdata->{text
} = '' if !defined $smartdata->{text
};
113 $smartdata->{text
} .= "$line\n";
119 # bit 0 and 1 mark an severe smartctl error
120 # all others are for disk status, so ignore them
122 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
123 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
126 $smartdata->{type
} = $type;
131 sub get_smart_health
{
134 return "NOT A DEVICE" if !assert_blockdev
($disk, 1);
138 run_command
([$SMARTCTL, '-H', $disk], noerr
=> 1, outfunc
=> sub {
141 if ($line =~ m/test result: (.*)$/) {
143 } elsif ($line =~ m/open device: (.*) failed: (.*)$/) {
144 $message = "FAILED TO OPEN";
145 } elsif ($line =~ m/^SMART Disabled/) {
146 $message = "SMART DISABLED";
153 sub get_zfs_devices
{
156 # use zpool and parttype uuid,
157 # because log and cache do not have
160 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
163 if ($line =~ m
|^\t([^\t]+)\t|) {
170 # because maybe zfs tools are not installed
173 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
174 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
176 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
178 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
179 $list->{$real_dev} = 1;
185 sub get_lvm_devices
{
188 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
191 if ($line =~ m
|^/dev/|) {
197 # if something goes wrong, we do not want
198 # to give up, but indicate an error has occured
201 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
203 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
205 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
206 $list->{$real_dev} = 1;
212 sub get_ceph_journals
{
213 my $journalhash = {};
215 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
217 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
219 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
220 $journalhash->{$real_dev} = 1;
232 run_command
([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc
=> sub {
238 return undef if !$info;
240 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
241 return undef if $info =~ m/^E: ID_CDROM/m;
243 # we use this, because some disks are not simply in /dev
244 # e.g. /dev/cciss/c0d0
245 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
246 $data->{devpath
} = $1;
248 return if !defined($data->{devpath
});
250 $data->{serial
} = 'unknown';
251 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
252 $data->{serial
} = $1;
256 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
262 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
266 if ($info =~ m/^E: ID_BUS=usb$/m) {
270 $data->{wwn
} = 'unknown';
271 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
278 sub get_sysdir_info
{
283 my $size = file_read_firstline
("$sysdir/size");
284 return undef if !$size;
286 # linux always considers sectors to be 512 bytes,
287 # independently of real block size
288 $data->{size
} = $size * 512;
290 # dir/queue/rotational should be 1 for hdd, 0 for ssd
291 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational");
293 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
294 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
300 my ($disk, $nosmart) = @_;
305 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
307 foreach my $mount (@$mounts) {
308 next if $mount->[0] !~ m
|^/dev/|;
309 $mounted->{abs_path
($mount->[0])} = $mount->[1];
312 my $dev_is_mounted = sub {
314 return $mounted->{$dev};
317 my $dir_is_empty = sub {
320 my $dh = IO
::Dir-
>new ($dir);
323 while (defined(my $tmp = $dh->read)) {
324 next if $tmp eq '.' || $tmp eq '..';
332 my $journalhash = get_ceph_journals
();
334 my $zfslist = get_zfs_devices
();
336 my $lvmlist = get_lvm_devices
();
338 dir_glob_foreach
('/sys/block', '.*', sub {
340 return if defined($disk) && $disk ne $dev;
341 # whitelisting following devices
342 # hdX: ide block device
343 # sdX: sd block device
344 # vdX: virtual block device
345 # xvdX: xen virtual block device
346 # nvmeXnY: nvme devices
347 # cXnY: cciss devices
348 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
349 $dev !~ m/^nvme\d+n\d+$/ &&
350 $dev !~ m/^c\d+d\d+$/;
352 my $data = get_udev_info
($dev);
353 return if !defined($data);
354 my $devpath = $data->{devpath
};
356 my $sysdir = "/sys/block/$dev";
358 return if ! -d
"$sysdir/device";
360 # we do not want iscsi devices
361 return if readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|;
363 my $sysdata = get_sysdir_info
($sysdir);
364 return if !defined($sysdata);
366 my $type = 'unknown';
368 if ($sysdata->{rotational
} == 0) {
371 } elsif ($sysdata->{rotational
} == 1) {
372 if ($data->{rpm
} != -1) {
374 } elsif ($data->{usb
}) {
380 my $health = 'UNKNOWN';
385 if ($type eq 'ssd') {
386 # if we have an ssd we try to get the wearout indicator
388 my $smartdata = get_smart_data
($devpath);
389 $health = $smartdata->{health
};
390 foreach my $attr (@{$smartdata->{attributes
}}) {
391 # ID 233 is media wearout indicator on intel and sandisk
392 # ID 177 is media wearout indicator on samsung
393 next if ($attr->{id
} != 233 && $attr->{id
} != 177);
394 next if ($attr->{name
} !~ m/wear/i);
395 $wearout = $attr->{value
};
397 # prefer the 233 value
398 last if ($attr->{id
} == 233);
401 # else we just get the health
402 $health = get_smart_health
($devpath);
409 $used = 'LVM' if $lvmlist->{$devpath};
411 $used = 'mounted' if &$dev_is_mounted($devpath);
413 $used = 'ZFS' if $zfslist->{$devpath};
415 $disklist->{$dev} = {
416 vendor
=> $sysdata->{vendor
},
417 model
=> $sysdata->{model
},
418 size
=> $sysdata->{size
},
419 serial
=> $data->{serial
},
431 my $journal_count = 0;
433 my $found_partitions;
435 my $found_mountpoints;
438 my $partpath = $devpath;
440 # remove part after last / to
441 # get the base path for the partitions
442 # e.g. from /dev/cciss/c0d0 get /dev/cciss
443 $partpath =~ s/\/[^\/]+$//;
445 dir_glob_foreach
("$sysdir", "$dev.+", sub {
448 $found_partitions = 1;
450 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
451 $found_mountpoints = 1;
452 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
457 if ($lvmlist->{"$partpath/$part"}) {
461 if ($zfslist->{"$partpath/$part"}) {
465 $journal_count++ if $journalhash->{"$partpath/$part"};
467 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
472 $used = 'mounted' if $found_mountpoints && !$used;
473 $used = 'LVM' if $found_lvm && !$used;
474 $used = 'ZFS' if $found_zfs && !$used;
475 $used = 'Device Mapper' if $found_dm && !$used;
476 $used = 'partitions' if $found_partitions && !$used;
478 # multipath, software raid, etc.
479 # this check comes in last, to show more specific info
481 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
483 $disklist->{$dev}->{used
} = $used if $used;
484 $disklist->{$dev}->{osdid
} = $osdid;
485 $disklist->{$dev}->{journals
} = $journal_count;