#!/bin/sh
#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
# Front-end script to run the bootblock zapper.
#
# This is run on nodes that were behind a firewall and presumed tainted.
# It prevents them from ever booting from the disk by zeroing the MBR and
# partition boot blocks.
#

args="-BvZ"	# the real deal
#args="-Bv"	# fakin it

if [ -r /etc/emulab/paths.sh ]; then
    . /etc/emulab/paths.sh
else
    BINDIR=/etc/testbed
fi

if [ ! -x "$BINDIR/zapdisk" ]; then
    echo "$BINDIR/zapdisk not found!"
    exit -1
fi

#
# XXX We really should not be using a heuristic to figure this out.
#     We should get the info from Emulab central.
#

islinux=0
if [ $# -eq 0 ]; then
    if [ `uname` = FreeBSD ]; then
	set -- `dmesg | egrep '(ad|da|ar|aacd|amrd|mfid|mfisyspd)[0-9]: [0-9]+MB' | \
	    sed -e 's/^\([a-z][^:]*\):.*/\1/'`
    else
	# Linux
	set -- `/bin/ls -d /sys/block/sd* | sed 's#.*/##'`
	islinux=1
    fi
fi
status=0
for disk in $*; do
    echo -n "Zapping bootblocks for $disk..."

    # XXX hack: check for RO dongle
    if [ $islinux -ne 0 ] && grep -q 1 /sys/block/$disk/ro; then
	echo "read-only, SKIPPED!"
    elif [ -r "/dev/$disk" ]; then
        $BINDIR/zapdisk $args /dev/$disk
	if [ $? -ne 0 ]; then
	    echo "FAILED!"
	    status=`expr $status + 1`
        else
	    echo "OK"
        fi
    else
	echo "SKIPPED!"
    fi
done
exit $status
