From 34f9b684c0b77030b8d318b5949b60e790d1b654 Mon Sep 17 00:00:00 2001 From: Petr Uzel Date: Fri, 30 Sep 2011 12:05:26 +0200 Subject: losetup: new -D option to delete all used loop devices Signed-off-by: Petr Uzel Signed-off-by: Karel Zak --- mount/lomount.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'mount/lomount.c') diff --git a/mount/lomount.c b/mount/lomount.c index 441860b71..2ce30abb0 100644 --- a/mount/lomount.c +++ b/mount/lomount.c @@ -938,6 +938,32 @@ set_loop(const char *device, const char *file, unsigned long long offset, return 0; } +static int +delete_all_devices (void) +{ + struct looplist ll; + int fd; + int ok = 0; + + if (looplist_open(&ll, LLFLG_USEDONLY) == -1) { + error(_("%s: /dev directory does not exist."), progname); + return 1; + } + + while((fd = looplist_next(&ll)) != -1) { + close(fd); + ok |= del_loop(ll.name); + } + looplist_close(&ll); + + if (!ll.ct_succ && ll.ct_perm) { + error(_("%s: no permission to look at /dev/loop%s"), progname, + (ll.flag & LLFLG_SUBDIR) ? "/" : ""); + return 1; + } + return ok; +} + int del_loop (const char *device) { int fd, errsv; @@ -1015,6 +1041,7 @@ usage(FILE *out) { _(" %1$s loop_device give info\n" " %1$s -a | --all list all used\n" " %1$s -d | --detach [ ...] delete\n" + " %1$s -D | --detach-all delete all used\n" " %1$s -f | --find find unused\n" " %1$s -c | --set-capacity resize\n" " %1$s -j | --associated [-o ] list all associated with \n" @@ -1037,7 +1064,7 @@ usage(FILE *out) { int main(int argc, char **argv) { char *p, *offset, *sizelimit, *encryption, *passfd, *device, *file, *assoc; - int delete, find, c, all, capacity; + int delete, delete_all, find, c, all, capacity; int res = 0; int showdev = 0; int ro = 0; @@ -1048,6 +1075,7 @@ main(int argc, char **argv) { { "all", 0, 0, 'a' }, { "set-capacity", 0, 0, 'c' }, { "detach", 0, 0, 'd' }, + { "detach-all", 0, 0, 'D' }, { "encryption", 1, 0, 'e' }, { "find", 0, 0, 'f' }, { "help", 0, 0, 'h' }, @@ -1065,14 +1093,14 @@ main(int argc, char **argv) { bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - capacity = delete = find = all = 0; + capacity = delete = delete_all = find = all = 0; assoc = offset = sizelimit = encryption = passfd = NULL; progname = argv[0]; if ((p = strrchr(progname, '/')) != NULL) progname = p+1; - while ((c = getopt_long(argc, argv, "acde:E:fhj:o:p:rsv", + while ((c = getopt_long(argc, argv, "acdDe:E:fhj:o:p:rsv", longopts, NULL)) != -1) { switch (c) { case 'a': @@ -1087,6 +1115,9 @@ main(int argc, char **argv) { case 'd': delete = 1; break; + case 'D': + delete_all = 1; + break; case 'E': case 'e': encryption = optarg; @@ -1128,10 +1159,14 @@ main(int argc, char **argv) { if (argc < optind+1 || encryption || offset || sizelimit || capacity || find || all || showdev || assoc || ro) usage(stderr); + } else if (delete_all) { + if (argc > optind || encryption || offset || sizelimit || + capacity || find || all || showdev || assoc || ro) + usage(stderr); } else if (find) { if (capacity || all || assoc || argc < optind || argc > optind+1) usage(stderr); - } else if (all) { + } else if (all || delete_all) { /* only -v is allowed */ if ((argc == 3 && verbose == 0) || argc > 3) usage(stderr); @@ -1159,6 +1194,8 @@ main(int argc, char **argv) { if (all) return show_used_loop_devices(); + else if (delete_all) + return delete_all_devices(); else if (assoc) return show_associated_loop_devices(assoc, off, offset ? 1 : 0); else if (find) { -- cgit v1.2.3-55-g7522