How to Create loopback device in Linux

Source: Internet
Author: User

How to Create loopback device in Linux

Have you ever thought about creating another file system in the file management system of Linux, just like creating a new disk partition in Windows (but you don't have to do it directly ). In this case, you need to use the loop device ).

In Linux, the loopback device allows you to virtualize a block device with a common disk file. Imagine a disk device. All its read and write operations will be redirected to the reading and writing of a common file named virtualfs, rather than operating the track and sector of the actual disk or partition.

Of course, you can obtain the virtualfs name at will, but this file must exist on an actual disk, and the actual disk must have a larger capacity than the virtual disk (virtualfs. The loopback device allows you to use a common file like this. With a loopback device in Linux, one can easily create a Linux loopback file system on a regular disk file, and not directly using a disk partition!

The loopback device is used in the same way as any other block device. In particular, you can create a file system on this device and mount it to the system like a normal disk. Such a file system that stores all content in a common file is called a virtual file system ).

The following describes how to create a Linux loopback file system with a regular disk file.

[Step 1] Create an empty file that carries the virtual file system. The file size will be the size of the mounted file system. To create a file of the specified size, run the dd command. This command copies data from one file to another in block units (usually 512 bytes, or you can customize the block size. The/dev/zero file is a good data source.

To create a 30 MB (zero-filled) file named virtualfs in the root directory (root directory), run the following command:

$ Sudo dd if =/dev/zero of =/virtualfs bs = 1024 count = 30720

You can use the following command to view the file you just created:

$ Ls-l/virtualfs

[Step 2] The loop0 and/dev/loop1 devices are named respectively. Each device can Virtualize a block device. To confirm whether the current system is using a loopback device, use the following statement:

$ Losetup/dev/loop0

If/dev/loop0 already exists, you will get a result similar to the following. Then you need to replace/dev/loop0 with/dev/loop1, or/dev/loop1 with/dev/loop2, and so on, until an empty loop device is found.

Assume that/dev/loop0 is not in use, as shown in:

[Step 3] use the losetup command to associate a regular file or block device (/dev/loop0) with a loop file (virtualfs. Note that only Super Users have the permission to set the loopback device.

$ Sudo losetup/dev/loop0/virtualfs

In this step, you can enter echo $? To confirm the previous step is completed successfully without error. When source code compilation or command execution fails to confirm whether the executed command is successfully executed, we all use echo $? . If the returned value is 0, the execution is successful. If the returned value is a value other than 0, the execution fails.

This actually utilizes the principle of "process lifecycle": when a process is completed, this process will call a routine named _ exit to notify the kernel that it is ready for "extinction. The process provides an exit code (an integer) to indicate the reason for the process to exit. By convention, 0 indicates a normal or "successful" termination. That is to say, we are executing echo $? The reverse return value is the exit code of the process. In addition, this exit code is provided to the system kernel by the process that has just been executed.

[Step 4] Next, you need to create a Linux EXT3 File System (with 1% reserved block count) on the loopback device. The file is already associated with a common disk file. Enter:

$ Sudo mkfs-t ext3-m 1-v/dev/loop0

As shown in:

In this way, we create another file system in the existing file system, and this process is not directly partitioned by the disk.

Step 5: Create a folder in/mnt as the mount point.

$ Sudo mkdir/mnt/vfs

Next, mount the loop File System (which is actually a common Disk File) to the directory created above (/mnt/vfs ), this completes the creation of a "regular" Linux EXT3 file system. To do this, enter

$ Sudo mount-t ext3/dev/loop0/mnt/vfs

As shown in:

The so-called "Mount" means to use a directory as the entry point and place the data in the partition slot of the disk under this directory. That is to say, you can read the partition slot from this directory). This action is called "Mount", and the directory of the entry point is called "mount point 』. Since the root directory is the most important part of the Linux system, the root directory must be mounted to a split slot. Other directories can be mounted to different split slots as needed.

Now, all Linux File System-related commands can be used in the file system created above. For example, you can enter df-h to confirm its "disk usage", or enter tune2fs-l/dev/loop0 to output its file system settings. For example, in the following example, we create a folder tmp_dir in it, and then use ls to list the content in this disk. Note that lost + found is a folder automatically created by mkfs. Once the file system is damaged, some data is replied but not associated with any files, and will be placed in this folder.

Note: Enter sudo echo 'Hello world! '> Test.txt is not allowed, which causes Permission denied. Because The redirection (>) is done by the shell before sudo is even started. You need to use The command shown in the figure above. In addition, to use nano to create or edit a file, you must also use the superuser permission, that is, sudo nano test.txt. Otherwise, the file may not be saved.

[Step 6] the file system needs to be uninstalled after it is used up. In fact, you can delete a file or mount it again and use the file. You can even copy this file to a remote host, mount it there, and use the File System content without any impact.

As mentioned above, losetup is used to create and control loop files and associate regular files or block devices with a loop file. However, when-d is used, the file or device associated with the loop device is deleted. So first enter

$ Sudo losetup-d/dev/loop0

Then use the following command

$ Sudo umount/mnt/vfs

You can unmount the mounted loop file system.

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.