# Adding script to FreeBSD image to run on initial boot



## chuckj (Jan 28, 2021)

I am experimenting with FreeBSD, primarily to ensure GNU projects I create can also be built on BSD.  To that end, I'm using a RaspberryPi to easily make clean FreeBSD installs.

Setting up ssh and avahi-daemon (so I can find the device with hostname.local) are multi-step processes that have been hard to get right, so I wrote a setup script to automate preparation of the device for ssh access.

After copying the image to the SD card and unable to access the rootfs partition, I copied my setup script to the MSDOSBOOT partition.  However, when I first booted with the fresh SD card, I found that the MSDOSBOOT partition had been pruned of several files, including my script.

So my question is of two parts:
1. Can I protect the script on MSDOSBOOT so it will not be deleted?
2. Is there a better way to copy a script that will survive the first boot?

Thanks,
Chuck Jungmann


----------



## SirDice (Jan 28, 2021)

Look into sysutils/firstboot-pkgs, sysutils/firstboot-freebsd-update and sysutils/firstboot-growfs. Besides using it to install packages, grow the filesystem and run freebsd-update(8) at first boot you can also look at how the scripts work.


----------



## chuckj (Jan 28, 2021)

Thank you for your quick response.

Correct me if I'm wrong, but it looks like those provisions install packages but do not make configuration changes (I'm not experienced preparing packages, so I am likely to be underestimating this opportunity).  Enabling ssh, for my purposes, requires creating a new user in the "wheel" group so I can have root access over ssh, as well as adding text to /etc/rc.conf to enable ssh and avahi-daemon.  That's why I think I need to use a shell script to prepare the system.  I don't expect the script to auto-run, but I just want it available to the root user when the system first booted.

Also, I can't seem to access the rootfs partition while preparing the SD card.  That's why I'm trying to use MSDOSBOOT.  I know I can't get to it on GNU, but I feel like I was unable to access the partition from a BSD laptop, either.


----------



## SirDice (Jan 28, 2021)

chuckj said:


> Correct me if I'm wrong, but it looks like those provisions install packages but do not make configuration changes (I'm not experienced preparing packages, so I am likely to be underestimating this opportunity). Enabling ssh, for my purposes, requires creating a new user in the "wheel" group so I can have root access over ssh, as well as adding text to /etc/rc.conf to enable ssh and avahi-daemon. That's why I think I need to use a shell script to prepare the system. I don't expect the script to auto-run, but I just want it available to the root user when the system first booted.


Look into running a customized bsdinstall(8). That might be a better solution in your case.


----------



## Snurg (Jan 28, 2021)

SirDice said:


> Look into running a customized bsdinstall(8). That might be a better solution in your case.





			
				bsdinstall man page said:
			
		

> *SETUP* *SCRIPT*
> Following the preamble is an optional shell script, beginning with    a #!
> declaration.  This    script will be run at the end of the installation
> process inside a chroot(8)    environment in the newly installed system and
> ...


My problem is similar to the OP's.
There is one thing I do not understand, however.
The man page says internet services are not available. Is that really correct?
Iirc after I installed (including setting up the connected network interface) internet was there after the installation finished, but I may be wrong, possibly having forgotten the reboot.

So I am still not sure, can't I just attach my postinstaller bootstrap at the end of the standard installation script?
Because, I'd love to make some changes in /boot/loader.conf and /etc/rc.conf and some other files before reboot, as this could save me another reboot.


----------



## balanga (Mar 2, 2021)

Snurg said:


> So I am still not sure, can't I just attach my postinstaller bootstrap at the end of the standard installation script?
> Because, I'd love to make some changes in /boot/loader.conf and /etc/rc.conf and some other files before reboot, as this could save me another reboot.


I've been trying to get unattended installs working properly for some time but never found the ideal way, but have recently started looking at functions built into FreeBSD installer program bsdinstall() which automatically uses /etc/installerconfig if present, otherwise you have an interactive install.

According to the manual this is '_a typical bsdinstall script_'_:-_


```
PARTITIONS=ada0
DISTRIBUTIONS="kernel.txz base.txz"

#!/bin/sh
gpart bootcode -b /boot/pmbr    -p /boot/gptboot -i 1 ada0
sysrc ifconfig_em0=DHCP
sysrc sshd_enable=YES
pkg install puppet
```

This allows you to set various values in /etc/rc.conf and there are probably options to configure /boot/loader.conf which I have yet to explore.

It looks like a powerful way of configuring new installations with little effort, although I have yet to find a way of including the 'firstboot' facility which FreeBSD has.  I did have it working, but have forgotten the specifics of setting it up.

The scripting is not very well documented in the manual, and so far the above code goes, it creates a bootable installation but the disk is MBR based in spite of the bootcode parameter above.

Hope this helps.


----------



## Snurg (Mar 2, 2021)

Thank you very much, balanga! That was helpful!

Regarding the firstboot facility, I have not yet managed to look into that, I will do as soon as the necessity arises.
Maybe SirDice's comments in this post might be helpful to recall what you exactly did when you had it working...


----------

