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 } elsif ($line =~ m/SMART Disabled/) {
134 $smartdata->{health
} = "SMART Disabled";
140 # bit 0 and 1 mark an severe smartctl error
141 # all others are for disk status, so ignore them
143 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
144 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
147 $smartdata->{type
} = $type;
152 sub get_zfs_devices
{
155 return {} if !(-x
$ZPOOL);
157 # use zpool and parttype uuid,
158 # because log and cache do not have
161 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
164 if ($line =~ m
|^\t([^\t]+)\t|) {
171 # because maybe zfs tools are not installed
174 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
175 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
177 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
179 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
180 $list->{$real_dev} = 1;
186 sub get_lvm_devices
{
189 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
192 if ($line =~ m
|^/dev/|) {
198 # if something goes wrong, we do not want
199 # to give up, but indicate an error has occured
202 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
204 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
206 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
207 $list->{$real_dev} = 1;
213 sub get_ceph_journals
{
214 my $journalhash = {};
216 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
217 my $db_uuid = '30cd0809-c2b2-499c-8879-2d6b78529876';
218 my $wal_uuid = '5ce17fce-4087-4169-b7ff-056cc58473f9';
219 my $block_uuid = 'cafecafe-9b03-4f30-b4c6-b4b80ceff106';
221 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
222 my ($entry, $type) = @_;
223 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
224 if ($type eq $journal_uuid) {
225 $journalhash->{$real_dev} = 1;
226 } elsif ($type eq $db_uuid) {
227 $journalhash->{$real_dev} = 2;
228 } elsif ($type eq $wal_uuid) {
229 $journalhash->{$real_dev} = 3;
230 } elsif ($type eq $block_uuid) {
231 $journalhash->{$real_dev} = 4;
244 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
250 return undef if !$info;
252 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
253 return undef if $info =~ m/^E: ID_CDROM/m;
255 # we use this, because some disks are not simply in /dev
256 # e.g. /dev/cciss/c0d0
257 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
258 $data->{devpath
} = $1;
260 return if !defined($data->{devpath
});
262 $data->{serial
} = 'unknown';
263 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
264 $data->{serial
} = $1;
268 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
274 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
278 if ($info =~ m/^E: ID_BUS=usb$/m) {
282 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
286 $data->{wwn
} = 'unknown';
287 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
294 sub get_sysdir_info
{
297 return undef if ! -d
"$sysdir/device";
301 my $size = file_read_firstline
("$sysdir/size");
302 return undef if !$size;
304 # linux always considers sectors to be 512 bytes,
305 # independently of real block size
306 $data->{size
} = $size * 512;
308 # dir/queue/rotational should be 1 for hdd, 0 for ssd
309 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
311 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
312 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
314 if (defined(my $device = readlink("$sysdir/device"))) {
315 # strip directory and untaint:
316 ($data->{device
}) = $device =~ m!([^/]+)$!;
322 sub get_wear_leveling_info
{
323 my ($attributes, $model) = @_;
336 # find target attr id
340 foreach my $vendor (keys %$vendormap) {
341 if ($model =~ m/$vendor/i) {
342 $attrid = $vendormap->{$vendor};
343 # found the attribute
349 $attrid = $vendormap->{default};
352 foreach my $attr (@$attributes) {
353 next if $attr->{id
} != $attrid;
354 $wearout = $attr->{value
};
364 my $dh = IO
::Dir-
>new ($dir);
367 while (defined(my $tmp = $dh->read)) {
368 next if $tmp eq '.' || $tmp eq '..';
379 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
387 my ($disk, $nosmart) = @_;
392 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
394 foreach my $mount (@$mounts) {
395 next if $mount->[0] !~ m
|^/dev/|;
396 $mounted->{abs_path
($mount->[0])} = $mount->[1];
399 my $dev_is_mounted = sub {
401 return $mounted->{$dev};
404 my $journalhash = get_ceph_journals
();
406 my $zfslist = get_zfs_devices
();
408 my $lvmlist = get_lvm_devices
();
410 # we get cciss/c0d0 but need cciss!c0d0
411 if (defined($disk) && $disk =~ m
|^cciss
/|) {
412 $disk =~ s
|cciss
/|cciss
!|;
415 dir_glob_foreach
('/sys/block', '.*', sub {
417 return if defined($disk) && $disk ne $dev;
418 # whitelisting following devices
419 # hdX: ide block device
420 # sdX: sd block device
421 # vdX: virtual block device
422 # xvdX: xen virtual block device
423 # nvmeXnY: nvme devices
424 # cciss!cXnY: cciss devices
425 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
426 $dev !~ m/^nvme\d+n\d+$/ &&
427 $dev !~ m/^cciss\!c\d+d\d+$/;
429 my $data = get_udev_info
("/sys/block/$dev");
430 return if !defined($data);
431 my $devpath = $data->{devpath
};
433 my $sysdir = "/sys/block/$dev";
435 # we do not want iscsi devices
436 return if is_iscsi
($sysdir);
438 my $sysdata = get_sysdir_info
($sysdir);
439 return if !defined($sysdata);
441 my $type = 'unknown';
443 if ($sysdata->{rotational
} == 0) {
446 } elsif ($sysdata->{rotational
} == 1) {
447 if ($data->{rpm
} != -1) {
449 } elsif ($data->{usb
}) {
455 my $health = 'UNKNOWN';
460 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
461 $health = $smartdata->{health
} if $smartdata->{health
};
463 if ($type eq 'ssd') {
464 # if we have an ssd we try to get the wearout indicator
465 my $wearval = get_wear_leveling_info
($smartdata->{attributes
}, $data->{model
} || $sysdir->{model
});
466 $wearout = $wearval if $wearval;
473 $used = 'LVM' if $lvmlist->{$devpath};
475 $used = 'mounted' if &$dev_is_mounted($devpath);
477 $used = 'ZFS' if $zfslist->{$devpath};
479 # we replaced cciss/ with cciss! above
480 # but in the result we need cciss/ again
481 # because the caller might want to check the
482 # result again with the original parameter
483 if ($dev =~ m
|^cciss
!|) {
484 $dev =~ s
|^cciss
!|cciss
/|;
487 $disklist->{$dev} = {
488 vendor
=> $sysdata->{vendor
},
489 model
=> $data->{model
} || $sysdata->{model
},
490 size
=> $sysdata->{size
},
491 serial
=> $data->{serial
},
504 my $journal_count = 0;
508 my $found_partitions;
510 my $found_mountpoints;
513 my $partpath = $devpath;
515 # remove part after last / to
516 # get the base path for the partitions
517 # e.g. from /dev/cciss/c0d0 get /dev/cciss
518 $partpath =~ s/\/[^\/]+$//;
520 dir_glob_foreach
("$sysdir", "$dev.+", sub {
523 $found_partitions = 1;
525 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
526 $found_mountpoints = 1;
527 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
532 if ($lvmlist->{"$partpath/$part"}) {
536 if ($zfslist->{"$partpath/$part"}) {
540 if ($journalhash->{"$partpath/$part"}) {
541 $journal_count++ if $journalhash->{"$partpath/$part"} == 1;
542 $db_count++ if $journalhash->{"$partpath/$part"} == 2;
543 $wal_count++ if $journalhash->{"$partpath/$part"} == 3;
544 $bluestore = 1 if $journalhash->{"$partpath/$part"} == 4;
547 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
552 $used = 'mounted' if $found_mountpoints && !$used;
553 $used = 'LVM' if $found_lvm && !$used;
554 $used = 'ZFS' if $found_zfs && !$used;
555 $used = 'Device Mapper' if $found_dm && !$used;
556 $used = 'partitions' if $found_partitions && !$used;
558 # multipath, software raid, etc.
559 # this check comes in last, to show more specific info
561 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
563 $disklist->{$dev}->{used
} = $used if $used;
564 $disklist->{$dev}->{osdid
} = $osdid;
565 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
566 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
567 $disklist->{$dev}->{db
} = $db_count if $db_count;
568 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
576 my ($part_path) = @_;
578 my ($mode, $rdev) = (stat($part_path))[2,6];
580 next if !$mode || !S_ISBLK
($mode) || !$rdev;
581 my $major = PVE
::Tools
::dev_t_major
($rdev);
582 my $minor = PVE
::Tools
::dev_t_minor
($rdev);
583 my $partnum_path = "/sys/dev/block/$major:$minor/";
587 $partnum = file_read_firstline
("${partnum_path}partition");
589 die "Partition does not exists\n" if !defined($partnum);
591 #untaint and ensure it is a int
592 if ($partnum =~ m/(\d+)/) {
594 die "Partition number $partnum is invalid\n" if $partnum > 128;
596 die "Failed to get partition number\n";
603 my ($part_path) = @_;
605 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
606 my $link = readlink "/sys/class/block/$dev";
607 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
609 die "Can't parse parent device\n" if !defined($block_dev);
610 die "No valid block device\n" if index($dev, $block_dev) == -1;
612 $block_dev = "/dev/$block_dev";
613 die "Block device does not exsists\n" if !(-b
$block_dev);
618 sub locked_disk_action
{
620 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
625 sub assert_disk_unused
{
628 die "device '$dev' is already in use\n" if disk_is_used
($dev);