1. Creating the Image File
1.1 When Loop Storage Fits
Treat an ordinary file as a /dev/loopN block device: reads and writes go through the block device interface, while the backing file is still just a file on the host.
- Mounting ISO and disk images
- Local practice and stress testing
- Encrypted volumes and ciphertext images
- Simulating a cloud disk or a CSI block volume
- Exposing files read-only
- Temporary mounts
Compared with a real disk, loop device performance is affected by the filesystem hosting the backing file; the association is not persistent by default, so after a reboot you generally have to run losetup again or add it to /etc/fstab.
1.2 Creating the Image
Prepare a backing file on the host.
- Sparse file, space allocated on demand
1
| truncate -s 10G /data/tmp/disk.img
|
- Pre-allocate the full size, suitable for stress testing
1
| dd if=/dev/zero of=/data/tmp/disk.img bs=1M count=10240
|
- Pre-allocate the full size, faster than dd
1
| fallocate -l 10G /data/tmp/disk.img
|
- Specify the block size, aligned with the target filesystem’s blocks
1
| dd if=/dev/zero of=/data/tmp/disk.img bs=4k count=2621440
|
2. Associating the Loop Device
Attach the image file to /dev/loopN.
- Automatically allocate a free loop device and associate it
1
| losetup -f --show /data/tmp/disk.img
|
- Associate a specific loop device
1
| losetup /dev/loop0 /data/tmp/disk.img
|
- Specify the sector size and read-only mode when associating
1
2
3
| losetup /dev/loop0 /data/tmp/disk.img \
--sector-size 4096 \
--read-only
|
- List all associated loop devices
- Show the details of one loop device
- Look up the loop device for an image file
1
| losetup -j /data/tmp/disk.img
|
- Create XFS on the whole disk
1
| xfs_repair -n /dev/loop0
|
4. Mounting and Using
Mount the whole-disk loop device or the image file.
1
| mount /dev/loop0 /mnt/looptest
|
1
| mount -o ro /dev/loop0 /mnt/looptest
|
- Mount the image file directly
1
| mount -t xfs -o loop /data/tmp/disk.img /mnt/looptest
|
mount will find a free loop device automatically.
1
2
| findmnt /mnt/looptest
mount | grep loop0
|
5. Growing
After growing the image, update the backing file, the loop device capacity, and the filesystem size in that order.
- Grow the image file to 20G
1
| fallocate -l 20G /data/tmp/disk.img
|
- Change only the image size metadata
1
| truncate -s 20G /data/tmp/disk.img
|
- Tell the loop device to re-read the capacity
1
| xfs_growfs /mnt/looptest
|
6. Unmounting and Detaching
Release in the order mount → loop association.
1
| umount -l /mnt/looptest
|
- Kill the processes holding the mount point
1
| fuser -km /mnt/looptest
|
7. Cleanup
Detach the loop association first, then delete the image file. Do it in the wrong order and losetup -a is left with a stale entry whose backing file carries (deleted).
1
2
3
| umount /mnt/looptest
losetup -d /dev/loop0
rm -f /data/tmp/disk.img
|
- Confirm there is no leftover loop device
1
2
| losetup -a
ls /dev/loop* 2>/dev/null
|
- Clean up a
(deleted) leftover
1
2
3
| findmnt /dev/loop0
umount /mnt/looptest # 若仍挂载
losetup -d /dev/loop0
|