FreeBSD buildworld helper script

This script makes a lot of assumptions.
First, you'll need to already have the source code installed.
Second, you'll need to also have several packages already installed like,
bash
portaudit
cvsup-without-gui

This script is a work in progress. It is not considered complete.

Feel free to send any suggestions or corrections to jneufferjr at gmail dot com

Creative Commons License
Unixadmin.cc/freebsd_buildworld/ site content is licensed under a Creative Commons Attribution 3.0 License off site link.


#!/usr/local/bin/bash
#
# Script Name: run_process_buildworld.sh
# Version: 0.2.1
# Description: Bring the FreeBSD system current with updates/patches
# Created By: Jeff Neuffer Jr  jneufferjr@gmail.com
# Created On: 9/30/2006
# Updated On: $Id: index.html 81 2012-12-22 04:01:26Z jneuffer $
#
# Notes:
# This little script is meant to be run with sudo or as UID 0
#
#----------------------------------------------------------------------------------
# The process is as follows,
#
#  <shell> portsnap fetch
#  <shell> portsnap extract
#
#  <shell> cd /usr/ports/net/cvsup-without-gui
#  <shell> make && make install
#  <shell> cp /usr/share/examples/cvsup/stable-supfile /etc/cvsup-stable
#  (edit file for settings)
#  <shell> cvsup /etc/cvsup-stable
#
#  <shell> cd /usr/src
#  <shell>  make buildworld
#  <shell>  make -DALWAYS_CHECK_MAKE buildkernel KERNCONF=SMP
#  <shell>  make -DALWAYS_CHECK_MAKE installkernel KERNCONF=SMP
#
#  <shell>  reboot  (boot -s)
#
#  <shell> fsck -p
#  <shell> mount -u /
#  <shell> mount -a
#  <shell> cd /usr/src
#  <shell> adjkerntz -i            # if CMOS is wall time
#
#  <shell>  mergemaster -p
#  <shell>  make installworld
#  <shell>  make delete-old
#  <shell>  mergemaster
#  <shell>  reboot
#
#  That should be it.
#  ------------
#  What do I do if something goes wrong?
#  Make absolutely sure your environment has no extraneous cruft from earlier builds. This is simple enough.
#
#  <shell>  chflags -R noschg /usr/obj/usr
#  <shell>  rm -rf /usr/obj/usr
#  <shell>  cd /usr/src
#  <shell>  make cleandir
#  <shell>  make cleandir
#
#  Yes, make cleandir really should be run twice.
#  Then restart the whole process, starting with make buildworld.
#----------------------------------------------------------------------------------


# Various variables
SCRIPT_VERSION='0.2.1';
PRESENT_RUNNING_REL=$(uname -r);
PROG_EGREP=$(which egrep);
PROG_PORTAUDIT=$(which portaudit);
PROG_PORTSNAP=$(which portsnap);
PROG_CVSUP=$(which cvsup);


# Various functions
list_patches_in_src()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='list_patches_in_src()';
        clear;
        echo "Display the 3 most current patches only";
        egrep "^[0-9]+" /usr/src/UPDATING | egrep -m 3 "p[0-9]+"
}

portaudit()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='portaudit()';
        clear;
        echo "portaudit -- system to check installed packages";
        echo "             for known vulnerabilities";
        if [[ ! -e "$PROG_PORTAUDIT" ]]
        then
                echo "portaudit not found, unable to run";
                return;
        fi
        echo "Running portaudit as portaudit -F && portaudit"
        $PROG_PORTAUDIT -F && $PROG_PORTAUDIT;
}

portsnap()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='portsnap()';
        clear;
        echo "portsnap -- fetch and extract compressed";
        echo "            snapshots of the ports tree";
        if [[ ! -e "$PROG_PORTSNAP" ]]
        then
                echo "portsnap not found, unable to run";
                return;
        fi
        echo "Running portsnap as portsnap fetch && portsnap update"
        $PROG_PORTSNAP fetch && $PROG_PORTSNAP update;
}


