[lxc-devel] [distrobuilder/master] vm: Ensure loop device is unmounted before creating image

monstermunchkin on Github lxc-bot at linuxcontainers.org
Wed Feb 19 07:50:50 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 460 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200218/775e836a/attachment.bin>
-------------- next part --------------
From fd97da5615893036a0c0e0291b19d9b1e65a46b9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 19 Feb 2020 08:37:28 +0100
Subject: [PATCH] vm: Ensure loop device is unmounted before creating image

This ensures that the VM directory and loop device are unmounted before
creating the image.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main_lxd.go | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/distrobuilder/main_lxd.go b/distrobuilder/main_lxd.go
index 38efc123..59ca2e22 100644
--- a/distrobuilder/main_lxd.go
+++ b/distrobuilder/main_lxd.go
@@ -203,9 +203,11 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
 
 	rootfsDir := overlayDir
 	var mounts []shared.ChrootMount
+	var vmDir string
+	var vm *vm
 
 	if c.flagVM {
-		vmDir := filepath.Join(c.global.flagCacheDir, "vm")
+		vmDir = filepath.Join(c.global.flagCacheDir, "vm")
 
 		err := os.Mkdir(vmDir, 0755)
 		if err != nil {
@@ -219,7 +221,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
 
 		imgFile := filepath.Join(c.global.flagCacheDir, imgFilename)
 
-		vm, err := newVM(imgFile, vmDir, c.global.definition.Targets.LXD.VM.Filesystem, c.global.definition.Targets.LXD.VM.Size)
+		vm, err = newVM(imgFile, vmDir, c.global.definition.Targets.LXD.VM.Filesystem, c.global.definition.Targets.LXD.VM.Size)
 		if err != nil {
 			return errors.Wrap(err, "Failed to instanciate VM")
 		}
@@ -249,12 +251,12 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
 		if err != nil {
 			return errors.Wrap(err, "failed to mount root partion")
 		}
+		defer lxd.RunCommand("umount", "-R", vmDir)
 
 		err = vm.createUEFIFS()
 		if err != nil {
 			return errors.Wrap(err, "Failed to create UEFI filesystem")
 		}
-		defer lxd.RunCommand("umount", "-R", vmDir)
 
 		err = vm.mountUEFIPartition()
 		if err != nil {
@@ -299,6 +301,19 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
 
 	exitChroot()
 
+	// Unmount VM directory and loop device before creating the image.
+	if c.flagVM {
+		_, err := lxd.RunCommand("umount", "-R", vmDir)
+		if err != nil {
+			return err
+		}
+
+		err = vm.umountImage()
+		if err != nil {
+			return err
+		}
+	}
+
 	err = img.Build(c.flagType == "unified", c.flagCompression, c.flagVM)
 	if err != nil {
 		return fmt.Errorf("Failed to create LXD image: %s", err)


More information about the lxc-devel mailing list