Unix Power ToolsUnix Power ToolsSearch this book

44.7. Loopback Mounts

Some platforms provide the capability to mount a file as if it were a block device (like a disk partition (Section 44.4)). This allows mounting a file as if it were a hard disk, CD-ROM, or any other physical media. The primary advantage to this is that it's a simple way to create or work with a floppy, Zip, or CD-ROM image without needing the physical device. You can mount a CD image without having to burn an actual CD or manipulate a floppy boot image. Of course, different platforms call it different things and use different tools.

Mounting file images on Linux uses the loop device and is called a loop mount or a loopback mount. To mount an existing image as a filesystem, use the loop option to mount:

% mount -t iso9660 -o loop image.iso /mnt
% ls /mnt

To create a new image, you first create an empty file of the correct size (this is effectively creating a partition (Section 44.4) -- in this case, a 100 megabyte image. You then attach the image to one of the available loop device and use mkfs to create a new filesystem in the image. Then you can mount the image normally. In this example, we'll release the loop device we had to allocate specifically and let the mount find an available loop device automatically.

% dd if=/dev/zero of=image.file bs=1k count=100000
% losetup /dev/loop image.file
% mkfs -c /dev/loop 100000
% losetup -d /dev/loop
% mount -o loop image.file /mnt

FreeBSD has a similar capability, called vnode disks, with very similar syntax, but you use /dev/vn instead of /dev/loop and vnconfig instead of losetup. See FreeBSD's vnconfig(8) manpage.

Solaris also has loop devices as of Solaris 8. The device is /dev/lofi instead of /dev/loop, and you use lofiadm to configure it. See Solaris's lofiadm(1M) and lofi(7D) manpages.

FreeBSD and Solaris don't provide an equivalent to the loop option to mount; instead you just use vnconfig or lofiadm to explicitly associate a particular block device with the file and mount the specific block device just like any other device.

-- DJPH



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.