This tip (or more appropriately tips) cover the Linux loop device. The loop device is a kernel module that can be used to mount a file as a file system. This is most commonly used to mount ISO image files, but it can be used for other files as you will soon see.
Since the later tips rely on the fact that the loop device is actually loaded into the kernel, let's explore how we can determine whether or not it is actually loaded.
One obvious indicator that the module is not loaded is the lack of any loop device special files in
/dev, which would become evident if you tried to mount an ISO file to a mount point.
# mount -o loop RHEL52-DVD.iso /mnt/image mount: could not find any device /dev/loop#
/dev/loop*device special files.
# ls /dev/loop* ls: /dev/loop*: No such file or directory
# lsmod | grep loop
In the previous example we illustrated what would occur if the loop device was not mounted. Well, in order to continue this tip we need to load the loop device. How is this accomplished?
# modprobe loop
Is the kernel module loaded?
# lsmod | grep loop loop 19145 0
/dev/loop* devices created?
# ls /dev/loop* /dev/loop0 /dev/loop2 /dev/loop4 /dev/loop6 /dev/loop1 /dev/loop3 /dev/loop5 /dev/loop7
Once you have downloaded an ISO image to your Linux machine, you can mount it as a loop device. This will give you access to the files in the ISO without you first having to burn it to a CD or DVD.
For example if you would like to mount
/mnt/isoimage you would run the following commands:
# mkdir /mnt/isoimage
# mount -o loop -t iso9660 FILENAME.iso /mnt/isoimage
In addition, you can create file systems within a file!
# dd if=/dev/zero of=/tmp/loopfs.img bs=1024 count=10000 10000+0 records in 10000+0 records out
# file /tmp/loopfs.img /tmp/loopfs.img: data
losetup(8) command, associate our new file with a loop device.
# losetup /dev/loop0 /tmp/loopfs.img
# file -s /dev/loop0 /dev/loop0: data
# mkfs -t ext3 /dev/loop0 mke2fs 1.35 (28-Feb-2004) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 2512 inodes, 10000 blocks 500 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=10485760 2 block groups 8192 blocks per group, 8192 fragments per group 1256 inodes per group Superblock backups stored on blocks: 8193 Writing inode tables: done Creating journal (1024 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
# mkdir /mnt/image && mount /dev/loop0 /mnt/image
# mount | grep loop0 /dev/loop0 on /mnt/image type ext3 (rw)
loop? mount option.
# umount /mnt/image && losetup -d /dev/loop0
# mount -o loop /tmp/loopfs.img /mnt/image
# mount | grep loopfs /tmp/loopfs.img on /mnt/image type ext3 (rw,loop=/dev/loop0)
# cd /mnt/image
# touch myNewFile
# ls -l total 22 drwxr-xr-x 3 root root 1024 Dec 24 02:01 . drwxr-xr-x 5 root root 4096 Dec 24 01:41 .. drwx------ 2 root root 12288 Dec 24 02:00 lost+found -rw-r--r-- 1 root root 0 Dec 24 02:01 myNewFile
We hope that you've found this tip useful. If you have any questions, comments, or suggestions for future tips, please contact us.