Skip to content

Instantly share code, notes, and snippets.

@rocapp
Created October 25, 2024 00:10
Show Gist options
  • Save rocapp/537dc2e5e6cf34c8208680c28c2b2e02 to your computer and use it in GitHub Desktop.
Save rocapp/537dc2e5e6cf34c8208680c28c2b2e02 to your computer and use it in GitHub Desktop.
low-level IO using `dd` in parallel.
#!/usr/bin/env /bin/bash
##
# dd_parallel.sh
# Low-level IO to disks using 'dd' in parallel.
# ref: https://unix.stackexchange.com/a/160526/217521
##
usage() {
echo "Usage: $0 -i <input_file> -o <output_file> -b <block_size:512000B> -c <count:1000> -s <status:progress>"
exit 1
}
while getopts ":i:o:b:c:s:" opt; do
case $opt in
i) INFILE="$OPTARG"
;;
o) OUTFILE="$OPTARG"
;;
b) BLOCKSIZE="$OPTARG"
;;
c) COUNT="$OPTARG"
;;
s) STATUS="$OPTARG"
;;
*) usage
;;
esac
done
##
# handle required args
##
if [ -z "$INFILE" ] || [ -z "$OUTFILE" ]; then
usage
fi
##
# Compute the output device size
##
OUTSIZE=`python -c $'import os; p = os.popen("sudo fdisk -l '"${OUTFILE}"'"); print(p.readlines()[0].split(",")[1].strip().split(" ")[0].strip())'`
##
# handle optional args
##
if [ -z "$STATUS" ]; then
STATUS="progress"
fi
if [ -z "$BLOCKSIZE" ]; then
BLOCKSIZE="512000"
fi
if [ -z "$COUNT" ]; then
COUNT="1000"
fi
##
# Execute
##
NSEQ=$(($OUTSIZE / $BLOCKSIZE))
seq 0 $COUNT $NSEQ |
parallel -k \
sudo dd \
if=$INFILE \
bs=$BLOCKSIZE \
skip={} \
conv=sparse \
seek={} \
count=$COUNT \
of=$OUTFILE \
status=$STATUS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment