]>
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 } elsif ($line =~ m/SMART Disabled/) {
135 $smartdata->{health
} = "SMART Disabled";
141 # bit 0 and 1 mark an severe smartctl error
142 # all others are for disk status, so ignore them
144 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
145 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
148 $smartdata->{type
} = $type;
153 sub get_zfs_devices
{
156 return {} if ! -x
$ZPOOL;
158 # use zpool and parttype uuid,
159 # because log and cache do not have
162 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
165 if ($line =~ m
|^\t([^\t]+)\t|) {
172 # because maybe zfs tools are not installed
175 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
176 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
178 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
180 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
181 $list->{$real_dev} = 1;
187 sub get_lvm_devices
{
190 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
193 if ($line =~ m
|^/dev/|) {
199 # if something goes wrong, we do not want
200 # to give up, but indicate an error has occured
203 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
205 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
207 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
208 $list->{$real_dev} = 1;
214 sub get_ceph_journals
{
215 my $journalhash = {};
217 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
218 my $db_uuid = '30cd0809-c2b2-499c-8879-2d6b78529876';
219 my $wal_uuid = '5ce17fce-4087-4169-b7ff-056cc58473f9';
220 my $block_uuid = 'cafecafe-9b03-4f30-b4c6-b4b80ceff106';
222 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
223 my ($entry, $type) = @_;
224 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
225 if ($type eq $journal_uuid) {
226 $journalhash->{$real_dev} = 1;
227 } elsif ($type eq $db_uuid) {
228 $journalhash->{$real_dev} = 2;
229 } elsif ($type eq $wal_uuid) {
230 $journalhash->{$real_dev} = 3;
231 } elsif ($type eq $block_uuid) {
232 $journalhash->{$real_dev} = 4;
239 # reads the lv_tags and matches them with the devices
240 sub get_ceph_volume_infos
{
243 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
244 '--noheadings', '--readonly', '--separator', ';' ];
246 run_command
($cmd, outfunc
=> sub {
248 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
250 my $fields = [ split(';', $line) ];
252 # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need)
253 my ($dev) = $fields->[0] =~ m
|^(/dev/[a-z
]+)|;
254 if ($fields->[1] =~ m
|^osd-
([^-]+)-|) {
256 # $result autovivification is wanted, to not creating empty hashes
257 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,])/) {
258 $result->{$dev}->{osdid
} = $1;
259 $result->{$dev}->{bluestore
} = ($type eq 'block');
261 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
262 $result->{$dev}->{$type}++;
276 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
282 return undef if !$info;
284 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
285 return undef if $info =~ m/^E: ID_CDROM/m;
287 # we use this, because some disks are not simply in /dev
288 # e.g. /dev/cciss/c0d0
289 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
290 $data->{devpath
} = $1;
292 return if !defined($data->{devpath
});
294 $data->{serial
} = 'unknown';
295 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
296 $data->{serial
} = $1;
300 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
306 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
310 if ($info =~ m/^E: ID_BUS=usb$/m) {
314 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
318 $data->{wwn
} = 'unknown';
319 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
326 sub get_sysdir_info
{
329 return undef if ! -d
"$sysdir/device";
333 my $size = file_read_firstline
("$sysdir/size");
334 return undef if !$size;
336 # linux always considers sectors to be 512 bytes,
337 # independently of real block size
338 $data->{size
} = $size * 512;
340 # dir/queue/rotational should be 1 for hdd, 0 for ssd
341 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
343 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
344 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
346 if (defined(my $device = readlink("$sysdir/device"))) {
347 # strip directory and untaint:
348 ($data->{device
}) = $device =~ m!([^/]+)$!;
354 sub get_wear_leveling_info
{
355 my ($attributes, $model) = @_;
368 # find target attr id
372 foreach my $vendor (keys %$vendormap) {
373 if ($model =~ m/$vendor/i) {
374 $attrid = $vendormap->{$vendor};
375 # found the attribute
381 $attrid = $vendormap->{default};
384 foreach my $attr (@$attributes) {
385 next if $attr->{id
} != $attrid;
386 $wearout = $attr->{value
};
396 my $dh = IO
::Dir-
>new ($dir);
399 while (defined(my $tmp = $dh->read)) {
400 next if $tmp eq '.' || $tmp eq '..';
411 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
419 my ($disks, $nosmart) = @_;
424 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
426 foreach my $mount (@$mounts) {
427 next if $mount->[0] !~ m
|^/dev/|;
428 $mounted->{abs_path
($mount->[0])} = $mount->[1];
431 my $dev_is_mounted = sub {
433 return $mounted->{$dev};
436 my $journalhash = get_ceph_journals
();
437 my $ceph_volume_infos = get_ceph_volume_infos
();
439 my $zfslist = get_zfs_devices
();
441 my $lvmlist = get_lvm_devices
();
443 my $disk_regex = ".*";
444 if (defined($disks)) {
447 } elsif (ref($disks) ne 'ARRAY') {
448 die "disks is not a string or array reference\n";
450 # we get cciss/c0d0 but need cciss!c0d0
451 map { s
|cciss
/|cciss
!| } @$disks;
453 $disk_regex = "(?:" . join('|', @$disks) . ")";
456 dir_glob_foreach
('/sys/block', $disk_regex, sub {
458 # whitelisting following devices
459 # hdX: ide block device
460 # sdX: sd block device
461 # vdX: virtual block device
462 # xvdX: xen virtual block device
463 # nvmeXnY: nvme devices
464 # cciss!cXnY: cciss devices
465 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
466 $dev !~ m/^nvme\d+n\d+$/ &&
467 $dev !~ m/^cciss\!c\d+d\d+$/;
469 my $data = get_udev_info
("/sys/block/$dev");
470 return if !defined($data);
471 my $devpath = $data->{devpath
};
473 my $sysdir = "/sys/block/$dev";
475 # we do not want iscsi devices
476 return if is_iscsi
($sysdir);
478 my $sysdata = get_sysdir_info
($sysdir);
479 return if !defined($sysdata);
481 my $type = 'unknown';
483 if ($sysdata->{rotational
} == 0) {
486 } elsif ($sysdata->{rotational
} == 1) {
487 if ($data->{rpm
} != -1) {
489 } elsif ($data->{usb
}) {
495 my $health = 'UNKNOWN';
500 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
501 $health = $smartdata->{health
} if $smartdata->{health
};
503 if ($type eq 'ssd') {
504 # if we have an ssd we try to get the wearout indicator
505 my $wearval = get_wear_leveling_info
($smartdata->{attributes
}, $data->{model
} || $sysdir->{model
});
506 $wearout = $wearval if $wearval;
513 $used = 'LVM' if $lvmlist->{$devpath};
515 $used = 'mounted' if &$dev_is_mounted($devpath);
517 $used = 'ZFS' if $zfslist->{$devpath};
519 # we replaced cciss/ with cciss! above
520 # but in the result we need cciss/ again
521 # because the caller might want to check the
522 # result again with the original parameter
523 if ($dev =~ m
|^cciss
!|) {
524 $dev =~ s
|^cciss
!|cciss
/|;
527 $disklist->{$dev} = {
528 vendor
=> $sysdata->{vendor
},
529 model
=> $data->{model
} || $sysdata->{model
},
530 size
=> $sysdata->{size
},
531 serial
=> $data->{serial
},
544 my $journal_count = 0;
548 my $found_partitions;
550 my $found_mountpoints;
553 my $partpath = $devpath;
555 # remove part after last / to
556 # get the base path for the partitions
557 # e.g. from /dev/cciss/c0d0 get /dev/cciss
558 $partpath =~ s/\/[^\/]+$//;
560 dir_glob_foreach
("$sysdir", "$dev.+", sub {
563 $found_partitions = 1;
565 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
566 $found_mountpoints = 1;
567 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
572 if ($lvmlist->{"$partpath/$part"}) {
576 if ($zfslist->{"$partpath/$part"}) {
580 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
581 $journal_count++ if $journal_part == 1;
582 $db_count++ if $journal_part == 2;
583 $wal_count++ if $journal_part == 3;
584 $bluestore = 1 if $journal_part == 4;
587 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
592 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
593 $journal_count += $ceph_volume->{journal
} // 0;
594 $db_count += $ceph_volume->{db
} // 0;
595 $wal_count += $ceph_volume->{wal
} // 0;
596 if ($ceph_volume->{osdid
}) {
597 $osdid = $ceph_volume->{osdid
};
598 $bluestore = 1 if $ceph_volume->{bluestore
};
602 $used = 'mounted' if $found_mountpoints && !$used;
603 $used = 'LVM' if $found_lvm && !$used;
604 $used = 'ZFS' if $found_zfs && !$used;
605 $used = 'Device Mapper' if $found_dm && !$used;
606 $used = 'partitions' if $found_partitions && !$used;
608 # multipath, software raid, etc.
609 # this check comes in last, to show more specific info
611 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
613 $disklist->{$dev}->{used
} = $used if $used;
614 $disklist->{$dev}->{osdid
} = $osdid;
615 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
616 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
617 $disklist->{$dev}->{db
} = $db_count if $db_count;
618 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
626 my ($part_path) = @_;
628 my ($mode, $rdev) = (stat($part_path))[2,6];
630 next if !$mode || !S_ISBLK
($mode) || !$rdev;
631 my $major = PVE
::Tools
::dev_t_major
($rdev);
632 my $minor = PVE
::Tools
::dev_t_minor
($rdev);
633 my $partnum_path = "/sys/dev/block/$major:$minor/";
637 $partnum = file_read_firstline
("${partnum_path}partition");
639 die "Partition does not exists\n" if !defined($partnum);
641 #untaint and ensure it is a int
642 if ($partnum =~ m/(\d+)/) {
644 die "Partition number $partnum is invalid\n" if $partnum > 128;
646 die "Failed to get partition number\n";
653 my ($part_path) = @_;
655 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
656 my $link = readlink "/sys/class/block/$dev";
657 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
659 die "Can't parse parent device\n" if !defined($block_dev);
660 die "No valid block device\n" if index($dev, $block_dev) == -1;
662 $block_dev = "/dev/$block_dev";
663 die "Block device does not exsists\n" if !(-b
$block_dev);
668 sub locked_disk_action
{
670 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
675 sub assert_disk_unused
{
678 die "device '$dev' is already in use\n" if disk_is_used
($dev);
683 sub append_partition
{
684 my ($dev, $size) = @_;
687 $devname =~ s
|^/dev/||;
690 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*?
(\d
+)/, sub {
691 my ($part, $partid) = @_;
693 if ($partid >= $newpartid) {
694 $newpartid = $partid + 1;
698 $size = PVE
::Tools
::convert_size
($size, 'b' => 'mb');
700 run_command
([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
701 errmsg
=> "error creating partition '$newpartid' on '$dev'");
705 # loop again to detect the real partiton device which does not always follow
706 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
707 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*$newpartid/, sub {
710 $partition = "/dev/$part";