cryptonas/cbox-build.sh

295 lines
8.1 KiB
Bash
Raw Normal View History

2005-07-21 11:38:53 +02:00
#!/bin/sh
#
# managing our work at the cryptobox
#
# usual workflow:
# dfsbuild - create the image directory with dfsbuild
# config - apply cryptobox specific changes to the image directory
# harden - remove unnecessary packages and disable developer features
# iso - create the iso image (out of the image directory)
2005-07-21 11:38:53 +02:00
# burn - tries to burn the the image on a cd-rw (maybe it works)
#
# development actions:
# chroot - run first tests in a chroot environment
# qemu - run the qemu emulation
# upload - copy your local files to tmpfs on a running cryptobox
# diff - compare tmpfs-files on a running cryptobox with the original
# merge - apply the diff to the local copy
2005-07-21 11:38:53 +02:00
#
# final action:
2005-08-04 00:32:11 +02:00
# release - the same as "dfsbuild config harden iso"
2005-07-21 11:38:53 +02:00
#
#
# problems of this script:
# - has to run as root, because dfsbuild, config, iso, chroot, devel
# and release need root privileges
2005-08-04 00:32:11 +02:00
# - 'harden' is strangely integrated
2005-07-21 11:38:53 +02:00
#
# you may run this script with multiple arguments, e.g.:
# cb-build.sh dfsbuild config iso qemu
#
# the action "release" does what it says :)
# (all developer's features like sshd, writable templates and
2005-08-04 00:32:11 +02:00
# the test-suite are deactivated, some packages get removed)
2005-07-21 11:38:53 +02:00
#
set -ue
2005-08-04 01:03:14 +02:00
# get the path of a configuration file - local configuration files
# supersede default files
# parameter: base name of the configuration file
function get_config_file()
{
[ -e "$LOCALCONF_DIR/$1" ] && echo "$LOCALCONF_DIR/$1" && return 0
[ -e "$DEFAULTCONF_DIR/$1" ] && echo "$DEFAULTCONF_DIR/$1" && return 0
echo "configuration file ($1) not found!" >&2
exit 1
}
2005-08-04 00:32:11 +02:00
# the base directory of your local development files
ROOT_DIR=$(dirname "$0")
# the template (default) configuration directory
DEFAULTCONF_DIR="$ROOT_DIR/etc-defaults.d"
# your local configuration directory (existing files supersede the defaults)
LOCALCONF_DIR="$ROOT_DIR/etc-local.d"
# local configuration directory - contains scripts to be executed after
# 'configure'
CUSTOM_CONFIGURE_DIR="$ROOT_DIR/configure-local.d"
2005-08-04 00:32:11 +02:00
2005-07-21 11:38:53 +02:00
# template directory for cryptobox specific configuration
2005-08-04 00:32:11 +02:00
TEMPLATE_DIR="cbox-tree.d"
# dfsbuild config
2005-09-05 17:42:01 +02:00
CONFIG=$(get_config_file dfs-cbox.conf)
2005-08-04 00:32:11 +02:00
# the chroot-wrapper within the cryptobox
CHROOT_START="/usr/lib/cryptobox/chroot-start.sh"
2005-08-04 00:32:11 +02:00
2005-08-04 01:03:14 +02:00
# qemu network configuration file
QEMU_IFUP_FILE=$(get_config_file qemu-ifup)
# to connect to a development cryptobox with ssh
SSH_CONFIG_FILE=$(get_config_file ssh_config)
2005-08-04 00:32:11 +02:00
# extract the hostname of the cryptobox from the ssh_config file
SSH_HOST=$(grep "^Host " "$SSH_CONFIG_FILE" | head -1 | sed 's/^Host *\(.*\)$/\1/')
2005-08-04 00:32:11 +02:00
############# include local configuration ##############
if [ -e "$(get_config_file cbox-dev.conf)" ]
then source "$(get_config_file cbox-dev.conf)"
else echo "local cbox-dev.conf ($(get_config_file cbox-dev.conf)) does not exist!" >&2
exit 1
fi
# image directory created by dfsbuild
# the BUILD_DIR is defined in the local cbox-dev.conf
IMAGE_DIR="$BUILD_DIR/image"
2005-07-21 11:38:53 +02:00
2005-08-04 01:03:14 +02:00
####################### functions ######################
2005-07-21 11:38:53 +02:00
function run_dfsbuild()
{
[ ! -e "$BUILD_DIR" ] && mkdir -p "$BUILD_DIR" && echo "das BuildDir ($BUILD_DIR) wurde angelegt ..."
dfsbuild -c "$CONFIG" -w "$BUILD_DIR"
# remove iso image of dfsbuild - it is not necessary
[ -e "$BUILD_DIR/image.iso" ] && rm "$BUILD_DIR/image.iso"
2005-07-21 11:38:53 +02:00
}
function create_iso()
{
echo "Creating the iso ..."
mkisofs $MKISOFS_OPTIONS -o "$IMAGE_FILE" "$IMAGE_DIR"
2005-07-21 11:38:53 +02:00
}
function qemu_boot()
{
# create a virtual harddisk image file
2005-07-21 11:38:53 +02:00
if [ ! -e "$HD_IMAGE" ]
then echo "Creating temporary harddisk image ..."
dd if=/dev/zero of="$HD_IMAGE" bs=1M count=20
2005-07-21 11:38:53 +02:00
fi
echo "Starting qemu ..."
2005-08-04 01:03:14 +02:00
qemu -cdrom "$IMAGE_FILE" -m 96 -hda "$IMAGE_FILE" -boot d -n "$QEMU_IFUP_FILE" || true
2005-07-21 11:38:53 +02:00
# remove iptables rules
2005-08-04 01:03:14 +02:00
"$QEMU_IFUP_FILE" stop
2005-07-21 11:38:53 +02:00
}
function configure_cb()
{
if [ ! -e "$IMAGE_DIR" ]; then
echo -e "Directory \"$IMAGE_DIR\" not found!"
2005-07-21 11:38:53 +02:00
echo -e "Did you run \"$0 dfsbuild\"?"
exit
fi
echo "Copying files to the box ..."
[ -e "$TMP_DIR" ] && rm -rf "$TMP_DIR"
cp -dr "$TEMPLATE_DIR/." "$TMP_DIR"
rm -rf `find "$TMP_DIR" -type d -name ".svn"`
cp -dr "$TMP_DIR/." "$IMAGE_DIR"
rm -rf "$TMP_DIR"
2005-07-21 11:38:53 +02:00
echo "Configuring the cryptobox ..."
2005-07-23 21:52:52 +02:00
# "harden" removes /etc/issue ...
if [ -e "$IMAGE_DIR/etc/issue" ]
then sed -i "s/^Version:.*/Revision: $(fetch_revision)/" "$IMAGE_DIR/etc/issue"
else echo "Version:.*/Revision: $(fetch_revision)" >"$IMAGE_DIR/etc/issue"
2005-07-23 21:52:52 +02:00
fi
fetch_revision >"$IMAGE_DIR/etc/cryptobox/revision"
chroot "$IMAGE_DIR" "$CHROOT_START" /usr/lib/cryptobox/configure-cryptobox.sh normal
# source local configure scripts
[ -d "$CUSTOM_CONFIGURE_DIR" ] && \
find "$CUSTOM_CONFIGURE_DIR" -xtype f | sort | while read file
do echo "Sourcing custom configure script $(basename $file):"
# execute it in its own environment (to be safe)
# 'source' implicitly imports all current settings
# indent these lines to improve the output
( source "$file" ) 2>&1 | sed 's/^/\t/'
done
2005-07-21 11:38:53 +02:00
}
2005-07-21 11:38:53 +02:00
function fetch_revision()
{
2005-08-04 01:03:14 +02:00
svn -R info 2>&1 | grep ^Revision| cut -f2 -d " " | sort | tail -1 \
|| echo "unknown release"
}
function upload2box()
2005-07-21 11:38:53 +02:00
# upload local files to a development cryptobox
# this is necessary to use an "old" development cd with
# new code - this affects only the web-interface and the
2005-07-21 22:21:27 +02:00
# cbox-manage.sh-script (the boot behaviour stays the same)
2005-07-21 11:38:53 +02:00
#
# of course, only the directories that are mapped to tmpfs can
# be updated this way
{
2005-07-21 22:15:57 +02:00
local DIRS="var/www usr/share/cryptobox usr/lib/cryptobox"
[ -e "$TMP_DIR" ] || mkdir -p "$TMP_DIR"
2005-07-21 11:38:53 +02:00
for a in $DIRS
do mkdir -p "$TMP_DIR/$a"
cp -r "$TEMPLATE_DIR/$a/." "$TMP_DIR/$a"
2005-07-21 11:38:53 +02:00
done
find "$TMP_DIR" -type d -name '\.svn' | while read a
2005-07-21 11:38:53 +02:00
do rm -rf "$a"
done
echo "Copying local files to the cryptobox ... "
if scp -F "$SSH_CONFIG_FILE" -rpq "$TMP_DIR/." cryptobox:/tmp/mirror
then echo "Set the base for future diffs to current state ..."
ssh -F "$SSH_CONFIG_FILE" "$SSH_HOST" "$DEV_FEATURES_SCRIPT" set_diff_base
else echo 'ERROR: copying failed!'
fi
rm -rf "$TMP_DIR"
2005-07-21 11:38:53 +02:00
}
function merge_from_box()
2005-07-21 11:38:53 +02:00
# merge a diff from a running development cryptobox into
# your local copy
2005-07-21 11:38:53 +02:00
{
echo "Check for collisions ... (dry-run)"
if box_diff | patch --dry-run -p1 -d "$TEMPLATE_DIR"
2005-07-21 11:38:53 +02:00
then echo
echo "Applying diff ..."
box_diff | patch -p1 -d "$TEMPLATE_DIR"
echo
echo "Set the base for future diffs to current state ..."
ssh -F "$SSH_CONFIG_FILE" "$SSH_HOST" "$DEV_FEATURES_SCRIPT" set_diff_base
2005-07-21 11:38:53 +02:00
else echo "Merging will fail - do it manually!"
fi
}
2005-08-04 01:03:14 +02:00
# get the diff of a running cryptobox system between its current state
# and its original content
function box_diff()
2005-08-04 01:03:14 +02:00
{
ssh -F "$SSH_CONFIG_FILE" "$SSH_HOST" "$DEV_FEATURES_SCRIPT" diff
2005-08-04 01:03:14 +02:00
}
2005-07-21 11:38:53 +02:00
function blanknburn_cdrw()
{
cdrecord -v dev=$CDWRITER blank=fast
cdrecord -v dev=$CDWRITER $IMAGE_FILE
2005-07-21 11:38:53 +02:00
}
################ do it! ######################
[ $# -eq 0 ] && echo "[`basename $0`] - no arguments supplied - maybe you want to use '--help'"
# initialize local directories (easier for users)
for a in $LOCALCONF_DIR $CUSTOM_CONFIGURE_DIR
do [ ! -e "$a" ] && mkdir "$a"
done
2005-07-21 11:38:53 +02:00
while [ $# -gt 0 ]
do case "$1" in
dfsbuild )
run_dfsbuild
;;
config )
2005-07-23 21:52:52 +02:00
configure_cb normal
2005-07-21 11:38:53 +02:00
;;
iso )
create_iso
;;
qemu )
qemu_boot
;;
diff )
# get a diff from a running development cryptobox
box_diff
2005-07-21 11:38:53 +02:00
;;
merge )
merge_from_box
2005-07-21 11:38:53 +02:00
;;
2005-07-23 21:52:52 +02:00
harden )
chroot "$IMAGE_DIR" "$CHROOT_START" /usr/lib/cryptobox/configure-cryptobox.sh secure
2005-07-23 21:52:52 +02:00
;;
2005-07-21 11:38:53 +02:00
upload )
upload2box
2005-07-21 11:38:53 +02:00
;;
chroot )
if [ ! -x "$IMAGE_DIR/$CHROOT_START" ]
then echo "the chroot init script ("$IMAGE_DIR/$CHROOT_START") is not executable"
2005-07-21 11:38:53 +02:00
echo "maybe you should run '`basename $0` cb-config' first"
else chroot "$IMAGE_DIR" "$CHROOT_START"
2005-07-21 11:38:53 +02:00
fi
;;
ssh )
ssh -F "$SSH_CONFIG_FILE" "$SSH_HOST"
;;
2005-07-21 11:38:53 +02:00
burn )
blanknburn_cdrw
;;
release )
2005-07-23 21:52:52 +02:00
$0 dfsbuild config harden iso
2005-07-21 11:38:53 +02:00
;;
help|--help )
echo "Syntax: `basename $0` ( release | dfsbuild | config | harden | iso | qemu | chroot | burn | upload | diff | merge | help )"
2005-07-21 11:38:53 +02:00
echo " (you may specify more than one action)"
echo
;;
* )
echo -e "unknown action: $1"
echo
$0 help
exit 1
;;
esac
shift
done