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=/filesystem.img bs=1k seek=1M count=1
mkfs.xfs /filesystem.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 /filesystem.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 (/filesystem.img)

Create the filesystem

1. Now lets create a ext4 filesystem on the loopback device.

# mkfs.xfs /filesystem.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.

# losetup -d /dev/loop0

3. Finally remove the file “/root/loopbackfile.img” used to create the loop device.

# rm /root/loopbackfile.img

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).