# firstboot



## balanga (Mar 15, 2018)

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?


----------



## Phishfry (Mar 15, 2018)

firstboot is a zero K file which resides in the root directory. It acts as a sentinel for rc.d scripts with the KEYWORD= firstboot
sysutils/firstboot-pkgs/

_firstboot___sentinel_
(_str_) This variable specifies the full path to a ``first
boot''sentinel file.If a file exists with this path, _rc.d_
scripts with the ``firstboot'' keyword will be run on startup
and the sentinel file will be deleted after the boot process
completes.  The sentinel file must be located on a writable
file system which is mounted no later than _early___late___divider_
to function properly.The default is _/firstboot_.

https://www.freebsd.org/cgi/man.cgi?query=rc.conf


----------



## balanga (Mar 15, 2018)

It would be useful to see an example of such an rc.d script.


----------



## SirDice (Mar 15, 2018)

Just install sysutils/firstboot-pkgs and look at the code? Or look at it on SVN: https://svnweb.freebsd.org/ports/he...les/firstboot_pkgs.in?revision=432859&view=co


----------



## balanga (Mar 15, 2018)

So it's basically a single file..../usr/local/etc/rc.d/firstpoot_pkgs...

I'll see if I can incorporate it into a new install script I'm working on..


----------



## SirDice (Mar 15, 2018)

It's really easy to use, I've used a couple of those firstboot-* ports for deployment on AWS. 

If you want to test it, simply create a file /firstboot: `touch /firstboot`. This is the trigger for those scripts, if that file exists the scripts are run and the firstboot file is removed afterwards.


----------



## balanga (Mar 15, 2018)

in /usr/local/etc/rc.d/firstboot_pkgs

it says to set 'firstboot_pkgs_enable="YES" so is that an alternative to `touch /firstboot` or in addition to?

Also, not entirely what 'firstboot-* ports' refers to


----------



## SirDice (Mar 15, 2018)

balanga said:


> it says to set 'firstboot_pkgs_enable="YES" so is that an alternative to `touch /firstboot` or in addition to?


No, they both need to be set. The firstboot_pkgs_enable is to ensure the rc(8) script is executed but the script is only functional if the /firstboot file exists. This file is removed after the first boot. On consecutive boots the script is still active and started (due to the *_enable in rc.conf) but won't do anything because the /firstboot file doesn't exist anymore.


----------



## balanga (Mar 15, 2018)

Useful link


----------



## balanga (Mar 15, 2018)

Steps to install www/apache24 on first boot...

```
touch /firstboot
sysrc firstboot_pkgs_enable=YES
echo 'firstboot_pkgs_list="apache24"' >>/etc/rc.conf
cp firstboot_pkgs /etc/rc.d
```

Is this more or less it? Are any changes to sysutils/firstboot-pkgs required?


----------



## SirDice (Mar 16, 2018)

balanga said:


> ```
> cp firstboot_pkgs /etc/rc.d
> ```


Don't copy this, if you install the port or package the script is already in /usr/local/etc/rc.d/.


----------



## balanga (Feb 6, 2019)

balanga said:


> Steps to install www/apache24 on first boot...
> 
> ```
> touch /firstboot
> ...



A year later, I can't workout how this works... I thought I understood at the time....

I get this bit:-


```
touch /firstboot
sysrc firstboot_pkgs_enable=YES
echo 'firstboot_pkgs_list="apache24"' >>/etc/rc.conf
```

but can't figure out what this is supposed to do:-

```
cp firstboot_pkgs /etc/rc.d
```

My idea is to automatically install some packages on first boot, ie before `pkg` has even been installed. It looks like I need something like `pkg bootstrap` first but that always causes a prompt which I'm trying to avoid as this is meant to be an automated process.


----------



## Phishfry (Feb 7, 2019)

I am not sure where you came up with that line. SirDice pretty much snuffed that out in post#11


balanga said:


> cp firstboot_pkgs /etc/rc.d



From the rc.d script itself:

```
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf (in the disk
# image, since this only runs on the first boot) to enable this:
#
# firstboot_pkgs_enable="YES"
#
# and place a list of packages in firstboot_pkgs_list, e.g.,
#
# firstboot_pkgs_list="apache22 php5 mysql56-server"
```

Unattended package installing could be tough because of pkg prompts (Y)
Let us know how you make out.
First off I would start with something small with few dependencies. Get a hang for how it works.



> Obviously, this port is not useful after a system is already running; it is
> intended to be included as part of the installation or disk image building
> process.


----------



## balanga (Feb 7, 2019)

Phishfry said:


> Unattended package installing could be tough because of pkg prompts (Y)
> Let us know how you make out.
> First off I would start with something small with few dependencies. Get a hang for how it works.



Managed to crack it after many attempts/hours using the following code, slightly amended... (/mnt) is the mounted partition where FreeBSD is being installed..


```
touch /mnt/firstboot
cat <<EOF  >> /mnt/etc/rc.d/firstboot
env ASSUME_ALWAYS_YES=YES pkg bootstrap
pkg install -y mc
EOF
```

misc/mc will be installed on first boot.


----------



## SirDice (Feb 8, 2019)

That will actually install mc _every time_ you boot the system.


----------



## Phishfry (Feb 9, 2019)

So his script is missing the delete sentinel step.
`rm /firstboot`


----------



## SirDice (Feb 11, 2019)

It's not the script's job to remove the sentinel (it's done at the end of /etc/rc). Besides that, it would still get executed with each boot. It's missing the rcorder(8) KEYWORD for example.


----------



## balanga (Feb 11, 2019)

SirDice said:


> That will actually install mc _every time_ you boot the system.



The sentinel file created is /firstboot and this gets deleted at the end of the 'firstboot' process. The script itself -  /etc/rc.d/firstboot remains but will not get invoked because the sentinel file no longer exists.

For the sake of tidying things up, maybe I should delete the script by including `rm /etc/rc.d/firstboot` as the last command in the script, although I'm not sure if you can delete a script which is currently running...


----------



## SirDice (Feb 12, 2019)

balanga said:


> The script itself - /etc/rc.d/firstboot remains but will not get invoked because the sentinel file no longer exists.


The script would be invoked every time regardless if the sentinel exists or not, because there's nothing limiting it to the firstboot only. See rc(8).


----------



## balanga (Feb 12, 2019)

That is not my experience.


----------



## balanga (Nov 2, 2019)

Revisiting an old topic which I never fully understood, I came across this article which helped me understand how it holds together:-

https://reviews.freebsd.org/D14275

The suggestion was to add firstboot-pkgs work into the base system, which makes a lot of  sense, because you have a catch-22 situation in that, AFAICS BICBW, you need to `pkg install sysutils/firstboot-pkgs` before the first boot!!!

I could never get my head around this and couldn't see how it could work.


----------

