Skip to content

Instantly share code, notes, and snippets.

@flxai
Created April 22, 2016 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flxai/7399f3755b46db07b40fa644c5e82147 to your computer and use it in GitHub Desktop.
Save flxai/7399f3755b46db07b40fa644c5e82147 to your computer and use it in GitHub Desktop.
Partition extraction from image files
#!/bin/bash
# Extracts a partition from a full disk image containing a partition table
# Colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
nc='\033[0m' # no color
# Text
ok="[ ${green}OK${nc} ]"
noy="[ ${yellow}NO${nc} ]"
err="[ ${red}ERROR${nc} ]"
info="[ ${cyan}INFO${nc} ]"
usage() {
echo "Usage:" 1>&2
echo "$(basename $0) IMAGEFILE" 1>&2
}
# Make sure script is run with one param
if [[ $# -ne 1 ]]; then
usage
exit 1
fi
img="$1"
# Lines containing header informartion in fdisk
skip=9
# Sector/Block size
bs=512
pcount=$(fdisk "$img" -l | tail -n+$skip | wc -l)
psize=( $(fdisk "$img" -l -o Size | tail -n+$skip | sed 's/ /\n/g') )
pstart=( $(fdisk "$img" -l -o Start | tail -n+$skip | sed 's/ /\n/g') )
psectors=( $(fdisk "$img" -l -o Sectors | tail -n+$skip | sed 's/ /\n/g') )
echo "Your file has $pcount partitions with the following sizes:"
for p in $(seq 0 $((pcount-1))); do
echo -e " ${cyan}#$p${nc}: ${psize[$p]}"
done
echo
echo -ne "Which one do you want to extract? ${cyan}"
read partition
echo -ne "${nc}"
if [[ $partition =~ ^-?[0-9]+$ && $partition -lt $pcount && $partition -ge 0 ]]; then
echo -e "You chose ${cyan}#$partition${nc} (${psize[$partition]})\n"
else
echo -e "Your input was not valid, aborting..."
exit 1
fi
tgt="$img.p$partition"
echo -e "Extracting to $tgt..."
skip=${pstart[$partition]}
count=${psectors[$partition]}
size=$(($count*$bs))
dd if="$img" bs="$bs" skip="$skip" count="$count" | pv -tpreb -s "$size" | dd of="$tgt" &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment