Solved bsdinstall scripting guide/tutorial

tingo

Son of Beastie

Reaction score: 633
Messages: 2,535

There is a bit on scripting in the man page, which (IMHO) explains what it can do quite well. Do you have questions that isn't answered in the man page?
 

ShelLuser

Son of Beastie

Reaction score: 2,091
Messages: 3,782

Still, why would you want to bother with scripting the installer while you could easily perform all the actions it takes in a script of your own? Keep in mind that "installing" FreeBSD is basically nothing more than extracting a few archives which is then followed by applying some specific settings to that pristine environment.
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

bsdinstall() has an example of a script consisting of
Code:
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 does not look like enough code to get FreeBSD installed. Am I missing something?
There is no mention creating a partition scheme or adding partitions... Presumably this needs to be specifically included...
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

There is a bit on scripting in the man page, which (IMHO) explains what it can do quite well. Do you have questions that isn't answered in the man page?
It looks as though I only need to add a few lines to /etc/installerconfig on the installation media which is what is built into the system. I'd prefer to use that if I could.
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

There is a bit on scripting in the man page, which (IMHO) explains what it can do quite well. Do you have questions that isn't answered in the man page?
I have made some progress and succeeded in installing FreeBSD 12.2-RELEASE by including a 'typical bsdinstall script' in /etc/installerconfig as shown in the manpage bsdinstall():-

Code:
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

I'm amazed that this worked! .... Although an MBR installation was created rather than GPT. Networking got installed and sshd was started on boot, but there was no indication of any pkg being installed. That was something I was hoping for...
 

T-Daemon

Daemon

Reaction score: 829
Messages: 1,680

Although an MBR installation was created rather than GPT.
Have a look at bsdinstall(8), scriptedpart ... disk [scheme] [{partitions}]:

Rich (BB code):
 scriptedpart parameters  Sets up disks like autopart and partedit, but
                              non-interactively according to the disk setup
                              specified in parameters.  Each disk setup is
                              specified by a three-part argument:

                              disk [scheme] [{partitions}]

                              Multiple disk setups are separated by
                              semicolons.  The disk argument specifies the
                              disk on which to operate (which will be erased),
                              while the scheme argument specifies the gpart(8)
                              partition scheme to apply to the disk. If
                              scheme is unspecified, scriptedpart will apply
                              the default bootable scheme on your platform.
                              ....
For GPT that would be:

Code:
PARTITIONS=ada0 gpt
You can also determine with size type [mount point] the size of the partitions, e.i:

Code:
PARTITIONS=ada0 gpt { 20G freebsd-ufs /, 4G freebsd-swap }
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

Have a look at bsdinstall(8), scriptedpart ... disk [scheme] [{partitions}]:

Rich (BB code):
 scriptedpart parameters  Sets up disks like autopart and partedit, but
                              non-interactively according to the disk setup
                              specified in parameters.  Each disk setup is
                              specified by a three-part argument:

                              disk [scheme] [{partitions}]

                              Multiple disk setups are separated by
                              semicolons.  The disk argument specifies the
                              disk on which to operate (which will be erased),
                              while the scheme argument specifies the gpart(8)
                              partition scheme to apply to the disk. If
                              scheme is unspecified, scriptedpart will apply
                              the default bootable scheme on your platform.
                              ....
For GPT that would be:

Code:
PARTITIONS=ada0 gpt
You can also determine with size type [mount point] the size of the partitions, e.i:

Code:
PARTITIONS=ada0 gpt { 20G freebsd-ufs /, 4G freebsd-swap }
I've spent several hours trying to get 'gpt' added to the PARTITIONS preamble and when I add it the installation falls over immediately, so I guess I'll just have to learn to live with it for the time being. As far as adding pkgs, I guess that can only happen after a reboot once the base system has been installed on the target device, otherwise wouldn't pkgs get installed on the installation media...?
 

T-Daemon

Daemon

Reaction score: 829
Messages: 1,680

I've spent several hours trying to get 'gpt' added to the PARTITIONS preamble and when I add it the installation falls over immediately, so I guess I'll just have to learn to live with it for the time being.
It's been almost two years since I experimented with unattended installation, I remembered the syntax incorrect, sorry. Put the parameters in quotes:

Code:
PARTITIONS="ada0 gpt"
----
PARTITIONS="ada0 gpt { 20G freebsd-ufs /, 4G freebsd-swap }"
 

debguy

Well-Known Member

Reaction score: 24
Messages: 275

It's fairly easy to read shell scripts. I read them during install so I could understand how to restore a clobbert boot file Win10 clobbered. Just put in the USB and read is one way to do it.

