add support to resize and create image from trcard

This commit is contained in:
CuttySark1869 2023-03-05 19:42:53 +00:00
parent 48d175607e
commit 7d4bd18564
1 changed files with 66 additions and 38 deletions

View File

@ -161,7 +161,7 @@ EOF1
help() { help() {
local help local help
read -r -d '' help << EOM read -r -d '' help << EOM
Usage: $0 [-adhrsvzZ] imagefile.img [newimagefile.img] Usage: $0 [-adhrsvzZn] imagefile.img [newimagefile.img]
-s Don't expand filesystem when image is booted the first time -s Don't expand filesystem when image is booted the first time
-v Be verbose -v Be verbose
@ -170,6 +170,7 @@ Usage: $0 [-adhrsvzZ] imagefile.img [newimagefile.img]
-Z Compress image after shrinking with xz -Z Compress image after shrinking with xz
-a Compress image in parallel using multiple cores -a Compress image in parallel using multiple cores
-d Write debug messages in a debug log file -d Write debug messages in a debug log file
-n Accept an input that is not a file (e.g. a tf card)
EOM EOM
echo "$help" echo "$help"
exit 1 exit 1
@ -181,8 +182,9 @@ repair=false
parallel=false parallel=false
verbose=false verbose=false
ziptool="" ziptool=""
notfile=false
while getopts ":adhrsvzZ" opt; do while getopts ":adhrsvzZn" opt; do
case "${opt}" in case "${opt}" in
a) parallel=true;; a) parallel=true;;
d) debug=true;; d) debug=true;;
@ -192,6 +194,7 @@ while getopts ":adhrsvzZ" opt; do
v) verbose=true;; v) verbose=true;;
z) ziptool="gzip";; z) ziptool="gzip";;
Z) ziptool="xz";; Z) ziptool="xz";;
n) notfile=true;;
*) help;; *) help;;
esac esac
done done
@ -215,10 +218,14 @@ if [[ -z "$img" ]]; then
help help
fi fi
if [[ ! -f "$img" ]]; then if [ "$notfile" = false ]; then
error $LINENO "$img is not a file..." echo "$notfile"
exit 2 if [[ ! -f "$img" ]]; then
error $LINENO "$img is not a file..."
exit 2
fi
fi fi
if (( EUID != 0 )); then if (( EUID != 0 )); then
error $LINENO "You need to be running as root." error $LINENO "You need to be running as root."
exit 3 exit 3
@ -257,19 +264,21 @@ done
#Copy to new file if requested #Copy to new file if requested
if [ -n "$2" ]; then if [ -n "$2" ]; then
f="$2" if [ "$notfile" = false ]; then #Skip copy if input is device
if [[ -n $ziptool && "${f##*.}" == "${ZIPEXTENSIONS[$ziptool]}" ]]; then # remove zip extension if zip requested because zip tool will complain about extension f="$2"
f="${f%.*}" if [[ -n $ziptool && "${f##*.}" == "${ZIPEXTENSIONS[$ziptool]}" ]]; then # remove zip extension if zip requested because zip tool will complain about extension
fi f="${f%.*}"
info "Copying $1 to $f..." fi
cp --reflink=auto --sparse=always "$1" "$f" info "Copying $1 to $f..."
if (( $? != 0 )); then cp --reflink=auto --sparse=always "$1" "$f"
error $LINENO "Could not copy file..." if (( $? != 0 )); then
exit 5 error $LINENO "Could not copy file..."
fi exit 5
old_owner=$(stat -c %u:%g "$1") fi
chown "$old_owner" "$f" old_owner=$(stat -c %u:%g "$1")
img="$f" chown "$old_owner" "$f"
img="$f"
fi
fi fi
# cleanup at script exit # cleanup at script exit
@ -288,17 +297,21 @@ fi
partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)" partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)"
partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')" partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')"
if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ]; then if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ]; then
parttype="primary" parttype="primary"
else else
parttype="logical" parttype="logical"
fi
if [ "$notfile" = false ]; then
loopback="$(losetup -f --show -o "$partstart" "$img")"
else
loopback=""$img""2""
fi fi
loopback="$(losetup -f --show -o "$partstart" "$img")"
tune2fs_output="$(tune2fs -l "$loopback")" tune2fs_output="$(tune2fs -l "$loopback")"
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
echo "$tune2fs_output" echo "$tune2fs_output"
error $LINENO "tune2fs failed. Unable to shrink this type of image" error $LINENO "tune2fs failed. Unable to shrink this type of image"
exit 7 exit 7
fi fi
currentsize="$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)" currentsize="$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)"
@ -374,21 +387,36 @@ if (( $rc )); then
fi fi
#Truncate the file #Truncate the file
info "Shrinking image" if [ "$notfile" = false ]; then
endresult=$(parted -ms "$img" unit B print free) info "Shrinking image"
rc=$? endresult=$(parted -ms "$img" unit B print free)
if (( $rc )); then rc=$?
error $LINENO "parted failed with rc $rc" if (( $rc )); then
exit 15 error $LINENO "parted failed with rc $rc"
fi exit 15
fi
endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B') endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
logVariables $LINENO endresult logVariables $LINENO endresult
truncate -s "$endresult" "$img" truncate -s "$endresult" "$img"
rc=$? rc=$?
if (( $rc )); then if (( $rc )); then
error $LINENO "trunate failed with rc $rc" error $LINENO "trunate failed with rc $rc"
exit 16 exit 16
fi
else #Skip truncate if input is device
if [ -n "$2" ]; then
target="$2"
info "Creating image"
fdiskresult=$(fdisk -l "$img")
count=$(tail -1 <<< "$fdiskresult" | tr -s ' ' | cut -d ' ' -f 3)
bs=$(head -3 <<< "$fdiskresult" | tail -1 | tr -s ' ' | rev | cut -d ' ' -f 2 | rev)
dd if="$img" of="$target" bs="$bs" count="$count"
img="$target"
else
info "File system shrinked from $beforesize to $aftersize, no image created"
exit 0
fi
fi fi
# handle compression # handle compression