#3: Get more loop devices

Posted by | Comments (3) | Trackbacks (2)

Loop devices on Linux are virtual devices which can be used to mount files like real devices. For instance to mount an ISO image you can use a loop device.

mkdir -p /mnt/myiso
mount -o loop -t iso9660 ./disk.iso /mnt/myiso

The parameter -o loop tells mount to mount this as a loop device. For each mounted loop device a block device file in /dev is used, named /dev/loopN where N is a serial number, starting at 0. Normally the first free device is used but you can also specify manually which one to use with losetup. To mount an ISO on /dev/loop3 run

losetup /dev/loop3 ./disk.iso
mount -t iso9660 /dev/loop3 /mnt/myiso

To unmount the ISO file and detatch it from /dev/loop3 run

umount /mnt/myiso
losetup -d /dev/loop3

For historical reasons you have 8 such loop devices by default, numbered from 0 to 7. If all devices are occupied, you will get the message “mount: could not find any free loop device” when trying to allocate more of them.

In current Kernel releases you can of course use more than just 8 loop devices. To get n loop devices append max_loop=n as parameter to the kernel command line of your bootloader config. Reboot and enjoy!

The second method is to build the kernel configuration directive CONFIG_BLK_DEV_LOOP as a module. You find this under

    Device Drivers  --->
        [*] Block devices  --->
            <M>   Loopback device support

The built module will be named loop. To configure this module append the following line to your modules config (normally /etc/modules.conf or one file in /etc/modprobe.d/):

options loop max_loop=n

Again, n is the number of devices to create. When you're done, reboot. If you have no loop devices mounted right now you can also just reload the module loop instead of rebooting.

All good things come in threes so there's yet another way to get more loop devices. You can create them manually with mknod. To create a ninth device run

mknod -m 660 /dev/loop8 b 7 8

The parameter -m specifies the file rights (here: -rw-rw----), /dev/loop8 is the name of the device to create and b stands for Block Device. The numbers at the end are the major and minor numbers. The first one (the major number) sets the device type and therefore which driver to use. Loop devices have the major number 7. The second digit (the minor number), is a number used internally by the driver. This can be any cipher, in this case it's the number of the loop device counted from 0.

As an alternative you can install the script MAKEDEV (yes, it's spelled all caps) which is easier to handle. Just run

MAKEDEV loop

from within the /dev directory. Personally I dislike this tool since it lacks a lot of flexibility and control but it's your decision which tool you prefer.

You should note that devices created with either mknod or MAKEDEV do not survive a reboot. If you steadily need more devices try the first or second method.

Incidentally, there's no way of setting the number of loop devices in the kernel config. This number is hardcoded in /linux/drivers/block/loop.c (C code with line numbers):

1618         if (max_loop) {
1619                 nr = max_loop;
1620                 range = max_loop;
1621         } else {
1622                 nr = 8;
1623                 range = 1UL << (MINORBITS - part_shift);
1624         }

If max_loop is not set, 8 loop devices are created by default (nr ist set to 8 on line 1622). You may hack this by changing that line but that's not a good idea. You'd run into trouble when updating the kernel.

One last thing to mention: loop devices are often also called loopback devices. This is not all wrong, even the kernel config calls it loopback. However, not to mix it up with network loopbacks (localhost, 127.0.0.1, ::1) I prefer the name loop device.

Trackbacks

Manko10 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short #Linux #Hints”, day 3: Get more #loop #devices http://bit.ly/guZcEQ

robo47 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short #Linux #Hints”, day 3: Get more #loop #devices http://bit.ly/guZcEQ

Comments

There have been 3 comments submitted yet. Add one as well!
Basti
Basti wrote on : (permalink)

Nice articles, read them all. Hope you got more readers, promoted your blog a bit on some irc channels tongue
keep on posting smile

best regards

Janek Bevendorff
Janek Bevendorff wrote on : (permalink)

Thank you!
We’ll see how many readers I get, the blog is still pretty new. Yet the record was the day before yesterday and the second best day was yesterday.

Stay tuned for more Advent calendar articles. I’ve graduated them, so more advanced articles will come later this month.

Wayne
Wayne wrote on : (permalink)

Excellent article. I learnt more about the loop device.

Write a comment:

HTML-Tags will be converted to Entities.
Textile-formatting allowed
Standard emoticons like :-) and ;-) are converted to images.
Design and Code Copyright © 2010-2017 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck