Skip to content

Instantly share code, notes, and snippets.

@brycied00d
Created October 31, 2012 22:02
Show Gist options
  • Save brycied00d/3990226 to your computer and use it in GitHub Desktop.
Save brycied00d/3990226 to your computer and use it in GitHub Desktop.
Script to help/automate the unpacking/edit/repacking of our netboot BIOS update images
#!/bin/bash
FILE=${1:-ERROR}
MOUNTPATH=${2:-/mnt/image/}
OWD=$PWD
if [ "x$FILE" = "xERROR" ]
then
echo "No file specified"
exit 1
fi
if [ ! -f "$FILE" ]
then
echo "Unable to open file."
exit 1
fi
# Use a trap to gracefully cleanup no matter what
trap 'handle_exit' EXIT
function handle_exit
{
echo "Exiting... re-packing image."
cd "$OWD"
[ "$LOOPDEV" ] && df -h $LOOPDEV
[ "$LOOPDEV" ] && umount $LOOPDEV
[ "$LOOPDEV" ] && dosfsck -a -t -V $LOOPDEV
[ "$LOOPDEV" ] && losetup -d $LOOPDEV
[ -f "$BASEFILE" ] && gzip --verbose -9 $BASEFILE
[ -f "$FILE" ] && du -cshx $FILE
}
# Find the name of the file post-gunzip
BASEFILE=$(basename "$FILE" .gz)
# We'll need to know the original file size later
SIZE=$(stat -c %s $FILE)
# Unpack the compressed image
gunzip $FILE || exit 1
# Check the filesize, which kind of losetup do we need?
LOSETUP_ARGS=
if [ $SIZE -gt 3000000 ]
then
# Needs offset since it's an HD image, not a floppy image
echo "Using --offset for losetup, filesize is $SIZE bytes"
LOSETUP_ARGS="--offset 32256"
fi
LOOPDEV=$(losetup -f)
losetup $LOSETUP_ARGS $LOOPDEV $BASEFILE || exit 1
echo "Loop device: $LOOPDEV" ; losetup $LOOPDEV
mount $LOOPDEV $MOUNTPATH || exit 1
echo "Mounted at $MOUNTPATH"
echo "OWD = $OWD"
cd $MOUNTPATH
bash
# Wait for bash to exit/return
# On exit, the EXIT trap will handle cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment