Re: Single-user actions on reboot

From: Jason Bacon <bacon4000_at_gmail.com>
Date: Fri, 29 Sep 2023 21:32:46 UTC
On 9/29/23 11:56, Warner Losh wrote:
> 
> 
> On Fri, Sep 29, 2023, 10:12 AM Chris <bsd-lists@bsdforge.com 
> <mailto:bsd-lists@bsdforge.com>> wrote:
> 
>     On 2023-09-29 06:49, Guido Falsi wrote:
>      > On 29/09/23 15:21, Jason Bacon wrote:
>      >>
>      >> I'm wondering if there's a canonical way to schedule a command
>     to run
>      >> automatically during the next boot cycle, but before going
>     multiuser.
>      >>
>      >> This would be useful, for example, to run certain tunefs
>     commands on /,
>      >> which can only be done in single-user mode, on remotely managed
>     systems.
>      >>
>      >> Running things after going multiuser is easy, of course, but not
>     sufficient
>      >> for tunefs commands that cannot be performed on a filesystem
>     mounted rw.
>      >>
>      >> Thanks...
>      >>
>      >
>      > AFAIK there is no ready made tool in base (or ports) for that.
>      >
>      > But ezjail uses this trick with rc scripts (removing itself):
>      >
>      >
>     https://erdgeist.org/gitweb/ezjail/tree/examples/example/etc/rc.d/ezjail.flavour.example <https://erdgeist.org/gitweb/ezjail/tree/examples/example/etc/rc.d/ezjail.flavour.example>
>      >
>      > This is effective but requires the disk mounted r/w which maybe
>     is not
>      > enough for you.
>      >
>      > Problem is, without any writable media mounted, how can any
>     script register
>      > it has
>      > already ran?
>      >
>      > One idea that comes to mind is adding some hook to the main rc
>     script,
>      > before
>      > mounting disk r/w, and a second script that removes existing
>     hooks once
>      > disks are
>      > r/w.
>     Indeed. Maybe at the same point the system tastes the disk(s) to see if
>     they've been dismounted
>     properly and then running fsck(8) if not. Using the same method to
>     perform
>     your task(s)?
> 
> 
> At work we just have a custom rc script to do that for all our content 
> disks (only os disks are in fstab).
> 
> As for writable media... you could set a uefi variable... or create a 
> small MD partition (call it /once) that an rc script in a later phase 
> could use to mop up and then free. The latter is easier, but in many 
> environments the former is more durable and reliable if there's an early 
> crash that happens before mopup and you can really only run something 
> once...
> 
> Warner
> 

Thanks for the brainstorming, guys.  This is about what I expected to 
hear.  What I had done to automate completion of base upgrades after the 
required reboot is add the following to /etc/rc.local:

# Would be nice if this were added to the default rc.local
rc_dir=/etc/rc.local.d
for script in $rc_dir/*; do
     /bin/sh $script
done

Then drop a very simple self-removing script into 
/etc/rc.local.d/freebsd-update-install.sh:

#!/bin/sh -e

/usr/sbin/freebsd-update install
/usr/sbin/pkg upgrade -y
rm -f $rc_dir/freebsd-update-install.sh

I just realized I could do the same thing by creating a new rc script 
such as /etc/rc.d/local.single, modeled on /etc/rc.d/local, but running 
earlier and using a different script and directory, e.g. 
/etc/rc.local.single and /etc/rc.local.single.d/.

Cheers,

	J

-- 
Life is a game.  Play hard.  Play fair.  Have fun.