From 1dc3038d40278957b35aea2cc5c571597c38357c Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 4 Jun 2019 12:35:23 +0200 Subject: [PATCH] Diskmanage: add append_partition sub we will use this for adding a partition to a disk when using a device for ceph osd db/wal which already has partitions on it first we search for the highest partition number, then add the partition and search for the resulting device (we cannot assume to simply append the number, e.g. from /dev/nvme0n1 we get /dev/nvme0n1pX) Signed-off-by: Dominik Csapak --- PVE/Diskmanage.pm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm index 281b378..5e33747 100644 --- a/PVE/Diskmanage.pm +++ b/PVE/Diskmanage.pm @@ -680,4 +680,37 @@ sub assert_disk_unused { return undef; } +sub append_partition { + my ($dev, $size) = @_; + + my $devname = $dev; + $devname =~ s|^/dev/||; + + my $newpartid = 1; + dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub { + my ($part, $partid) = @_; + + if ($partid >= $newpartid) { + $newpartid = $partid + 1; + } + }); + + $size = PVE::Tools::convert_size($size, 'b' => 'mb'); + + run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ], + errmsg => "error creating partition '$newpartid' on '$dev'"); + + my $partition; + + # loop again to detect the real partiton device which does not always follow + # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1 + dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub { + my ($part) = @_; + + $partition = "/dev/$part"; + }); + + return $partition; +} + 1;