Partition automation in bsdinstall auto script in FreeBSD

[This post is part of the bsdinstall automation series. It should be read with the main article posted @]

These lines should go out in the bsdinstall auto file, after commenting out bsdinstall autopart and bsdinstall mount section. Remember, this would wipe out the entire disk contents without any warning, so use it at your own risk. I am also assuming that if you have multiple disks connected, the first disk is what you want to install your OS on. Otherwise, you might have to modify the script accordingly.

Get the disk layout, and the first disk connected to the system
# disk_layout=`sysctl -n kern.disks`
# first_disk=${disk_layout%% *}

Destroy any existing partition, and then create a gpt partition
# gpart destroy -F $first_disk
# gpart create -s gpt $first_disk

Create a boot partition to hold the loader, size of 512K. Give it a GPT label of gpboot, which will show up in /dev/gpt when the device is discovered:
# gpart add -t freebsd-boot -l gpboot -b 40 -s 512K $first_disk

Install the GPT bootcode into the boot partition:
# gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 $first_disk

Create a swap parition
# gpart add -t freebsd-swap -l gpswap -s 4G $first_disk

Create a partition for /. It should start at the 1M boundary for proper sector alignment on 4K sector drives or SSDs. This is compatible with GPT drive layout for many other systems. Give it a GPT label of gprootfs.
# gpart add -t freebsd-ufs -l gprootfs -a 1M $first_disk

Format the root partition
# newfs -U /dev/gpt/gprootfs

Mount the partition at /mnt
# mount /dev/gpt/gprootfs /mnt

Add entry to fstab ($PATH_FSTAB)
# echo '# Device Mountpoint FStype Options Dump Pass#' > $PATH_FSTAB
# echo '/dev/gpt/gpswap none swap sw 0 0' >> $PATH_FSTAB
# echo '/dev/gpt/gprootfs / ufs rw 1 1' >> $PATH_FSTAB

(1) Although the post is meant as part of automating bsdinstall, the concept could be used to create partitions over a new disk with appropriate safeguards

Scripting bsdinstall in FreeBSD 9.0 for custom installation CD

Starting with FreeBSD 9.0, the default installer for FreeBSD has changed from sysinstall to bsdinstall. While there has been mixed reactions from old time users, the process of creating a custom install CD for FreeBSD has become a lot easier (although longer). bsdinstall uses a set of shell scripts which can be modified as per requirements.

First step is to install a fresh 9.0 RELEASE, and select the src to be installed along with the base system. Once the system with source is installed (by default /usr/src) execute the following commands

# cd /usr/src
# make buildworld buildkernel

Installation scripts are located at /usr/src/usr.sbin/bsdinstall/scripts/ . First file to run is ‘auto’, which then calls other files. Almost all of them are shell scripts. Remember, you do not need to repeat ‘make buildworld buildkernel’ if you are just making changes to the install script file.

# cd /usr/src/release
# make release

Once the release has been built we now copy the iso / usb image / ftp files to the desired directory

# make install DESTDIR=/usr/freebsd-snapshot clean

This creates the iso file, memory stick image and ftp folder for ftp install. In case you are wondering how to use the memory stick image, see this blog post: