Last active
December 16, 2015 09:39
-
-
Save risaacson/5414535 to your computer and use it in GitHub Desktop.
This is a continuation from gist:5409137 but based off of Euca linux image creation instructions. So as long as you have created your raw image with the three partitions correctly this script will pull out the root image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# set -x | |
# DEBUG='echo' | |
get_next_multiplier() { | |
current_multiplier=$1 | |
multiplier=$(( ${current_multiplier} * 2 )) | |
} | |
set_variables() { | |
start=$1 | |
count=$2 | |
bs=$3 | |
} | |
calc_max_divisor() { | |
multiplier=$1 | |
start_block=$2 | |
block_count=$3 | |
while true; do | |
start_divided=$(( ${start_block} / ${multiplier} )) | |
start_remainder=$(( ${start_block} % ${multiplier} )) | |
count_divided=$(( ${block_count} / ${multiplier} )) | |
count_remainder=$(( ${block_count} % ${multiplier} )) | |
[ "${start_divided}" -eq "0" ] && break | |
[ "${start_remainder}" -ne "0" ] && break | |
[ "${count_divided}" -eq "0" ] && break | |
[ "${count_remainder}" -ne "0" ] && break | |
[ "${start_remainder}" -eq "0" ] && [ "${count_remainder}" -eq "0" ] && set_variables ${start_divided} ${count_divided} ${multiplier} | |
get_next_multiplier ${multiplier} | |
done | |
} | |
get_output_device() { | |
device=$1 | |
[ -n "$( echo anImage.img | sed -n '/.img/p' )" ] && output_device=$( echo "${device}" | sed 's/.img/-root.img/g' ) || exit 3 | |
} | |
while getopts ":h" opt; do | |
case ${opt} in | |
h) | |
echo "rip_rootfs_from_raw_img INPUT_FILE <OUTPUT_FILE>" | |
exit 0 | |
;; | |
\?) | |
echo "Fail" | |
exit 1 | |
;; | |
esac | |
done | |
device=$1 | |
[ ! -f "${device}" ] && exit 1 | |
output_device=$2 | |
[ -n "${output_device}" ] && [ -f "${output_device}" ] && exit 2 || get_output_device "${device}" | |
block_size=1 | |
start_block=$( parted ${device} unit b print | grep ' 2 ' | awk '{print $2}' | sed 's/B//g' ) | |
block_count=$( parted ${device} unit b print | grep ' 2 ' | awk '{print $4}' | sed 's/B//g' ) | |
bs=0 | |
start=0 | |
count=0 | |
calc_max_divisor "${block_size}" "${start_block}" "${block_count}" | |
${DEBUG} dd if="${device}" of="${output_device}" bs="${bs}" skip="${start}" count="${count}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment