[pve-devel] [PATCH storage v2 3/4] Diskmanage: add append_partition sub

Dominik Csapak d.csapak at proxmox.com
Tue Jun 4 12:35:23 CEST 2019


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 <d.csapak at proxmox.com>
---
changes from v1:
* set devname to dev by default
* use dir_glob_foreach regex to parse out the partition id
* add explaining comment for the second loop

 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;
-- 
2.11.0





More information about the pve-devel mailing list