[pve-devel] [PATCH container v2] Fix pct skiplock

Alwin Antreich a.antreich at proxmox.com
Wed Mar 14 11:57:43 CET 2018


The method vm_start sets an environment variable that is not picked up
anymore by systemd. This patch removes the environment variable and
introduces a skiplock file that is picked up by the
lxc-pve-prestart-hook.

Signed-off-by: Alwin Antreich <a.antreich at proxmox.com>
---
note: after discussion with Fabian, I removed the ENV variable. But I left the
path of the skiplock file because we use it on other places in the code too.
Though, I added a cleanup step if the container start fails.

 src/PVE/LXC.pm            | 27 ++++++++++++++++++---------
 src/lxc-pve-prestart-hook |  5 ++++-
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 7adbcd1..acb5cfd 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -393,7 +393,7 @@ sub update_lxc_config {

     # some init scripts expect a linux terminal (turnkey).
     $raw .= "lxc.environment = TERM=linux\n";
-
+
     my $utsname = $conf->{hostname} || "CT$vmid";
     $raw .= "lxc.uts.name = $utsname\n";

@@ -454,7 +454,7 @@ sub update_lxc_config {
 	}
 	$raw .= "lxc.cgroup.cpuset.cpus = ".$cpuset->short_string()."\n";
     }
-
+
     File::Path::mkpath("$dir/rootfs");

     PVE::Tools::file_set_contents("$dir/config", $raw);
@@ -566,7 +566,7 @@ sub destroy_lxc_container {

 sub vm_stop_cleanup {
     my ($storage_cfg, $vmid, $conf, $keepActive) = @_;
-
+
     eval {
 	if (!$keepActive) {

@@ -1172,19 +1172,19 @@ sub mountpoint_mount {
     my $type = $mountpoint->{type};
     my $quota = !$snapname && !$mountpoint->{ro} && $mountpoint->{quota};
     my $mounted_dev;
-
+
     return if !$volid || !$mount;

     $mount =~ s!/+!/!g;

     my $mount_path;
     my ($mpfd, $parentfd, $last_dir);
-
+
     if (defined($rootdir)) {
 	($rootdir, $mount_path, $mpfd, $parentfd, $last_dir) =
 	    __mount_prepare_rootdir($rootdir, $mount);
     }
-
+
     my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1);

     die "unknown snapshot path for '$volid'" if !$storage && defined($snapname);
@@ -1288,7 +1288,7 @@ sub mountpoint_mount {
 	warn "cannot enable quota control for bind mounts\n" if $quota;
 	return wantarray ? ($volid, 0, undef) : $volid;
     }
-
+
     die "unsupported storage";
 }

@@ -1545,11 +1545,20 @@ sub vm_start {

     update_lxc_config($vmid, $conf);

-    local $ENV{PVE_SKIPLOCK}=1 if $skiplock;
+    my $skiplock_flag_fn = "/var/lib/lxc/$vmid/skiplock";
+
+    if ($skiplock) {
+	open(my $fh, '>', $skiplock_flag_fn) || die "failed to open $skiplock_flag_fn for writing: $!\n";
+	close($fh);
+    }

     my $cmd = ['systemctl', 'start', "pve-container\@$vmid"];

-    PVE::Tools::run_command($cmd);
+    eval { PVE::Tools::run_command($cmd); };
+    if (my $err = $@) {
+	unlink $skiplock_flag_fn if -e $skiplock_flag_fn;
+	die $err if $err;
+    }

     return;
 }
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index fd29423..eba23ee 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -57,13 +57,16 @@ __PACKAGE__->register_method ({
 	return undef if $param->{name} !~ m/^\d+$/;

 	my $vmid = $param->{name};
+	my $skiplock_flag_fn = "/var/lib/lxc/$vmid/skiplock";
+	my $skiplock = 1 if -e $skiplock_flag_fn;
+	unlink $skiplock_flag_fn if -e $skiplock_flag_fn;

 	PVE::Cluster::check_cfs_quorum(); # only start if we have quorum

 	return undef if ! -f PVE::LXC::Config->config_file($vmid);

 	my $conf = PVE::LXC::Config->load_config($vmid);
-	if (!$ENV{PVE_SKIPLOCK} && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+	if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
 	    PVE::LXC::Config->check_lock($conf);
 	}

--
2.11.0





More information about the pve-devel mailing list