The files (installer scripts) therein do mention scripting support - I do not remember if they cite a source for learning about it sorry - but they might.
 

T-Daemon

Daemon

Reaction score: 829
Messages: 1,680

As far as adding pkgs, I guess that can only happen after a reboot once the base system has been installed on the target device, otherwise wouldn't pkgs get installed on the installation media...?

The following describes a UFS, scripted, unattended installation, with a GUID partition table (GPT), and package installation. The main focus is on creating a internet connection and installing packages on the new system without reboot. In this example a run(4) wifi device is configured.

Configuration of the wlan interface on the installation media in /etc/rc.local:
Code:
ifconfig wlan0 create wlandev run0

# If necessary:

ifconfig wlan0 regdomain <regulatory domains like ETSI> country <country code>

# A list can be viewed with 'ifconfig wlan0 list countries' . More in ifconfig(8).

wpa_supplicant -i wlan0 -c /etc/wpa_supplicant &
Configure /etc/wpa_supplicant accordingly:
Code:
network={
        ssid="......"
        psk="......"
}
/etc/installerconfig (mind the quotes around the parameters in the preamble):
Code:
PARTITIONS="ada0 gpt"
DISTRIBUTIONS="kernel.txz base.txz"

#!/bin/sh
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0

# Add here all desired configuration:

# Installing packages. The following two lines is borrowed
# code from sysutils/firstboot-pkgs [1]

env ASSUME_ALWAYS_YES=YES pkg bootstrap -f | cat
env ASSUME_ALWAYS_YES=YES pkg upgrade -f | cat

pkg install -y <list of packages>
[1]
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

I tried the above, but am using a wired connection, so not sure if I need to do anything with /etc/rc.local... I didn't manage to see what happened and it didn't get written to /tmp/bsdinstall_log so not sure what ran and if there were any error msgs.... maybe I need to put a pause in at some point but it didn't work.

I do appreciate the help and would love to see this working... I'm sure that I've missed something straight forward.
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

It appears that pkg bootstrap is not working....

I wasn't sure if the network interface was working or not so added 'ifconfig_em0="DHCP"' to /etc/rc.conf on the installation media and I see that an IP address is assigned during bootup, but get 'Address resolution failed for http://pkg.FreeBSD.org/.....'
I can ping my router as well as '8.8.8.8' so it looks like a hostname resolution failure...
On further investigation, I looked at /etc/resolv.conf and this is symbolic link to /tmp/bsdinstall_etc/resolv.conf which does not actually exist although there are other files in that subdirectory.

So now I need to figure out how hostname resolution is done during a bsdinstall, since it doesn't seem to work for me when running a script...
 

T-Daemon

Daemon

Reaction score: 829
Messages: 1,680

so added 'ifconfig_em0="DHCP"' to /etc/rc.conf on the installation media
Please see response on
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

I'm back on this thread now that I solved the hostname resolution problem and finally have an /etc/installerconfig which works! ie one which will actually install pkgs...

Code:
# /etc/installerconfig

PARTITIONS=da1
DISTRIBUTIONS="kernel.txz base.txz"

#!/bin/sh
echo 'Running installerconfig...'
sysrc ifconfig_em0=DHCP
sysrc sshd_enable=YES
dhclient em0

# Installing packages. The following two lines is borrowed
# code from sysutils/firstboot-pkgs [1]

env ASSUME_ALWAYS_YES=YES pkg bootstrap -f | cat

pkg install -y mc

I'll probably spend some time developing this. It's the first time I've managed to install FreeBSD together with some pkgs. Previously I was messing with the 'firstboot' option which installed pkgs once a freshly installed copy of FreeBSD booted for the first time. This way, a newly installed FreeBSD will already have any required pkgs installed.

The one problem I still have at the moment is that I get an onscreen debug log produced which requires selecting 'exit' to go away. I don't see any way to prevent the debug messages, or at least have them written to a debug log file... maybe I just need to add reboot at the end of the script above.

Just a quick note of thanks to T-Daemon for his insight without which I wouldn't have got this far.
 

T-Daemon

Daemon

Reaction score: 829
Messages: 1,680

Just a quick note of thanks to [FONT=monospace]T-Daemon[/FONT] for his insight without which I wouldn't have got this far.
You are most welcome.


I like to suggest some settings for the /etc/installerconfig configuration:

- This one I advice to set implicitly: The installation is a unattended one, it should be put a poweroff or shutdown -p now as last command, otherwise, after installerconfig is processed, the system reboots and the installation begins over and over again, rebooting every time after installing the new system.

