From e580266914734898999f652025f9c7141023df66 Mon Sep 17 00:00:00 2001 From: Karel Zak 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 . Signed-off-by: Karel Zak --- 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 07edb5c2..9675cc62 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 a73b606c..6226ea72 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) { -- 2.39.5