]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
snapshots: move snapshot directory
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 16 Apr 2014 19:04:45 +0000 (14:04 -0500)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 28 Apr 2014 15:18:49 +0000 (10:18 -0500)
Originally we kept snapshots under /var/lib/lxcsnaps.  If a
separate btrfs is mounted at /var/lib/lxc, then we can't
make btrfs snapshots under /var/lib/lxcsnaps.

This patch moves the default directory to /var/lib/lxc/lxcsnaps.
If /var/lib/lxcsnaps already exists, then use that.  Don't allow
any container to be used with the name 'lxcsnaps'.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h

index c90b564f8b882510a95baed196c40e14b83f2a42..1059961cc1bbd70f905f48f9a140287f46bfdfa2 100644 (file)
@@ -2823,16 +2823,42 @@ static int get_next_index(const char *lxcpath, char *cname)
        }
 }
 
+static bool get_snappath_dir(struct lxc_container *c, char *snappath)
+{
+       int ret;
+       /*
+        * If the old style snapshot path exists, use it
+        * /var/lib/lxc -> /var/lib/lxcsnaps
+        */
+       ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path);
+       if (ret < 0 || ret >= MAXPATHLEN)
+               return false;
+       if (dir_exists(snappath)) {
+               ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
+               if (ret < 0 || ret >= MAXPATHLEN)
+                       return false;
+               return true;
+       }
+
+       /*
+        * Use the new style path
+        * /var/lib/lxc -> /var/lib/lxc + /lxcsnaps/ + c->name + \0
+        */
+       ret = snprintf(snappath, MAXPATHLEN, "%s/lxcsnaps/%s", c->config_path, c->name);
+       if (ret < 0 || ret >= MAXPATHLEN)
+               return false;
+       return true;
+}
+
 static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
 {
        int i, flags, ret;
        struct lxc_container *c2;
        char snappath[MAXPATHLEN], newname[20];
 
-       // /var/lib/lxc -> /var/lib/lxcsnaps \0
-       ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
-       if (ret < 0 || ret >= MAXPATHLEN)
+       if (!get_snappath_dir(c, snappath)) {
                return -1;
+       }
        i = get_next_index(snappath, c->name);
 
        if (mkdir_p(snappath, 0755) < 0) {
@@ -2966,7 +2992,7 @@ static char *get_timestamp(char* snappath, char *name)
 static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps)
 {
        char snappath[MAXPATHLEN], path2[MAXPATHLEN];
-       int dirlen, count = 0, ret;
+       int count = 0, ret;
        struct dirent dirent, *direntp;
        struct lxc_snapshot *snaps =NULL, *nsnaps;
        DIR *dir;
@@ -2974,9 +3000,7 @@ static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **r
        if (!c || !lxcapi_is_defined(c))
                return -1;
 
-       // snappath is ${lxcpath}snaps/${lxcname}/
-       dirlen = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
-       if (dirlen < 0 || dirlen >= MAXPATHLEN) {
+       if (!get_snappath_dir(c, snappath)) {
                ERROR("path name too long");
                return -1;
        }
@@ -3044,7 +3068,7 @@ out_free:
 static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
 {
        char clonelxcpath[MAXPATHLEN];
-       int flags = 0,ret;
+       int flags = 0;
        struct lxc_container *snap, *rest;
        struct bdev *bdev;
        bool b = false;
@@ -3067,8 +3091,7 @@ static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapnam
                        return false;
                }
        }
-       ret = snprintf(clonelxcpath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
-       if (ret < 0 || ret >= MAXPATHLEN) {
+       if (!get_snappath_dir(c, clonelxcpath)) {
                bdev_put(bdev);
                return false;
        }
@@ -3284,6 +3307,12 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
 {
        struct lxc_container *c;
 
+       if (!name)
+               return NULL;
+
+       if (strcmp(name, "lxcsnaps") == 0)
+               return NULL;
+
        c = malloc(sizeof(*c));
        if (!c) {
                fprintf(stderr, "failed to malloc lxc_container\n");
index ba15ab78a29e5323c606315b5f5c5be753fc2692..946a662dcf135ea05ab3386a3f0e848d89685fe6 100644 (file)
@@ -649,7 +649,7 @@ struct lxc_container {
         * \brief Create a container snapshot.
         *
         * Assuming default paths, snapshots will be created as
-        * \c /var/lib/lxcsnaps/\<c\>/snap\<n\>
+        * \c /var/lib/lxc/lxcsnaps/\<c\>/snap\<n\>
         * where \c \<c\> represents the container name and \c \<n\>
         * represents the zero-based snapshot number.
         *
@@ -691,7 +691,7 @@ struct lxc_container {
         *  fail if the  snapshot is overlay-based, since the snapshots
         *  will pin the original container.
         * \note As an example, if the container exists as \c /var/lib/lxc/c1, snapname might be \c 'snap0'
-        *  (representing \c /var/lib/lxcsnaps/c1/snap0). If \p newname is \p c2,
+        *  (representing \c /var/lib/lxc/lxcsnaps/c1/snap0). If \p newname is \p c2,
         *  then \c snap0 will be copied to \c /var/lib/lxc/c2.
         */
        bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);