From 3cb2413b02ff01f535b82eb630bd4f157ed7b1c8 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 6 Aug 2013 11:04:35 +0200 Subject: losetup: use loop-control to explicitly ask for device Now we use LOOP_CTL_GET_FREE ioctl to ask for free device, for example losetup -f foo.img Unfortunately, losetup(8) allows to ask for specified device losetup /dev/loop100 foo.img and in this case we assume that the device already exists in the system. This is incorrect, we should be able to use loop-control LOOP_CTL_ADD ioctl to ask for the specified device. Signed-off-by: Karel Zak --- lib/loopdev.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'lib/loopdev.c') diff --git a/lib/loopdev.c b/lib/loopdev.c index 9789feb88..27276bfab 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -1297,6 +1297,36 @@ int loopcxt_delete_device(struct loopdev_cxt *lc) return 0; } +int loopcxt_add_device(struct loopdev_cxt *lc) +{ + int rc = -EINVAL; + int ctl, nr = -1; + const char *p, *dev = loopcxt_get_device(lc); + + if (!dev) + goto done; + + if (!(lc->flags & LOOPDEV_FL_CONTROL)) { + rc = -ENOSYS; + goto done; + } + + p = strrchr(dev, '/'); + if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1) + || nr < 0) + goto done; + + ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); + if (ctl >= 0) { + DBG(lc, loopdev_debug("add_device %d", nr)); + rc = ioctl(ctl, LOOP_CTL_ADD, nr); + close(ctl); + } +done: + DBG(lc, loopdev_debug("add_device done [rc=%d]", rc)); + return rc; +} + /* * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older * kernels we have to check all loop devices to found unused one. -- cgit v1.2.3-55-g7522