partially fix #2285: api: disks: allow partitions for creation paths

The calls for directory and ZFS need slight adaptations. Except for
those, the only thing that needs to be done is support partitions in
the disk_is_used helper.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fabian Ebner
2021-10-06 11:18:45 +02:00
committed by Thomas Lamprecht
parent cc884f73d8
commit a2c34371e6
3 changed files with 26 additions and 13 deletions

View File

@ -214,20 +214,24 @@ __PACKAGE__->register_method ({
PVE::Diskmanage::locked_disk_action(sub {
PVE::Diskmanage::assert_disk_unused($dev);
# create partition
my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
print "# ", join(' ', @$cmd), "\n";
run_command($cmd);
my $part = $dev;
my ($devname) = $dev =~ m|^/dev/(.*)$|;
my $part = "/dev/";
dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
my ($partition) = @_;
$part .= $partition;
});
if (!PVE::Diskmanage::is_partition($dev)) {
# create partition
my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
print "# ", join(' ', @$cmd), "\n";
run_command($cmd);
my ($devname) = $dev =~ m|^/dev/(.*)$|;
$part = "/dev/";
dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
my ($partition) = @_;
$part .= $partition;
});
}
# create filesystem
$cmd = [$MKFS, '-t', $type, $part];
my $cmd = [$MKFS, '-t', $type, $part];
print "# ", join(' ', @$cmd), "\n";
run_command($cmd);

View File

@ -373,7 +373,16 @@ __PACKAGE__->register_method ({
PVE::Diskmanage::locked_disk_action(sub {
for my $dev (@$devs) {
PVE::Diskmanage::assert_disk_unused($dev);
my $sysfsdev = $dev =~ s!^/dev/!/sys/block/!r;
my $is_partition = PVE::Diskmanage::is_partition($dev);
my $sysfsdev = $is_partition ? PVE::Diskmanage::get_blockdev($dev) : $dev;
$sysfsdev =~ s!^/dev/!/sys/block/!;
if ($is_partition) {
my $part = $dev =~ s!^/dev/!!r;
$sysfsdev .= "/${part}";
}
my $udevinfo = PVE::Diskmanage::get_udev_info($sysfsdev);
$dev = $udevinfo->{by_id_link} if defined($udevinfo->{by_id_link});
}

View File

@ -78,7 +78,7 @@ sub disk_is_used {
my $dev = $disk;
$dev =~ s|^/dev/||;
my $disklist = get_disks($dev, 1);
my $disklist = get_disks($dev, 1, 1);
die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
return 1 if $disklist->{$dev}->{used};