Create additional loop devices in Linux

In Unix-like operating systems, Loop device is a pseudo-device that makes a file accessible as a block device. When you mount a file as partition, it would use one loopback device.

I faced a loop device related issue this morning, that’s why I create this article to share some ways to bypass limitation of the loop devices in Linux.
The error is caused by my creating a Xen domU image:

[root@xen ~]# xm create ubuntu1
Using config file "/etc/xen/ubuntu1".
Error: Device 2049 (vbd) could not be connected. Failed to find an unused loop device

Sometimes when mounting iso files, you would say similar error messages (especially when you wanna mount more than 8 ISO files):

mount: could not find any free loop device

This is because your host kernel is limited in the number of loopback devices it can have mounted at any one time. The default limit is 8 on common linux distributions like RHEL, CentOS and Debian. You can check it like this:

[xcaw@linux ~]$ ls /dev/loop*
/dev/loop0  /dev/loop2  /dev/loop4  /dev/loop6
/dev/loop1  /dev/loop3  /dev/loop5  /dev/loop7

The amount of loopback devices is adjustable. To increase this value, you need to tell the kernel and ask him for more loopback devices.

There are three ways to do so:

  1. If you have the “loop” driver compiled into your kernel, you need to add “max_loop=64” to your kernel boot arguments (the “append=” line in lilo.conf, or to the end of the “kernel” line in grub). Reboot is needed to make it take effect.
  2. If you have the “loop” driver compiled as a module, you need to insmod it with parameter “max_loop=64“. On Debian systems, you can edit /etc/modules.conf and add “options loop max_loop=64“, and run these two commands: “rmmod loop“, “modprobe loop“.
  3. If you’re using devfs, then stop here. The new /dev/loop* will appear automatically. If not, you need to edit the system command MAKEDEV, make the following changes:
                 for part in `seq 0 63` # Original line is "for part in 0 1 2 3 4 5 6 7" #
                         makedev loop$part b 7 $part $disk

    And run “MAKEDEV loop”.

Now the issue get fixed. For further queries, please raise a thread at Admon Community.

Leave a comment

