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