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