0b5e55
diff -up util-linux-2.23.2/include/loopdev.h.kzak util-linux-2.23.2/include/loopdev.h
0b5e55
--- util-linux-2.23.2/include/loopdev.h.kzak	2013-06-13 09:46:10.397650425 +0200
0b5e55
+++ util-linux-2.23.2/include/loopdev.h	2014-01-14 11:11:48.427643690 +0100
0b5e55
@@ -149,6 +149,7 @@ extern void loopcxt_enable_debug(struct
0b5e55
 extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
0b5e55
 				__attribute__ ((warn_unused_result));
0b5e55
 extern int loopcxt_has_device(struct loopdev_cxt *lc);
0b5e55
+extern int loopcxt_add_device(struct loopdev_cxt *lc);
0b5e55
 extern char *loopcxt_strdup_device(struct loopdev_cxt *lc);
0b5e55
 extern const char *loopcxt_get_device(struct loopdev_cxt *lc);
0b5e55
 extern struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc);
0b5e55
diff -up util-linux-2.23.2/lib/loopdev.c.kzak util-linux-2.23.2/lib/loopdev.c
0b5e55
--- util-linux-2.23.2/lib/loopdev.c.kzak	2013-07-30 11:19:20.143600300 +0200
0b5e55
+++ util-linux-2.23.2/lib/loopdev.c	2014-01-14 11:11:48.428643700 +0100
0b5e55
@@ -1298,6 +1298,36 @@ int loopcxt_delete_device(struct loopdev
0b5e55
 	return 0;
0b5e55
 }
0b5e55
 
0b5e55
+int loopcxt_add_device(struct loopdev_cxt *lc)
0b5e55
+{
0b5e55
+	int rc = -EINVAL;
0b5e55
+	int ctl, nr = -1;
0b5e55
+	const char *p, *dev = loopcxt_get_device(lc);
0b5e55
+
0b5e55
+	if (!dev)
0b5e55
+		goto done;
0b5e55
+
0b5e55
+	if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
0b5e55
+		rc = -ENOSYS;
0b5e55
+		goto done;
0b5e55
+	}
0b5e55
+
0b5e55
+	p = strrchr(dev, '/');
0b5e55
+	if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1)
0b5e55
+	       || nr < 0)
0b5e55
+		goto done;
0b5e55
+
0b5e55
+	ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
0b5e55
+	if (ctl >= 0) {
0b5e55
+		DBG(lc, loopdev_debug("add_device %d", nr));
0b5e55
+		rc = ioctl(ctl, LOOP_CTL_ADD, nr);
0b5e55
+		close(ctl);
0b5e55
+	}
0b5e55
+done:
0b5e55
+	DBG(lc, loopdev_debug("add_device done [rc=%d]", rc));
0b5e55
+	return rc;
0b5e55
+}
0b5e55
+
0b5e55
 /*
0b5e55
  * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
0b5e55
  * kernels we have to check all loop devices to found unused one.
0b5e55
diff -up util-linux-2.23.2/sys-utils/losetup.c.kzak util-linux-2.23.2/sys-utils/losetup.c
0b5e55
--- util-linux-2.23.2/sys-utils/losetup.c.kzak	2013-07-30 11:20:16.987117853 +0200
0b5e55
+++ util-linux-2.23.2/sys-utils/losetup.c	2014-01-14 11:11:48.428643700 +0100
0b5e55
@@ -600,6 +600,8 @@ int main(int argc, char **argv)
0b5e55
 	{
0b5e55
 		int hasdev = loopcxt_has_device(&lc);
0b5e55
 
0b5e55
+		if (hasdev && !is_loopdev(loopcxt_get_device(&lc)))
0b5e55
+			loopcxt_add_device(&lc);
0b5e55
 		do {
0b5e55
 			const char *errpre;
0b5e55