]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
losetup: properly use --sector-size when create a new device
authorKarel Zak <kzak@redhat.com>
Tue, 22 Jan 2019 10:50:20 +0000 (11:50 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 22 Jan 2019 10:50:20 +0000 (11:50 +0100)
The --partscan functionality depends on sector size. Make sure
sector size is set before we force kernel to scan the device for
partitions. For example:

 losetup -f loopfile --sector-size 4KiB --partscan --show

where 'loopfile' contains GPT with 4096 sectors.

Reported-by: Jeffrey Ferreira <jeffpferreira@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/losetup.8
sys-utils/losetup.c

index c31d7475f4d8eee861b4d428a2e99fdef53d7151..1195c697eacc9da5beb54a1712f79d2ea3bd4508 100644 (file)
@@ -124,7 +124,9 @@ Force the loop driver to reread the size of the file associated with the
 specified loop device.
 .TP
 .BR \-P , " \-\-partscan"
-Force the kernel to scan the partition table on a newly created loop device.
+Force the kernel to scan the partition table on a newly created loop device.  Note that the
+partition table parsing depends on sector sizes.  The default is sector size is 512 bytes,
+otherwise you need to use use the option \fB\-\-sector\-size\fR together with \fB\-\-partscan\fR.
 .TP
 .BR \-r , " \-\-read\-only"
 Set up a read-only loop device.
index 06feb8377f1806cd36a60ff22196ffc37cb520fb..6fc5d51cdeadc4965def38ec8d0c9042216aa1e6 100644 (file)
@@ -472,7 +472,8 @@ static void warn_size(const char *filename, uint64_t size, uint64_t offset, int
 
 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;
@@ -560,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;
@@ -834,13 +838,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, offset, flags);
-                       if (set_dio || set_blocksize)
-                               goto lo_set_post;
+                       if (set_dio)
+                               goto lo_set_dio;
                }
                break;
        case A_DELETE:
@@ -893,20 +898,17 @@ int main(int argc, char **argv)
                                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_ioctl_dio(&lc, use_dio);
-                       if (res)
-                               warn(_("%s: set direct io failed"),
-                                       loopcxt_get_device(&lc));
-               }
-               if (set_blocksize) {
-                       res = loopcxt_ioctl_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"));