Under some OperatingSystems (especially Unix-like ones), you can mount(8) a single file onto your VirtualFileSystem. There are several reasons why you might want to do this:

  • You have a cdrom ISO or other disk image, and want to access it
  • You want to have access to a particular filesystem type
  • You want to simulate a filesystem condition, such as low disk space, to test your software

Example under Linux

This is what I did to make sure to test that my software failed gracefully when disk space ran out.

1. Create a file containing a filesystem

  create a 2 MB file with an ext2 filesystem
  $ dd if=/dev/zero of=/tmp/loop.fs bs=1M count=2
  $ mke2fs -F /tmp/loop.fs

2. mount it as a loop back device (must be done as root)

  load the loop.ko kernel module if support is not compiled into the kernel
  # modprobe loop
  where I want it to be mounted
  # mount /tmp/loop.fs /mnt/directory -o loop

Now /mnt/directory will only allow around 2MB (minus filesystem overhead) of data to be written to it, and then the device will be out of space.

3. Determining Partition Offsets

If you're trying to mount a partition of a disk image that has been created via dd(1) or similar you will likely need to specify an offset to where the partition starts in the disk image. This offset can then be passed as an option to the mount command.

The offset can be determined by using fdisk to find out which sector the partition starts on and then multiplying that number by the sector size (which is also helpfully printed by fdisk). See the example below:

matt@argon:~/crcnet/ecn$ fdisk -u -l ecn-base.img
You must set cylinders.
You can do this from the extra functions menu.

Disk ecn-base.img: 0 MB, 0 bytes
4 heads, 62 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

       Device Boot      Start         End      Blocks   Id  System
ecn-base.img1              62      250727      125333   83  Linux

There is one partition, starting at sector 62 which equates to offset (62*512 = 31774) which can then be mounted like so:

matt@argon:~/crcnet/ecn$ sudo mount -o loop,offset=31744 -t ext2 ecn-base.img img/
matt@argon:~/crcnet/ecn$ cd img/
matt@argon:~/crcnet/ecn/img$ ls
bin   dev  lib         packages  root  tmp  var
boot  etc  lost+found  proc      sbin  usr  var.tgz


lib/main.php:944: Notice: PageInfo: Cannot find action page

lib/main.php:839: Notice: PageInfo: Unknown action