From 27d320d6f9219cc4238d3bf69e8f66185f123e7e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 19 Feb 2009 18:36:25 +0100 Subject: [PATCH] losetup: detach more devices by "-d [ ..]" This patch allows to detach more devices by "losetup -d", for example: # losetup -a /dev/loop0: [0804]:9955739 (/home/images/ary0.img) /dev/loop1: [0804]:9955740 (/home/images/ary1.img) /dev/loop2: [0804]:9955745 (/home/images/ary2.img) # losetup -d /dev/loop0 /dev/loop1 /dev/loop2 Signed-off-by: Karel Zak --- mount/lomount.c | 33 +++++++++++++++++++-------------- mount/losetup.8 | 4 +++- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/mount/lomount.c b/mount/lomount.c index ece0003e..93bcb466 100644 --- a/mount/lomount.c +++ b/mount/lomount.c @@ -807,23 +807,27 @@ set_loop(const char *device, const char *file, unsigned long long offset, int del_loop (const char *device) { - int fd; + int fd, errsv; if ((fd = open (device, O_RDONLY)) < 0) { - int errsv = errno; - fprintf(stderr, _("loop: can't delete device %s: %s\n"), - device, strerror (errsv)); - return 1; + errsv = errno; + goto error; } if (ioctl (fd, LOOP_CLR_FD, 0) < 0) { - perror ("ioctl: LOOP_CLR_FD"); - close(fd); - return 1; + errsv = errno; + goto error; } close (fd); if (verbose > 1) printf(_("del_loop(%s): success\n"), device); return 0; + +error: + fprintf(stderr, _("loop: can't delete device %s: %s\n"), + device, strerror(errsv)); + if (fd >= 0) + close(fd); + return 1; } #else /* no LOOP_SET_FD defined */ @@ -868,7 +872,7 @@ usage(void) { fprintf(stderr, _("\nUsage:\n" " %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 [ ...] delete\n" " %1$s -f | --find find unused\n" " %1$s -j | --associated [-o ] list all associated with \n" " %1$s [ options ] {-f|--find|loopdev} setup\n"), @@ -971,7 +975,7 @@ main(int argc, char **argv) { if (argc == 1) { usage(); } else if (delete) { - if (argc != optind+1 || encryption || offset || sizelimit || + if (argc < optind+1 || encryption || offset || sizelimit || find || all || showdev || assoc || ro) usage(); } else if (find) { @@ -1009,7 +1013,7 @@ main(int argc, char **argv) { return 0; } file = argv[optind]; - } else { + } else if (!delete) { device = argv[optind]; if (argc == optind+1) file = NULL; @@ -1017,9 +1021,10 @@ main(int argc, char **argv) { file = argv[optind+1]; } - if (delete) - res = del_loop(device); - else if (file == NULL) + if (delete) { + while (optind < argc) + res += del_loop(argv[optind++]); + } else if (file == NULL) res = show_loop(device); else { if (passfd && sscanf(passfd, "%d", &pfd) != 1) diff --git a/mount/losetup.8 b/mount/losetup.8 index 84f82e7a..17910189 100644 --- a/mount/losetup.8 +++ b/mount/losetup.8 @@ -19,6 +19,8 @@ Delete loop: .in +5 .B "losetup \-d" .I loop_device +.RB [ \fIloop_device\fP +.RB ...] .sp .in -5 Print name of first unused loop device: @@ -71,7 +73,7 @@ and finds the module that knows how to perform that encryption. .IP "\fB\-a, \-\-all\fP" show status of all loop devices .IP "\fB\-d, \-\-detach\fP" -detach the file or device associated with the specified loop device +detach the file or device associated with the specified loop device(s) .IP "\fB\-e, \-E, \-\-encryption \fIencryption_type\fP" enable data encryption with specified name or number .IP "\fB\-f, \-\-find\fP" -- 2.39.5