]>
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 $LVS = "/sbin/lvs";
17 my $UDEVADM = "/bin/udevadm";
19 sub verify_blockdev_path
{
22 die "missing path" if !$rel_path;
23 my $path = abs_path
($rel_path);
24 die "failed to get absolute path to $rel_path\n" if !$path;
26 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
28 $path = "/dev/$1"; # untaint
30 assert_blockdev
($path);
36 my ($dev, $noerr) = @_;
38 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
39 return undef if $noerr;
40 die "not a valid block device\n";
47 my ($disk, $uuid) = @_;
49 assert_blockdev
($disk);
51 # we should already have checked if it is in use in the api call
52 # but we check again for safety
53 die "disk $disk is already in use\n" if disk_is_used
($disk);
55 my $id = $uuid || 'R';
56 run_command
([$SGDISK, $disk, '-U', $id]);
66 my $disklist = get_disks
($dev, 1);
68 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
69 return 1 if $disklist->{$dev}->{used
};
75 my ($disk, $healthonly) = @_;
77 assert_blockdev
($disk);
83 if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) {
84 my $info = get_sysdir_info
("/sys/block/$1");
85 $disk = "/dev/".($info->{device
}
86 or die "failed to get nvme controller device for $disk\n");
89 my $cmd = [$SMARTCTL, '-H'];
90 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
94 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
97 # ATA SMART attributes, e.g.:
98 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
99 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
101 # SAS and NVME disks, e.g.:
102 # Data Units Written: 5,584,952 [2.85 TB]
103 # Accumulated start-stop cycles: 34
105 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+(.*)$/) {
109 $entry->{name
} = $2 if defined $2;
110 $entry->{flags
} = $3 if defined $3;
111 # the +0 makes a number out of the strings
112 $entry->{value
} = $4+0 if defined $4;
113 $entry->{worst
} = $5+0 if defined $5;
114 # some disks report the default threshold as --- instead of 000
115 if (defined($6) && $6 eq '---') {
116 $entry->{threshold
} = 0;
118 $entry->{threshold
} = $6+0 if defined $6;
120 $entry->{fail
} = $7 if defined $7;
121 $entry->{raw
} = $8 if defined $8;
122 $entry->{id
} = $1 if defined $1;
123 push @{$smartdata->{attributes
}}, $entry;
124 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
125 $smartdata->{health
} = $1;
126 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
128 delete $smartdata->{text
};
129 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
131 } elsif (defined($type) && $type eq 'text') {
132 $smartdata->{text
} = '' if !defined $smartdata->{text
};
133 $smartdata->{text
} .= "$line\n";
134 # extract wearout from nvme text, allow for decimal values
135 if ($line =~ m/Percentage Used:.*(\d+(?:\.\d+)?)\%/i) {
136 $smartdata->{wearout
} = 100 - $1;
138 } elsif ($line =~ m/SMART Disabled/) {
139 $smartdata->{health
} = "SMART Disabled";
145 # bit 0 and 1 mark an severe smartctl error
146 # all others are for disk status, so ignore them
148 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
149 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
152 $smartdata->{type
} = $type;
157 sub get_zfs_devices
{
160 return {} if ! -x
$ZPOOL;
162 # use zpool and parttype uuid,
163 # because log and cache do not have
166 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
169 if ($line =~ m
|^\t([^\t]+)\t|) {
176 # because maybe zfs tools are not installed
179 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
180 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
182 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
184 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
185 $list->{$real_dev} = 1;
191 sub get_lvm_devices
{
194 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
197 if ($line =~ m
|^/dev/|) {
203 # if something goes wrong, we do not want
204 # to give up, but indicate an error has occured
207 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
209 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
211 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
212 $list->{$real_dev} = 1;
218 sub get_ceph_journals
{
219 my $journalhash = {};
221 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
222 my $db_uuid = '30cd0809-c2b2-499c-8879-2d6b78529876';
223 my $wal_uuid = '5ce17fce-4087-4169-b7ff-056cc58473f9';
224 my $block_uuid = 'cafecafe-9b03-4f30-b4c6-b4b80ceff106';
226 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
227 my ($entry, $type) = @_;
228 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
229 if ($type eq $journal_uuid) {
230 $journalhash->{$real_dev} = 1;
231 } elsif ($type eq $db_uuid) {
232 $journalhash->{$real_dev} = 2;
233 } elsif ($type eq $wal_uuid) {
234 $journalhash->{$real_dev} = 3;
235 } elsif ($type eq $block_uuid) {
236 $journalhash->{$real_dev} = 4;
243 # reads the lv_tags and matches them with the devices
244 sub get_ceph_volume_infos
{
247 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
248 '--noheadings', '--readonly', '--separator', ';' ];
250 run_command
($cmd, outfunc
=> sub {
252 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
254 my $fields = [ split(';', $line) ];
256 # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need)
257 my ($dev) = $fields->[0] =~ m
|^(/dev/[a-z
]+)|;
258 if ($fields->[1] =~ m
|^osd-
([^-]+)-|) {
260 # $result autovivification is wanted, to not creating empty hashes
261 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) {
262 $result->{$dev}->{osdid
} = $1;
263 $result->{$dev}->{bluestore
} = ($type eq 'block');
265 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
266 $result->{$dev}->{$type}++;
280 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
286 return undef if !$info;
288 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
289 return undef if $info =~ m/^E: ID_CDROM/m;
291 # we use this, because some disks are not simply in /dev
292 # e.g. /dev/cciss/c0d0
293 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
294 $data->{devpath
} = $1;
296 return if !defined($data->{devpath
});
298 $data->{serial
} = 'unknown';
299 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
300 $data->{serial
} = $1;
304 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
310 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
314 if ($info =~ m/^E: ID_BUS=usb$/m) {
318 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
322 $data->{wwn
} = 'unknown';
323 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
330 sub get_sysdir_info
{
333 return undef if ! -d
"$sysdir/device";
337 my $size = file_read_firstline
("$sysdir/size");
338 return undef if !$size;
340 # linux always considers sectors to be 512 bytes,
341 # independently of real block size
342 $data->{size
} = $size * 512;
344 # dir/queue/rotational should be 1 for hdd, 0 for ssd
345 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
347 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
348 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
350 if (defined(my $device = readlink("$sysdir/device"))) {
351 # strip directory and untaint:
352 ($data->{device
}) = $device =~ m!([^/]+)$!;
358 sub get_wear_leveling_info
{
359 my ($smartdata, $model) = @_;
360 my $attributes = $smartdata->{attributes
};
362 if (defined($smartdata->{wearout
})) {
363 return $smartdata->{wearout
};
377 # find target attr id
381 foreach my $vendor (keys %$vendormap) {
382 if ($model =~ m/$vendor/i) {
383 $attrid = $vendormap->{$vendor};
384 # found the attribute
390 $attrid = $vendormap->{default};
393 foreach my $attr (@$attributes) {
394 next if $attr->{id
} != $attrid;
395 $wearout = $attr->{value
};
405 my $dh = IO
::Dir-
>new ($dir);
408 while (defined(my $tmp = $dh->read)) {
409 next if $tmp eq '.' || $tmp eq '..';
420 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
428 my ($disks, $nosmart) = @_;
433 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
435 foreach my $mount (@$mounts) {
436 next if $mount->[0] !~ m
|^/dev/|;
437 $mounted->{abs_path
($mount->[0])} = $mount->[1];
440 my $dev_is_mounted = sub {
442 return $mounted->{$dev};
445 my $journalhash = get_ceph_journals
();
446 my $ceph_volume_infos = get_ceph_volume_infos
();
448 my $zfslist = get_zfs_devices
();
450 my $lvmlist = get_lvm_devices
();
452 my $disk_regex = ".*";
453 if (defined($disks)) {
456 } elsif (ref($disks) ne 'ARRAY') {
457 die "disks is not a string or array reference\n";
459 # we get cciss/c0d0 but need cciss!c0d0
460 map { s
|cciss
/|cciss
!| } @$disks;
462 $disk_regex = "(?:" . join('|', @$disks) . ")";
465 dir_glob_foreach
('/sys/block', $disk_regex, sub {
467 # whitelisting following devices
468 # hdX: ide block device
469 # sdX: sd block device
470 # vdX: virtual block device
471 # xvdX: xen virtual block device
472 # nvmeXnY: nvme devices
473 # cciss!cXnY: cciss devices
474 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
475 $dev !~ m/^nvme\d+n\d+$/ &&
476 $dev !~ m/^cciss\!c\d+d\d+$/;
478 my $data = get_udev_info
("/sys/block/$dev");
479 return if !defined($data);
480 my $devpath = $data->{devpath
};
482 my $sysdir = "/sys/block/$dev";
484 # we do not want iscsi devices
485 return if is_iscsi
($sysdir);
487 my $sysdata = get_sysdir_info
($sysdir);
488 return if !defined($sysdata);
490 my $type = 'unknown';
492 if ($sysdata->{rotational
} == 0) {
495 } elsif ($sysdata->{rotational
} == 1) {
496 if ($data->{rpm
} != -1) {
498 } elsif ($data->{usb
}) {
504 my $health = 'UNKNOWN';
509 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
510 $health = $smartdata->{health
} if $smartdata->{health
};
512 if ($type eq 'ssd') {
513 # if we have an ssd we try to get the wearout indicator
514 my $wearval = get_wear_leveling_info
($smartdata, $data->{model
} || $sysdata->{model
});
515 $wearout = $wearval if defined($wearval);
522 $used = 'LVM' if $lvmlist->{$devpath};
524 $used = 'mounted' if &$dev_is_mounted($devpath);
526 $used = 'ZFS' if $zfslist->{$devpath};
528 # we replaced cciss/ with cciss! above
529 # but in the result we need cciss/ again
530 # because the caller might want to check the
531 # result again with the original parameter
532 if ($dev =~ m
|^cciss
!|) {
533 $dev =~ s
|^cciss
!|cciss
/|;
536 $disklist->{$dev} = {
537 vendor
=> $sysdata->{vendor
},
538 model
=> $data->{model
} || $sysdata->{model
},
539 size
=> $sysdata->{size
},
540 serial
=> $data->{serial
},
553 my $journal_count = 0;
557 my $found_partitions;
559 my $found_mountpoints;
562 my $partpath = $devpath;
564 # remove part after last / to
565 # get the base path for the partitions
566 # e.g. from /dev/cciss/c0d0 get /dev/cciss
567 $partpath =~ s/\/[^\/]+$//;
569 dir_glob_foreach
("$sysdir", "$dev.+", sub {
572 $found_partitions = 1;
574 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
575 $found_mountpoints = 1;
576 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
581 if ($lvmlist->{"$partpath/$part"}) {
585 if ($zfslist->{"$partpath/$part"}) {
589 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
590 $journal_count++ if $journal_part == 1;
591 $db_count++ if $journal_part == 2;
592 $wal_count++ if $journal_part == 3;
593 $bluestore = 1 if $journal_part == 4;
596 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
601 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
602 $journal_count += $ceph_volume->{journal
} // 0;
603 $db_count += $ceph_volume->{db
} // 0;
604 $wal_count += $ceph_volume->{wal
} // 0;
605 if (defined($ceph_volume->{osdid
})) {
606 $osdid = $ceph_volume->{osdid
};
607 $bluestore = 1 if $ceph_volume->{bluestore
};
611 $used = 'mounted' if $found_mountpoints && !$used;
612 $used = 'LVM' if $found_lvm && !$used;
613 $used = 'ZFS' if $found_zfs && !$used;
614 $used = 'Device Mapper' if $found_dm && !$used;
615 $used = 'partitions' if $found_partitions && !$used;
617 # multipath, software raid, etc.
618 # this check comes in last, to show more specific info
620 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
622 $disklist->{$dev}->{used
} = $used if $used;
623 $disklist->{$dev}->{osdid
} = $osdid;
624 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
625 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
626 $disklist->{$dev}->{db
} = $db_count if $db_count;
627 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
635 my ($part_path) = @_;
637 my ($mode, $rdev) = (stat($part_path))[2,6];
639 next if !$mode || !S_ISBLK
($mode) || !$rdev;
640 my $major = PVE
::Tools
::dev_t_major
($rdev);
641 my $minor = PVE
::Tools
::dev_t_minor
($rdev);
642 my $partnum_path = "/sys/dev/block/$major:$minor/";
646 $partnum = file_read_firstline
("${partnum_path}partition");
648 die "Partition does not exists\n" if !defined($partnum);
650 #untaint and ensure it is a int
651 if ($partnum =~ m/(\d+)/) {
653 die "Partition number $partnum is invalid\n" if $partnum > 128;
655 die "Failed to get partition number\n";
662 my ($part_path) = @_;
664 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
665 my $link = readlink "/sys/class/block/$dev";
666 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
668 die "Can't parse parent device\n" if !defined($block_dev);
669 die "No valid block device\n" if index($dev, $block_dev) == -1;
671 $block_dev = "/dev/$block_dev";
672 die "Block device does not exsists\n" if !(-b
$block_dev);
677 sub locked_disk_action
{
679 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
684 sub assert_disk_unused
{
687 die "device '$dev' is already in use\n" if disk_is_used
($dev);
692 sub append_partition
{
693 my ($dev, $size) = @_;
696 $devname =~ s
|^/dev/||;
699 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*?
(\d
+)/, sub {
700 my ($part, $partid) = @_;
702 if ($partid >= $newpartid) {
703 $newpartid = $partid + 1;
707 $size = PVE
::Tools
::convert_size
($size, 'b' => 'mb');
709 run_command
([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
710 errmsg
=> "error creating partition '$newpartid' on '$dev'");
714 # loop again to detect the real partiton device which does not always follow
715 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
716 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*$newpartid/, sub {
719 $partition = "/dev/$part";