What are loopback devices, how can I create more and how can I use them?
Environment
- Red Hat Enterprise Linux
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 4
- Red Hat Enterprise Linux 3
Issue
- What are loopback devices?
- How can I more loopback devices be made available?
- How can loopback devices be used?
Resolution
-
A loopback device is used to access filesystems that are not associated with a block device (Hard Disk drives and CD-ROM drives are examples of block devices).
-
For example, an ISO image (like the ones available for download from Red Hat Customer Portal ) is a file that contains a filesystem image. Linux uses the combination of a loopback device and a special device file to enable the filesystem image to be mounted and read as if it were a real disk or block device.
-
The rest of this article assumes that the system is using the 2.4 or 2.6 series kernels and that it has the loopback capability as a module. Red Hat Enterprise Linux versions until including version 5 use the kernel module
loop, for version RHEL6 the support is directly part of the kernel ('CONFIG_BLK_DEV_LOOP=y' in /boot/config-`uname -r`). -
By default there are 8 devices available, named
/dev/loop0through to/dev/loop7. -
For RHEL7 or later, please also refer to the following KCS:
Creating additional loopback device 'nodes'
-
If there is ever a need to create additional devices, this can be done by following these steps:
-
List the loop devices:
# ls -l /dev/loop* brw-r----- 1 root disk 7, 0 Jul 24 17:49 /dev/loop0 brw-r----- 1 root disk 7, 1 Jul 24 17:49 /dev/loop1 brw-r----- 1 root disk 7, 2 Jul 24 17:49 /dev/loop2 brw-r----- 1 root disk 7, 3 Jul 24 17:49 /dev/loop3 brw-r----- 1 root disk 7, 4 Jul 24 17:49 /dev/loop4 brw-r----- 1 root disk 7, 5 Jul 24 17:49 /dev/loop5 brw-r----- 1 root disk 7, 6 Jul 24 17:49 /dev/loop6 brw-r----- 1 root disk 7, 7 Jul 24 17:49 /dev/loop7 -
Create a new device node with the mknod program:
# mknod -m660 /dev/loop8 b 7 8Please refer to 'man mknod' for more details on the options.
-
Verify the device node has been created:
# ls -l /dev/loop8 brw-rw---- 1 root root 7, 8 Oct 3 14:54 /dev/loop8 -
Set the ownership correctly and verify the ownership:
# chown root.disk /dev/loop8 # ls -l /dev/loop8 brw-rw---- 1 root disk 7, 8 Oct 3 14:54 /dev/loop8
Extending the allowed number of allowed loopback devices
Red Hat Enterprise Linux 5 and earlier
-
If the loopback support is available as kernel module (RHEL5 and earlier) then the kernel module has to be loaded with an appropriate parameter. There is an absolute maximum of 256 devices. The default limit is set to 8 on a Red Hat Enterprise Linux 3 installation.
# modinfo loop filename: /lib/modules/2.6.9-34.0.1.ELsmp/kernel/drivers/block/loop.ko parm: max_loop:Maximum number of loop devices (1-256) license: GPL alias: block-major-7-* vermagic: 2.6.9-34.0.1.ELsmp SMP 686 REGPARM 4KSTACKS gcc-3.4 depends: -
To set the limit to 64 devices, edit the
/etc/modules.conffile, adding an options line for the loop module:# cat /etc/modules.conf ... options loop max_loop=64 ... -
When the loop module is loaded or when the system is rebooted, the new setting will take effect.
Red Hat Enterprise Linux 6
-
For RHEL6, which uses loopback support directly as part of the kernel, the kernel option '
max_loop' can be used. This option can be configured in/etc/grub.confand is used after a reboot of the system. -
To set the limit to 64 devices, edit the
/etc/grub.conffile, and addmax_loop=64at the end of kernel line.... kernel /vmlinuz-2.6.32-131.0.15.el6.x86_64 ro root=/dev/mapper/root rhgb quiet max_loop=64 initrd /initramfs-2.6.32-131.0.15.el6.x86_64.img
Using loopback devices
-
The
losetupprogram can be used to create mappings between files and loopback devices, as well as display existing mappings and remove mappings. -
Create a 64MB sparse file, have
losetupmap it to the next free loopback device, display all mappings, create a filesystem and mount the device:# dd if=/dev/zero of=file bs=1 count=1 seek=64M 1+0 records in 1+0 records out 1 byte (1 B) copied, 5.2385e-05 s, 19.1 kB/s # losetup -f file # losetup -a /dev/loop0: [fd00]:3177 (/root/file) # mkfs.ext4 /dev/loop0 mke2fs 1.41.12 (17-May-2010) [.....] # mount /dev/loop0 /mnt
Another Approach
-
Verify CONFIG_BLK_DEV_LOOP built into kernel.
#grep -i loop /boot/config-`uname -r` CONFIG_BLK_DEV_LOOP=y <======== loop device not a module it's built into the kernel CONFIG_BLK_DEV_CRYPTOLOOP=m CONFIG_LOOPBACK_TARGET=m -
Append this line grub.conf
default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux (2.6.32-279.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=/dev/mapper/vg_dhcp21051-lv_root rd_NO_LUKS\ rd_LVM_LV=vg_dhcp21051/lv_root LANG=en_US.UTF-8 rd_LVM_LV=vg_dhcp21051/lv_swap \ rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us \ rd_NO_DM rhgb quiet max_loop=64 <============== -
reboot
ls /dev/loop* /dev/loop0 /dev/loop12 /dev/loop16 /dev/loop2 /dev/loop23 /dev/loop27 /dev/loop30 /dev/loop34 /dev/loop38 /dev/loop41 /dev/loop45 /dev/loop49 /dev/loop52 /dev/loop56 /dev/loop6 /dev/loop63 /dev/loop1 /dev/loop13 /dev/loop17 /dev/loop20 /dev/loop24 /dev/loop28 /dev/loop31 /dev/loop35 /dev/loop39
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.