]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/losetup.c
script: add note about --log-in and passwords
[thirdparty/util-linux.git] / sys-utils / losetup.c
index 670bce2e393803a8c7fe12956ae8d1d45de8baf9..379b95f174cf8b6c492968e8815424f1f3c3f5d3 100644 (file)
@@ -141,8 +141,7 @@ static int printf_loopdev(struct loopdev_cxt *lc)
 
                if (loopcxt_get_sizelimit(lc, &x) == 0 && x)
                                printf(_(", sizelimit %ju"), x);
-               printf("\n");
-               return 0;
+               goto done;
        }
 
        printf("%s: [%04d]:%" PRIu64 " (%s)",
@@ -162,6 +161,9 @@ static int printf_loopdev(struct loopdev_cxt *lc)
                if (e && *e)
                        printf(_(", encryption %s (type %u)"), e, type);
        }
+
+done:
+       free(fname);
        printf("\n");
        return 0;
 }
@@ -417,7 +419,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(USAGE_SEPARATOR, out);
        fputs(_(" -o, --offset <num>            start at offset <num> into file\n"), out);
        fputs(_("     --sizelimit <num>         device is limited to <num> bytes of the file\n"), out);
-       fputs(_(" -b  --sector-size <num>       set the logical sector size to <num>\n"), out);
+       fputs(_(" -b, --sector-size <num>       set the logical sector size to <num>\n"), out);
        fputs(_(" -P, --partscan                create a partitioned loop device\n"), out);
        fputs(_(" -r, --read-only               set up a read-only loop device\n"), out);
        fputs(_("     --direct-io[=<on|off>]    open backing file with O_DIRECT\n"), out);
@@ -445,7 +447,7 @@ static void __attribute__((__noreturn__)) usage(void)
        exit(EXIT_SUCCESS);
 }
 
-static void warn_size(const char *filename, uint64_t size)
+static void warn_size(const char *filename, uint64_t size, uint64_t offset, int flags)
 {
        struct stat st;
 
@@ -453,6 +455,9 @@ static void warn_size(const char *filename, uint64_t size)
                if (stat(filename, &st) || S_ISBLK(st.st_mode))
                        return;
                size = st.st_size;
+
+               if (flags & LOOPDEV_FL_OFFSET)
+                       size -= offset;
        }
 
        if (size < 512)
@@ -467,7 +472,8 @@ static void warn_size(const char *filename, uint64_t size)
 
 static int create_loop(struct loopdev_cxt *lc,
                       int nooverlap, int lo_flags, int flags,
-                      const char *file, uint64_t offset, uint64_t sizelimit)
+                      const char *file, uint64_t offset, uint64_t sizelimit,
+                      uint64_t blocksize)
 {
        int hasdev = loopcxt_has_device(lc);
        int rc = 0;
@@ -503,7 +509,7 @@ static int create_loop(struct loopdev_cxt *lc,
                        }
 
                        lc->info.lo_flags &= ~LO_FLAGS_AUTOCLEAR;
-                       if (loopcxt_set_status(lc)) {
+                       if (loopcxt_ioctl_status(lc)) {
                                loopcxt_deinit(lc);
                                errx(EXIT_FAILURE, _("%s: failed to re-use loop device"), file);
                        }
@@ -555,6 +561,9 @@ static int create_loop(struct loopdev_cxt *lc,
                        loopcxt_set_sizelimit(lc, sizelimit);
                if (lo_flags)
                        loopcxt_set_flags(lc, lo_flags);
+               if (blocksize > 0)
+                       loopcxt_set_blocksize(lc, blocksize);
+
                if ((rc = loopcxt_set_backing_file(lc, file))) {
                        warn(_("%s: failed to use backing file"), file);
                        break;
@@ -633,7 +642,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        if (loopcxt_init(&lc, 0))
                err(EXIT_FAILURE, _("failed to initialize loopcxt"));
@@ -674,9 +683,6 @@ int main(int argc, char **argv)
                case 'f':
                        act = A_FIND_FREE;
                        break;
-               case 'h':
-                       usage();
-                       break;
                case 'J':
                        json = 1;
                        break;
@@ -721,18 +727,23 @@ int main(int argc, char **argv)
                        break;
                case 'v':
                        break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
                case OPT_SIZELIMIT:                     /* --sizelimit */
                        sizelimit = strtosize_or_err(optarg, _("failed to parse size"));
                        flags |= LOOPDEV_FL_SIZELIMIT;
                         break;
+
+               case 'h':
+                       usage();
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                default:
                        errtryhelp(EXIT_FAILURE);
                }
        }
 
+       ul_path_init_debug();
+       ul_sysfs_init_debug();
+
        /* default is --list --all */
        if (argc == 1) {
                act = A_SHOW;
@@ -762,6 +773,9 @@ int main(int argc, char **argv)
                 */
                act = A_CREATE;
                file = argv[optind++];
+
+               if (optind < argc)
+                       errx(EXIT_FAILURE, _("unexpected arguments"));
        }
 
        if (list && !act && optind == argc)
@@ -811,7 +825,7 @@ int main(int argc, char **argv)
            (sizelimit || lo_flags || showdev))
                errx(EXIT_FAILURE,
                        _("the options %s are allowed during loop device setup only"),
-                       "--{sizelimit,read-only,show}");
+                       "--{sizelimit,partscan,read-only,show}");
 
        if ((flags & LOOPDEV_FL_OFFSET) &&
            act != A_CREATE && (act != A_SHOW || !file))
@@ -823,13 +837,14 @@ int main(int argc, char **argv)
 
        switch (act) {
        case A_CREATE:
-               res = create_loop(&lc, no_overlap, lo_flags, flags, file, offset, sizelimit);
+               res = create_loop(&lc, no_overlap, lo_flags, flags, file,
+                                 offset, sizelimit, blocksize);
                if (res == 0) {
                        if (showdev)
                                printf("%s\n", loopcxt_get_device(&lc));
-                       warn_size(file, sizelimit);
-                       if (set_dio || set_blocksize)
-                               goto lo_set_post;
+                       warn_size(file, sizelimit, offset, flags);
+                       if (set_dio)
+                               goto lo_set_dio;
                }
                break;
        case A_DELETE:
@@ -876,26 +891,23 @@ int main(int argc, char **argv)
                        warn("%s", loopcxt_get_device(&lc));
                break;
        case A_SET_CAPACITY:
-               res = loopcxt_set_capacity(&lc);
+               res = loopcxt_ioctl_capacity(&lc);
                if (res)
                        warn(_("%s: set capacity failed"),
                                loopcxt_get_device(&lc));
                break;
        case A_SET_DIRECT_IO:
+lo_set_dio:
+               res = loopcxt_ioctl_dio(&lc, use_dio);
+               if (res)
+                       warn(_("%s: set direct io failed"),
+                               loopcxt_get_device(&lc));
+               break;
        case A_SET_BLOCKSIZE:
- lo_set_post:
-               if (set_dio) {
-                       res = loopcxt_set_dio(&lc, use_dio);
-                       if (res)
-                               warn(_("%s: set direct io failed"),
-                                       loopcxt_get_device(&lc));
-               }
-               if (set_blocksize) {
-                       res = loopcxt_set_blocksize(&lc, blocksize);
-                       if (res)
-                               warn(_("%s: set logical block size failed"),
-                                       loopcxt_get_device(&lc));
-               }
+               res = loopcxt_ioctl_blocksize(&lc, blocksize);
+               if (res)
+                       warn(_("%s: set logical block size failed"),
+                               loopcxt_get_device(&lc));
                break;
        default:
                warnx(_("bad usage"));