If losetup --find is called with parameters matching to an existing loop device, re-use this loop device instead of allocating new one. If --force is used, fall back to the old behavior. Signed-off-by: Stanislav Brabec <sbrabec@xxxxxxx> --- sys-utils/losetup.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c index 0a323a9..ef6f52b 100644 --- a/sys-utils/losetup.c +++ b/sys-utils/losetup.c @@ -682,6 +682,51 @@ int main(int argc, char **argv) { int hasdev = loopcxt_has_device(&lc); + if (!hasdev && !force) { + res = loopcxt_find_by_backing_file(&lc, + file, offset, sizelimit, + LOOPDEV_FL_OFFSET | LOOPDEV_FL_SIZELIMIT); + if (res < 0) { + loopcxt_deinit(&lc); + errx(EXIT_FAILURE, _("failed to inspect loop devices")); + } + if (res == 0) { + uint32_t lc_encrypt_type; + + res = -1; + + /* Once a loop is initialized RO, there is no + way to change its parameters. */ + if (loopcxt_is_readonly(&lc) && !(lo_flags & LO_FLAGS_READ_ONLY)) { + loopcxt_deinit(&lc); + errx(EXIT_FAILURE, _("%s: overlapping read-only loop device exists; use --force to ignore"), + loopcxt_get_device(&lc)); + } + + /* This is no more supported, but check to be + * safe. */ + if (loopcxt_get_encrypt_type(&lc, &lc_encrypt_type)) { + loopcxt_deinit(&lc); + errx(EXIT_FAILURE, _("%s: encryption check failed"), + loopcxt_get_device(&lc)); + } + if (lc_encrypt_type != LO_CRYPT_NONE) { + loopcxt_deinit(&lc); + errx(EXIT_FAILURE, _("%s: encryption check failed"), + loopcxt_get_device(&lc)); + } + + res = 0; + /* We found a device to recycle. Do not check + * for conflict. If it is conflicting, then + * user was already warned in past. */ + goto got_device; + } + loopcxt_deinit(&lc); + if (loopcxt_init(&lc, 0)) + err(EXIT_FAILURE, _("failed to initialize loopcxt")); + } + if (hasdev && !is_loopdev(loopcxt_get_device(&lc))) loopcxt_add_device(&lc); do { @@ -718,6 +763,7 @@ int main(int argc, char **argv) break; } while (hasdev == 0); + got_device: if (res == 0) { if (showdev) printf("%s\n", loopcxt_get_device(&lc)); -- 2.9.2 -- Best Regards / S pozdravem, Stanislav Brabec software developer --------------------------------------------------------------------- SUSE LINUX, s. r. o. e-mail: sbrabec@xxxxxxxx Křižíkova 148/34 (Corso IIa) tel: +49 911 7405384547 186 00 Praha 8-Karlín fax: +420 284 084 001 Czech Republic http://www.suse.cz/ PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html