Loop equipment (Loop-back devices)

Source: Internet
Author: User
The loopback device (' loopback device ') allows the user to virtual a block device with a normal disk file. Imagine a disk device in which all read and write operations are redirected to read and write a normal file named Disk-image instead of the track and sector of the actual disk or partition. (Of course, the disk-image must exist on an actual disk, which must be larger than the virtual disk capacity.) The loopback device allows you to use an ordinary file in this way.

The loop equipment is named/dev/loop0,/dev/loop1 and so on. Each device can be virtual one block device. Note Only super users have permission to set loopback devices.

The use of loopback devices is the same as any other block device. In particular, you can create a file system on this device and mount it on the system like a regular disk. Such a file system that keeps all content in a common file is called the virtual file system, which is not a common use. VFS usually requires, for example, the code hierarchy in the Linux kernel about the abstraction of the file system, and so on.

You can create a virtual file system and mount it through a loopback device by following these steps:

Create an empty file to host the virtual file system. The size of this file becomes the size of the file system after it is mounted.
The simple way to create a specified size file is through the DD command. This command copies data from one file to another in block units (typically 512 bytes). /dev/zero files are a good source of data.
To create a MB-wide file named Disk-image, you can use the following command:
% DD If=/dev/zero of=/tmp/disk-image count=20480
20480+0 Records in
20480+0 Records out
% ls-l/tmp/disk-image
-RW-RW----1 root 10485760 Mar 8 01:56/tmp/disk-image
The newly created file is filled with 0 bytes. Before mounting, a file system must be established on it. This process establishes a number of control units for organizing and storing files and constructs the root directory structure.
You can build any type of file system on top of this disk image. Take the example of creating a Ext2 file System (Ext2 is the most common file system in a Linux system) that can be done with MKE2FS. Because this command is typically operated on a block device, it asks for confirmation when operating on a normal file:
% Mke2fs-q/tmp/disk-image
Mke2fs 1.18, 11-nov-1999 for EXT2 FS 0.5b, 95/08/09
Disk-image is isn't a block special device.
Proceed anyway? (y,n) Y
Here the-q parameter is used to omit the output of the profile for the newly established file system. If you want to see this information, omit this argument.
The Disk-image file now contains a newly created file system, just like a disk that has been initialized to a size of MB.
Mount the file system with a loopback device. The method is to use the Mount command to specify that the disk files are mounted devices. also specifies Loop=loopback-device as a parameter to the-o option, telling the Mount command which loopback device to use.
The commands in the following example can be used to mount our Disk-image file system. Keep in mind that only super users can use loopback devices. The first command creates a directory/tmp/virtual-fs that will be used to mount our filesystem.
% MKDIR/TMP/VIRTUAL-FS
% Mount-o Loopback=/dev/loop0/tmp/disk-image/tmp/virtual-fs
At this point, this device should have been mounted, just like a normal 10M disk.
% df-h/tmp/virtual-fs
FileSystem Size Used Avail use% mounted on
/tmp/disk-image 9.7M 13k 9.2M 0%/tmp/virtual-fs
You can use this device like any other disk:
% CD/TMP/VIRTUAL-FS
% Echo ' Hello, world! ' > Test.txt
% Ls-l
Total 19
Drwxr-xr-x 2 root 12288 Mar 8 02:00 Lost+found
-RW-RW----1 root 8 02:12 test.txt
% Cat Test.txt
Hello, world!.
Note that Lost+found is a folder that is automatically created by MKE2FS once the file system is corrupted and some of the data is returned but not associated with any files, it will be placed in this folder.
After you finish using, uninstall the file system:
% cd/tmp
% UMOUNT/TMP/VIRTUAL-FS
You can delete the disk-image, or mount it again later and use the files in it. You can even copy this file to a remote host and mount it there, using--the contents of the file system are not affected at all.
In addition to creating a new file system, you can also copy an image from an existing file system. For example, you can create an image as a CD-ROM by using normal copy exercises.

If you have a CD-ROM drive with an IDE interface in your system, use the device name mentioned previously, such as/dev/hda. If the CD-ROM is a SCSI interface, the device file name may be/dev/scd0 or something. The system may also contain a symbolic link/dev/cdrom to the actual optical drive. Refer to the/etc/fstab (translator's note: manual) to see which devices the system actually uses for the optical drive.

The next thing to do is to copy the device to a file-the resulting file will be a full image of the CD-ROM content on the hard drive. For example:

% Cp/dev/cdrom/tmp/cdrom-image
Depending on the speed of the device and the capacity of the CD-ROM, this operation may take several minutes. The final file will be quite large, with the same volume as the CD-ROM.

At this point, you can mount the disc in the system without inserting the original CD-ROM disc. To be mounted in the/mnt/cdrom directory:

Mount-o Loopback=/dev/loop0/tmp/cdrom-image/mnt/cdrom
Because the image is on disk, the file system will be much faster than when it is mounted directly from the actual optical drive. Please note that most of the file system types used by the CDs are iso9660.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.