- Setting in /boot/loader.conf autoboot_delay="-1" will not stop at the boot menu waiting for user interaction before autobooting after 10 seconds, it will load directly the kernel, speeding up the boot process. Configure this last after you are sure installerconfig works as instructed.

- If instead of the "quarterly" package repository "latest" is desire it can be put in the #!/bin/sh setup script environment to bootstrap latest pkg, before the pkg bootstrap command:

Code:
env PACKAGESITE="pkg+http://pkg.FreeBSD.org/${ABI}/latest"

It must be set also "latest" repository to the new system:
Code:
mkdir -p /usr/local/etc/pkg/repos

cat << EOF > /usr/local/etc/pkg/repos/latest.conf
FreeBSD: { enabled: no }

latest: {                                                                     
  url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest",
  mirror_type: "srv",                                                           
  signature_type: "fingerprints",                                               
  fingerprints: "/usr/share/keys/pkg",                                         
  enabled: yes                                                                 
}
EOF

- If there is a lot of settings to put in /etc/rc.conf of the new system (or any other configuration file) use "here document" to output into file instead of putting sysrc to every settings option line, i.e.:
Code:
cat << EOF > /etc/rc.conf
hostname="fbsd122.home"
keymap="uk"
zfs_enable="YES"
dumpdev="AUTO"

ifconfig_em0=DHCP
sshd_enable=YES

rpcbind_enable="YES"
nfs_server_enable="YES"
mountd_enable="YES"
nfsv4_server_enable="YES"

firewall_enable="YES"
firewall_type="workstation"
EOF
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

Thanks for your post lots of good info there - I will take some time to study the examples...

I am a big fan of 'heredocs' and have attempted to create the following /etc/installerconfig which is supposed to install Wordpress on a new FreeBSD installation, so that browsing to http://xxx.xxx.xxx.xxx/wordpress 'should' take you to the Wordpress installation screen.. Unfortunately it doesn't quite work because the 'wordpress' database doesn't get created, although if I run that snippet of code independently it does work... Not yet figured out what might have gone wrong. What does work is
http://xxx.xxx.xxx.xxx/info.php which shows the php is correctly interpreted and the required php modules are installed/loaded.


Code:
ARTITIONS="da1"
DISTRIBUTIONS="kernel.txz base.txz"

#!/bin/sh
echo 'Running installerconfig...'
sysrc ifconfig_em0=DHCP
sysrc sshd_enable=YES
sysrc apache24_enable=YES
sysrc mysql_enable=YES

cat /etc/rc.conf
dhclient em0

sed -i ".org" 's/#PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config.

# Installing packages. The following two lines is borrowed
# code from sysutils/firstboot-pkgs [1]

env ASSUME_ALWAYS_YES=YES pkg bootstrap -f | cat

pkg install -y mc apache24 mod_php74 mysql56-server wordpress

#configure php

cat <<EOF >/usr/local/etc/apache24/Includes/php.conf
<IfModule dir_module>
    DirectoryIndex index.php index.html

    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>
EOF

echo '<?php phpinfo(); ?>' > /usr/local/www/apache24/data/info.php
cat /usr/local/www/apache24/data/info.php

service mysql-server start

#install wordpress database in mysql

mysql -u root <<EOF
CREATE DATABASE wordpress;
CREATE USER wp_user@localhost IDENTIFIED BY 'wp_pass';
GRANT ALL PRIVILEGES ON wordpress.* TO wp_user@localhost;
FLUSH PRIVILEGES;
exit
EOF


#configure wordpress

mv  /usr/local/www/wordpress/ /usr/local/www/apache24/data

cd /usr/local/www/apache24/data/wordpress
sed -e 's/database_name_here/wordpress/' -e 's/username_here/wp_user/' -e 's/password_here/wp_pass/' wp-config-sample.php >wp-config.php
chown -R www:www /usr/local/www/apache24/data/
chmod -R 775 /usr/local/www/apache24/data/

#service apache24 start

# sleep 20   <------ used for debugging

echo 'Installerconfig exiting...'

reboot

I can see that I will be spending quite a bit of time developing and testing /etc/installerconfig, but having a tailored FreeBSD built out of the box has been a long term ambition of mine.
 

fumanchu

Member

Reaction score: 1
Messages: 22

I know this is an solved issue; however, I have a question. Now that you have created a successful /etc/installconfig, where does said installconfig file go? I have created my own bootable memstick image and added an /etc/installconfig; however, every time I boot from this new .img, I get the standard installation screens and I never see my /etc/installconfig executed...
 
Top