Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
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 '11 at 9:13
5  
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 '11 at 14:22
    
@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 '11 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 '11 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 '11 at 7:10

1 Answer 1

up vote 5 down vote accepted

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
task
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.

share|improve this answer
    
How the /etc/init/losetup.conf invoked ??? Its not working for me... –  Imran Nov 12 '13 at 9:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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