1

Note: I'm not asking about running a service (or whatever) when booting is completed (I know how to do that).

Background

System boots from an SDCard, which has the full system on it. Attached to it is either an external USB disk, or a SATA SSD – or in some cases nothing. I want to "outsource" some "stuff" to the external medium, including e.g. /var/log, if that medium is available. If it's not available, the "internal" SD-card should be used. (And if you wonder: Yes, it's a small single-board computer).

Issue

Obviously, before the Init process starts, the usual directory structure must already be present. Mounting of file systems (from /etc/fstab) is done by the kernel before that. Sticking to the example of /var/log (which is not the only one to be dealt with) and one external drive mounted as /mnt/external when present:

How can I put /var/log (e.g. via symlink) on /mnt/external if the drive is present, but put it below e.g. /mnt/local otherwise – in a safe and clean way?

2
  • For the one example you mention, you don't need a "script" and this would suffice: superuser.com/questions/449922/…
    – sawdust
    Commented Aug 18, 2015 at 19:48
  • Nope, completely different thing. It's not about the mounting in my case, but about what happens afterwards. I've got a raw idea meanwhile, but am not (yet) aware of possible implications: the right place for my stuff would be running things from /etc/init.d/mountall-bootclean.sh (where e.g. /tmp is cleaned up: this script is run immediately after all local file systems have been mounted). But it doesn't check for any *local* script to call-if-exists, so I had to modify it directly – with the danger of my modifications being gone with some system update.
    – Izzy
    Commented Aug 18, 2015 at 19:51

2 Answers 2

0

I know you are "not asking about running a service", but unfortunately the nature of the question requires you to put your script into a service, and hook it up to to proper place right after the disks are mounted.

I agree that you are not running a service, but the script wrapped in that service does not need to keep running. All you need from Linux boot is to trigger/call it at the correct moment, then exit after you've done proper setup.

I believe once you've overcome this obstacle, you will be able to finish the rest, but do let me know if you need more info.

3
  • "The script wrapped in that service" would start too late: everything you can place in /etc/rc.X will be run a while after mounting is completed. For my case, I would need to plugin to the mount process itself, which is performed by the kernel before the init process starts. So putting that script into a service cannot work. And yes, all I need from Linux is "trigger/call it at the correct moment" – but unfortunately, in my case this can't be done via the init system by simply placing a "SXY-do_my_job" file there.
    – Izzy
    Commented Aug 20, 2015 at 7:45
  • Oh, you are not using systemed then.
    – xpt
    Commented Aug 20, 2015 at 14:03
  • I'm used to the SYS-V model, and have not much played with systemd. And while SYS-V is supported by most *nix systems, the alternatives differ between "distributions": one uses systemd, the other upstart, and so on. Until it's settled what will be the "common successor" of SYS-V, I didn't want to invest to much time in the "wrong candidate" ;) // Nevertheless: the issue became rather "philosophical" meanwhile, as what I had in mind looked a bit too complicated when implementing it (too much room for errors) – so I decided for an easier alternative. Thanks nevertheless for your input!
    – Izzy
    Commented Aug 20, 2015 at 14:52
0

The correct place to do this seems to be one of:

  • /etc/init.d/checkroot-bootclean.sh: run when the root partition (/) is mounted, but before other partitions become available. This is probably the most appropriate place for the described case, as targets of symlinks from other file systems are in-place then before those get mounted – and especially e.g. an outsourced /var/log would be there when the system starts to write logs
  • /etc/init.d/mountall-bootclean.sh: run when all local file systems are mounted. This is where e.g. /tmp will be cleaned up – but here I'm not sure whether logs already need to be accessible.

Ideally, our script would be placed "between those two". And depending on the system, there might be other places.

On Debian (and derivates, as in my case) there's /etc/init/mountall.conf where mounting stuff is configured:

# This helper mounts filesystems in the correct order as the devices
# and mountpoints become available.

This one calls mountall to take care of the "correct order", so some details can be found in man mountall – but unfortunately neither this nor /usr/share/doc/mountall/* gives details on how to hook in. Issueing a find for mountall turned up a few more files for me, but nothing giving me further clues – so unless I learn something else, the above two locations would be the "current hooks".


Before you ask: No, placing a "startup script" into /etc/rc.X (or the equivalent with whatever is used instead of SYS-V) wouldn't do – as mounting is done by the kernel before the init process starts, and those mounts need be there before the system starts writing e.g. logs to disk.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .