]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
1 package PVE
::Diskmanage
;
14 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
16 my $SMARTCTL = "/usr/sbin/smartctl";
17 my $ZPOOL = "/sbin/zpool";
18 my $SGDISK = "/sbin/sgdisk";
19 my $PVS = "/sbin/pvs";
20 my $LVS = "/sbin/lvs";
21 my $LSBLK = "/bin/lsblk";
23 my sub strip_dev
:prototype($) {
25 $devpath =~ s
|^/dev/||;
34 sub verify_blockdev_path
{
37 die "missing path" if !$rel_path;
38 my $path = abs_path
($rel_path);
39 die "failed to get absolute path to $rel_path\n" if !$path;
41 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
43 $path = "/dev/$1"; # untaint
45 assert_blockdev
($path);
51 my ($dev, $noerr) = @_;
53 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
54 return undef if $noerr;
55 die "not a valid block device\n";
62 my ($disk, $uuid) = @_;
64 assert_blockdev
($disk);
66 # we should already have checked these in the api call, but we check again for safety
67 die "$disk is a partition\n" if is_partition
($disk);
68 die "disk $disk is already in use\n" if disk_is_used
($disk);
70 my $id = $uuid || 'R';
71 run_command
([$SGDISK, $disk, '-U', $id]);
81 my $disklist = get_disks
($dev, 1, 1);
83 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
84 return 1 if $disklist->{$dev}->{used
};
90 my ($disk, $healthonly) = @_;
92 assert_blockdev
($disk);
98 if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) {
99 my $info = get_sysdir_info
("/sys/block/$1");
100 $disk = "/dev/".($info->{device
}
101 or die "failed to get nvme controller device for $disk\n");
104 my $cmd = [$SMARTCTL, '-H'];
105 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
109 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
112 # ATA SMART attributes, e.g.:
113 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
114 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
116 # SAS and NVME disks, e.g.:
117 # Data Units Written: 5,584,952 [2.85 TB]
118 # Accumulated start-stop cycles: 34
120 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+(.*)$/) {
123 $entry->{name
} = $2 if defined $2;
124 $entry->{flags
} = $3 if defined $3;
125 # the +0 makes a number out of the strings
126 # fixme remove next line in major release, use normalized instead
127 $entry->{value
} = $4+0 if defined $4;
128 $entry->{normalized
} = $4+0 if defined $4;
129 $entry->{worst
} = $5+0 if defined $5;
130 # some disks report the default threshold as --- instead of 000
131 if (defined($6) && $6 eq '---') {
132 $entry->{threshold
} = 0;
134 $entry->{threshold
} = $6+0 if defined $6;
136 $entry->{fail
} = $7 if defined $7;
137 $entry->{raw
} = $8 if defined $8;
138 $entry->{id
} = $1 if defined $1;
139 push @{$smartdata->{attributes
}}, $entry;
140 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
141 $smartdata->{health
} = $1;
142 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
144 delete $smartdata->{text
};
145 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
147 } elsif (defined($type) && $type eq 'text') {
148 $smartdata->{text
} = '' if !defined $smartdata->{text
};
149 $smartdata->{text
} .= "$line\n";
150 # extract wearout from nvme/sas text, allow for decimal values
151 if ($line =~ m/Percentage Used(?: endurance indicator)?:\s*(\d+(?:\.\d+)?)\%/i) {
152 $smartdata->{wearout
} = 100 - $1;
154 } elsif ($line =~ m/SMART Disabled/) {
155 $smartdata->{health
} = "SMART Disabled";
161 # bit 0 and 1 mark an severe smartctl error
162 # all others are for disk status, so ignore them
164 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
165 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
168 $smartdata->{type
} = $type;
173 sub get_lsblk_info
() {
174 my $cmd = [$LSBLK, '--json', '-o', 'path,parttype,fstype'];
178 run_command
($cmd, outfunc
=> sub {
180 $output .= "$line\n";
184 return $res if $output eq '';
186 my $parsed = eval { decode_json
($output) };
188 my $list = $parsed->{blockdevices
} // [];
192 parttype
=> $_->{parttype
},
193 fstype
=> $_->{fstype
}
200 my $get_devices_by_partuuid = sub {
201 my ($lsblk_info, $uuids, $res) = @_;
203 $res = {} if !defined($res);
205 foreach my $dev (sort keys %{$lsblk_info}) {
206 my $uuid = $lsblk_info->{$dev}->{parttype
};
207 next if !defined($uuid) || !defined($uuids->{$uuid});
208 $res->{$dev} = $uuids->{$uuid};
214 sub get_zfs_devices
{
215 my ($lsblk_info) = @_;
218 return {} if !check_bin
($ZPOOL);
220 # use zpool and parttype uuid,
221 # because log and cache do not have
224 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
227 if ($line =~ m
|^\t([^\t]+)\t|) {
234 # because maybe zfs tools are not installed
238 "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple
239 "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd
243 $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
248 sub get_lvm_devices
{
249 my ($lsblk_info) = @_;
252 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
255 if ($line =~ m
|^/dev/|) {
261 # if something goes wrong, we do not want
262 # to give up, but indicate an error has occurred
266 "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1,
269 $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
274 sub get_ceph_journals
{
275 my ($lsblk_info) = @_;
279 '45b0969e-9b03-4f30-b4c6-b4b80ceff106' => 1, # journal
280 '30cd0809-c2b2-499c-8879-2d6b78529876' => 2, # db
281 '5ce17fce-4087-4169-b7ff-056cc58473f9' => 3, # wal
282 'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block
285 $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
290 # reads the lv_tags and matches them with the devices
291 sub get_ceph_volume_infos
{
294 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
295 '--noheadings', '--readonly', '--separator', ';' ];
297 run_command
($cmd, outfunc
=> sub {
299 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
301 my $fields = [ split(';', $line) ];
303 # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need)
304 my ($dev) = $fields->[0] =~ m
|^(/dev/[a-z
]+[^(]*)|;
305 if ($fields->[1] =~ m
|^osd-
([^-]+)-|) {
307 # $result autovivification is wanted, to not creating empty hashes
308 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) {
309 $result->{$dev}->{osdid
} = $1;
310 $result->{$dev}->{bluestore
} = ($type eq 'block');
311 if ($fields->[2] =~ m/ceph\.encrypted=1/) {
312 $result->{$dev}->{encrypted
} = 1;
315 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
316 $result->{$dev}->{$type}++;
330 run_command
(['udevadm', 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
336 return undef if !$info;
338 return undef if $info !~ m/^E: DEVTYPE=(disk|partition)$/m;
339 return undef if $info =~ m/^E: ID_CDROM/m;
341 # we use this, because some disks are not simply in /dev
342 # e.g. /dev/cciss/c0d0
343 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
344 $data->{devpath
} = $1;
346 return if !defined($data->{devpath
});
348 $data->{serial
} = 'unknown';
349 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
350 $data->{serial
} = $1;
354 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
360 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
364 if ($info =~ m/^E: ID_BUS=usb$/m) {
368 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
372 $data->{wwn
} = 'unknown';
373 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
377 if ($info =~ m/^E: DEVLINKS=(.+)$/m) {
378 my @devlinks = grep(m
#^/dev/disk/by-id/(ata|scsi|nvme(?!-eui))#, split (/ /, $1));
379 $data->{by_id_link
} = $devlinks[0] if defined($devlinks[0]);
385 sub get_sysdir_size
{
388 my $size = file_read_firstline
("$sysdir/size");
391 # linux always considers sectors to be 512 bytes,
392 # independently of real block size
396 sub get_sysdir_info
{
399 return undef if ! -d
"$sysdir/device";
403 $data->{size
} = get_sysdir_size
($sysdir) or return;
405 # dir/queue/rotational should be 1 for hdd, 0 for ssd
406 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational") // -1;
408 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
409 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
411 if (defined(my $device = readlink("$sysdir/device"))) {
412 # strip directory and untaint:
413 ($data->{device
}) = $device =~ m!([^/]+)$!;
419 sub get_wear_leveling_info
{
420 my ($smartdata) = @_;
421 my $attributes = $smartdata->{attributes
};
423 if (defined($smartdata->{wearout
})) {
424 return $smartdata->{wearout
};
429 # Common register names that represent percentage values of potential
430 # failure indicators used in drivedb.h of smartmontool's. Order matters,
431 # as some drives may have multiple definitions
432 my @wearoutregisters = (
433 "Media_Wearout_Indicator",
435 "Wear_Leveling_Count",
436 "Perc_Write\/Erase_Ct_BC",
437 "Perc_Rated_Life_Remain",
438 "Remaining_Lifetime_Perc",
439 "Percent_Lifetime_Remain",
441 "PCT_Life_Remaining",
442 "Lifetime_Remaining",
443 "Percent_Life_Remaining",
444 "Percent_Lifetime_Used",
445 "Perc_Rated_Life_Used"
448 # Search for S.M.A.R.T. attributes for known register
449 foreach my $register (@wearoutregisters) {
450 last if defined $wearout;
451 foreach my $attr (@$attributes) {
452 next if $attr->{name
} !~ m/$register/;
453 $wearout = $attr->{value
};
464 my $dh = IO
::Dir-
>new ($dir);
467 while (defined(my $tmp = $dh->read)) {
468 next if $tmp eq '.' || $tmp eq '..';
479 if (-l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|) {
488 return $type eq 'ssd' || $type eq 'nvme';
491 sub mounted_blockdevs
{
494 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
496 foreach my $mount (@$mounts) {
497 next if $mount->[0] !~ m
|^/dev/|;
498 $mounted->{abs_path
($mount->[0])} = $mount->[1];
504 # returns hashmap of abs mount path -> first part of /proc/mounts (what)
508 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
510 foreach my $mount (@$mounts) {
511 $mounted->{abs_path
($mount->[1])} = $mount->[0];
518 my ($disks, $nosmart, $include_partitions) = @_;
521 my $mounted = mounted_blockdevs
();
523 my $lsblk_info = get_lsblk_info
();
525 my $journalhash = get_ceph_journals
($lsblk_info);
526 my $ceph_volume_infos = get_ceph_volume_infos
();
528 my $zfshash = get_zfs_devices
($lsblk_info);
530 my $lvmhash = get_lvm_devices
($lsblk_info);
532 my $disk_regex = ".*";
533 if (defined($disks)) {
536 } elsif (ref($disks) ne 'ARRAY') {
537 die "disks is not a string or array reference\n";
539 # we get cciss/c0d0 but need cciss!c0d0
540 $_ =~ s
|cciss
/|cciss
!| for @$disks;
542 if ($include_partitions) {
543 # Proper blockdevice is needed for the regex, use parent for partitions.
544 for my $disk ($disks->@*) {
545 next if !is_partition
("/dev/$disk");
546 $disk = strip_dev
(get_blockdev
("/dev/$disk"));
550 $disk_regex = "(?:" . join('|', @$disks) . ")";
553 dir_glob_foreach
('/sys/block', $disk_regex, sub {
555 # whitelisting following devices
556 # hdX: ide block device
557 # sdX: sd block device
558 # vdX: virtual block device
559 # xvdX: xen virtual block device
560 # nvmeXnY: nvme devices
561 # cciss!cXnY: cciss devices
562 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
563 $dev !~ m/^nvme\d+n\d+$/ &&
564 $dev !~ m/^cciss\!c\d+d\d+$/;
566 my $data = get_udev_info
("/sys/block/$dev");
567 return if !defined($data);
568 my $devpath = $data->{devpath
};
570 my $sysdir = "/sys/block/$dev";
572 # we do not want iscsi devices
573 return if is_iscsi
($sysdir);
575 my $sysdata = get_sysdir_info
($sysdir);
576 return if !defined($sysdata);
578 my $type = 'unknown';
580 if ($sysdata->{rotational
} == 0) {
582 $type = 'nvme' if $dev =~ m/^nvme\d+n\d+$/;
584 } elsif ($sysdata->{rotational
} == 1) {
585 if ($data->{rpm
} != -1) {
587 } elsif ($data->{usb
}) {
593 my $health = 'UNKNOWN';
598 my $smartdata = get_smart_data
($devpath, !is_ssdlike
($type));
599 $health = $smartdata->{health
} if $smartdata->{health
};
601 if (is_ssdlike
($type)) {
602 # if we have an ssd we try to get the wearout indicator
603 my $wearval = get_wear_leveling_info
($smartdata);
604 $wearout = $wearval if defined($wearval);
609 # we replaced cciss/ with cciss! above
610 # but in the result we need cciss/ again
611 # because the caller might want to check the
612 # result again with the original parameter
613 if ($dev =~ m
|^cciss
!|) {
614 $dev =~ s
|^cciss
!|cciss
/|;
617 $disklist->{$dev} = {
618 vendor
=> $sysdata->{vendor
},
619 model
=> $data->{model
} || $sysdata->{model
},
620 size
=> $sysdata->{size
},
621 serial
=> $data->{serial
},
630 $disklist->{$dev}->{mounted
} = 1 if exists $mounted->{$devpath};
632 my $by_id_link = $data->{by_id_link
};
633 $disklist->{$dev}->{by_id_link
} = $by_id_link if defined($by_id_link);
637 my $osdencrypted = 0;
639 my $journal_count = 0;
643 my $partpath = $devpath;
645 # remove part after last / to
646 # get the base path for the partitions
647 # e.g. from /dev/cciss/c0d0 get /dev/cciss
648 $partpath =~ s/\/[^\/]+$//;
650 my $determine_usage = sub {
651 my ($devpath, $sysdir, $is_partition) = @_;
653 return 'LVM' if $lvmhash->{$devpath};
654 return 'ZFS' if $zfshash->{$devpath};
656 my $info = $lsblk_info->{$devpath} // {};
658 my $parttype = $info->{parttype
};
659 if (defined($parttype)) {
661 if $parttype eq '21686148-6449-6e6f-744e-656564454649';
663 if $parttype eq 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b';
664 return 'ZFS reserved'
665 if $parttype eq '6a945a3b-1dd2-11b2-99a6-080020736631';
668 my $fstype = $info->{fstype
};
669 return "${fstype}" if defined($fstype);
670 return 'mounted' if $mounted->{$devpath};
672 return if !$is_partition;
674 # for devices, this check is done explicitly later
675 return 'Device Mapper' if !dir_is_empty
("$sysdir/holders");
677 return; # unused partition
680 my $collect_ceph_info = sub {
683 my $ceph_volume = $ceph_volume_infos->{$devpath} or return;
684 $journal_count += $ceph_volume->{journal
} // 0;
685 $db_count += $ceph_volume->{db
} // 0;
686 $wal_count += $ceph_volume->{wal
} // 0;
687 if (defined($ceph_volume->{osdid
})) {
688 $osdid = $ceph_volume->{osdid
};
689 $bluestore = 1 if $ceph_volume->{bluestore
};
690 $osdencrypted = 1 if $ceph_volume->{encrypted
};
693 my $result = { %{$ceph_volume} };
694 $result->{journals
} = delete $result->{journal
}
695 if $result->{journal
};
701 dir_glob_foreach
("$sysdir", "$dev.+", sub {
704 $partitions->{$part} = $collect_ceph_info->("$partpath/$part");
705 my $lvm_based_osd = defined($partitions->{$part});
707 $partitions->{$part}->{devpath
} = "$partpath/$part";
708 $partitions->{$part}->{parent
} = "$devpath";
709 $partitions->{$part}->{mounted
} = 1 if exists $mounted->{"$partpath/$part"};
710 $partitions->{$part}->{gpt
} = $data->{gpt
};
711 $partitions->{$part}->{type
} = 'partition';
712 $partitions->{$part}->{size
} =
713 get_sysdir_size
("$sysdir/$part") // 0;
714 $partitions->{$part}->{used
} =
715 $determine_usage->("$partpath/$part", "$sysdir/$part", 1);
716 $partitions->{$part}->{osdid
} //= -1;
718 # Avoid counting twice (e.g. partition on which the LVM for the
719 # DB OSD resides is present in the $journalhash)
720 return if $lvm_based_osd;
722 # Legacy handling for non-LVM based OSDs
724 if (my $mp = $mounted->{"$partpath/$part"}) {
725 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
727 $partitions->{$part}->{osdid
} = $osdid;
731 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
732 $journal_count++ if $journal_part == 1;
733 $db_count++ if $journal_part == 2;
734 $wal_count++ if $journal_part == 3;
735 $bluestore = 1 if $journal_part == 4;
737 $partitions->{$part}->{journals
} = 1 if $journal_part == 1;
738 $partitions->{$part}->{db
} = 1 if $journal_part == 2;
739 $partitions->{$part}->{wal
} = 1 if $journal_part == 3;
740 $partitions->{$part}->{bluestore
} = 1 if $journal_part == 4;
744 my $used = $determine_usage->($devpath, $sysdir, 0);
745 if (!$include_partitions) {
746 foreach my $part (sort keys %{$partitions}) {
747 $used //= $partitions->{$part}->{used
};
750 # fstype might be set even if there are partitions, but showing that is confusing
751 $used = 'partitions' if scalar(keys %{$partitions});
753 $used //= 'partitions' if scalar(keys %{$partitions});
754 # multipath, software raid, etc.
755 # this check comes in last, to show more specific info
757 $used //= 'Device Mapper' if !dir_is_empty
("$sysdir/holders");
759 $disklist->{$dev}->{used
} = $used if $used;
761 $collect_ceph_info->($devpath);
763 $disklist->{$dev}->{osdid
} = $osdid;
764 $disklist->{$dev}->{journals
} = $journal_count if $journal_count;
765 $disklist->{$dev}->{bluestore
} = $bluestore if $osdid != -1;
766 $disklist->{$dev}->{osdencrypted
} = $osdencrypted if $osdid != -1;
767 $disklist->{$dev}->{db
} = $db_count if $db_count;
768 $disklist->{$dev}->{wal
} = $wal_count if $wal_count;
770 if ($include_partitions) {
771 foreach my $part (keys %{$partitions}) {
772 $disklist->{$part} = $partitions->{$part};
782 my ($part_path) = @_;
784 my $st = stat($part_path);
786 die "error detecting block device '$part_path'\n"
787 if !$st || !$st->mode || !S_ISBLK
($st->mode) || !$st->rdev;
789 my $major = PVE
::Tools
::dev_t_major
($st->rdev);
790 my $minor = PVE
::Tools
::dev_t_minor
($st->rdev);
791 my $partnum_path = "/sys/dev/block/$major:$minor/";
795 $partnum = file_read_firstline
("${partnum_path}partition");
797 die "Partition does not exist\n" if !defined($partnum);
799 #untaint and ensure it is a int
800 if ($partnum =~ m/(\d+)/) {
802 die "Partition number $partnum is invalid\n" if $partnum > 128;
804 die "Failed to get partition number\n";
811 my ($part_path) = @_;
813 my ($dev, $block_dev);
814 if ($part_path =~ m
|^/dev/(.*)$|) {
816 my $link = readlink "/sys/class/block/$dev";
817 $block_dev = $1 if $link =~ m
|([^/]*)/$dev$|;
820 die "Can't parse parent device\n" if !defined($block_dev);
821 die "No valid block device\n" if index($dev, $block_dev) == -1;
823 $block_dev = "/dev/$block_dev";
824 die "Block device does not exists\n" if !(-b
$block_dev);
832 return defined(eval { get_partnum
($dev_path) });
835 sub locked_disk_action
{
837 my $res = PVE
::Tools
::lock_file
('/run/lock/pve-diskmanage.lck', undef, $sub);
842 sub assert_disk_unused
{
845 die "device '$dev' is already in use\n" if disk_is_used
($dev);
850 sub append_partition
{
851 my ($dev, $size) = @_;
854 $devname =~ s
|^/dev/||;
857 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*?
(\d
+)/, sub {
858 my ($part, $partid) = @_;
860 if ($partid >= $newpartid) {
861 $newpartid = $partid + 1;
865 $size = PVE
::Tools
::convert_size
($size, 'b' => 'mb');
867 run_command
([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
868 errmsg
=> "error creating partition '$newpartid' on '$dev'");
872 # loop again to detect the real partition device which does not always follow
873 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
874 dir_glob_foreach
("/sys/block/$devname", qr
/\Q
$devname\E.*$newpartid/, sub {
877 $partition = "/dev/$part";
883 # Check if a disk or any of its partitions has a holder.
884 # Can also be called with a partition.
885 # Expected to be called with a result of verify_blockdev_path().
889 my $dev = strip_dev
($devpath);
891 return $devpath if !dir_is_empty
("/sys/class/block/${dev}/holders");
894 dir_glob_foreach
("/sys/block/${dev}", "${dev}.+", sub {
896 $found = "/dev/${part}" if !dir_is_empty
("/sys/class/block/${part}/holders");
902 # Basic check if a disk or any of its partitions is mounted.
903 # Can also be called with a partition.
904 # Expected to be called with a result of verify_blockdev_path().
908 my $mounted = mounted_blockdevs
();
910 return $devpath if $mounted->{$devpath};
912 my $dev = strip_dev
($devpath);
915 dir_glob_foreach
("/sys/block/${dev}", "${dev}.+", sub {
917 my $partpath = "/dev/${part}";
919 $found = $partpath if $mounted->{$partpath};
925 # Currently only supports GPT-partitioned disks.
926 sub change_parttype
{
927 my ($partpath, $parttype) = @_;
929 my $err = "unable to change partition type for $partpath";
931 my $partnum = get_partnum
($partpath);
932 my $blockdev = get_blockdev
($partpath);
933 my $dev = strip_dev
($blockdev);
935 my $info = get_disks
($dev, 1);
936 die "$err - unable to get disk info for '$blockdev'\n" if !defined($info->{$dev});
937 die "$err - disk '$blockdev' is not GPT partitioned\n" if !$info->{$dev}->{gpt
};
939 run_command
(['sgdisk', "-t${partnum}:${parttype}", $blockdev], errmsg
=> $err);
942 # Wipes all labels and the first 200 MiB of a disk/partition (or the whole if it is smaller).
943 # If called with a partition, also sets the partition type to 0x83 'Linux filesystem'.
944 # Expected to be called with a result of verify_blockdev_path().
948 my $devname = basename
($devpath);
949 my $dev_size = PVE
::Tools
::file_get_contents
("/sys/class/block/$devname/size");
951 ($dev_size) = $dev_size =~ m
|(\d
+)|; # untaint $dev_size
952 die "Couldn't get the size of the device $devname\n" if !defined($dev_size);
954 my $size = ($dev_size * 512 / 1024 / 1024);
955 my $count = ($size < 200) ?
$size : 200;
958 dir_glob_foreach
("/sys/class/block/${devname}", "${devname}.+", sub {
960 push $to_wipe->@*, "/dev/${part}" if -b
"/dev/${part}";
963 if (scalar($to_wipe->@*) > 0) {
964 print "found child partitions to wipe: ". join(', ', $to_wipe->@*) ."\n";
966 push $to_wipe->@*, $devpath; # put actual device last
968 print "wiping block device ${devpath}\n";
970 run_command
(['wipefs', '--all', $to_wipe->@*], errmsg
=> "error wiping '${devpath}'");
973 ['dd', 'if=/dev/zero', "of=${devpath}", 'bs=1M', 'conv=fdatasync', "count=${count}"],
974 errmsg
=> "error wiping '${devpath}'",
977 if (is_partition
($devpath)) {
978 eval { change_parttype
($devpath, '8300'); };
983 # FIXME: Remove once we depend on systemd >= v249.
984 # Work around udev bug https://github.com/systemd/systemd/issues/18525 ensuring database is updated.
985 sub udevadm_trigger
{
988 return if scalar(@devs) == 0;
990 eval { run_command
(['udevadm', 'trigger', @devs]); };