[lxc-devel] [lxd/master] VM: Wait for onStop when restarting

tomponline on Github lxc-bot at linuxcontainers.org
Mon Apr 20 16:37:58 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 345 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200420/9bfcbf85/attachment-0001.bin>
-------------- next part --------------
From d40b4416e9b1e19fd003547199df04d27acd8e3a Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 20 Apr 2020 17:35:03 +0100
Subject: [PATCH 1/3] lxd/instance/drivers/driver/qemu: Wait for onStop when
 restarting

Fixes #7223

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index 38df3d9e5c..5a57f4ba56 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -562,9 +562,7 @@ func (vm *qemu) Shutdown(timeout time.Duration) error {
 	if timeout > 0 {
 		select {
 		case <-chDisconnect:
-			op.Done(nil)
-			vm.state.Events.SendLifecycle(vm.project, "instance-shutdown", fmt.Sprintf("/1.0/virtual-machines/%s", vm.name), nil)
-			return nil
+			break
 		case <-time.After(timeout):
 			op.Done(fmt.Errorf("Instance was not shutdown after timeout"))
 			return fmt.Errorf("Instance was not shutdown after timeout")
@@ -573,6 +571,12 @@ func (vm *qemu) Shutdown(timeout time.Duration) error {
 		<-chDisconnect // Block until VM is not running if no timeout provided.
 	}
 
+	// Wait for onStop.
+	err = op.Wait()
+	if err != nil && vm.IsRunning() {
+		return err
+	}
+
 	op.Done(nil)
 	vm.state.Events.SendLifecycle(vm.project, "instance-shutdown", fmt.Sprintf("/1.0/virtual-machines/%s", vm.name), nil)
 	return nil

From 7839cf599c52e8b3b1dbb27385be365afa73ddfa Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 20 Apr 2020 17:36:54 +0100
Subject: [PATCH 2/3] lxd/instance/drivers/driver/qemu: Makes onStop unexported

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index 5a57f4ba56..1e79d38cbe 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -370,7 +370,7 @@ func (vm *qemu) getMonitorEventHandler() func(event string, data map[string]inte
 				target = "reboot"
 			}
 
-			err = inst.(*qemu).OnStop(target)
+			err = inst.(*qemu).onStop(target)
 			if err != nil {
 				logger.Errorf("Failed to cleanly stop instance '%s': %v", project.Instance(inst.Project(), inst.Name()), err)
 				return
@@ -480,8 +480,8 @@ func (vm *qemu) Freeze() error {
 	return nil
 }
 
-// OnStop is run when the instance stops.
-func (vm *qemu) OnStop(target string) error {
+// onStop is run when the instance stops.
+func (vm *qemu) onStop(target string) error {
 	// Pick up the existing stop operation lock created in Stop() function.
 	op := operationlock.Get(vm.id)
 	if op != nil && op.Action() != "stop" {
@@ -1982,7 +1982,7 @@ func (vm *qemu) Stop(stateful bool) error {
 	// Wait for QEMU to exit (can take a while if pending I/O).
 	<-chDisconnect
 
-	// Wait for OnStop.
+	// Wait for onStop.
 	err = op.Wait()
 	if err != nil && vm.IsRunning() {
 		return err

From b57d956008b8bb2134e78c4e6b39b5fd0a5795c5 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 20 Apr 2020 17:37:12 +0100
Subject: [PATCH 3/3] lxd/instance/drivers/driver/qemu: Comment

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index 1e79d38cbe..99e41f78ce 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -1929,7 +1929,7 @@ func (vm *qemu) pid() (int, error) {
 	return pid, nil
 }
 
-// Stop stops the VM.
+// Stop the VM.
 func (vm *qemu) Stop(stateful bool) error {
 	// Check that we're not already stopped.
 	if !vm.IsRunning() {


More information about the lxc-devel mailing list