# Running /etc/rc.local first on boot



## Alain De Vos (Apr 4, 2021)

As zpools are not imported by default in 13.0 & openzfs I have in rc.local :
zpool import -a
zfs mount -a


rcorder /etc/rc.d/* /usr/local/etc/rc.d/*
lists local script rc.local somewhat late on boot.

How can I run rc.local in the beginning on boot ?


----------



## Phishfry (Apr 4, 2021)

Try firstboot instead of rc.local








						firstboot
					

I recently became aware of the firstboot option for rc scripts but The Handbook does not mention anything about the option as far as I can see.  Where would I find any documentation on how to use this?  And would it be possible to install various pkgs as part of a first boot?




					forums.freebsd.org
				



rc.local is meant to run last by design.

Checkout /etc/rc.d/growfs for an example of firstboot.


----------



## Phishfry (Apr 4, 2021)

Here is a rather complex firstboot enabled script.








						Is it possible to script starting up a FreeBSD VM, running a program in it, and fetching the result?
					

I have a library that I'd like to test on FreeBSD. My CI setup doesn't have any FreeBSD systems, and adding them would be difficult, but I can spin up a VM inside my CI script. (In fact, I already do




					stackoverflow.com
				




Here is the origin:




__





						"firstboot" keyword?
					





					lists.freebsd.org


----------



## sidetone (Apr 4, 2021)

Some of what can be run in rc.conf or rc.conf.local can be run in /boot/loader.conf only if it needs to be run before other services.


----------



## Alain De Vos (Apr 4, 2021)

I had bad results with firstboot. The file /firstboot disappears even if you put do not removeflags on it.
I ended up editing /etc/rc.d/mountcritlocal and added this at the end, this works :

```
/bin/sleep 3
/sbin/zpool import -a
/sbin/zpool import -f 2296658633266446156
/sbin/zpool import -f 17441234428618508281
/sbin/zfs mount -a
```
The sleep 5 was needed because otherwise the pools are not imported because the USB disk is not fully spinned up.
I think it is an USB quirk.
[ Thought : Editing these rc shell scripts are a piece of cake]


----------



## zirias@ (Apr 5, 2021)

Alain De Vos said:


> As zpools are not imported by default in 13.0 & openzfs I have in rc.local :
> zpool import -a
> zfs mount -a
> 
> ...


You shouldn't have to do any custom scripting at all. /etc/rc.d/zpool is responsible for importing your pools.

So, first check whether you have this file and it contains `zpool import -c $cachefile -a -N && break`. Then, verify you have either /etc/zfs/zpool.cache or /boot/zfs/zpool.cache. Then verify the pool that isn't automatically imported actually uses the cachefile (`zpool get cachefile <poolname>`). If not, importing it once with e.g. `zpool import -o cachefile=/boot/zfs/zpool.cache <poolname>` _should_ fix the problem. I can't verify cause I don't have a second pool in any of my machines.


----------



## SirDice (Apr 6, 2021)

Do you have `zfs_enable="YES"` in rc.conf?


----------



## 1Nikolay (Jan 2, 2022)

If you want automatic mount  >  individual <   encrypted dataset in ZFS pool,   on boot:

update startup  script  on   /etc/rc.d/zfs
- add line in block    zfs_start {    }
insert first line in  this block 
  zfs mount -al

and where you  system boot  - ZFS ask you passphrase  and continue booting another service.

zfs_enable_="yes"  - it is a first line in rc.conf   to RUN ZFS  file system.


----------



## grahamperrin@ (Jan 3, 2022)

Not for boot time, but users of 13.0-STABLE might also find:

/etc/rc.d/zfskeys


```
% sysrc zfskeys_enable
zfskeys_enable: YES
% uname -KU
1400045 1400045
%
```

<https://cgit.freebsd.org/src/commit/?id=33ff39796ffe469a764e485ac49c31700a51fd6f>
<https://cgit.freebsd.org/src/commit/?id=bfb7a31b6a22f4a2c6fb9881bd1e3ae7523a2290>
<https://cgit.freebsd.org/src/commit/?id=a58135eb092748caf383bc82306af522ff311d9d>

Postscript, 2022-01-29



grahamperrin said:


> Spun off from <https://markmail.org/message/dia3yrtsdbp2ur7c>:
> 
> FreeBSD: zfskeys_enable: encryption key not loaded for a file system within a pool that imports automatically at startup · Issue #13038 · openzfs/zfs


----------

