]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
1 package PVE
::Diskmanage
;
10 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
12 my $SMARTCTL = "/usr/sbin/smartctl";
13 my $ZPOOL = "/sbin/zpool";
14 my $SGDISK = "/sbin/sgdisk";
15 my $PVS = "/sbin/pvs";
16 my $UDEVADM = "/bin/udevadm";
18 sub verify_blockdev_path
{
21 die "missing path" if !$rel_path;
22 my $path = abs_path
($rel_path);
23 die "failed to get absolute path to $rel_path\n" if !$path;
25 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
27 $path = "/dev/$1"; # untaint
29 assert_blockdev
($path);
35 my ($dev, $noerr) = @_;
37 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
38 return undef if $noerr;
39 die "not a valid block device\n";
46 my ($disk, $uuid) = @_;
48 assert_blockdev
($disk);
50 # we should already have checked if it is in use in the api call
51 # but we check again for safety
52 die "disk $disk is already in use\n" if disk_is_used
($disk);
54 my $id = $uuid || 'R';
55 run_command
([$SGDISK, $disk, '-U', $id]);
65 my $disklist = get_disks
($dev, 1);
67 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
68 return 1 if $disklist->{$dev}->{used
};
74 my ($disk, $healthonly) = @_;
76 assert_blockdev
($disk);
83 if $disk =~ m!^/dev/nvme\d+n\d+$!;
85 my $cmd = [$SMARTCTL, '-H'];
86 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
90 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
93 # ATA SMART attributes, e.g.:
94 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
95 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
97 # SAS and NVME disks, e.g.:
98 # Data Units Written: 5,584,952 [2.85 TB]
99 # Accumulated start-stop cycles: 34
101 if (defined($type) && $type eq 'ata' && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(.*)$/) {
105 $entry->{name
} = $2 if defined $2;
106 $entry->{flags
} = $3 if defined $3;
107 # the +0 makes a number out of the strings
108 $entry->{value
} = $4+0 if defined $4;
109 $entry->{worst
} = $5+0 if defined $5;
110 # some disks report the default threshold as --- instead of 000
111 if (defined($6) && $6 eq '---') {
112 $entry->{threshold
} = 0;
114 $entry->{threshold
} = $6+0 if defined $6;
116 $entry->{fail
} = $7 if defined $7;
117 $entry->{raw
} = $8 if defined $8;
118 $entry->{id
} = $1 if defined $1;
119 push @{$smartdata->{attributes
}}, $entry;
120 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
121 $smartdata->{health
} = $1;
122 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
124 delete $smartdata->{text
};
125 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
127 } elsif (defined($type) && $type eq 'text') {
128 $smartdata->{text
} = '' if !defined $smartdata->{text
};
129 $smartdata->{text
} .= "$line\n";
130 } elsif ($line =~ m/SMART Disabled/) {
131 $smartdata->{health
} = "SMART Disabled";
137 # bit 0 and 1 mark an severe smartctl error
138 # all others are for disk status, so ignore them
140 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
141 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
144 $smartdata->{type
} = $type;
149 sub get_zfs_devices
{
152 # use zpool and parttype uuid,
153 # because log and cache do not have
156 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
159 if ($line =~ m
|^\t([^\t]+)\t|) {
166 # because maybe zfs tools are not installed
169 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
170 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
172 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
174 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
175 $list->{$real_dev} = 1;
181 sub get_lvm_devices
{
184 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
187 if ($line =~ m
|^/dev/|) {
193 # if something goes wrong, we do not want
194 # to give up, but indicate an error has occured
197 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
199 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
201 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
202 $list->{$real_dev} = 1;
208 sub get_ceph_journals
{
209 my $journalhash = {};
211 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
212 my $db_uuid = '30cd0809-c2b2-499c-8879-2d6b78529876';
213 my $wal_uuid = '5ce17fce-4087-4169-b7ff-056cc58473f9';
214 my $block_uuid = 'cafecafe-9b03-4f30-b4c6-b4b80ceff106';
216 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
217 my ($entry, $type) = @_;
218 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
219 if ($type eq $journal_uuid) {
220 $journalhash->{$real_dev} = 1;
221 } elsif ($type eq $db_uuid) {
222 $journalhash->{$real_dev} = 2;
223 } elsif ($type eq $wal_uuid) {
224 $journalhash->{$real_dev} = 3;
225 } elsif ($type eq $block_uuid) {
226 $journalhash->{$real_dev} = 4;
239 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
245 return undef if !$info;
247 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
248 return undef if $info =~ m/^E: ID_CDROM/m;
250 # we use this, because some disks are not simply in /dev
251 # e.g. /dev/cciss/c0d0
252 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
253 $data->{devpath
} = $1;
255 return if !defined($data->{devpath
});
257 $data->{serial
} = 'unknown';
258 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
259 $data->{serial
} = $1;
263 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
269 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
273 if ($info =~ m/^E: ID_BUS=usb$/m) {
277 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
281 $data->{wwn
} = 'unknown';
282 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
289 sub get_sysdir_info
{
292 return undef if ! -d
"$sysdir/device";
296 my $size = file_read_firstline
("$sysdir/size");
297 return undef if !$size;
299 # linux always considers sectors to be 512 bytes,
300 # independently of real block size
301 $data->{size
} = $size * 512;
303 # dir/queue/rotational should be 1 for hdd, 0 for ssd
304 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
306 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
307 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
312 sub get_wear_leveling_info
{
313 my ($attributes, $model) = @_;
326 # find target attr id
330 foreach my $vendor (keys %$vendormap) {
331 if ($model =~ m/$vendor/i) {
332 $attrid = $vendormap->{$vendor};
333 # found the attribute
339 $attrid = $vendormap->{default};
342 foreach my $attr (@$attributes) {
343 next if $attr->{id
} != $attrid;
344 $wearout = $attr->{value
};
354 my $dh = IO
::Dir-
>new ($dir);
357 while (defined(my $tmp = $dh->read)) {
358 next if $tmp eq '.' || $tmp eq '..';
367 my ($disk, $nosmart) = @_;
372 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
374 foreach my $mount (@$mounts) {
375 next if $mount->[0] !~ m
|^/dev/|;
376 $mounted->{abs_path
($mount->[0])} = $mount->[1];
379 my $dev_is_mounted = sub {
381 return $mounted->{$dev};
384 my $journalhash = get_ceph_journals
();
386 my $zfslist = get_zfs_devices
();
388 my $lvmlist = get_lvm_devices
();
390 # we get cciss/c0d0 but need cciss!c0d0
391 if (defined($disk) && $disk =~ m
|^cciss
/|) {
392 $disk =~ s
|cciss
/|cciss
!|;
395 dir_glob_foreach
('/sys/block', '.*', sub {
397 return if defined($disk) && $disk ne $dev;
398 # whitelisting following devices
399 # hdX: ide block device
400 # sdX: sd block device
401 # vdX: virtual block device
402 # xvdX: xen virtual block device
403 # nvmeXnY: nvme devices
404 # cciss!cXnY: cciss devices
405 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
406 $dev !~ m/^nvme\d+n\d+$/ &&
407 $dev !~ m/^cciss\!c\d+d\d+$/;
409 my $data = get_udev_info
("/sys/block/$dev");
410 return if !defined($data);
411 my $devpath = $data->{devpath
};
413 my $sysdir = "/sys/block/$dev";
415 # we do not want iscsi devices
416 return if -l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|;
418 my $sysdata = get_sysdir_info
($sysdir);
419 return if !defined($sysdata);
421 my $type = 'unknown';
423 if ($sysdata->{rotational
} == 0) {
426 } elsif ($sysdata->{rotational
} == 1) {
427 if ($data->{rpm
} != -1) {
429 } elsif ($data->{usb
}) {
435 my $health = 'UNKNOWN';
440 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
441 $health = $smartdata->{health
} if $smartdata->{health
};
443 if ($type eq 'ssd') {
444 # if we have an ssd we try to get the wearout indicator
445 my $wearval = get_wear_leveling_info
($smartdata->{attributes
}, $data->{model
} || $sysdir->{model
});
446 $wearout = $wearval if $wearval;
453 $used = 'LVM' if $lvmlist->{$devpath};
455 $used = 'mounted' if &$dev_is_mounted($devpath);
457 $used = 'ZFS' if $zfslist->{$devpath};
459 # we replaced cciss/ with cciss! above
460 # but in the result we need cciss/ again
461 # because the caller might want to check the
462 # result again with the original parameter
463 if ($dev =~ m
|^cciss
!|) {
464 $dev =~ s
|^cciss
!|cciss
/|;
467 $disklist->{$dev} = {
468 vendor
=> $sysdata->{vendor
},
469 model
=> $data->{model
} || $sysdata->{model
},
470 size
=> $sysdata->{size
},
471 serial
=> $data->{serial
},
484 my $journal_count = 0;
488 my $found_partitions;
490 my $found_mountpoints;
493 my $partpath = $devpath;
495 # remove part after last / to
496 # get the base path for the partitions
497 # e.g. from /dev/cciss/c0d0 get /dev/cciss
498 $partpath =~ s/\/[^\/]+$//;
500 dir_glob_foreach
("$sysdir", "$dev.+", sub {
503 $found_partitions = 1;
505 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
506 $found_mountpoints = 1;
507 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
512 if ($lvmlist->{"$partpath/$part"}) {
516 if ($zfslist->{"$partpath/$part"}) {
520 if ($journalhash->{"$partpath/$part"}) {
521 $journal_count++ if $journalhash->{"$partpath/$part"} == 1;
522 $db_count++ if $journalhash->{"$partpath/$part"} == 2;
523 $wal_count++ if $journalhash->{"$partpath/$part"} == 3;
524 $bluestore = 1 if $journalhash->{"$partpath/$part"} == 4;
527 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
532 $used = 'mounted' if $found_mountpoints && !$used;
533 $used = 'LVM' if $found_lvm && !$used;
534 $used = 'ZFS' if $found_zfs && !$used;
535 $used = 'Device Mapper' if $found_dm && !$used;
536 $used = 'partitions' if $found_partitions && !$used;
538 # multipath, software raid, etc.
539 # this check comes in last, to show more specific info
541 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
543 $disklist->{$dev}->{used
} = $used if $used;
544 $disklist->{$dev}->{osdid
} = $osdid;
545 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
546 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
547 $disklist->{$dev}->{db
} = $db_count if $db_count;
548 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
556 my ($part_path) = @_;
558 my ($mode, $rdev) = (stat($part_path))[2,6];
560 next if !$mode || !S_ISBLK
($mode) || !$rdev;
561 my $major = int($rdev / 0x100);
562 my $minor = $rdev % 0x100;
563 my $partnum_path = "/sys/dev/block/$major:$minor/";
567 $partnum = file_read_firstline
("${partnum_path}partition");
569 die "Partition does not exists\n" if !defined($partnum);
571 #untaint and ensure it is a int
572 if ($partnum =~ m/(\d+)/) {
574 die "Partition number $partnum is invalid\n" if $partnum > 128;
576 die "Failed to get partition number\n";
583 my ($part_path) = @_;
585 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
586 my $link = readlink "/sys/class/block/$dev";
587 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
589 die "Can't parse parent device\n" if !defined($block_dev);
590 die "No valid block device\n" if index($dev, $block_dev) == -1;
592 $block_dev = "/dev/$block_dev";
593 die "Block device does not exsists\n" if !(-b
$block_dev);