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

Dominik Csapak d.csapak at proxmox.com
Tue Jun 4 09:21:15 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>
---
 PVE/Diskmanage.pm | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index daad6df..645d67b 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -683,4 +683,40 @@ sub assert_disk_unused {
     return undef;
 }
 
+sub append_partition {
+    my ($dev, $size) = @_;
+
+    my $devname;
+    if ($dev =~ m|^/dev/(.*)$|) {
+	$devname = $1;
+    }
+
+    my $newpartid = 1;
+    dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
+	my ($part) = @_;
+
+	my ($partid) = $part =~ m/(\d+)$/;
+	$partid = int($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;
+
+    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