The loop devices are a possibility under Linux to mount regular files. It is often used to mount an iso image, but it can also be used to play with Raid, LVM, or a file system.
To use the loop devices it is mandatory to load the loop module: ”
modprobe loop”. This will create the loop devices /dev/loop0, /dev/loop1, etc.
Remark: work inside a tmpfs like /dev/shm to gain speed when running tests.
Creating a file-system out of a file, plus being able to mount it, works as follow:
- Create a file of a hundred mega-bytes:
dd bs=1M count=100 if=/dev/zero of=output
- Format the file:
- Mount the file with a loop device:
mount -o loop output loop-mount
- When using the option ”
-o loop” the first available loop device is taken. It is possible to select the loop device to use by giving the option ”
mount”. It displays a line like that one:
/dev/shm/output on /dev/shm/loop-mount type ext2 (rw,loop=/dev/loop0)It is possible to create files and directories within loop-mount, then unmount the device and remount it back.
Using loop devices is very convenient when training with Raid for instance. To create usable devices for a Raid, it works as for a file-system, except that instead of mounting the file as a loop device they must be setup with the tool ”
losetup” that is part of the Linux utilities. In fact, it is not possible to prepare devices for a Raid that are already mounted, and
losetup makes a loop device in /dev/ ready to be mounted in a way that it is possible to run ”
mount /dev/loop0 mount-point” instead of ”
mount -o loop file mount-point”.
Preparing a Raid5 works as follow:
- Create 3 files:
dd bs=1M count=100 if=/dev/zero of=0
dd bs=1M count=100 if=/dev/zero of=1
dd bs=1M count=100 if=/dev/zero of=2
- Format the files:
- Setup loop devices:
losetup /dev/loop0 0
losetup /dev/loop1 1
losetup /dev/loop2 2
- Prepare the Raid5 device:
mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/loop0 /dev/loop1 /dev/loop2
- Mount the Raid5 device:
mount /dev/md0 mount-point