This page looks best with JavaScript enabled

Managing Boot Environments in FreeBSD

 ·  🎃 kr0m

FreeBSD supports what is called boot environments, which work based on clones of ZFS, so it will be necessary to use this file system to take advantage of it. Boot environments will allow us to perform updates on physical equipment or VMs without danger since we can revert the changes in case something fails.

The default boot environment management tool that comes installed in FreeBSD is Bectl , although there are other options such as Beadm .

As we can see, FreeBSD generates a “boot environment” by default in the installation of the operating system.

bectl list

BE      Active Mountpoint Space Created
default NR     /          967M  2021-03-28 15:26

A “boot environment” comprises the entire operating system except for the following directories:

/tmp  
/usr/home  
/usr/ports  
/usr/src  
/var/audit  
/var/crash  
/var/log  
/var/mail  
/var/tmp

These directories are not considered since they may contain user or application information and it is not convenient to lose them in any case.

For demonstration purposes, we create a “boot environment” called pre_install:

bectl create pre_install

We check the “boot environments”:

bectl list

BE          Active Mountpoint Space Created  
default     NR     /          967M  2021-03-28 15:26  
pre_install -      -          8K    2021-03-28 16:39

We can see that a new ZFS dataset has been created:

zfs list

NAME                     USED  AVAIL  REFER  MOUNTPOINT  
zroot                    970M  6.32G    96K  /zroot  
zroot/ROOT               967M  6.32G    96K  none  
zroot/ROOT/default       967M  6.32G   967M  /  
zroot/ROOT/pre_install     8K  6.32G   967M  /

We will normally use “boot environments” when we are going to update the OS, but to test it we will install the htop tool:

pkg install htop

And we will generate some files to check their existence later when starting one “boot environment” or another:

touch /home/kr0m/AA
touch /var/BB

We activate the pre_install “boot environment”:

bectl activate pre_install

Successfully activated boot environment pre_install

We check the current status of the “boot environments”:

bectl list

BE          Active Mountpoint Space Created  
default     N      /          420K  2021-03-28 15:26  
pre_install R      -          967M  2021-03-28 16:39

We restart:

shutdown -r now

By default, it will start the “boot environment” that we have indicated, but we can manually override the configuration from the FreeBSD boot itself, we access the following option:
8 Boot Environments

In this menu, we can see that by default, pre_install is active since it is the option we activated before the restart, but by pressing the “2” key, we can alternate to start one or the other.


Actually, it was not necessary to access this menu since we have left the pre_install “boot environment” configured by default, but I show it so that it is clear that we can choose the “boot environment” we want from the loader.

We leave it in pre_install and press ENTER to start.

We access the system and verify that the computer is in the state just before installing htop:

htop

htop: Command not found.

The file inside the excluded directory of the “boot environment” will still exist since they are user data and we do not want to lose them:

ls -la /home/kr0m/AA

-rw-r--r--  1 root  kr0m  0 Mar 28 16:40 /home/kr0m/AA

However, the file generated in /var no longer exists:

ls -la /var/BB

ls: /var/BB: No such file or directory

If we want to delete any “boot environment,” we just have to pass the destroy parameter followed by the name of the “boot environment”:

bectl destroy -o default

We check that it has been destroyed:

bectl list

BE          Active Mountpoint Space Created  
pre_install NR     /          967M  2021-03-28 16:39

In this case, since we have reverted, it is best to rename the “boot environment” to default to avoid problems since in the next update, we can use the name pre_install again:

bectl rename pre_install default

We check the “boot environments”:

bectl list

BE      Active Mountpoint Space Created  
default NR     /          967M  2021-03-28 16:39

In this article, we have only used bectl in a very basic way. It allows more complex operations such as generating a Jail from a specific “boot environment,” thus converting a physical machine to a Jail.

Please paste the Markdown content you want me to translate.

If you liked the article, you can treat me to a RedBull here