[BusyBox] [PATCH] Read-only option for losetup.c

Rob Landley rob at landley.net
Sun Apr 10 13:33:50 UTC 2005


The losetup command always opens its argument read/write.  Sometimes it needs
to open it read only, and there's no -r option.  This patch adds one, at a
cost of 8 bytes in losetup_main, plus more for the description and such.

The way I noticed this is I appended a squashfs partition to the end of a User
Mode Linux executable.  In order to use that squashfs as the UML instance's
root partition, I and made an initramfs that mounts the host filesystem, and
tries to attach a loop device to /proc/self/exe at the appropriate offset. 
(The offset the squashfs starts at is written at the 4 bytes beginning at byte
12, by the way, which is the end of 6 bytes of ELF header padding.)

If losetup doesn't open the file read-only, it gets the error "text file busy"
and barfs.  When the mount command is told -o ro, it does indeed open the file
read-only and life is good, except in that case there's no way to specify an
offset.

If this patch is considered too much bloat to losetup, I can fix this to
read-only fallback sanely in my mount rewrite (which already took a weed
whacker to libbb/loop.c, which is the common code this calls).  Probably a
better general solution, but the retry code is probably going to be about as
much bloat as extra losetup option.

The point of losetup is to be able to specify the loop setup in more detail
than the mount command can, and right now it can't specify everything mount
can...

Rob

--- busybox.old/util-linux/losetup.c	2005-03-11 02:20:21.000000000 -0500
+++ busybox/util-linux/losetup.c	2005-04-08 23:09:56.000000000 -0400
@@ -29,11 +29,10 @@
 {
   int delete = 0;
   int offset = 0;
-  int opt;
+  int readonly = 0;
 
-  while ((opt = getopt (argc, argv, "do:")) != -1)
-    switch (opt)
-      {
+  for(;;) {
+    switch (getopt(argc, argv, "do:r")) {
       case 'd':
 	delete = 1;
 	break;
@@ -41,19 +40,26 @@
       case 'o':
 	offset = bb_xparse_number (optarg, NULL);
 	break;
-
+	
+      case 'r':
+	readonly=1;
+	break;
+	
+      case -1:
+	goto done;
+	
       default:
 	bb_show_usage();
       }
-
+  }
+done:
   if ((delete && (offset || optind + 1 != argc))
       || (!delete && optind + 2 != argc))
     bb_show_usage();
 
-  opt = 0;
   if (delete)
     return del_loop (argv[optind]) ? EXIT_SUCCESS : EXIT_FAILURE;
   else
-    return set_loop (argv[optind], argv[optind + 1], offset, &opt)
+    return set_loop (argv[optind], argv[optind + 1], offset, &readonly)
       ? EXIT_FAILURE : EXIT_SUCCESS;
 }

--- busybox.old/include/usage.h	2005-03-11 02:20:16.000000000 -0500
+++ busybox/include/usage.h	2005-04-10 09:32:22.000000000 -0400
@@ -1478,7 +1478,8 @@
 	"Associate LOOPDEVICE with FILE.\n\n" \
 	"Options:\n" \
 	"\t-d\t\tDisassociate LOOPDEVICE.\n" \
-	"\t-o OFFSET\tStart OFFSET bytes into FILE."
+	"\t-o OFFSET\tStart OFFSET bytes into FILE." \
+	"\t-r\t\tRead Only.\n"
 
 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
   #define USAGE_LS_TIMESTAMPS(a) a



More information about the busybox mailing list