Linux Shell Scripting Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How it works...

First we had to create a file to make a loopback filesystem. For this, we used dd, which is a generic command for copying raw data. It copies data from the file specified in the if parameter to the file specified in the of parameter. We instruct dd to copy data in blocks of size 1 GB and copy one such block, creating a 1 GB file. The /dev/zero file is a special file, which will always return 0 when you read from it.

We used the mkfts.ext4 command to create an ext4 filesystem in the file. A filesystem is needed on any device that can be mounted. Common filesystems include ext4, ext3, and vfat.

The mount command attaches the loopback file to a mountpoint (/mnt/loopback in this case). A mountpoint makes it possible for users to access the files stored on a filesystem. The mountpoint must be created using the mkdir command before executing the mount command. We pass the -o loop option to mount to tell it that we are mounting a loopback file, not a device.

When mount knows it is operating on a loopback file, it sets up a device in /dev corresponding to the loopback file and then mounts it. If we wish to do it manually, we use the losetup command to create the device and then the mount command to mount it.