[lxc-devel] [lxd/master] extract restart logic to new instance interface function of lxc and qemu

dlemel8 on Github lxc-bot at linuxcontainers.org
Thu Oct 22 19:46:36 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201022/093e00c0/attachment.bin>
-------------- next part --------------
From 11239b22efea6000b56396335373ca94a0f2db83 Mon Sep 17 00:00:00 2001
From: Daniel Segal <dlemel8 at gmail.com>
Date: Thu, 22 Oct 2020 22:45:05 +0300
Subject: [PATCH] extract restart logic to new instance interface function of
 lxc and qemu

Signed-off-by: Daniel Segal <dlemel8 at gmail.com>
---
 lxd/instance/drivers/driver_lxc.go  | 19 +++++++++++++++++++
 lxd/instance/drivers/driver_qemu.go | 19 +++++++++++++++++++
 lxd/instance/instance_interface.go  |  1 +
 lxd/instance_state.go               | 25 ++++---------------------
 4 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index ccb8945587..b02928e5c9 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -2786,6 +2786,25 @@ func (c *lxc) Shutdown(timeout time.Duration) error {
 	return nil
 }
 
+// Restart restart the instance.
+func (c *lxc) Restart(timeout time.Duration) error {
+	if timeout == 0 {
+		if err := c.Stop(false); err != nil {
+			return err
+		}
+	} else {
+		if c.IsFrozen() {
+			return errors.New("Instance is not running")
+		}
+
+		if err := c.Shutdown(timeout * time.Second); err != nil {
+			return err
+		}
+	}
+
+	return c.Start(false)
+}
+
 // onStopNS is triggered by LXC's stop hook once a container is shutdown but before the container's
 // namespaces have been closed. The netns path of the stopped container is provided.
 func (c *lxc) onStopNS(args map[string]string) error {
diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index d1436b9263..1a8798ad9d 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -629,6 +629,25 @@ func (vm *qemu) Shutdown(timeout time.Duration) error {
 	return nil
 }
 
+// Restart restart the instance.
+func (vm *qemu) Restart(timeout time.Duration) error {
+	if timeout == 0 {
+		if err := vm.Stop(false); err != nil {
+			return err
+		}
+	} else {
+		if vm.IsFrozen() {
+			return errors.New("Instance is not running")
+		}
+
+		if err := vm.Shutdown(timeout * time.Second); err != nil {
+			return err
+		}
+	}
+
+	return vm.Start(false)
+}
+
 func (vm *qemu) ovmfPath() string {
 	if os.Getenv("LXD_OVMF_PATH") != "" {
 		return os.Getenv("LXD_OVMF_PATH")
diff --git a/lxd/instance/instance_interface.go b/lxd/instance/instance_interface.go
index e49458bd86..02f675c227 100644
--- a/lxd/instance/instance_interface.go
+++ b/lxd/instance/instance_interface.go
@@ -50,6 +50,7 @@ type Instance interface {
 	Shutdown(timeout time.Duration) error
 	Start(stateful bool) error
 	Stop(stateful bool) error
+	Restart(timeout time.Duration) error
 	Unfreeze() error
 	RegisterDevices()
 	SaveConfigFile() error
diff --git a/lxd/instance_state.go b/lxd/instance_state.go
index b0fab52b0b..dfcb4b15be 100644
--- a/lxd/instance_state.go
+++ b/lxd/instance_state.go
@@ -167,28 +167,11 @@ func containerStatePut(d *Daemon, r *http.Request) response.Response {
 				}()
 			}
 
-			if raw.Timeout == 0 || raw.Force {
-				err = c.Stop(false)
-				if err != nil {
-					return err
-				}
-			} else {
-				if c.IsFrozen() {
-					return fmt.Errorf("Instance is not running")
-				}
-
-				err = c.Shutdown(time.Duration(raw.Timeout) * time.Second)
-				if err != nil {
-					return err
-				}
-			}
-
-			err = c.Start(false)
-			if err != nil {
-				return err
+			timeout := raw.Timeout
+			if raw.Force {
+				timeout = 0
 			}
-
-			return nil
+			return c.Restart(time.Duration(timeout))
 		}
 	case shared.Freeze:
 		if !d.os.CGInfo.Supports(cgroup.Freezer, nil) {


More information about the lxc-devel mailing list