Automate a FreeBSD Installation

Introduction:

By now most of you have used the /stand/sysinstall application to either install FreeBSD or done a post-configuration of FreeBSD. While most of us do not have the need to automate FreeBSD installations there are many reasons why you may want to consider building your own automated FreeBSD installation.

FreeBSD comes with a scripting tool built into the sysinstall application that allows you to automate FreeBSD installations. This option is triggered by the presence of an install.cfg file in the /stand folder on mfsroot disk. There are several options that you can set and they are listed in the man pages. While this tutorial does not actually discuss all of the options it does explain how to create your own custom FreeBSD installation disks.

Please note that if you are an experienced FreeBSD user you will notice that some of the commands are unecessary or contain additional unecessary switches. This is just for tutorial and learning purposes.

Installation:

Make some new directories on a current freebsd box. One for each of the floppies, one for files, and a work directory

# mkdir -p /usr/custom_install/bootdisk/files/kern.flp

# mkdir -p /usr/custom_install/bootdisk/files/mfsroot.flp

# mkdir -p /usr/custom_install/bootdisk/files/src

# mkdir -p /usr/custom_install/bootdisk/work/kern.flp

# mkdir -p /usr/custom_install/bootdisk/work/mfsroot.flp

# mkdir -p /usr/custom_install/bootdisk/floppies/

Copy the floppy images into the files directory and also into our floppies directory (floppies will eventually contain the new flp images.

# cd /usr/custom_install/bootdisk/files/src/

# fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/4.6.2-RELEASE/floppies/kern.flp

# fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/4.6.2-RELEASE/floppies/mfsroot.flp

# cp /usr/custom_install/bootdisk/files/src/*.flp /usr/custom_install/bootdisk/floppies/

Now we are going to mount the image and copy over the information for both kern and mfsroot.

First kern

# vnconfig vn0 /usr/custom_install/bootdisk/files/src/kern.flp

# mount /dev/vn0 /mnt

# cp -R /mnt/* /usr/custom_install/bootdisk/files/kern.flp

# umount /mnt

# vnconfig -u vn0

Now for mfsroot

# vnconfig vn0 /usr/custom_install/bootdisk/files/src/mfsroot.flp

# mount /dev/vn0 /mnt

# cp -R /mnt/* /usr/custom_install/bootdisk/files/mfsroot.flp

# umount /mnt

# vnconfig -u vn0

Now if you want to compile a custom kernel for the custom install boot disks simply follow the directions in the "Kernel Customization" tutorial and ensure you have all the options and configurations you need (but not too large that it won't fit on a floppy) in the kernel for your custom install but don't actually have it installed on your box. You will want to keep your original so make sure that you backup a copy of your original kernel on your box just in case you accidently overwrite it. Once that is finished copy the kernel to your file directory.

# cp /path_to_new_kernel /usr/custom_install/bootdisk/files/src/

Note: You may just want to do a typical install of your kernel but backup your original one and instead of rebooting your machine simply:

# chflags noschg /kernel

# mv /kernel /kernel.CUSTOM_INSTALL

# cp /kernel.old /kernel

# chflags schg /kernel

Now copy in all the files we will need for our bootdisk:

# cp -R /usr/custom_install/bootdisk/files/kern.flp/ /usr/custom_install/bootdisk/work/kern.flp/

# cp -R /usr/custom_install/bootdisk/files/mfsroot.flp/ /usr/custom_install/bootdisk/work/mfsroot.flp/

If you choose to make a custom kernel add the new custom kernel and replace the previous one.

# cp -R /usr/custom_install/bootdisk/files/src/kernel /usr/custom_install/bootdisk/work/kern.flp/

# rm /usr/custom_install/bootdisk/work/kernel.gz

# gzip /usr/custom_install/bootdisk/work/kernel

The next step is to optionally modify the loader.rc file in our work/kern.flp/boot directory with any custom messages, etc

# vi /usr/custom_install/bootdisk/work/kern.flp/boot/loader.rc

The next step is to create a custom install.cfg file. Create our install.cfg file in the work directory. There is an example of this file in the src/releases distribution and if you have it installed it is found under /usr/src/releases/sysinstall/install.cfg. You may also check the man pages for sysinstall.

# vi /usr/custom_install/bootdisk/files/src/install.cfg

Once you are satisfied with your install.cfg file we must now add it to our mfsroot.gz file.

# gunzip /usr/custom_install/bootdisk/work/mfsroot.flp/mfsroot.gz

# vnconfig /dev/vn0 /usr/custom_install/bootdisk/work/mfsroot.flp/mfsroot

# mount /dev/vn0 /mnt

# cp /usr/custom_install/bootdisk/files/src/install.cfg /mnt/stand/install.cfg

# umount /mnt

# vnconfig -u /dev/vn0

# gzip /usr/custom_install/bootdisk/work/mfsroot.flp/mfsroot

Now all that is left is to recombine the information back into the kern.flp and mfsroot.flp files

First kern

# vnconfig vn0 /usr/custom_install/bootdisk/floppies/kern.flp

# mount /dev/vn0 /mnt

# cp -R /usr/custom_install/bootdisk/files/kern.flp/* /mnt/

# umount /mnt

# vnconfig -u vn0

Now for mfsroot

# vnconfig vn0 /usr/custom_install/bootdisk/floppies/mfsroot.flp

# mount /dev/vn0 /mnt

# cp -R /usr/custom_install/bootdisk/files/mfsroot.flp/* /mnt/

# umount /mnt

# vnconfig -u vn0

Other Suggestions:

If you are going to be installing ports/packages from your own server you may also include a custom port that will run many of your post-installation tasks to further reduce the workload required by you. Some of these items might be dumping over a version of your password files, any custom /etc/rc.conf options, as well as an installation script. While you are limited in how much you can actually do before the machine reboots you will find that you have a lot of options available to you and you are only limited by your imagination.

Some other items to consider would be using rsync for creating mirrored freebsd installations or using tar to dump a copy of all the current files and setup that you have or would like to have on your new server. While this is somewhat risky in some sense either option will allow you to quickly and easily install serveral identical FreeBSD servers. Add a couple of custom sh scripts that will modify any conf files and you have FreeBSD installations being painless and not very time consuming.