Skip to content

Instantly share code, notes, and snippets.

@coffeemakr
Last active August 29, 2015 14:03
Show Gist options
  • Save coffeemakr/dc6a3abd6cbd50d3ee51 to your computer and use it in GitHub Desktop.
Save coffeemakr/dc6a3abd6cbd50d3ee51 to your computer and use it in GitHub Desktop.
#!/bin/sh
TEMP_OUTPUT_FILENAME="vdistart"
# command paths
SFDISK=`which sfdisk`
DD=`which dd`
OD=`which od`
AWK=`which awk`
GREP=`which grep`
RM=`which rm`
MOUNT=`which mount`
if [ `id -u` != "0" ]; then
echo "This script must be run as root." 1>&2
exit 1
fi
if [ -z $1 ]; then
echo "Image file argument is missing." 1>&2
exit 1
fi
if [ -z $2 ]; then
echo "Mount point argument is missing." 1>&2
exit 1
fi
if [ ! -e $1 ]; then
echo "Passed image file does not exist." 1>&2
exit 1
fi
if [ ! -d $2 ]; then
echo "Mount point does not exist." 1>&2
exit 1
fi
# Check if chosen image file is fixed-size
if [ `$OD -j76 -N4 -td4 $1 | $AWK 'NR==1{print $2}'` = "2" ]; then
# determine offset to beginning of filesystem
DATA_OFFSET=`$OD -j344 -N4 -td4 $1 | $AWK 'NR==1{print $2}'`
# determine offset to beginning of partition
$DD if=$1 of=$TEMP_OUTPUT_FILENAME bs=1 skip=$DATA_OFFSET count=1b 2> /dev/null
BLOCK_OFFSET=`$SFDISK -luS $TEMP_OUTPUT_FILENAME 2>/dev/null | $GREP "^$TEMP_OUTPUT_FILENAME" | $AWK 'NR==1{print $2}'`
$RM $TEMP_OUTPUT_FILENAME
# mount the image
BLOCK_OFFSET=$(($BLOCK_OFFSET*512))
$MOUNT -o loop,offset=$(($DATA_OFFSET+$BLOCK_OFFSET)) $1 $2 2>/dev/null
if [ $? = "0" ]; then
echo "Mounted image file successfully."
exit 0
else
echo "Failed to mount image file. (Is the file already mounted?)"
exit 1
fi
else
echo "Image file must be fixed size.\n" 1>&2
echo "You can clone it to a raw image by using the following command:"
echo
echo " VBoxManage clonehd --format RAW <filename>.vdi <filename>.img"
echo
echo "and mount it afterwards:"
echo
echo " mount -t ext3 -o loop,rw ./ubuntu.img /mnt"
echo
echo "sorry... :("
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment