losetup: set blocksize before partition scan
Subsystems (2)
PATCH 1/1 losetup: set blocksize before partition scan
When creating a loop device the user may use the --partscan option to request that the kernel scan for partitions. To correctly scan for partitions the kernel must first have the correct blocksize.
Also, update an error message to add --partscan to the list of options only allowed during loop device setup.
--- disk-utils/partx.c | 2 +- include/loopdev.h | 2 +- lib/loopdev.c | 13 ++++++++++++- libmount/src/context_loopdev.c | 2 +- sys-utils/losetup.c | 11 ++++++----- 5 files changed, 21 insertions(+), 9 deletions(-)
disk-utils/partx.c +1 -1
@@ -125,7 +125,7 @@ static void assoc_loopdev(const char *fname)if (loopcxt_set_backing_file (&lc, fname))err (EXIT_FAILURE ,_ ("%s: failed to set backing file" ), fname); - rc =loopcxt_setup_device (&lc); + rc =loopcxt_setup_device (&lc,0 );if (rc == -EBUSY )err (EXIT_FAILURE ,_ ("%s: failed to set up loop device" ), fname);
include/loopdev.h +1 -1
@@ -163,7 +163,7 @@ extern int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags);extern int loopcxt_deinit_iterator (struct loopdev_cxt *lc);extern int loopcxt_next (struct loopdev_cxt *lc); -extern int loopcxt_setup_device (struct loopdev_cxt *lc); +extern int loopcxt_setup_device (struct loopdev_cxt *lc,uint64_t blocksize);extern int loopcxt_set_status (struct loopdev_cxt *lc);extern int loopcxt_delete_device (struct loopdev_cxt *lc);extern int loopcxt_set_capacity (struct loopdev_cxt *lc);
lib/loopdev.c +12 -1
@@ -1256,7 +1256,7 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)* * Returns: <0 on error, 0 on success. */ -int loopcxt_setup_device (struct loopdev_cxt *lc) +int loopcxt_setup_device (struct loopdev_cxt *lc,uint64_t blocksize) {int file_fd, dev_fd, mode =O_RDWR , rc =-1 , cnt =0 ;int errsv =0 ;
@@ -1331,6 +1331,17 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)DBG (SETUP ,ul_debugobj (lc,"LOOP_SET_FD: OK" )); +if (blocksize >0 ) { +if (ioctl (dev_fd,LOOP_SET_BLOCK_SIZE , (unsigned long ) blocksize) <0 ) { + rc = -errno; + errsv = errno; +DBG (SETUP ,ul_debugobj (lc,"LOOP_SET_BLOCK_SIZE failed: %m" )); + goto err; + } + +DBG (SETUP ,ul_debugobj (lc,"LOOP_SET_BLOCK_SIZE: OK" )); + } +if (ioctl (dev_fd,LOOP_SET_STATUS64 , &lc->info)) { rc = -errno; errsv = errno;
libmount/src/context_loopdev.c +1 -1
@@ -336,7 +336,7 @@ int mnt_context_setup_loopdev(struct libmnt_context *cxt) }/* setup the device */ - rc =loopcxt_setup_device (&lc); + rc =loopcxt_setup_device (&lc,0 );if (!rc)break ;/* success */
sys-utils/losetup.c +6 -5
@@ -468,8 +468,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) +int nooverlap,int lo_flags,int flags,const char *file, +uint64_t offset,uint64_t sizelimit,uint64_t blocksize) {int hasdev =loopcxt_has_device (lc);int rc =0 ;
@@ -562,7 +562,7 @@ static int create_loop(struct loopdev_cxt *lc,break ; } errno =0 ; - rc =loopcxt_setup_device (lc); + rc =loopcxt_setup_device (lc, blocksize);if (rc ==0 )break ;/* success */ if (errno ==EBUSY && !hasdev)
@@ -819,7 +819,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))
@@ -831,7 +831,8 @@ 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));
Signature · 1 line
2.17.1
Karel Zak
maintainerQuoted 3 lines · Jeffrey Ferreira
When creating a loop device the user may use the --partscan option to request that the kernel scan for partitions. To correctly scan for partitions the kernel must first have the correct blocksize.
Really good catch. Thanks!
I have implemented it in a little different way to follow internal API from lib/loopdev.c.
Quoted 2 lines
Also, update an error message to add --partscan to the list of options only allowed during loop device setup.
Merged this part.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com