#!/bin/sh # BASEDIR=`dirname $0` . ${BASEDIR}/common.inc.sh NANOBSD="$NANO_SRC/tools/tools/nanobsd/nanobsd.sh" usage() { cat < # # Wrapper around nanobsd.sh with autodetection of already processed steps to # provide some failsafe net, which avoids building world and/or kernel by # default. # # Rick van der Zwet # # Arguments: # build - Build NanoBSD parts which are not build yet # build force kernel - Build NanoBSD and force rebuilding the kernel # build force world - Build NanoBSD and force rebuilding world # edit - Manually edit the image # config [for ] - Configure image to be used for # rebuild - Rebuild NanoBSD (aka force rebuilding all) # deploy on [and reboot] - Deploy the image on node and reboot if needed # ports update - Update the packages from ports # ports force rebuild - Forcefully rebuilding all required packages EOF } deploy_image() { # Find object directory img=${OBJDIR}/_.disk.image if [ ! -r "$img" ]; then p_err Source $img does not exists exit 1 fi prompt_timeout=5 p_warn "Going to DEPLOY $img to $host:" p_warn "You will need to type the root password at least twice, consider using a key" $do_reboot && p_warn "AND will REBOOT the $host" p_warn "Press CTRL+C in $prompt_timeout seconds to CANCEL" # sleep $prompt_timeout echo "# Trying to connect to $host" release=`ssh $host 'uname -r'` || exit 1 # Release update cycle depends on the release we are coming from echo "# Host has FreeBSD Release: $release" updatecmd="/tmp/update$$" cat >> $updatecmd <<'EOF' # # Pre-cleaning of /cfg # mount -wo noatime /cfg || exit 1 find /cfg -type f | grep -v -e '^/cfg/\(pf.hybrid.conf.local\|resolv.conf\|motd\|rc.conf.local\)$' \ -e '^/cfg/local/\(ntp.drift\|dhcpd.conf\|dnsmasq.conf\|unbound.wleiden.conf\|wleiden.yaml\)$' \ -e '^/cfg/ssh/ssh_host_*' \ -e '^/cfg/ssh/authorized_keys$' \ | xargs rm -v || exit 1 umount /cfg || exit 1 # # Release specific update # EOF if echo $release | grep -q -e '^9\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then cat >> $updatecmd <<'EOF' . /etc/nanobsd.conf NANO_LABEL="WLIMG" tunefs -L ${NANO_LABEL}s3 /dev/${NANO_DRIVE}s3 || exit 1 /tools/update || exit 1 # Quirck to fix partition table & label if partion is copied to second image. if mount | grep -q ${NANO_DRIVE}s1; then tunefs -L ${NANO_LABEL}s2a /dev/${NANO_DRIVE}s2a || exit 1 trap "umount /mnt" 1 2 15 EXIT mount /dev/${NANO_DRIVE}s2a /mnt sed -i "" "s/${NANO_LABEL}s1/${NANO_LABEL}s2/" /mnt/conf/base/etc/fstab sed -i "" "s/${NANO_LABEL}s1/${NANO_LABEL}s2/" /mnt/etc/fstab umount /mnt trap 1 2 15 EXIT fi EOF elif echo $release | grep -q -e '^11\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then cat >> $updatecmd <<'EOF' /tools/update || exit 1 EOF else p_err "$release not supported for upgrade" exit 1 fi cat $updatecmd | ssh $host "cat > $updatecmd" || exit 1 cat $img | ssh -C $host "sh -x $updatecmd" || exit 1 if $do_reboot; then echo "# Reboot requested, press CTRL+C in $prompt_timeout seconds to cancel" sleep $prompt_timeout ssh $host reboot || exit 1 fi } config_image() { node_name=${1:+"-b -c $1"} img=$OBJDIR/_.disk.full mnt=`mktemp -d -t $(basename $0)` md=`mdconfig -a -t vnode -f $img` # Clean up when done trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0 trap "exit 1" 1 2 3 15 # Root filesystem mount /dev/${md}s1a $mnt || exit 1 # /dev/null in chroot mount -t devfs devfs ${mnt}/dev || exit 1 # Config files lives at /cfg location mount /dev/${md}s3 $mnt/cfg || exit 1 # Try to fetch and store config chroot $mnt /tools/wl-config -d -n -m startup $node_name || exit 1 } edit_image() { img=$OBJDIR/_.disk.full mnt=`mktemp -d -t $(basename $0)` md=`mdconfig -a -t vnode -f $img` # Clean up when done trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0 trap "exit 1" 1 2 3 15 # Root filesystem mount /dev/${md}s1a $mnt || exit 1 # /dev/null in chroot mount -t devfs devfs ${mnt}/dev || exit 1 # Config files lives at /cfg location mount /dev/${md}s3 $mnt/cfg || exit 1 # Nasty hack to set custom prompt prompt='set prompt = "image# "' echo $prompt >> $mnt/root/.cshrc p_info "Type exit when done" chroot $mnt /bin/csh p_info "Any changes are made permanent on image $img" # Unset prompt again sed -I '' "/^$prompt$/d" $mnt/root/.cshrc } build_image() { p_info Forcefully building kernel: $FORCE_KERNEL p_info Forcefully building world : $FORCE_WORLD NANOBSD_EXTRA=${NANOBSD_EXTRA:-''} if [ ! -r "${NANOBSD}" ]; then p_err ${NANOBSD} does not exists exit 1 fi if [ ! -x "${NANOBSD}" ]; then NANOBSD="sh ${NANOBSD}" fi # Find object directory OBJDIR="/usr/obj/nanobsd.${NANO_NAME}" if [ -d "${OBJDIR}" ]; then # No clean during run which was interrupted if [ "$FORCE_WORLD" = "no" -a "$FORCE_KERNEL" = "no" ]; then NANOBSD_FLAGS="-n" else NANOBSD_FLAGS="" fi # Detect succesfull buildworld tail -10 ${OBJDIR}/_.bw | grep 'World build completed' if [ $? -eq 0 -a ${FORCE_WORLD} = "no" ]; then p_info NO building of world NANOBSD_FLAGS="${NANOBSD_FLAGS} -w" fi # Detect succesfull buildkernel tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed' if [ $? -eq 0 -a ${FORCE_KERNEL} = "no" ]; then p_info NO building of kernel NANOBSD_FLAGS="${NANOBSD_FLAGS} -k" fi else p_warn Nothing yet, starting fresh NANOBSD_FLAGS="" fi # Provide verbose output by default COMMAND="${NANOBSD} ${NANOBSD_FLAGS} -c ${NANO_CFG_FILE} -v ${NANOBSD_EXTRA}" f_time ${COMMAND} RETVAL=$? # Verify on build failures tail -10 ${OBJDIR}/_.bw | grep 'World build completed' if [ $? -eq 1 ]; then p_err Building world FAILED, check ${OBJDIR}/_.bw fi tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed' if [ $? -eq 1 ]; then p_err Building kernel FAILED, check ${OBJDIR}/_.bk fi if [ $RETVAL -ne 0 ]; then p_err "Errors in building NanoBSD Image ($RETVAL)" fi p_info End time: `date` exit ${RETVAL} } # # Argument parsing # FORCE_KERNEL=${FORCE_KERNEL:-"no"} FORCE_WORLD=${FORCE_WORLD:-"no"} if [ -z "$1" ]; then usage; exit 1 elif [ "$1" = "build" ]; then if [ -z "$2" ]; then elif [ "$2" = "force" ]; then if [ "$3" = "kernel" ]; then FORCE_KERNEL="yes" elif [ "$3" = "world" ]; then FORCE_WORLD="yes" else echo "Argument Error - '$3'"; exit 128 fi else echo "Argument Error - '$2'"; exit 128 fi build_image elif [ "$1" = "rebuild" ]; then FORCE_KERNEL="yes" FORCE_WORLD="yes" build_image elif [ "$1" = "deploy" -a "$2" = "on" ]; then if [ -z "$3" ]; then echo "Argument Error - '$3'"; exit 128 fi host=$3 do_reboot=false if [ -n "$4" -o -n "$5" ]; then if [ "$4" = "and" -a "$5" = "reboot" ]; then do_reboot=true else echo "Argument Error - '$4 $5'"; exit 128 fi fi deploy_image elif [ "$1" = "ports" ]; then if [ "$2" = "update" ]; then ${BASEDIR}/package-build.sh update elif [ "$2" = "force" -a "$3" = "rebuild" ]; then ${BASEDIR}/package-build.sh rebuild else shift 1 echo "Arguments Error - '$*'"; exit 128 fi elif [ "$1" = "config" ]; then if [ "$2" = "for" ]; then if [ -n "$3" ]; then node_name=$3 else echo "Arguments Error - '$*'"; exit 128 fi else echo "Arguments Error - '$*'"; exit 128 fi config_image $node_name elif [ "$1" = "edit" ]; then edit_image else echo "Argument Error - '$1'"; exit 128 fi