- How to create virtual block device (loop device/filesystem) in Linux
- Create a file
- Create the loop device
- Create the filesystem
- Mount the loopback filesystem
- Removing loop device
- Enable Encryption on loop filesystem
- Maximum Lopback devices allowed
- Some more articles you might also be interested in …
- 10+ losetup command examples in Linux [Cheat Sheet]
- How to use losetup command
- Different examples to use losetup command
- 1. Create a loop device with losetup
- 2. Display information about all loop devices
- 3. List all used loop devices
- 4. Detach loop devices
- 5. Detach all used devices
- 6. Find first unused device
- 7. List all devices associated with a file
- 8. Print the list in JSON format
- 9. Set up a read-only loop device
- 10. Create a loop device with the size limit
- 11. Set offset value in loop device
- 12. Create a partitioned loop device
- Conclusion
- References
How to create virtual block device (loop device/filesystem) in Linux
Linux supports a special block device called the loop device, which maps a normal file onto a virtual block device. This allows for the file to be used as a “virtual file system” inside another file. With Linux it’s possible to create a file-system inside a single file. These storage devices are available as device files such as /dev/device_name.
Create a file
1. First step is to create a file of desired size. The following command will create a file that is 1 GB in size:
# dd if=/dev/zero of=loopbackfile.img bs=100M count=10 10+0 records in 10+0 records out 1048576000 bytes (1.0 GB) copied, 1.26748 s, 827 MB/s
2. Verify the size of the file you have just created.
# du -sh loopbackfile.img 1000M loopbackfile.img
Create the loop device
1. Next step is to create a loop device with the file. Use the command “losetup” to create a loop device “loop0”
# losetup -fP loopbackfile.img
Here,
-f – find the first unused loop device. If a file argument is present, use this device. Otherwise, print its name.
-P – force kernel to scan partition table on newly created loop device.
2. To print the loop device generated using the above command use “losetup -a”.
# losetup -a /dev/loop0: [64769]:4199216 (/root/loopbackfile.img)
Create the filesystem
1. Now lets create a ext4 filesystem on the loopback device.
# mkfs.ext4 /root/loopbackfile.img mke2fs 1.42.9 (28-Dec-2013) /root/loopbackfile.img is not a block special device. Proceed anyway? (y,n) y Discarding device blocks: done Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 64000 inodes, 256000 blocks 12800 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=262144000 8 block groups 32768 blocks per group, 32768 fragments per group 8000 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done
Mount the loopback filesystem
1. We can now mount the loopback filesystem onto a directory. The “-o loop” additional option is used to mount loopback filesystems.
# mkdir /loopfs # mount -o loop /dev/loop0 /loopfs
2. Verify the size of the new mount point and type of filesystem using below commands.
# df -hP /loopfs/ Filesystem Size Used Avail Use% Mounted on /dev/loop1 969M 2.5M 900M 1% /loopfs
# mount | grep loopfs /dev/loop0 on /loopfs type ext4 (rw,relatime,seclabel,data=ordered)
Removing loop device
If you want remove the new filesystem, use the following steps:
1. Umount and delete the directory /loopfs
umount /loopfs rmdir /loopfs
2. Delete the loopback device “loop0” created using the “losetup -d” command.
3. Finally remove the file “/root/loopbackfile.img” used to create the loop device.
Enable Encryption on loop filesystem
‘losetup’ also allows to enable data encryption in order to get a crypted filesystem. The syntax to created a encrypted loop device si as shown below:
# losetup [ -e encryption ] loop_device file
The following encryption algorithms are accepted:
- NONE use no encryption (default).
- XOR use a simple XOR encryption.
- DES use DES encryption.
DES encryption is only available if the optional DES package has been added to the kernel. DES encryption uses an additional start value that is used to protect passwords against dictionary attacks.
Maximum Lopback devices allowed
You may get an error message ‘no such device’ while creating more than 8 loopback devices. This happens because You are being limited by the amount of loop devices available. Depending on your system, add ‘options loop max_loop=X’ to /etc/modprobe.conf on CentOS/RHEL 5 or create a new file named like disk.conf in directory /etc/modprobe.d on CentOS/RHEL 6. (Where X is number of loop device you need).
Some more articles you might also be interested in …
10+ losetup command examples in Linux [Cheat Sheet]
The loop device is a block device that maps its data to the blocks of a regular file or another block device. Loop devices are mounted as file systems on Unix-like operating systems.
losetup is a handy command-line utility to set up and control loop devices in Linux. It also allows you to detach loop devices and display the information of used loop devices.
How to use losetup command
The syntax to use losetup command is as follows:
Different examples to use losetup command
1. Create a loop device with losetup
You can use the losetup command to create a new loop device in Linux.
First, you need to create a file that will be used as a block device. The following command creates a file named loopfile which is 1GB in size.
$ sudo dd if=/dev/zero of=loopfile bs=100M count=10
Sample Output:
golinux@ubuntu-PC:~$ sudo dd if=/dev/zero of=loopfile bs=100M count=10 10+0 records in 10+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 1.40495 s, 746 MB/s
Next, run the losetup command specifying the loop device and file you want to map it to.
$ sudo losetup /dev/loop13 loopfile
If the loop device is already in use, you will get the following error.
losetup: loopfile: failed to set up loop device: Device or resource busy
You can use the -f option to avoid errors. The available loop device will be automatically used.
To verify whether the loop device is created with a file, run this command.
Sample Output:
golinux@ubuntu-PC:~$ losetup -j loopfile /dev/loop13: []: (/home/golinux/loopfile)
2. Display information about all loop devices
The losetup command without any arguments prints information about all used loop devices.
Sample Output:
- NAME: loop device name
- SIZELIMIT: size limit of the file in bytes
- OFFSET: offset from the beginning
- AUTOCLEAR: autoclear flag set
- RO: read-only device
- BACK-FILE: device backing file
- DIO: access backing file with direct-io
- LOG-SEC: logical sector size in bytes
3. List all used loop devices
The losetup command with -a or —all option gets the list of all used loop devices and associated files. It does not display columns in the output.
Sample Output:
4. Detach loop devices
The -d or —detach option is used to detach the file or device associated with the loop device. You can specify one or more loop devices.
These are the available loop devices on my server:
server:~ # losetup -a /dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso) /dev/loop1: [64772]:38 (/Images/OneNDS-XX-IS-20.0.0.05_dvd.iso) /dev/loop2: [64773]:131302 (//opt/secure/loopback.img)
I will detach /dev/loop2 as the other 2 devices are basically image mounted on the system so we cannot detach them using losetup command:
server:~ # losetup -d /dev/loop2 server:~ # losetup -a /dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso) /dev/loop1: [64772]:38 (/Images/Dummy-XX-IS-20.0.0.05_dvd.iso)
5. Detach all used devices
If you want to detach all loop devices, you can run the losetup command with -D or —detach-all option.
6. Find first unused device
The -f or —find option helps to find the first unused loop device.
Sample Output:
golinux@ubuntu-PC:~$ losetup -f /dev/loop13
7. List all devices associated with a file
The -j or —associated option instructs losetup to display the list of all loop devices associated with a given file.
Sample Output:
The following command prints the loop device associated with a file named blockfile in the current directory
golinux@ubuntu-PC:~$ losetup -j blockfile /dev/loop14: []: (/home/golinux/blockfile)
8. Print the list in JSON format
The -J or —json option uses the JSON output format to display all devices.
Sample Output:
9. Set up a read-only loop device
To set up a read-only loop device, you have to use the losetup command with -r or —read-only option.
The following command creates a read-only loop device using the file named loopfile .
$ sudo losetup -r /dev/loop13 loopfile
Sample Output:
10. Create a loop device with the size limit
You can set the size limit in the loop device with —sizelimit option. The following command creates a new loop device with a size limit of 200M.
$ sudo losetup --sizelimit loopdev loopfile
Sample Output:
11. Set offset value in loop device
The -o or —offset option helps to create a loop device with a specified offset value.
$ sudo losetup -o loopdev loopfile
$ sudo losetup --offset loopdev loopfile
Sample Output:
12. Create a partitioned loop device
The -P or —partscan can be used to create a partitioned loop device.
$ sudo losetup --partscan -f loopfile
Conclusion
Now you know how to set up and manage loop devices in Linux. You have also learned to delete loop devices and get detailed information about used loop devices in the system.
We hope you find this article helpful. If you have any confusion, please let us know in the comment section.
References
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!