retrieve_RELEASE_via_cvsup()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='retrieve_RELEASE_via_cvsup()';
        clear;
        if [[ ! -e "$PROG_CVSUP" ]]
        then
                echo "cvsup not found, unable to run";
                return;
        fi

        if [[ ! -e /etc/cvsup-stable ]]
        then
                echo "/etc/cvsup-stable not found, unable to run";
                return;
        fi

        echo "Running cvsup as cvsup /etc/cvsup-stable "
        $PROG_CVSUP /etc/cvsup-stable
}


run_buildworld()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='run_buildworld()';
        clear;
        if [[ ! -e /usr/src/UPDATING ]]
        then
                echo "Source does not appear installed, unable to run";
                return;
        fi

        echo "Preparing and running buildworld"
        #---
        chflags -R noschg /usr/obj/usr
        rm -rf /usr/obj/usr
        cd /usr/src
        make cleandir
        make cleandir
        #---
        cd /usr/src
        make buildworld
        make -DALWAYS_CHECK_MAKE buildkernel KERNCONF=SMP
        echo
        echo '----------------------------------------------------'
        echo 'Select Run installkernel then reboot'
        echo 'into single user mode and finish the update'
        echo '----------------------------------------------------'
        echo
}


run_installkernel()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='run_installkernel()';
        clear;
        echo "Running installkernel"
        cd /usr/src
        make -DALWAYS_CHECK_MAKE installkernel KERNCONF=SMP
        echo
        echo '----------------------------------------------------'
        echo 'Reboot into single user mode and finish the update'
        echo
        echo '#  <shell>  reboot  (boot -s)'
        echo '#  '
        echo '#  <shell> fsck -p'
        echo '#  <shell> mount -u /'
        echo '#  <shell> mount -a'
        echo '----------------------------------------------------'
        echo
}


run_installworld()
{
        RUN_TIME_STAMP=$(date);
        LAST_RUN='run_installworld()';
        clear;
        echo "Running installworld"
        cd /usr/src
        mergemaster -p
        make installworld
        make delete-old
        mergemaster
        echo
        echo '----------------------------------------------------'
        echo 'Reboot'
        echo '----------------------------------------------------'
        echo
}


clear
# Check to make sure root is running the script
if [[ "$(id -u)" -ne 0 ]]
then
        echo "The root user is required to complete this script";
        echo "You can su as root and then run again or use sudo $0";
        echo;echo "You appear to be,";
        echo "$(id)";echo;echo;
        exit;
fi


continue=yes
while [ "$continue" = yes ]
do
        echo; echo;
        echo "FreeBSD System Update ($SCRIPT_VERSION)";
        echo "--------------------------------------------";
        echo "Currently system @: $PRESENT_RUNNING_REL";
        echo;
        if [[ -n $RUN_TIME_STAMP ]]
        then
                echo "Last run stamp:  $RUN_TIME_STAMP";
                echo "Last run option: $LAST_RUN";echo;
        fi
        echo "  1) Portaudit (update/run)";
        echo "  2) Portsnap (fetch/update)";
        echo "  3) List patches already in /usr/src";
        echo "  4) Retrieve RELEASE updates via cvsup";
        echo "  5) Run buildworld (prep and run)";
        echo "  6) Run installkernel (after buildworld)";
        echo "  7) Run installworld (from single user mode)";

        echo "  9) Quit";
        read -p "Select an option [1-7,9]: " ANSWER
        if [[ "$ANSWER" == 1 ]]
        then
                portaudit;
                continue=yes;
        elif [[ "$ANSWER" == 2 ]]
        then
                portsnap;
                continue=yes;
        elif [[ "$ANSWER" == 3 ]]
        then
                list_patches_in_src;
                continue=yes;
        elif [[ "$ANSWER" == 4 ]]
        then
                retrieve_RELEASE_via_cvsup;
                continue=yes;
        elif [[ "$ANSWER" == 5 ]]
        then
                run_buildworld;
                continue=yes;
        elif [[ "$ANSWER" == 6 ]]
        then
                run_installkernel;
                continue=yes;
        elif [[ "$ANSWER" == 7 ]]
        then
                run_installworld;
                continue=yes;
        elif [[ "$ANSWER" == [9Qq] ]]
        then
                echo "Quiting"; echo;echo;
                continue=no;
        else
                clear;
                continue=yes;
        fi
done