]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Use /dev/loop-control if it exists
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 3 Jul 2015 12:10:17 +0000 (14:10 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 14 Aug 2015 17:23:34 +0000 (13:23 -0400)
Loop devices can be added on the fly when needed, they're
not always created beforehand. The loop-control device can
be used to find and allocate the next available number
instead of going through the /dev directory contents (which
is now only a fallback mechanism).

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/lxc/bdev.c

index 29f4f21e0150f6f10fa562e3af21ad93195c9520..3dfab73ab3e1712656594cdb6169a894d0f6b7ac 100644 (file)
@@ -1874,7 +1874,7 @@ static int loop_detect(const char *path)
        return 0;
 }
 
-static int find_free_loopdev(int *retfd, char *namep)
+static int find_free_loopdev_no_control(int *retfd, char *namep)
 {
        struct dirent dirent, *direntp;
        struct loop_info64 lo;
@@ -1914,6 +1914,26 @@ static int find_free_loopdev(int *retfd, char *namep)
        return 0;
 }
 
+static int find_free_loopdev(int *retfd, char *namep)
+{
+       int rc, fd = -1;
+       int ctl = open("/dev/loop-control", O_RDWR);
+       if (ctl < 0)
+               return find_free_loopdev_no_control(retfd, namep);
+       rc = ioctl(ctl, LOOP_CTL_GET_FREE);
+       if (rc >= 0) {
+               snprintf(namep, 100, "/dev/loop%d", rc);
+               fd = open(namep, O_RDWR);
+       }
+       close(ctl);
+       if (fd == -1) {
+               ERROR("No loop device found");
+               return -1;
+       }
+       *retfd = fd;
+       return 0;
+}
+
 static int loop_mount(struct bdev *bdev)
 {
        int lfd, ffd = -1, ret = -1;