-
-
Save jadonk/52733f2c8ab003c184f3d576516dbe64 to your computer and use it in GitHub Desktop.
Quickly backup and deploy bootable SD cards with variable size ext4 root partitions
This file contains hidden or 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
#!/bin/sh | |
set -e | |
[ -z "$PART_PREFIX" ] && PART_PREFIX='' | |
DD_ARGS='bs=4K status=progress conv=fsync' | |
backup() { | |
src="$1" | |
dst="$2" | |
sfdisk -d "${src}" > "${dst}-table.txt" | |
e2image -ra -p "${src}${PART_PREFIX}2" "${dst}-2.img" | |
e2fsck -f "${dst}-2.img" | |
resize2fs -M -p "${dst}-2.img" | |
dd if="${src}${PART_PREFIX}1" of="${dst}-1.img" ${DD_ARGS} | |
} | |
deploy() { | |
src="$1" | |
dst="$2" | |
sfdisk "${dst}" < "${src}-table.txt" | |
e2image -ra -p "${src}-2.img" "${dst}${PART_PREFIX}2" | |
e2fsck -f "${dst}${PART_PREFIX}2" | |
resize2fs -p "${dst}${PART_PREFIX}2" | |
dd if="${src}-1.img" of="${dst}${PART_PREFIX}1" ${DD_ARGS} | |
} | |
disk_image() { | |
src="$1" | |
dst="$2" | |
start_sector_list=$( | |
grep -o -P "(?<=start=)\s*\d+" "${src}-table.txt" | tr -d ' ' | |
) | |
last_sector=$(echo "${start_sector_list}" | tail -n 1) | |
dd if=/dev/zero of="${dst}.img" count="${last_sector}" | |
partition_index=1 | |
for start_sector in ${start_sector_list}; do | |
echo "Copying partition ${partition_index} ..." | |
dd if="${src}-${partition_index}.img" of="${dst}.img" \ | |
seek="${start_sector}" ${DD_ARGS} | |
partition_index=$((partition_index+1)) | |
done | |
sfdisk "${dst}.img" < "${src}-table.txt" | |
} | |
help=" | |
[backup/deploy/disk_image] [src] [dst] | |
Example: backup /dev/sdc working-image | |
" | |
if [ $# -eq 3 ]; then | |
case $1 in | |
"backup") | |
backup "$2" "$3";; | |
"deploy") | |
deploy "$2" "$3";; | |
"disk_image") | |
disk_image "$2" "$3";; | |
*) | |
echo "$help" | |
esac | |
else | |
echo "$help" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment