[pve-devel] [RFC pve-container 1/2] add support to add mountpoints with pct set

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Sep 1 16:33:53 CEST 2015


made create_disk non-private
made print_ct_mountpoint include 'mp=' unless 'nomp' is set
added an mkdirs flag to mount_all
added support to mount_all to have an array specified as
  $format_raw_images to limit formatting to new disks.
create and format new disks simply by calling mount_all
  followed by umount_all
---
 src/PVE/API2/LXC.pm |  4 ++--
 src/PVE/LXC.pm      | 36 +++++++++++++++++++++++++++++-------
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 0595e35..0d5d8b8 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -54,7 +54,7 @@ my $destroy_disks = sub {
     }
 };
  
-my $create_disks = sub {
+sub create_disks {
     my ($storecfg, $vmid, $settings, $conf) = @_;
 
     my $vollist = [];
@@ -337,7 +337,7 @@ __PACKAGE__->register_method({
 		    }
 		}
 
-		$vollist = &$create_disks($storage_cfg, $vmid, $param, $conf);
+		$vollist = create_disks($storage_cfg, $vmid, $param, $conf);
 
 		PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf, $archive, $password, $restore);
 		# set some defaults
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index eb69fac..bf2a964 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -783,7 +783,7 @@ sub parse_ct_mountpoint {
 }
 
 sub print_ct_mountpoint {
-    my ($info) = @_;
+    my ($info, $nomp) = @_;
 
     my $opts = '';
 
@@ -792,6 +792,7 @@ sub print_ct_mountpoint {
     foreach my $o ('size', 'backup') {
 	$opts .= ",$o=$info->{$o}" if defined($info->{$o});
     }
+    $opts .= ",mp=$info->{mp}" if !$nomp;
 
     return "$info->{volume}$opts";
 }
@@ -1072,6 +1073,8 @@ sub update_pct_config {
 
     my @nohotplug;
 
+    my $new_disks = [];
+
     my $rootdir;
     if ($running) {
 	my $pid = find_lxc_pid($vmid);
@@ -1097,7 +1100,11 @@ sub update_pct_config {
 		next if !$running;
 		my $netid = $1;
 		PVE::Network::veth_delete("veth${vmid}i$netid");
-	    } elsif ($opt eq 'rootfs' || $opt =~ m/^mp(\d+)$/) {
+	    } elsif ($opt =~ m/^mp(\d+)$/) {
+		delete $conf->{$opt};
+		push @nohotplug, $opt;
+		next if $running;
+	    } elsif ($opt eq 'rootfs') {
 		die "implement me"
 	    } else {
 		die "implement me"
@@ -1165,7 +1172,12 @@ sub update_pct_config {
 	    } else {
 		update_net($vmid, $conf, $opt, $net, $netid, $rootdir);
 	    }
-        } elsif ($opt eq 'rootfs' || $opt =~ m/^mp(\d+)$/) {
+        } elsif ($opt =~ m/^mp(\d+)$/) {
+	    $conf->{$opt} = $value;
+	    push @$new_disks, $opt;
+	    push @nohotplug, $opt;
+	    next;
+        } elsif ($opt eq 'rootfs') {
 	    die "implement me: $opt";
 	} else {
 	    die "implement me: $opt";
@@ -1176,6 +1188,13 @@ sub update_pct_config {
     if ($running && scalar(@nohotplug)) {
 	die "unable to modify " . join(',', @nohotplug) . " while container is running\n";
     }
+
+    if (@$new_disks) {
+	my $storage_cfg = PVE::Storage::config();
+	PVE::API2::LXC::create_disks($storage_cfg, $vmid, $conf, $conf);
+	mount_all($vmid, $storage_cfg, $conf, $new_disks, 1);
+	umount_all($vmid, $storage_cfg, $conf, 0);
+    }
 }
 
 sub has_dev_console {
@@ -1753,7 +1772,7 @@ sub template_create {
 
     my $template_volid = PVE::Storage::vdisk_create_base($storecfg, $volid);
     $rootinfo->{volume} = $template_volid;
-    $conf->{rootfs} = print_ct_mountpoint($rootinfo);
+    $conf->{rootfs} = print_ct_mountpoint($rootinfo, 1);
 
     write_config($vmid, $conf);
 }
@@ -1948,7 +1967,7 @@ sub umount_all {
 }
 
 sub mount_all {
-    my ($vmid, $storage_cfg, $conf, $format_raw_images) = @_;
+    my ($vmid, $storage_cfg, $conf, $format_raw_images, $mkdirs) = @_;
 
     my $rootdir = "/var/lib/lxc/$vmid/rootfs";
 
@@ -1973,10 +1992,13 @@ sub mount_all {
 	    die "unable to mount base volume - internal error" if $isBase;
 
 	    if ($format_raw_images && $format eq 'raw') {
-		my $cmd = ['mkfs.ext4', '-O', 'mmp', $image_path];
-		PVE::Tools::run_command($cmd);
+		if (ref($format_raw_images) ne 'ARRAY' || grep {$_ eq $ms} @$format_raw_images) {
+		    my $cmd = ['mkfs.ext4', '-O', 'mmp', $image_path];
+		    PVE::Tools::run_command($cmd);
+		}
 	    }
 
+	    File::Path::make_path "$rootdir/$mount" if $mkdirs;
 	    mountpoint_mount($mountpoint, $rootdir, $storage_cfg, $loopdevs);
         });
     };
-- 
2.1.4





More information about the pve-devel mailing list