[PATCH] losetup: set blocksize before partition scan

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Signed-off-by: Jeffrey Ferreira <jeffpferreira@xxxxxxxxx>
---
 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(-)

diff --git a/disk-utils/partx.c b/disk-utils/partx.c
index e10efa96a..0cf7b3330 100644
--- a/disk-utils/partx.c
+++ b/disk-utils/partx.c
@@ -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);
diff --git a/include/loopdev.h b/include/loopdev.h
index 3f2cec5f5..b62df00b1 100644
--- a/include/loopdev.h
+++ b/include/loopdev.h
@@ -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);
diff --git a/lib/loopdev.c b/lib/loopdev.c
index de0d50ba2..ad56895f8 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -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;
diff --git a/libmount/src/context_loopdev.c b/libmount/src/context_loopdev.c
index c5fc80d77..b916d03b3 100644
--- a/libmount/src/context_loopdev.c
+++ b/libmount/src/context_loopdev.c
@@ -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 */
 
diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c
index 7d14f566c..ae23b3507 100644
--- a/sys-utils/losetup.c
+++ b/sys-utils/losetup.c
@@ -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));
-- 
2.17.1




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux