Re: [PATCH] mount: Automagically detect and loop-mount regular files.

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

 



On Fri, Jan 08, 2010 at 04:40:51PM -0500, Adam Jackson wrote:
> Signed-off-by: Adam Jackson <ajax@xxxxxxxxxx>
> ---
>  mount/mount.c |    6 ++++++
>  1 files changed, 6 insertions(+), 0 deletions(-)
> 
> diff --git a/mount/mount.c b/mount/mount.c
> index 0cef101..054ea9a 100644
> --- a/mount/mount.c
> +++ b/mount/mount.c
> @@ -1075,6 +1075,12 @@ loop_check(const char **spec, const char **type, int *flags,
>    *loop = ((*flags & MS_LOOP) || *loopdev || opt_offset || opt_sizelimit || opt_encryption);
>    *loopfile = *spec;
>  
> +  if (!*loop) {
> +    struct stat st_buf;
> +    stat(*spec, &st_buf);
> +    *loop = S_ISREG(st_buf.st_mode);
> +  }

 Applied a little different version (see below). 
 
 The problem with this feature is that mount(2) syscall allows to use
 regular files (or whatever) as the source argument. The interpretation
 of the source argument completely depends on filesystem driver. So you
 can create FS that is able to mount regular files...

 This is reason why we should not automatically create loop devices for

    mount -t foo disk.img /mnt

 because we know nothing about 'foo'. It means that the feature will
 be used only when fylesystem type is not specified, for example:
 
    mount disk.img /mnt

 I guess it's 99% of all use cases. Thanks to Miklos for his comments.

    Karel

>From e580266914734898999f652025f9c7141023df66 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@xxxxxxxxxx>
Date: Mon, 15 Mar 2010 17:10:35 +0100
Subject: [PATCH] mount: automatically detect and loop-mount regular files

This patch allows to automatically create a loop device from a regular
file if a filesystem type is not specified, for example:

   mount /path/disk.img /mnt

If the filesystem type is specified than "-o loop" is required.

Note that there is not a restriction (on kernel side) that prevents
regular file as a mount(2) source argument. A filesystem that is able
to mount regular files could be implemented.

Based on a patch from Adam Jackson <ajax@xxxxxxxxxx>.

Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 mount/mount.8 |   36 +++++++++++++++++++++++++-----------
 mount/mount.c |   16 ++++++++++++++++
 2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/mount/mount.8 b/mount/mount.8
index 07edb5c..9675cc6 100644
--- a/mount/mount.8
+++ b/mount/mount.8
@@ -2536,18 +2536,37 @@ Since Linux version 2.1.21 xiafs is no longer part of the kernel source.
 .SH "THE LOOP DEVICE"
 One further possible type is a mount via the loop device. For example,
 the command
-
-.nf
-.B "  mount /tmp/fdimage /mnt -t vfat -o loop=/dev/loop3
-.fi
-
+.RS
+.sp
+.B "mount /tmp/disk.img /mnt -t vfat -o loop=/dev/loop"
+.sp
+.RE
 will set up the loop device
 .I /dev/loop3
 to correspond to the file
-.IR /tmp/fdimage ,
+.IR /tmp/disk.img ,
 and then mount this device on
 .IR /mnt .
 
+If no explicit loop device is mentioned
+(but just an option `\fB\-o loop\fP' is given), then
+.B mount
+will try to find some unused loop device and use that, for example
+.RS
+.sp
+.B "mount /tmp/disk.img /mnt -o loop"
+.sp
+.RE
+The mount command
+.B automatically
+creates a loop device from a regular file if a filesystem type is
+not specified, for example:
+.RS
+.sp
+.B "mount /tmp/disk.img /mnt"
+.sp
+.RE
+
 This type of mount knows about four options, namely
 .BR loop ", " offset ", " sizelimit " and " encryption ,
 that are really options to
@@ -2555,11 +2574,6 @@ that are really options to
 (These options can be used in addition to those specific
 to the filesystem type.)
 
-If no explicit loop device is mentioned
-(but just an option `\fB\-o loop\fP' is given), then
-.B mount
-will try to find some unused loop device and use that.
-
 Since Linux 2.6.25 is supported auto-destruction of loop devices and
 then any loop device allocated by
 .B mount
diff --git a/mount/mount.c b/mount/mount.c
index a73b606..6226ea7 100644
--- a/mount/mount.c
+++ b/mount/mount.c
@@ -1103,6 +1103,22 @@ loop_check(const char **spec, const char **type, int *flags,
   *loop = ((*flags & MS_LOOP) || *loopdev || opt_offset || opt_sizelimit || opt_encryption);
   *loopfile = *spec;
 
+  /* Automatically create a loop device from a regular file if a filesystem
+   * is not specified.
+   *
+   * Note that there is not a restriction (on kernel side) that prevents regular
+   * file as a mount(2) source argument. A filesystem that is able to mount
+   * regular files could be implemented.
+   *
+   * If the filesystem type is specified than "-o loop" is required to create a
+   * loop device.
+   */
+  if (!*loop && (!*type || strcmp(*type, "auto") == 0)) {
+    struct stat st;
+    if (stat(*loopfile, &st) == 0)
+      *loop = S_ISREG(st.st_mode);
+  }
+
   if (*loop) {
     *flags |= MS_LOOP;
     if (fake) {
-- 
1.6.6

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[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