Skip to content

Instantly share code, notes, and snippets.

@bdrewery
Last active December 17, 2015 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdrewery/5684829 to your computer and use it in GitHub Desktop.
Save bdrewery/5684829 to your computer and use it in GitHub Desktop.
FreeBSD beadm upgrade script (requires sysutils/beadm)
#! /bin/sh
#
# Copyright (c) 2013 Bryan Drewery <bryan@shatow.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
[ ${_FLOCKED:-0} -eq 0 ] && _FLOCKED=1 exec lockf -s -t0 /tmp/${0##*/}.lock /usr/bin/time -hl $0 "$@"
which screen>/dev/null 2>&1 && test -z "${STY}" && exec screen -h 10000 -S ${0##*/} $0 "$@"
set -e
set -v
: ${MAKEJOBS:=-j$(sysctl -n hw.ncpu)}
: ${JAILNAME:=beadm}
: ${CLEAN_KERNEL=-DNO_CLEAN}
: ${CLEAN_WORLD=-DNO_CLEAN}
injail() {
jexec -U root ${JAILNAME} "$@"
}
unmount() {
[ ${UNMOUNTED:-0} -eq 1 ] && return 0
if [ -n "${BE_MNT}" ]; then
umount ${BE_MNT}/dev
[ ${CCACHE_MOUNTED:-0} -eq 1 ] && umount ${BE_MNT}/root/.ccache
fi
jail -r ${JAILNAME}
beadm umount ${BE_NAME}
UNMOUNTED=1
}
cleanup() {
unmount || :
[ ${SUCCESS:-0} -eq 1 ] || yes | beadm destroy -F ${BE_NAME}
[ -n "${BE_MNT}" ] && rm -rf ${BE_MNT} || :
echo DONE;read n
}
sigint() {
SUCCESS=0
exit
}
trap sigint SIGINT
trap cleanup EXIT
if [ -n "$1" ]; then
URL="$1"
else
URL=$(svn info /usr/src | awk '/^URL: / {print substr($0, 6)}')
fi
[ -n "${URL}" ] || false
REVISION=$(svn info ${URL} | awk '/Last Changed Rev: / {print substr($0, 19)}')
BRANCH=$(svn info ${URL} | sed -n 's,^URL:.*base/\(.*\),\1,p' | tr '/' '_')
[ -n "${REVISION}" ] || false
[ -n "${BRANCH}" ] || false
BE_NAME=${BRANCH}-r${REVISION}
BE_MNT=$(mktemp -dt ${BE_NAME})
if beadm list | awk '{print $1}' | grep -q "^${BE_NAME}\$"; then
echo "Already have a ${BE_NAME}"
# Mark successful to avoid destroying existing BE
SUCCESS=1
exit
fi
beadm create ${BE_NAME}
beadm mount ${BE_NAME} ${BE_MNT}
# Update from host so jail does not need networking
/usr/local/bin/svn switch -r ${REVISION} ${URL} ${BE_MNT}/usr/src
ipargs="ip4.addr=127.0.0.1 ip6.addr=::1"
# For network in jail
#ipargs="ip4=inherit ip6=inherit"
jail -c persist name=${JAILNAME} path=${BE_MNT} host.hostname=$(hostname) ${ipargs} allow.chflags
jls
mount -t devfs devfs ${BE_MNT}/dev
# FIXME: This will fail if /root/.ccache is an absolute symlink
if [ -d /root/.ccache -a -d ${BE_MNT}/root/.ccache ]; then
mount -t nullfs /root/.ccache ${BE_MNT}/root/.ccache
CCACHE_MOUNTED=1
fi
injail make -C /usr/src ${MAKEJOBS} ${CLEAN_WORLD} buildworld
injail make -C /usr/src ${MAKEJOBS} ${CLEAN_KERNEL} kernel
injail mergemaster -p
injail make -C /usr/src installworld
yes | injail make -C /usr/src delete-old
injail mergemaster -iUF
#yes | injail make -C /usr/src delete-old-libs
SUCCESS=1
unmount
beadm activate ${BE_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment