]>
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);
82 if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) {
83 my $info = get_sysdir_info
("/sys/block/$1");
84 $disk = "/dev/".($info->{device
}
85 or die "failed to get nvme controller device for $disk\n");
88 my $cmd = [$SMARTCTL, '-H'];
89 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
93 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
96 # ATA SMART attributes, e.g.:
97 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
98 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
100 # SAS and NVME disks, e.g.:
101 # Data Units Written: 5,584,952 [2.85 TB]
102 # Accumulated start-stop cycles: 34
104 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+(.*)$/) {
108 $entry->{name
} = $2 if defined $2;
109 $entry->{flags
} = $3 if defined $3;
110 # the +0 makes a number out of the strings
111 $entry->{value
} = $4+0 if defined $4;
112 $entry->{worst
} = $5+0 if defined $5;
113 # some disks report the default threshold as --- instead of 000
114 if (defined($6) && $6 eq '---') {
115 $entry->{threshold
} = 0;
117 $entry->{threshold
} = $6+0 if defined $6;
119 $entry->{fail
} = $7 if defined $7;
120 $entry->{raw
} = $8 if defined $8;
121 $entry->{id
} = $1 if defined $1;
122 push @{$smartdata->{attributes
}}, $entry;
123 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
124 $smartdata->{health
} = $1;
125 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
127 delete $smartdata->{text
};
128 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
130 } elsif (defined($type) && $type eq 'text') {
131 $smartdata->{text
} = '' if !defined $smartdata->{text
};
132 $smartdata->{text
} .= "$line\n";
133 # extract wearout from nvme text, allow for decimal values
134 if ($line =~ m/Percentage Used:.*(\d+(?:\.\d+)?)\%/i) {
135 $smartdata->{wearout
} = 100 - $1;
137 } elsif ($line =~ m/SMART Disabled/) {
138 $smartdata->{health
} = "SMART Disabled";
144 # bit 0 and 1 mark an severe smartctl error
145 # all others are for disk status, so ignore them
147 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
148 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
151 $smartdata->{type
} = $type;
156 sub get_zfs_devices
{
159 return {} if ! -x
$ZPOOL;
161 # use zpool and parttype uuid,
162 # because log and cache do not have
165 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
168 if ($line =~ m
|^\t([^\t]+)\t|) {
175 # because maybe zfs tools are not installed
178 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
179 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
181 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
183 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
184 $list->{$real_dev} = 1;
190 sub get_lvm_devices
{
193 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
196 if ($line =~ m
|^/dev/|) {
202 # if something goes wrong, we do not want
203 # to give up, but indicate an error has occured
206 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
208 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
210 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
211 $list->{$real_dev} = 1;
217 sub get_ceph_journals
{
218 my $journalhash = {};
220 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
221 my $db_uuid = '30cd0809-c2b2-499c-8879-2d6b78529876';
222 my $wal_uuid = '5ce17fce-4087-4169-b7ff-056cc58473f9';
223 my $block_uuid = 'cafecafe-9b03-4f30-b4c6-b4b80ceff106';
225 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
226 my ($entry, $type) = @_;
227 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
228 if ($type eq $journal_uuid) {
229 $journalhash->{$real_dev} = 1;
230 } elsif ($type eq $db_uuid) {
231 $journalhash->{$real_dev} = 2;
232 } elsif ($type eq $wal_uuid) {
233 $journalhash->{$real_dev} = 3;
234 } elsif ($type eq $block_uuid) {
235 $journalhash->{$real_dev} = 4;
248 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
254 return undef if !$info;
256 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
257 return undef if $info =~ m/^E: ID_CDROM/m;
259 # we use this, because some disks are not simply in /dev
260 # e.g. /dev/cciss/c0d0
261 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
262 $data->{devpath
} = $1;
264 return if !defined($data->{devpath
});
266 $data->{serial
} = 'unknown';
267 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
268 $data->{serial
} = $1;
272 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
278 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
282 if ($info =~ m/^E: ID_BUS=usb$/m) {
286 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
290 $data->{wwn
} = 'unknown';
291 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
298 sub get_sysdir_info
{
301 return undef if ! -d
"$sysdir/device";
305 my $size = file_read_firstline
("$sysdir/size");
306 return undef if !$size;
308 # linux always considers sectors to be 512 bytes,
309 # independently of real block size
310 $data->{size
} = $size * 512;
312 # dir/queue/rotational should be 1 for hdd, 0 for ssd
313 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
315 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
316 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
318 if (defined(my $device = readlink("$sysdir/device"))) {
319 # strip directory and untaint:
320 ($data->{device
}) = $device =~ m!([^/]+)$!;
326 sub get_wear_leveling_info
{
327 my ($smartdata, $model) = @_;
328 my $attributes = $smartdata->{attributes
};
330 if (defined($smartdata->{wearout
})) {
331 return $smartdata->{wearout
};
345 # find target attr id
349 foreach my $vendor (keys %$vendormap) {
350 if ($model =~ m/$vendor/i) {
351 $attrid = $vendormap->{$vendor};
352 # found the attribute
358 $attrid = $vendormap->{default};
361 foreach my $attr (@$attributes) {
362 next if $attr->{id
} != $attrid;
363 $wearout = $attr->{value
};
373 my $dh = IO
::Dir-
>new ($dir);
376 while (defined(my $tmp = $dh->read)) {
377 next if $tmp eq '.' || $tmp eq '..';
388 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
396 my ($disk, $nosmart) = @_;
401 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
403 foreach my $mount (@$mounts) {
404 next if $mount->[0] !~ m
|^/dev/|;
405 $mounted->{abs_path
($mount->[0])} = $mount->[1];
408 my $dev_is_mounted = sub {
410 return $mounted->{$dev};
413 my $journalhash = get_ceph_journals
();
415 my $zfslist = get_zfs_devices
();
417 my $lvmlist = get_lvm_devices
();
419 # we get cciss/c0d0 but need cciss!c0d0
420 if (defined($disk) && $disk =~ m
|^cciss
/|) {
421 $disk =~ s
|cciss
/|cciss
!|;
424 dir_glob_foreach
('/sys/block', '.*', sub {
426 return if defined($disk) && $disk ne $dev;
427 # whitelisting following devices
428 # hdX: ide block device
429 # sdX: sd block device
430 # vdX: virtual block device
431 # xvdX: xen virtual block device
432 # nvmeXnY: nvme devices
433 # cciss!cXnY: cciss devices
434 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
435 $dev !~ m/^nvme\d+n\d+$/ &&
436 $dev !~ m/^cciss\!c\d+d\d+$/;
438 my $data = get_udev_info
("/sys/block/$dev");
439 return if !defined($data);
440 my $devpath = $data->{devpath
};
442 my $sysdir = "/sys/block/$dev";
444 # we do not want iscsi devices
445 return if is_iscsi
($sysdir);
447 my $sysdata = get_sysdir_info
($sysdir);
448 return if !defined($sysdata);
450 my $type = 'unknown';
452 if ($sysdata->{rotational
} == 0) {
455 } elsif ($sysdata->{rotational
} == 1) {
456 if ($data->{rpm
} != -1) {
458 } elsif ($data->{usb
}) {
464 my $health = 'UNKNOWN';
469 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
470 $health = $smartdata->{health
} if $smartdata->{health
};
472 if ($type eq 'ssd') {
473 # if we have an ssd we try to get the wearout indicator
474 my $wearval = get_wear_leveling_info
($smartdata, $data->{model
} || $sysdata->{model
});
475 $wearout = $wearval if defined($wearval);
482 $used = 'LVM' if $lvmlist->{$devpath};
484 $used = 'mounted' if &$dev_is_mounted($devpath);
486 $used = 'ZFS' if $zfslist->{$devpath};
488 # we replaced cciss/ with cciss! above
489 # but in the result we need cciss/ again
490 # because the caller might want to check the
491 # result again with the original parameter
492 if ($dev =~ m
|^cciss
!|) {
493 $dev =~ s
|^cciss
!|cciss
/|;
496 $disklist->{$dev} = {
497 vendor
=> $sysdata->{vendor
},
498 model
=> $data->{model
} || $sysdata->{model
},
499 size
=> $sysdata->{size
},
500 serial
=> $data->{serial
},
513 my $journal_count = 0;
517 my $found_partitions;
519 my $found_mountpoints;
522 my $partpath = $devpath;
524 # remove part after last / to
525 # get the base path for the partitions
526 # e.g. from /dev/cciss/c0d0 get /dev/cciss
527 $partpath =~ s/\/[^\/]+$//;
529 dir_glob_foreach
("$sysdir", "$dev.+", sub {
532 $found_partitions = 1;
534 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
535 $found_mountpoints = 1;
536 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
541 if ($lvmlist->{"$partpath/$part"}) {
545 if ($zfslist->{"$partpath/$part"}) {
549 if ($journalhash->{"$partpath/$part"}) {
550 $journal_count++ if $journalhash->{"$partpath/$part"} == 1;
551 $db_count++ if $journalhash->{"$partpath/$part"} == 2;
552 $wal_count++ if $journalhash->{"$partpath/$part"} == 3;
553 $bluestore = 1 if $journalhash->{"$partpath/$part"} == 4;
556 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
561 $used = 'mounted' if $found_mountpoints && !$used;
562 $used = 'LVM' if $found_lvm && !$used;
563 $used = 'ZFS' if $found_zfs && !$used;
564 $used = 'Device Mapper' if $found_dm && !$used;
565 $used = 'partitions' if $found_partitions && !$used;
567 # multipath, software raid, etc.
568 # this check comes in last, to show more specific info
570 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
572 $disklist->{$dev}->{used
} = $used if $used;
573 $disklist->{$dev}->{osdid
} = $osdid;
574 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
575 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
576 $disklist->{$dev}->{db
} = $db_count if $db_count;
577 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
585 my ($part_path) = @_;
587 my ($mode, $rdev) = (stat($part_path))[2,6];
589 next if !$mode || !S_ISBLK
($mode) || !$rdev;
590 my $major = PVE
::Tools
::dev_t_major
($rdev);
591 my $minor = PVE
::Tools
::dev_t_minor
($rdev);
592 my $partnum_path = "/sys/dev/block/$major:$minor/";
596 $partnum = file_read_firstline
("${partnum_path}partition");
598 die "Partition does not exists\n" if !defined($partnum);
600 #untaint and ensure it is a int
601 if ($partnum =~ m/(\d+)/) {
603 die "Partition number $partnum is invalid\n" if $partnum > 128;
605 die "Failed to get partition number\n";
612 my ($part_path) = @_;
614 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
615 my $link = readlink "/sys/class/block/$dev";
616 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
618 die "Can't parse parent device\n" if !defined($block_dev);
619 die "No valid block device\n" if index($dev, $block_dev) == -1;
621 $block_dev = "/dev/$block_dev";
622 die "Block device does not exsists\n" if !(-b
$block_dev);
627 sub locked_disk_action
{
629 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
634 sub assert_disk_unused
{
637 die "device '$dev' is already in use\n" if disk_is_used
($dev);