#!/bin/bash # ----------------------------------------------------------------------------- # Copyright (c) 2011 - OpenSLX GmbH # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING # # If you have any feedback please consult http://openslx.org/feedback and # send your suggestions, praise, or complaints to feedback@openslx.org # # General information about OpenSLX can be found at http://openslx.org/ # ----------------------------------------------------------------------------- # PreBoot USB stick installer # ----------------------------------------------------------------------------- ROOT_DIR=$(dirname $(readlink -f $0)) SELF=$(readlink -f $0) TMP_DIR="/tmp/pbstick-$$" DEBUG=0 FORCE=0 VERSION="" VDATE="" SHORT_OPTS=":vfdh" LONG_OPTS="version,force,debug,help" print_usage() { echo "Usage: $(basename $SELF) [OPTIONS] DEVICE" echo -e " -d --debug \t give more debug output" echo -e " -f --force \t don't ask questions" echo -e " -h --help \t print help" echo -e " -v --version \t print version information" echo "DEVICE is the device used by the usb stick (e.g. \"sdX\" for /dev/sdX)." } TEMP=`getopt -o $SHORT_OPTS --long $LONG_OPTS \ -n "$0" -- "$@"` if [ $? != 0 ] ; then echo -e "[Error] Unknown option(s).\n" print_usage exit 1 fi eval set -- "$TEMP" while true ; do case "$1" in -v|--version) echo "OpenSLX PreBoot USB stick installer ($VERSION - $VDATE)." exit 0 ;; -h|--help) print_usage exit 0 ;; -f|--force) echo "[Info] Disable user-interaction."; FORCE=1; shift ;; -d|--debug) echo "[Info] Enabled debugmode."; DEBUG=1; shift ;; --) shift ; break ;; *) echo "[Error] Internal error!" ; exit 1 ;; esac done if [ $(whoami) != "root" ]; then echo -e "[Error] You should be root." exit 1; fi OUT_DEV=$1 shift if [ ! -z $1 ]; then echo -e "[Error] Too many parameters. \n" print_usage exit 1 fi if [ -z $OUT_DEV ]; then echo -e "[Error] No output device specified.\n" print_usage exit 1 fi if [ ! -e "/dev/$OUT_DEV" ]; then echo -e "[Error] There is no /dev/${OUT_DEV}." exit 1 fi if [ -z $(which uuencode) ]; then echo -e "[Error] uuencode is missing (if you are on a debian/ubuntu system: apt-get install sharutils)" exit 1 fi # unpack script payload mkdir -p $TMP_DIR set_quiet () { if [ "x$DEBUG" != "x1" ]; then touch $TMP_DIR/stdout.log touch $TMP_DIR/stderr.log exec 6>&1 > $TMP_DIR/stdout.log exec 2> $TMP_DIR/stderr.log fi } unset_quiet () { if [ "x$DEBUG" != "x1" ]; then exec 1>&6 6>&- exec 2>&- fi } set_quiet match=$(grep --text --line-number '^PAYLOAD:$' $SELF | cut -d ':' -f 1) payload_start=$((match + 1)) cd $TMP_DIR tail -n +$payload_start $SELF | uudecode | tar -xjf - cd $ROOT_DIR PATH="$TMP_DIR/bin/:$PATH" if [ "x$FORCE" != "x1" ]; then unset_quiet echo "Device \"/dev/${OUT_DEV}\" will be repartitioned and formatted." echo -n "Do you really want to proceed? Then type in 'yes': " read cont [ "x${cont}" != "xyes" ] && exit 1 set_quiet fi for i in 1 2 3 4 5 6 7 8 9; do umount /dev/${OUT_DEV}$i &> /dev/null done # get the total size of the device DISKSIZE=$(sfdisk -s /dev/${OUT_DEV} 2>/dev/null || echo 0) DISKSIZE=$((${DISKSIZE}/1024)) # get the size of boot stuff and calculate the left over free space on device KSIZE=$(ls -l ${TMP_DIR}/boot/kernel|awk '{print $5}') ISIZE=$(ls -l ${TMP_DIR}/boot/initramfs|awk '{print $5}') IMAGESIZE=$(((${KSIZE}+${ISIZE}+6000000)/1048576)) FREE=$((${DISKSIZE}-${IMAGESIZE})) # create bootable vfat at the end of the device if big enough, otherwise use # the entire device if [ ${FREE} -ge 50 ] ; then sfdisk /dev/${OUT_DEV} -uM << EOF ,${FREE},L, ,,6,* EOF else NO_USRHOME=true sfdisk /dev/${OUT_DEV} << EOF ,,6,* EOF fi sync # copy mbr to stick dd bs=440 count=1 conv=notrunc if=$TMP_DIR/share/mbr.bin of=/dev/${OUT_DEV} $DEBUG_OUT # create filesystems on newly createt partitions if [ "x$NO_USRHOME" != "xtrue" ]; then mkfs.vfat -F 16 -n openslx-stick /dev/${OUT_DEV}2 mkfs.ext2 -L openslx-usrhome /dev/${OUT_DEV}1 else mkfs.vfat -F 16 -n openslx-stick /dev/${OUT_DEV}1 fi mkdir -p /media/openslx-stick /media/openslx-stick-usrhome if [ "x$NO_USRHOME" != "xtrue" ]; then mount /dev/${OUT_DEV}2 /media/openslx-stick mount /dev/${OUT_DEV}1 /media/openslx-stick-usrhome else mount /dev/${OUT_DEV}1 /media/openslx-stick fi mkdir -p /media/openslx-stick/boot cp -v $TMP_DIR/boot/* /media/openslx-stick/boot/ umount /media/openslx-stick umount /media/openslx-stick-usrhome $DEBUG_OUT rm -rf /media/openslx-stick* $DEBUG_OUT if [ "x$NO_USRHOME" != "xtrue" ]; then ${SYSLINUX}/linux/syslinux --install -d /boot -f /dev/${OUT_DEV}2 else ${SYSLINUX}/linux/syslinux --install -d /boot -f /dev/${OUT_DEV}1 fi unset_quiet echo "Successfully created OpenSLX PreBoot USB Stick .." exit 0