9.8. Mounting a disk image using the loop device

This section describes how to access a floppy or hard disk image within Linux using the loop device. Why would you want to do this? Let's say you have made a small Linux disk image for Bochs, and you want to install some more software on it. You have already downloaded the software onto your real system, so now you want to transfer it to the Bochs disk image. A fast way to transfer the files is to mount the disk image using the loop device.

9.8.1. ...on Linux

This section was contributed by Volker Ruppert.

Today I have made some tests with the loop device, because I want to exchange
files with the Bochs disk images.  This is what I found out:

1.  Using Floppy images is easy, because there is no partition table:

    losetup /dev/loop0 /usr/local/bochs/dlxlinux/floppya.img

    Now you can use the image like a real floppy:

    - format           : mkfs.minix /dev/loop0
    - filesystem check : fsck.minix /dev/loop0
    - mount            : mount /dev/loop0 -o loop /mnt/floppy

    Before you want to restart Bochs you must do this:

    losetup -d /dev/loop0

    Don't forget to umount before.

2.  If you want access to a hard disk image, you have to calculate the size of
    the first cylinder. This value is the offset argument for losetup.

    offset = bytes per sector * sectors per cylinder

    The command for dlxlinux image looks like this:

    losetup /dev/loop0 /usr/local/bochs/dlxlinux/hd10meg.img -o 8704

    For images created by bximage you must use the value 32256.

3.  The hard disk image access doesn't work if the image contains more than
    one partition.

4.  I have made this test with Linux and I don't know how
    this could be done with other operating systems.

9.8.2. ...on FreeBSD

This section was contributed by Alexander Schuch.

The following example mounts a Windows 95 hard disk image called Windows 95 B (2031-16-63) into the FreeBSD file system. It is specific to FreeBSD 5.x; for hints on how to do the same task on FreeBSD 4.x, or for more information in general, check the proper section of the FreeBSD handbook: Network, Memory, and File-Backed File Systems. You can use the same procedure for mounting floppy disk images.

# mdconfig -a -t vnode -f "Windows 95 B (2031-16-63)"
md0
mdconfig returns the device, your file now is accessable from.
# mount -t msdosfs /dev/md0s1 /mnt
If you already have other md devices configured, you need to substitute md0s1 with, for example, md6s1.

Once you are done working with the image, unmount the md device and detach it.

# umount /mnt
# mdconfig -d -u 0
And again, if there are other md devices configured, use the proper device number. In case you forgot the number, just ask mdconfig, like:
# mdconfig -l
md7
# mdconfig -d -u 7