The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4910

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
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 <stgra...@ubuntu.com>
From 04cd0e42dabdf518fab426408787b92e1dad46df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@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 <stgra...@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")
 
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to