]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
cac944d46ca81e121fc6b8567e946770a8aa24bd
1 package PVE
::Diskmanage
;
12 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
14 my $SMARTCTL = "/usr/sbin/smartctl";
15 my $ZPOOL = "/sbin/zpool";
16 my $SGDISK = "/sbin/sgdisk";
17 my $PVS = "/sbin/pvs";
18 my $LVS = "/sbin/lvs";
19 my $UDEVADM = "/bin/udevadm";
20 my $LSBLK = "/bin/lsblk";
22 sub verify_blockdev_path
{
25 die "missing path" if !$rel_path;
26 my $path = abs_path
($rel_path);
27 die "failed to get absolute path to $rel_path\n" if !$path;
29 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
31 $path = "/dev/$1"; # untaint
33 assert_blockdev
($path);
39 my ($dev, $noerr) = @_;
41 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
42 return undef if $noerr;
43 die "not a valid block device\n";
50 my ($disk, $uuid) = @_;
52 assert_blockdev
($disk);
54 # we should already have checked if it is in use in the api call
55 # but we check again for safety
56 die "disk $disk is already in use\n" if disk_is_used
($disk);
58 my $id = $uuid || 'R';
59 run_command
([$SGDISK, $disk, '-U', $id]);
69 my $disklist = get_disks
($dev, 1);
71 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
72 return 1 if $disklist->{$dev}->{used
};
78 my ($disk, $healthonly) = @_;
80 assert_blockdev
($disk);
86 if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) {
87 my $info = get_sysdir_info
("/sys/block/$1");
88 $disk = "/dev/".($info->{device
}
89 or die "failed to get nvme controller device for $disk\n");
92 my $cmd = [$SMARTCTL, '-H'];
93 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
97 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
100 # ATA SMART attributes, e.g.:
101 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
102 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
104 # SAS and NVME disks, e.g.:
105 # Data Units Written: 5,584,952 [2.85 TB]
106 # Accumulated start-stop cycles: 34
108 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+(.*)$/) {
112 $entry->{name
} = $2 if defined $2;
113 $entry->{flags
} = $3 if defined $3;
114 # the +0 makes a number out of the strings
115 $entry->{value
} = $4+0 if defined $4;
116 $entry->{worst
} = $5+0 if defined $5;
117 # some disks report the default threshold as --- instead of 000
118 if (defined($6) && $6 eq '---') {
119 $entry->{threshold
} = 0;
121 $entry->{threshold
} = $6+0 if defined $6;
123 $entry->{fail
} = $7 if defined $7;
124 $entry->{raw
} = $8 if defined $8;
125 $entry->{id
} = $1 if defined $1;
126 push @{$smartdata->{attributes
}}, $entry;
127 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
128 $smartdata->{health
} = $1;
129 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
131 delete $smartdata->{text
};
132 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
134 } elsif (defined($type) && $type eq 'text') {
135 $smartdata->{text
} = '' if !defined $smartdata->{text
};
136 $smartdata->{text
} .= "$line\n";
137 # extract wearout from nvme text, allow for decimal values
138 if ($line =~ m/Percentage Used:\s*(\d+(?:\.\d+)?)\%/i) {
139 $smartdata->{wearout
} = 100 - $1;
141 } elsif ($line =~ m/SMART Disabled/) {
142 $smartdata->{health
} = "SMART Disabled";
148 # bit 0 and 1 mark an severe smartctl error
149 # all others are for disk status, so ignore them
151 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
152 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
155 $smartdata->{type
} = $type;
160 sub get_parttype_info
() {
161 my $cmd = [$LSBLK, '--json', '-o', 'path,parttype'];
165 run_command
($cmd, outfunc
=> sub {
167 $output .= "$line\n";
171 return $res if $output eq '';
173 my $parsed = eval { decode_json
($output) };
175 my $list = $parsed->{blockdevices
} // [];
177 foreach my $dev (@$list) {
178 next if !($dev->{parttype
});
179 my $type = $dev->{parttype
};
180 $res->{$type} = [] if !defined($res->{$type});
181 push @{$res->{$type}}, $dev->{path
};
187 my $get_devices_by_partuuid = sub {
188 my ($parttype_map, $uuids, $res) = @_;
190 $res = {} if !defined($res);
192 foreach my $uuid (sort keys %$uuids) {
193 map { $res->{$_} = $uuids->{$uuid} } @{$parttype_map->{$uuid}};
199 sub get_zfs_devices
{
200 my ($parttype_map) = @_;
203 return {} if ! -x
$ZPOOL;
205 # use zpool and parttype uuid,
206 # because log and cache do not have
209 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
212 if ($line =~ m
|^\t([^\t]+)\t|) {
219 # because maybe zfs tools are not installed
223 "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple
224 "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd
228 $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
233 sub get_lvm_devices
{
234 my ($parttype_map) = @_;
237 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
240 if ($line =~ m
|^/dev/|) {
246 # if something goes wrong, we do not want
247 # to give up, but indicate an error has occured
251 "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1,
254 $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
259 sub get_ceph_journals
{
260 my ($parttype_map) = @_;
264 '45b0969e-9b03-4f30-b4c6-b4b80ceff106' => 1, # journal
265 '30cd0809-c2b2-499c-8879-2d6b78529876' => 2, # db
266 '5ce17fce-4087-4169-b7ff-056cc58473f9' => 3, # wal
267 'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block
270 $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
275 # reads the lv_tags and matches them with the devices
276 sub get_ceph_volume_infos
{
279 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
280 '--noheadings', '--readonly', '--separator', ';' ];
282 run_command
($cmd, outfunc
=> sub {
284 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
286 my $fields = [ split(';', $line) ];
288 # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need)
289 my ($dev) = $fields->[0] =~ m
|^(/dev/[a-z
]+)|;
290 if ($fields->[1] =~ m
|^osd-
([^-]+)-|) {
292 # $result autovivification is wanted, to not creating empty hashes
293 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) {
294 $result->{$dev}->{osdid
} = $1;
295 $result->{$dev}->{bluestore
} = ($type eq 'block');
296 if ($fields->[2] =~ m/ceph\.encrypted=1/) {
297 $result->{$dev}->{encrypted
} = 1;
300 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
301 $result->{$dev}->{$type}++;
315 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
321 return undef if !$info;
323 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
324 return undef if $info =~ m/^E: ID_CDROM/m;
326 # we use this, because some disks are not simply in /dev
327 # e.g. /dev/cciss/c0d0
328 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
329 $data->{devpath
} = $1;
331 return if !defined($data->{devpath
});
333 $data->{serial
} = 'unknown';
334 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
335 $data->{serial
} = $1;
339 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
345 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
349 if ($info =~ m/^E: ID_BUS=usb$/m) {
353 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
357 $data->{wwn
} = 'unknown';
358 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
365 sub get_sysdir_info
{
368 return undef if ! -d
"$sysdir/device";
372 my $size = file_read_firstline
("$sysdir/size");
373 return undef if !$size;
375 # linux always considers sectors to be 512 bytes,
376 # independently of real block size
377 $data->{size
} = $size * 512;
379 # dir/queue/rotational should be 1 for hdd, 0 for ssd
380 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
382 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
383 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
385 if (defined(my $device = readlink("$sysdir/device"))) {
386 # strip directory and untaint:
387 ($data->{device
}) = $device =~ m!([^/]+)$!;
393 sub get_wear_leveling_info
{
394 my ($smartdata, $model) = @_;
395 my $attributes = $smartdata->{attributes
};
397 if (defined($smartdata->{wearout
})) {
398 return $smartdata->{wearout
};
412 # find target attr id
416 foreach my $vendor (keys %$vendormap) {
417 if ($model =~ m/$vendor/i) {
418 $attrid = $vendormap->{$vendor};
419 # found the attribute
425 $attrid = $vendormap->{default};
428 foreach my $attr (@$attributes) {
429 next if $attr->{id
} != $attrid;
430 $wearout = $attr->{value
};
440 my $dh = IO
::Dir-
>new ($dir);
443 while (defined(my $tmp = $dh->read)) {
444 next if $tmp eq '.' || $tmp eq '..';
455 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
463 my ($disks, $nosmart) = @_;
468 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
470 foreach my $mount (@$mounts) {
471 next if $mount->[0] !~ m
|^/dev/|;
472 $mounted->{abs_path
($mount->[0])} = $mount->[1];
475 my $dev_is_mounted = sub {
477 return $mounted->{$dev};
480 my $parttype_map = get_parttype_info
();
482 my $journalhash = get_ceph_journals
($parttype_map);
483 my $ceph_volume_infos = get_ceph_volume_infos
();
485 my $zfshash = get_zfs_devices
($parttype_map);
487 my $lvmhash = get_lvm_devices
($parttype_map);
489 my $disk_regex = ".*";
490 if (defined($disks)) {
493 } elsif (ref($disks) ne 'ARRAY') {
494 die "disks is not a string or array reference\n";
496 # we get cciss/c0d0 but need cciss!c0d0
497 map { s
|cciss
/|cciss
!| } @$disks;
499 $disk_regex = "(?:" . join('|', @$disks) . ")";
502 dir_glob_foreach
('/sys/block', $disk_regex, sub {
504 # whitelisting following devices
505 # hdX: ide block device
506 # sdX: sd block device
507 # vdX: virtual block device
508 # xvdX: xen virtual block device
509 # nvmeXnY: nvme devices
510 # cciss!cXnY: cciss devices
511 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
512 $dev !~ m/^nvme\d+n\d+$/ &&
513 $dev !~ m/^cciss\!c\d+d\d+$/;
515 my $data = get_udev_info
("/sys/block/$dev");
516 return if !defined($data);
517 my $devpath = $data->{devpath
};
519 my $sysdir = "/sys/block/$dev";
521 # we do not want iscsi devices
522 return if is_iscsi
($sysdir);
524 my $sysdata = get_sysdir_info
($sysdir);
525 return if !defined($sysdata);
527 my $type = 'unknown';
529 if ($sysdata->{rotational
} == 0) {
532 } elsif ($sysdata->{rotational
} == 1) {
533 if ($data->{rpm
} != -1) {
535 } elsif ($data->{usb
}) {
541 my $health = 'UNKNOWN';
546 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
547 $health = $smartdata->{health
} if $smartdata->{health
};
549 if ($type eq 'ssd') {
550 # if we have an ssd we try to get the wearout indicator
551 my $wearval = get_wear_leveling_info
($smartdata, $data->{model
} || $sysdata->{model
});
552 $wearout = $wearval if defined($wearval);
559 $used = 'LVM' if $lvmhash->{$devpath};
561 $used = 'mounted' if &$dev_is_mounted($devpath);
563 $used = 'ZFS' if $zfshash->{$devpath};
565 # we replaced cciss/ with cciss! above
566 # but in the result we need cciss/ again
567 # because the caller might want to check the
568 # result again with the original parameter
569 if ($dev =~ m
|^cciss
!|) {
570 $dev =~ s
|^cciss
!|cciss
/|;
573 $disklist->{$dev} = {
574 vendor
=> $sysdata->{vendor
},
575 model
=> $data->{model
} || $sysdata->{model
},
576 size
=> $sysdata->{size
},
577 serial
=> $data->{serial
},
589 my $osdencrypted = 0;
591 my $journal_count = 0;
595 my $found_partitions;
597 my $found_mountpoints;
600 my $partpath = $devpath;
602 # remove part after last / to
603 # get the base path for the partitions
604 # e.g. from /dev/cciss/c0d0 get /dev/cciss
605 $partpath =~ s/\/[^\/]+$//;
607 dir_glob_foreach
("$sysdir", "$dev.+", sub {
610 $found_partitions = 1;
612 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
613 $found_mountpoints = 1;
614 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
619 if ($lvmhash->{"$partpath/$part"}) {
623 if ($zfshash->{"$partpath/$part"}) {
627 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
628 $journal_count++ if $journal_part == 1;
629 $db_count++ if $journal_part == 2;
630 $wal_count++ if $journal_part == 3;
631 $bluestore = 1 if $journal_part == 4;
634 if (!dir_is_empty
("$sysdir/$part/holders") && !$found_lvm) {
639 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
640 $journal_count += $ceph_volume->{journal
} // 0;
641 $db_count += $ceph_volume->{db
} // 0;
642 $wal_count += $ceph_volume->{wal
} // 0;
643 if (defined($ceph_volume->{osdid
})) {
644 $osdid = $ceph_volume->{osdid
};
645 $bluestore = 1 if $ceph_volume->{bluestore
};
646 $osdencrypted = 1 if $ceph_volume->{encrypted
};
650 $used = 'mounted' if $found_mountpoints && !$used;
651 $used = 'LVM' if $found_lvm && !$used;
652 $used = 'ZFS' if $found_zfs && !$used;
653 $used = 'Device Mapper' if $found_dm && !$used;
654 $used = 'partitions' if $found_partitions && !$used;
656 # multipath, software raid, etc.
657 # this check comes in last, to show more specific info
659 $used = 'Device Mapper' if !$used && !dir_is_empty
("$sysdir/holders");
661 $disklist->{$dev}->{used
} = $used if $used;
662 $disklist->{$dev}->{osdid
} = $osdid;
663 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
664 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
665 $disklist->{$dev}->{osdencrypted
} = $osdencrypted if $osdid != -1;
666 $disklist->{$dev}->{db
} = $db_count if $db_count;
667 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
675 my ($part_path) = @_;
677 my $st = stat($part_path);
679 next if !$st->mode || !S_ISBLK
($st->mode) || !$st->rdev;
680 my $major = PVE
::Tools
::dev_t_major
($st->rdev);
681 my $minor = PVE
::Tools
::dev_t_minor
($st->rdev);
682 my $partnum_path = "/sys/dev/block/$major:$minor/";
686 $partnum = file_read_firstline
("${partnum_path}partition");
688 die "Partition does not exist\n" if !defined($partnum);
690 #untaint and ensure it is a int
691 if ($partnum =~ m/(\d+)/) {
693 die "Partition number $partnum is invalid\n" if $partnum > 128;
695 die "Failed to get partition number\n";
702 my ($part_path) = @_;
704 my $dev = $1 if $part_path =~ m
|^/dev/(.*)$|;
705 my $link = readlink "/sys/class/block/$dev";
706 my $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
708 die "Can't parse parent device\n" if !defined($block_dev);
709 die "No valid block device\n" if index($dev, $block_dev) == -1;
711 $block_dev = "/dev/$block_dev";
712 die "Block device does not exsists\n" if !(-b
$block_dev);
717 sub locked_disk_action
{
719 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
724 sub assert_disk_unused
{
727 die "device '$dev' is already in use\n" if disk_is_used
($dev);
732 sub append_partition
{
733 my ($dev, $size) = @_;
736 $devname =~ s
|^/dev/||;
739 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*?
(\d
+)/, sub {
740 my ($part, $partid) = @_;
742 if ($partid >= $newpartid) {
743 $newpartid = $partid + 1;
747 $size = PVE
::Tools
::convert_size
($size, 'b' => 'mb');
749 run_command
([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
750 errmsg
=> "error creating partition '$newpartid' on '$dev'");
754 # loop again to detect the real partiton device which does not always follow
755 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
756 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*$newpartid/, sub {
759 $partition = "/dev/$part";