Skip to content

Instantly share code, notes, and snippets.

@risaacson
Last active December 16, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save risaacson/5409137 to your computer and use it in GitHub Desktop.
Save risaacson/5409137 to your computer and use it in GitHub Desktop.
Efficient root fs extraction using dd. As a part of my training I am learning how to create a linux image from a VM installed under KVM. I got to the point of ripping the root fs out of my original image and the instructions had told me to use a block size of 1. It also suggested how it could be sped up and gave the general idea. Finding the gre…
#!/usr/bin/env bash
# set -x
while getopts ":h" opt; do
case ${opt} in
h)
echo "efficent_dd_with_start_and_count.sh INPUT_FILE OUTPUT_FILE START_BYTE BYTE_SIZE"
exit 0
;;
\?)
echo "Fail"
exit 1
;;
esac
done
get_next_multiplier() {
current_multiplier=$1
multiplier=$(( ${current_multiplier} * 2 ))
}
set_variables() {
start=$1
count=$2
byte_size=$3
}
multiplier=1
input_file=$1
output_file=$2
initial_start=$3
initial_count=$4
start=0
count=0
byte_size=${multiplier}
while true; do
start_divided=$(( ${initial_start} / ${multiplier} ))
start_remainder=$(( ${initial_start} % ${multiplier} ))
count_divided=$(( ${initial_count} / ${multiplier} ))
count_remainder=$(( ${initial_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
echo "dd if=${input_file} of=${output_file} bs=${byte_size} start=${start} count=${count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment