Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 25068  / 2 Years ago, tue, september 6, 2022, 7:51:17

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.


More From » boot

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


[#44066] Wednesday, September 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
puccop

Total Points: 66
Total Questions: 99
Total Answers: 93

Location: Aruba
Member since Sun, Nov 27, 2022
1 Year ago
puccop questions
Thu, Feb 17, 22, 08:16, 2 Years ago
Fri, Jan 13, 23, 18:55, 1 Year ago
Fri, Jan 21, 22, 07:21, 2 Years ago
Thu, Jul 28, 22, 06:32, 2 Years ago
;