Index: util-linux-ng-2.12r+git20070330/mount/lomount.c =================================================================== --- util-linux-ng-2.12r+git20070330.orig/mount/lomount.c +++ util-linux-ng-2.12r+git20070330/mount/lomount.c @@ -341,8 +341,15 @@ set_loop(const char *device, const char } if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { - perror("ioctl: LOOP_SET_FD"); - return 1; + switch (errno) { + case EBUSY: + if (verbose) + perror("ioctl: LOOP_SET_FD"); + return 2; + default: + perror("ioctl: LOOP_SET_FD"); + return 1; + } } close (ffd); Index: util-linux-ng-2.12r+git20070330/mount/mount.c =================================================================== --- util-linux-ng-2.12r+git20070330.orig/mount/mount.c +++ util-linux-ng-2.12r+git20070330/mount/mount.c @@ -712,20 +712,34 @@ loop_check(const char **spec, const char printf(_("mount: skipping the setup of a loop device\n")); } else { int loopro = (*flags & MS_RDONLY); + int res; - if (!*loopdev || !**loopdev) - *loopdev = find_unused_loop_device(); - if (!*loopdev) - return EX_SYSERR; /* no more loop devices */ - if (verbose) - printf(_("mount: going to use the loop device %s\n"), *loopdev); - offset = opt_offset ? strtoull(opt_offset, NULL, 0) : 0; - if (set_loop(*loopdev, *loopfile, offset, - opt_encryption, pfd, &loopro)) { + do { + if (!*loopdev || !**loopdev) + *loopdev = find_unused_loop_device(); + if (!*loopdev) + return EX_SYSERR; /* no more loop devices */ if (verbose) - printf(_("mount: failed setting up loop device\n")); - return EX_FAIL; - } + printf(_("mount: going to use the loop device %s\n"), *loopdev); + offset = opt_offset ? strtoull(opt_offset, NULL, 0) : 0; + if (res = set_loop (*loopdev, *loopfile, offset, + opt_encryption, pfd, &loopro)) { + switch(res) { + case 2: + /* loop dev has been grabbed by some other process, + try again */ + if (verbose) + printf("mount: stolen loop=%s ...trying again\n", *loopdev); + *loopdev = NULL; + continue; + default: + if (verbose) + printf(_("mount: failed setting up loop device\n")); + return EX_FAIL; + } + } + } while (!*loopdev); + if (verbose > 1) printf(_("mount: setup loop device successfully\n")); *spec = *loopdev;