1. Introduction to loop devices in UNIX-like systems, the loop device is a pseudo-device or a simulation device. It allows us to access a file like a block device. Before use, a loop device must be connected to a file. This combination provides you with an interface to replace block special files. Therefore, if the package contains a complete file system, the file can be mounted like a disk device. 1. Introduction to loop devices
In UNIX-like systems, a loop device is a pseudo-device, or a simulation device. It allows us to access a file like a block device.
Before use, a loop device must be connected to a file. This combination provides you with an interface to replace block special files. Therefore, if the package contains a complete file system, the file can be mounted like a disk device.
As mentioned above, we often see CD or dvd iso disc image files or a floppy disk (hard disk) *. img image files. Through this loop mount method, these image files can be mounted to a directory of the current file system.
So far, we can understand the meaning of loop: for the first-level file system, it is directly installed on the physical devices of our computer; for this mounted Image file (which also contains a file system), it is built on the first-level file system. In this case, it is like a file system that is circled on the first level of the file system, so it is called loop.
In Linux, the device name of the loop device is as follows:
Ls/dev/loop *
/Dev/loop0/dev/loop2/dev/loop4/dev/loop6
/Dev/loop1/dev/loop3/dev/loop5/dev/loop7
......
For example, to mount a file containing a disk image in a directory, you need to take two steps:
Losetup/dev/loop0 disk. img # link the disk image file with the circulating device
Mount/dev/loop0/home/groad/disk_test # mount the circulating device to the disk_test directory.
After the preceding two commands, the image file is mounted to the disk_test directory as a file system. of course, you can also add files to the image.
In fact, the above two steps can be written as one step:
Mount-t minix-o loop./disk. img./disk_test
After the-o loop is specified, the losetup command of the first line is executed. A simple experiment proves that the losetup and mount commands are executed separately. we can see that the/dev/loop0 device is mounted. When we specify the-o loop parameter in the mount object, the image file is actually associated with/dev/loop1. after mounting the image file, run mount-t minix/dev/loop1 again. /disk_test:
Linux-z13e:/usr/local/share/bochs/Linux011/temp # mount-t minix/dev/loop1./test_dir/
Mount:/dev/loop1 already mounted or./test_dir/busy
Mount: according to mtab,/dev/loop1 is already mounted on/usr/local/share/bochs/Linux011/temp/test_dir
Finally, you can directly unmount umount/dev/loop0. For details about losetup, see: http://www.groad.net/bbs/read.php? Tid-2353.html
A complete test example:
1. create an empty 1 GB file:
# Dd if =/dev/zero of = loopfile. img bs = 1G count = 1
1 + 0 records in
1 + 0 records out
1073741824 bytes (1.1 GB) copied, 69.3471 s, 15.5 MB/s
2. format the file as ext4:
# Mkfs. ext4 loopfile. img
Mke2fs 1.41.11 (14-Mar-2010)
Loopfile. img is not a block special device.
Proceed anyway? (Y, n) y
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
Stride = 0 blocks, Stripe width = 0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229 376
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs-c or-I to override.
3. run the file command to view the formatted file type:
# File loopfile. img
Loopfile. img: Linux rev 1.0 ext4 filesystem data, UUID = a9dfb4a0-6653-4407-ae05-7044d92c1159 (extents) (large files) (huge files)
4. mount the above file:
# Mkdir/mnt/loopback
# Mount-o loop loopfile. img/mnt/loopback
The-o loop option of the mount command can mount any loopback file system.
The preceding mount command is equivalent to the following two commands:
# Losetup/dev/loop0 loopfile. img
# Mount/dev/loop0/mnt/loopback
Therefore, the mount-o loop has mounted the file and/dev/loop0 by default.
However, the first method (mount-o loop) is not applicable to all scenarios. For example, if we want to create a hard disk file, partition the file, and mount a subpartition, we cannot use the-o loop method. Therefore, you must do the following:
# Losetup/dev/loop1 loopfile. img
# Fdisk/dev/loop1
6. detach a mount point
# Umount/mnt/loopback