[lxc-devel] [distrobuilder/master] main: Remove rootfsDir variable

monstermunchkin on Github lxc-bot at linuxcontainers.org
Wed Mar 14 09:47:27 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 363 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180314/56a08eb2/attachment.bin>
-------------- next part --------------
From c75fa7a7787dbc7fdbbee98a4402fa55eab15d03 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 14 Mar 2018 10:45:56 +0100
Subject: [PATCH] main: Remove rootfsDir variable

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

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index 8cc5fd8..c101f21 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -74,7 +74,6 @@ type cmdGlobal struct {
 	definition *shared.Definition
 	sourceDir  string
 	targetDir  string
-	rootfsDir  string
 }
 
 func main() {
@@ -121,7 +120,8 @@ func main() {
 }
 
 func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
-	c.sourceDir = c.flagCacheDir
+	// Clean up cache directory before doing anything
+	os.RemoveAll(c.flagCacheDir)
 
 	if len(args) > 1 {
 		// Create and set target directory if provided
@@ -140,12 +140,16 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 	}
 
 	if cmd.CalledAs() == "build-dir" {
-		c.rootfsDir = c.targetDir
+		c.sourceDir = c.targetDir
 	} else {
-		c.rootfsDir = filepath.Join(c.flagCacheDir, "rootfs")
+		c.sourceDir = filepath.Join(c.flagCacheDir, "rootfs")
 	}
 
-	var err error
+	// Create source directory if it doesn't exist
+	err := os.MkdirAll(c.sourceDir, 0755)
+	if err != nil {
+		return err
+	}
 
 	// Get the image definition
 	c.definition, err = getDefinition(args[0])
@@ -174,16 +178,18 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 	}
 
 	// Download the root filesystem
-	err = downloader.Run(c.definition.Source, c.definition.Image.Release, arch, c.rootfsDir)
+	err = downloader.Run(c.definition.Source, c.definition.Image.Release, arch, c.sourceDir)
 	if err != nil {
 		return fmt.Errorf("Error while downloading source: %s", err)
 	}
 
 	// Setup the mounts and chroot into the rootfs
-	exitChroot, err := setupChroot(c.rootfsDir)
+	exitChroot, err := setupChroot(c.sourceDir)
 	if err != nil {
 		return fmt.Errorf("Failed to setup chroot: %s", err)
 	}
+	// Unmount everything and exit the chroot
+	defer exitChroot()
 
 	// Run post unpack hook
 	for _, hook := range getRunnableActions("post-unpack", c.definition) {
@@ -197,7 +203,6 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 	err = managePackages(c.definition.Packages,
 		getRunnableActions("post-update", c.definition))
 	if err != nil {
-		exitChroot()
 		return fmt.Errorf("Failed to manage packages: %s", err)
 	}
 
@@ -209,15 +214,15 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 		}
 	}
 
-	// Unmount everything and exit the chroot
-	exitChroot()
-
 	return nil
 }
 
 func (c *cmdGlobal) preRunPack(cmd *cobra.Command, args []string) error {
 	var err error
 
+	// Clean up cache directory before doing anything
+	os.RemoveAll(c.flagCacheDir)
+
 	// resolve path
 	c.sourceDir, err = filepath.Abs(args[1])
 	if err != nil {
@@ -239,9 +244,8 @@ func (c *cmdGlobal) preRunPack(cmd *cobra.Command, args []string) error {
 }
 
 func (c *cmdGlobal) postRun(cmd *cobra.Command, args []string) error {
-	// Clean up cache directory if needed. Do not clean up if the build-dir
-	// sub-command is run since the directory is needed for further actions.
-	if c.flagCleanup && cmd.CalledAs() != "build-dir" {
+	// Clean up cache directory
+	if c.flagCleanup {
 		return os.RemoveAll(c.flagCacheDir)
 	}
 


More information about the lxc-devel mailing list