Thursday, September 23, 2010

Linux: Increase number of loop devices

It is a common practice to mount any iso files to file system and make it available in your network environment for usage like remote installation.  You may modify /etc/fstab to make these iso files mounted during system startup.

Mounting iso files consume loop devices (e.g.: /etc/loop*).  Linux kernel limits the number of loop devices to 8 by default.  If you have more than 8 iso files to mount, you will encounter an error: “mount: could not find any free loop device”.

Loop Device

Loop devices may be deployed as a module or built with Linux kernel.  It depends various Linux Distro releases.  You may use the following command to check if the loop devices are deploy with module, if not it should have built with kernel:

-bash-3.2$ /sbin/lsmod |grep -i loop
loop                   16581  0

If lsmod able to list out loop module, the loop device are deployed as module.  You may then using module configuration to increase the number of loop devices.  Or else you should use append kernel parameters to increase the number of loop devices.

Solution: loop device is a module

  1. Un-mount all files mounted to loop device
  2. Add a line in /etc/modprobe.conf to increase number of loop devices: 
      options loop max_loop=64
  3. Run
      # /sbin/rmmod loop
    to remove the loop devices module
  4. Run
      # /sbin/modprobe loop
    to re-insert the loop devices
  5. Run
      # ls /dev/loop*
    to verify loop devices has increased
  6. Run
      # mount –a
    to re-mount static file system information in /etc/fstab

Solution: loop device built into kernel

The following shows an example of change the kernel parameter in grub boot file to increase the number of loop devices:

[root@dolphin mnt]# cat /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_dolphin-root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=0
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora (2.6.33.3-85.fc13.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.33.3-85.fc13.x86_64 ro root=/dev/mapper/vg_dolphin-root \
rd_LVM_LV=vg_dolphin/root rd_LVM_LV=vg_dolphin/swap \
rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 \
SYSFONT=latarcyrheb-sun16 KEYTABLE=us \
rhgb quiet max_loop=64 initrd /initramfs-2.6.33.3-85.fc13.x86_64.img

A “max_loop=64” is appended into kernel parameter to increase the number of loop devices to 64.  You should reboot the Linux OS to make the changes take effects.

5 comments:

Hiney said...

/sbin/modprobe loop . beats rebooting.

thanks

Anonymous said...

good points and the details are more specific than elsewhere, thanks.

- Murk

suryaworldedu said...

good points! informative blog.

dlangh said...

Helpful post. The information was not correct for Ubuntu 12.04, but your post did help me understand that there were 2 ways the system loads support for loopback, either as a module or in the kernel. That tidbit led me ultimately to the right answer...

For Ubuntu 12.04, you need to edit the file /etc/default/grub and modify the line GRUB_CMDLINE_LINUX="" and insert max_loop=XX inbetween the quotes (where XX is the number of loop devices you need). Then run update-grub (as per instructions in that file) and reboot and your all set...

Unknown said...

Can also add the option in /etc/modules instead of editing the grub command. The grub command change probably won't be saved in a kernel update (depending on your distro)