]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
allow site-wide customization of zfsroot and lvm vg
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Sat, 27 Apr 2013 02:59:11 +0000 (04:59 +0200)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 30 Apr 2013 14:14:30 +0000 (09:14 -0500)
/etc/lxc/lxc.conf can contain

zfsroot = custom1
lvm_vg = vg0

(Otherwise the defaults are 'lxc' for lvm_vg, and 'lxc' for zfsroot)

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

index 4ba550cf298b24a236d11afc9013fe412a2f6d9c..a3577a2074f32a68d31a9a38d98fb74e3142afc3 100644 (file)
@@ -492,34 +492,33 @@ static int zfs_clone(const char *opath, const char *npath, const char *oname,
 {
        // use the 'zfs list | grep opath' entry to get the zfsroot
        char output[MAXPATHLEN], option[MAXPATHLEN], *p;
+       const char *zfsroot = output;
        int ret;
        pid_t pid;
 
-       if (!zfs_list_entry(opath, output))
-               // default is tank.  I'd prefer lxc, but apparently this is
-               // tradition.
-               sprintf(output, "tank");
-
-       if ((p = index(output, ' ')) == NULL)
-               return -1;
-       *p = '\0';
-       if ((p = rindex(output, '/')) == NULL)
-               return -1;
-       *p = '\0';
+       if (zfs_list_entry(opath, output)) {
+               // zfsroot is output up to ' '
+               if ((p = index(output, ' ')) == NULL)
+                       return -1;
+               *p = '\0';
+               if ((p = rindex(output, '/')) == NULL)
+                       return -1;
+               *p = '\0';
+       } else
+               zfsroot = default_zfs_root();
 
        ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs",
                lxcpath, nname);
        if (ret < 0  || ret >= MAXPATHLEN)
                return -1;
 
-       // zfsroot is output up to ' '
        // zfs create -omountpoint=$lxcpath/$lxcname $zfsroot/$nname
        if (!snapshot) {
                if ((pid = fork()) < 0)
                        return -1;
                if (!pid) {
                        char dev[MAXPATHLEN];
-                       ret = snprintf(dev, MAXPATHLEN, "%s/%s", output, nname);
+                       ret = snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, nname);
                        if (ret < 0  || ret >= MAXPATHLEN)
                                exit(1);
                        execlp("zfs", "zfs", "create", option, dev, NULL);
@@ -532,11 +531,11 @@ static int zfs_clone(const char *opath, const char *npath, const char *oname,
                // zfs clone zfsroot/oname@nname zfsroot/nname
                char path1[MAXPATHLEN], path2[MAXPATHLEN];
 
-               ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", output,
+               ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", zfsroot,
                        oname, nname);
                if (ret < 0 || ret >= MAXPATHLEN)
                        return -1;
-               (void) snprintf(path2, MAXPATHLEN, "%s/%s", output, nname);
+               (void) snprintf(path2, MAXPATHLEN, "%s/%s", zfsroot, nname);
 
                // if the snapshot exists, delete it
                if ((pid = fork()) < 0)
@@ -758,18 +757,18 @@ static int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldna
                return -1;
 
        if (strcmp(orig->type, "lvm")) {
+               const char *vg;
+
                if (snap) {
                        ERROR("LVM snapshot from %s backing store is not supported",
                                orig->type);
                        return -1;
                }
-               // Use VG 'lxc' by default
-               // We will want to support custom VGs, at least as specified through
-               // /etc/lxc/lxc.conf, preferably also over cmdline
-               len = strlen("/dev/lxc/") + strlen(cname) + 1;
+               vg = default_lvm_vg();
+               len = strlen("/dev/") + strlen(vg) + strlen(cname) + 2;
                if ((new->src = malloc(len)) == NULL)
                        return -1;
-               ret = snprintf(new->src, len, "/dev/lxc/%s", cname);
+               ret = snprintf(new->src, len, "/dev/%s/%s", vg, cname);
                if (ret < 0 || ret >= len)
                        return -1;
        } else {
index 2fa2b0cc86046cb5d2248ca363dbb559198c3df4..be1ce886006899efb49bc57d321745ff4bc56d01 100644 (file)
@@ -137,7 +137,80 @@ static char *copypath(char *p)
 }
 
 char *default_lxcpath;
+#define DEFAULT_VG "lxc"
+char *default_lvmvg;
+#define DEFAULT_ZFSROOT "lxc"
+char *default_zfsroot;
 
+const char *default_lvm_vg(void)
+{
+       char buf[1024], *p;
+       FILE *fin;
+
+       if (default_lvmvg)
+               return default_lvmvg;
+
+       fin = fopen(LXC_GLOBAL_CONF, "r");
+       if (fin) {
+               while (fgets(buf, 1024, fin)) {
+                       if (buf[0] == '#')
+                               continue;
+                       p = strstr(buf, "lvm_vg");
+                       if (!p)
+                               continue;
+                       p = strchr(p, '=');
+                       if (!p)
+                               continue;
+                       p++;
+                       while (*p && (*p == ' ' || *p == '\t')) p++;
+                       if (!*p)
+                               continue;
+                       default_lvmvg = copypath(p);
+                       goto out;
+               }
+       }
+       default_lvmvg = DEFAULT_VG;
+
+out:
+       if (fin)
+               fclose(fin);
+       return default_lvmvg;
+}
+
+const char *default_zfs_root(void)
+{
+       char buf[1024], *p;
+       FILE *fin;
+
+       if (default_zfsroot)
+               return default_zfsroot;
+
+       fin = fopen(LXC_GLOBAL_CONF, "r");
+       if (fin) {
+               while (fgets(buf, 1024, fin)) {
+                       if (buf[0] == '#')
+                               continue;
+                       p = strstr(buf, "zfsroot");
+                       if (!p)
+                               continue;
+                       p = strchr(p, '=');
+                       if (!p)
+                               continue;
+                       p++;
+                       while (*p && (*p == ' ' || *p == '\t')) p++;
+                       if (!*p)
+                               continue;
+                       default_zfsroot = copypath(p);
+                       goto out;
+               }
+       }
+       default_zfsroot = DEFAULT_ZFSROOT;
+
+out:
+       if (fin)
+               fclose(fin);
+       return default_zfsroot;
+}
 const char *default_lxc_path(void)
 {
        char buf[1024], *p;
index ee463a7f8b1deb6469a44cfed9683694a596bdf5..09af34ac36a51442b883bde7fb5275175de465ef 100644 (file)
@@ -31,6 +31,8 @@ extern int mkdir_p(const char *dir, mode_t mode);
  * path.  Caller must free this buffer.
  */
 extern const char *default_lxc_path(void);
+extern const char *default_zfs_root(void);
+extern const char *default_lvm_vg(void);
 
 /**
  * BUILD_BUG_ON - break compile if a condition is true.