I want to set up a loop device at boot time. I wonder what is the best way to do this.

I've searched for it on google. Some says adding a udev rule, some says putting a scripts in /etc/init.d, some even modifies the initramfs.

So what is the best or right way to do this? Could you please give some details (about how to do)?

Thank you.

  • I would go with /etc/init.d/something, or /etc/rc.local, but I suggest to stay with whatever method that works.
    – enzotib
    Jul 29, 2011 at 9:13
  • 7
    What for? If you want a file to be mounted at boot time, just add it to /etc/fstab with the loop option and mount will take care of configuring the loopback device for you.
    – psusi
    Jul 29, 2011 at 14:22
  • 1
    @psusi: Thank you for your advice, but for my purpose, mounting is not enough, and in fact the file cannot be simply mounted. It is a disk image rather than a partition image. And I need to know the exact name of the loop device in order to use it to make a virtual disk in VirtualBox. (I make this kind of virtual disk thinking it is easier to share files between virtual machine and the host machine. The built-in shared folder does not work for some application.)
    – Roun
    Jul 29, 2011 at 14:36
  • 4
    You can just point the virtual machine to the file rather than the loopback device. You also need to be very careful to make sure that you never run the virtual machine ( or have it suspended ) and mount the filesystem on the host at the same time, or you will corrupt the fs. For that reason, it is a much better idea to use networking to have the guest access files the host is sharing via nfs or samba or sshfs.
    – psusi
    Jul 29, 2011 at 15:55
  • @Roun: Partitions in the file can still be mounted, though you have to specify the sector offset of the partition from the beginning of the file (e.g. mount /disk.img /mnt -o loop,offset=2048 for the first partition). Offsets can be found using parted or fdisk once the loop device is set up (see my answer below for that).
    – zanfur
    Nov 21, 2011 at 7:10

2 Answers 2


There are at least three "appropriate" ways to do startup things in Ubuntu (presuming you don't merely want to mount it), listed here in decreasing order of preference:

  1. /etc/init/*.conf (Upstart)
  2. /etc/init.d/* (sysvinit)
  3. /etc/rc.local (runlevel change script)

There's also to possibility of using udev rules, but it will be convoluted. I recommend against it. The sysvinit script is overkill for such a small thing, so I'll focus on the other two options. The runlevel change script is simplest (though least preferred), so I'll start there.

The /etc/rc.local file is simply executed by the /etc/init.d/rc.local sysvinit script, which is (by default) executed last upon switching to any normal "on" runlevel (e.g. when you boot normally, but not when you shut down or go to recovery mode). To implement setting up the boot device, just add this at the end of /etc/rc.local, assuming that your file is called /srv/diskimage and you wish to use /dev/loop0 as the loop device:

losetup /dev/loop0 /srv/diskimage

That's really all there is to it. Of course, replace the filenames with whatever works best on your setup.

If you want to be a bit more specific about it, you can create an Upstart job for it that sets it up once the filesystem holding it has been mounted. An example would be the following, assuming that /srv was the mount point containing your diskimage file:

description     "Setup loop devices after filesystems are mounted"

start on mounted MOUNTPOINT=/srv
exec losetup /dev/loop0 /srv/diskimage

This should be simple enough to follow conceptually: The first line is just a comment for human readers, and it's instructions to Upstart to wait until the /srv filesystem is mounted, execute the losetup process, and consider yourself finished when the process exits successfully (a "task", as opposed to a service that is persistent). Just put those contents in a file matching /etc/init/*.conf. I would choose /etc/init/losetup.conf, myself.

The advantages of Upstart jobs over /etc/rc.local for such a simple thing are small but important. First, /etc/rc.local is deprecated, and its use as just a "messy sandbox to get things done" is frowned upon by pretty much everyone these days. Among other reasons for frowning upon its use, some programs that do use that file will simply overwrite the contents, and it's difficult to reconcile different scripts that need to go into the same file. Second, Upstart actually handles the dependency checking for you (making sure the filesystem is mounted first), so if there's a problem mounting the filesystem it won't try to set up the loop device....but will once you've resolved and mounted it. Third, Upstart is vastly more flexible: you can, for instance, have it be smart enough to find an unused loop device, and "emit" that loop device for use in other upstart jobs, similar to how we used the "MOUNTPOINT" variable in this example above.

  • How the /etc/init/losetup.conf invoked ??? Its not working for me...
    – Imran
    Nov 12, 2013 at 9:24

As @psusi pointed out, the easiest way is to add the following line into your /etc/fstab file

/path/to/loop/device       /path/to/mount/point       auto       loop       0 0

Now reboot or run mount -a as root. Then the device should be mounted. You can check that with lsblk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.