diff options
-rwxr-xr-x | share/hydra/bootless | 165 |
1 files changed, 127 insertions, 38 deletions
diff --git a/share/hydra/bootless b/share/hydra/bootless index e49c396..8eb268a 100755 --- a/share/hydra/bootless +++ b/share/hydra/bootless @@ -76,16 +76,6 @@ EOF usbdevice=`echo ${device} | sed -e s/[0-9]\$//g` - # Target device consistency check - if [ ! -b ${device} ]; then - echo "Error: device \"${device}\" not found." - exit 1 - fi - - if [ "`mount | grep ${device}`" != "" ]; then - echo "Error: device \"${device}\" is mounted." - fi - # Issue a warning cat <<EOF @@ -105,32 +95,9 @@ EOF exit 1 fi - # Target device consistency check - if [ ! -b ${device} ]; then - echo "Error: device \"${device}\" not found." - exit 1 - fi - - # Remove old partitions - for partition in `$sudo parted -s -- ${usbdevice} print | awk '/^ / {print $1}'`; do - $sudo parted -s -- ${usbdevice} rm $partition - done + hydra_bootless_device ${usbdevice} ${device} - # Create a single partition - $sudo parted -s -- ${usbdevice} mkpart primary ext2 2 -1 - $sudo parted -s -- ${usbdevice} set 1 boot on - - # Format and mount - $sudo mke2fs ${device} - if [ $? != 0 ]; then - echo "Error: mke2fs failed in \"${device}\" (errno: $?)." - exit 1 - fi - $sudo tune2fs -c 0 -i 0 ${device} - if [ $? != 0 ]; then - echo "Error: tune2fs failed in \"${device}\" (errno: $?)." - exit 1 - fi + # Mount tmpdir=`mktemp -d` $sudo mount ${device} ${tmpdir} if [ $? != 0 ]; then @@ -160,7 +127,12 @@ EOF #quit" | grub --device-map=${devicemap} --batch # Grub 2 - $sudo grub-install --root-directory=${tmpdir} ${usbdevice} --force + #$sudo grub-install --root-directory=${tmpdir} ${usbdevice} --force + $sudo grub-install --boot-directory=${tmpdir} ${usbdevice} --force + if [ $? != 0 ]; then + echo "Error: grub-install failed (errno: $?)." + exit 1 + fi # Finalize #rm -f ${devicemap} @@ -293,14 +265,128 @@ function hydra_bootless_git { ) } +# Create a target device +# TODO: properly support $subdevice in parted +function hydra_bootless_device { + local device="$1" + local subdevice="$2" + + # Check if device is mounted + if [ "`mount | grep ${subdevice}`" != "" ]; then + echo "Error: device \"${subdevice}\" is mounted." + exit 1 + fi + + # Target device consistency check + if [ ! -b ${subdevice} ]; then + echo "Error: device \"${subdevice}\" not found." + exit 1 + fi + + # Remove old partitions + for partition in `$sudo parted -s -- ${device} print | awk '/^ / {print $1}'`; do + $sudo parted -s -- ${device} rm $partition + done + + # Create a single partition + $sudo parted -s -- ${device} mkpart primary ext2 2 -1 + $sudo parted -s -- ${device} set 1 boot on + + # Create filesystem + $sudo mke2fs ${subdevice} + if [ $? != 0 ]; then + echo "Error: mke2fs failed in \"${subdevice}\" (errno: $?)." + exit 1 + fi + + # Tune + $sudo tune2fs -c 0 -i 0 ${subdevice} + if [ $? != 0 ]; then + echo "Error: tune2fs failed in \"${subdevice}\" (errno: $?)." + exit 1 + fi +} + +# Generate a bootless disk image +function hydra_bootless_image { + local output="$1" + local size="$2" + + if [ -z "$output" ]; then + output="bootless.img" + fi + + if [ -z "$size" ]; then + # TODO: calculate minimum size. Hint: + # du --exclude='.git' -hs ${BOOTLESS_DIR} + size="200" + fi + + # Set folder + hydra_bootless_folder + + # Set sudo config + local sudo + if [ "`whoami`" != 'root' ]; then + sudo="sudo" + fi + + echo "Creating an empty image..." + dd if=/dev/zero of=$output bs=1M count=$size + + echo "Partitioning image..." + parted -s -- ${output} mklabel msdos + parted -s -- ${output} mkpart primary ext2 2 -1 + parted -s -- ${output} set 1 boot on + + echo "Creating device mappings..." + tmpdir=`mktemp -d` + mapping="`$sudo kpartx -av ${output}`" + if [ "$?" != "0" ]; then + echo "Error: failed to create device mapping for \"${output}\" (errno: $?)." + exit 1 + fi + + # Set devices + device="`echo $mapping | head | awk '{ print $8 }'`" + subdevice="/dev/mapper/`echo $mapping | head | awk '{ print $3 }'`" + + # Create filesystem + $sudo mke2fs ${subdevice} + + # Mount + $sudo mount ${subdevice} ${tmpdir} + if [ $? != 0 ]; then + echo "Error: failed to mount \"${subdevice}\" filesystem in \"${tmpdir}\" (errno: $?)." + exit 1 + fi + + # Copy data + $sudo rsync -Cav ${BOOTLESS_DIR}/ ${tmpdir}/boot/ + + # Install grub + #$sudo grub-install --root-directory=${tmpdir} ${device} --force + $sudo grub-install --boot-directory=${tmpdir} ${device} --force + if [ $? != 0 ]; then + echo "Error: grub-install failed (errno: $?)." + exit 1 + fi + + # Finalize + $sudo umount ${tmpdir} + $sudo kpartx -dv ${output} + $sudo rm -rf ${tmpdir} + echo "Image saved at ${output}" +} + # Usage function hydra_bootless_usage { echo "Usage: `basename $0` <action> [arguments]" exit 1 } -# Check for requirements. -for req in parted; do +# Check for requirements +for req in parted kpartx; do hydra_install_package $req done @@ -322,6 +408,9 @@ elif [ "$1" == "update" ]; then elif [ "$1" == "git" ]; then shift hydra_bootless_git $* +elif [ "$1" == "image" ]; then + shift + hydra_bootless_image $* else hydra_bootless_usage fi |