[lxc-devel] [lxd/master] lxd/images: Cleanup any leftovers on startup

stgraber on Github lxc-bot at linuxcontainers.org
Mon Aug 13 04:53:01 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 702 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180813/303c37f7/attachment.bin>
-------------- next part --------------
From 04cd0e42dabdf518fab426408787b92e1dad46df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 13 Aug 2018 00:51:32 -0400
Subject: [PATCH] lxd/images: Cleanup any leftovers on startup
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As LXD may have been downloading or publishing an image as it got
restarted or crashed, it's not that unusual for there to be leftovers in
the images directory.

This adds a function which compares the content of the images directory
with what's expected to be there according to the database and deletes
any file that shouldn't be there.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/daemon.go |  3 +++
 lxd/images.go | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/lxd/daemon.go b/lxd/daemon.go
index 5d91c3069..81121278b 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -573,6 +573,9 @@ func (d *Daemon) init() error {
 	/* Log expiry */
 	d.tasks.Add(expireLogsTask(d.State()))
 
+	// Cleanup leftover images
+	pruneLeftoverImages(d)
+
 	/* Setup the proxy handler, external authentication and MAAS */
 	macaroonEndpoint := ""
 	maasAPIURL := ""
diff --git a/lxd/images.go b/lxd/images.go
index c3dfbf27a..05103fe68 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -1043,6 +1043,40 @@ func pruneExpiredImagesTask(d *Daemon) (task.Func, task.Schedule) {
 	return f, schedule
 }
 
+func pruneLeftoverImages(d *Daemon) {
+	logger.Infof("Pruning leftover image files")
+
+	// Get all images
+	images, err := d.cluster.ImagesGet(false)
+	if err != nil {
+		logger.Error("Unable to retrieve the list of images", log.Ctx{"err": err})
+		return
+	}
+
+	// Look at what's in the images directory
+	entries, err := ioutil.ReadDir(shared.VarPath("images"))
+	if err != nil {
+		logger.Error("Unable to list the images directory", log.Ctx{"err": err})
+		return
+	}
+
+	// Check and delete leftovers
+	for _, entry := range entries {
+		fp := strings.Split(entry.Name(), ".")[0]
+		if !shared.StringInSlice(fp, images) {
+			err = os.RemoveAll(shared.VarPath("images", entry.Name()))
+			if err != nil {
+				logger.Error("Unable to remove leftover image", log.Ctx{"err": err, "file": entry.Name()})
+				continue
+			}
+
+			logger.Debugf("Removed leftover image file: %s", entry.Name())
+		}
+	}
+
+	logger.Infof("Done pruning leftover image files")
+}
+
 func pruneExpiredImages(ctx context.Context, d *Daemon) {
 	logger.Infof("Pruning expired images")
 


More information about the lxc-devel mailing list