summaryrefslogtreecommitdiffstats
path: root/mount/lomount.c
diff options
context:
space:
mode:
authorKarel Zak2007-06-18 15:44:02 +0200
committerKarel Zak2007-06-18 15:44:02 +0200
commitba3809b01233e6afb24e90018700b3f145c2aee7 (patch)
tree9bd037d95f4df8be1bfd37ceb7c973821845db3a /mount/lomount.c
parenttests: add mount /dev/symlink test (diff)
downloadkernel-qcow2-util-linux-ba3809b01233e6afb24e90018700b3f145c2aee7.tar.gz
kernel-qcow2-util-linux-ba3809b01233e6afb24e90018700b3f145c2aee7.tar.xz
kernel-qcow2-util-linux-ba3809b01233e6afb24e90018700b3f145c2aee7.zip
losetup: add a new option -s
The losetup provides an -f option to discover an unused loop device. As implemented, it is racy. The problem is that -f prints the loop device to stdout only when used standalone. This means a script has to do: lodev=$(losetup -f) losetup $lodev $filename which is racy if another script may be running. This patch add a new option '-s'. The '-s' option prints device name if the -f option and a file argument are present. For example: lodev=$(losetup -f -s $filename) Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'mount/lomount.c')
-rw-r--r--mount/lomount.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/mount/lomount.c b/mount/lomount.c
index 88214f71c..4774a8539 100644
--- a/mount/lomount.c
+++ b/mount/lomount.c
@@ -444,7 +444,7 @@ usage(void) {
" %1$s -f # find unused\n"
" %1$s -a # list all used\n"
" %1$s -r # read-only loop\n"
- " %1$s [-e encryption] [-o offset] [-r] {-f|loop_device} file # setup\n"),
+ " %1$s [-e encryption] [-o offset] [-r] {-f [-s] |loop_device} file # setup\n"),
progname);
exit(1);
}
@@ -481,6 +481,7 @@ main(int argc, char **argv) {
char *p, *offset, *encryption, *passfd, *device, *file;
int delete, find, c, all;
int res = 0;
+ int showdev = 0;
int ro = 0;
int pfd = -1;
unsigned long long off;
@@ -497,7 +498,7 @@ main(int argc, char **argv) {
if ((p = strrchr(progname, '/')) != NULL)
progname = p+1;
- while ((c = getopt(argc, argv, "ade:E:fo:p:vr")) != -1) {
+ while ((c = getopt(argc, argv, "ade:E:fo:p:vrs")) != -1) {
switch (c) {
case 'a':
all = 1;
@@ -521,6 +522,9 @@ main(int argc, char **argv) {
case 'p':
passfd = optarg;
break;
+ case 's':
+ showdev = 1;
+ break;
case 'v':
verbose = 1;
break;
@@ -532,7 +536,7 @@ main(int argc, char **argv) {
if (argc == 1) {
usage();
} else if (delete) {
- if (argc != optind+1 || encryption || offset || find || all)
+ if (argc != optind+1 || encryption || offset || find || all || showdev)
usage();
} else if (find) {
if (all || argc < optind || argc > optind+1)
@@ -576,6 +580,9 @@ main(int argc, char **argv) {
if (passfd && sscanf(passfd, "%d", &pfd) != 1)
usage();
res = set_loop(device, file, off, encryption, pfd, &ro);
+ if (res == 0 && showdev && find) {
+ printf("%s\n", device);
+ }
}
return res;
}