Files
general-scripts-and-system-…/customscripts/pi_create_image
T
2025-10-30 10:20:01 -06:00

73 lines
1.6 KiB
Plaintext

#/bin/bash
timestamp=`date +"%Y-%m-%d-%H%M-%S"`
# handle filename
if [ -z "$1" ]; then
tag=$timestamp-pi
else
tag=$timestamp-$1
fi
# fiel paths
imgname=$PWD/$tag.img
xzname=$imgname.xz
sizes=sizes-$tag.txt
checksums=sha256sum-$tag.sha256
# get real username (not root) if run with sudo
if [ ! -z $SUDO_USER ]; then
username=$SUDO_USER
else
username=$USER
fi
# get disk
lsblk
echo "Enter disk name (including /dev/) ex /dev/sdb"
read dadisk
# start
webhook "starting copy $dadisk to $imgname to $xzname at $(date)($(date +%s))\nsizes: $sizes\nsha256 checksums: $checksums" true
# copy disk
sudo dd if=$dadisk of=$imgname bs=4M status=progress
imgsize=$(du -h $imgname)
# img sha256
sha256sum $imgname | tee $checksums
# shrink and compress the image
webhook "Copied the disk to $imgname ($imgsize), compressing to $xzname"
sudo pishrink.sh -Z -a $imgname
xzsize=$(du -h $xzname)
# xz sha256
webhook "$imgname ($imgsize) shrunk to $xzname ($xzsize), calculating sha256 checksums..."
sha256sum $xzname | tee -a $checksums
# log filesizes
webhook "saving sizes"
echo -e "imgsize: $imgsize\nxzsize: $xzsize" | tee $sizes
# change archive to be friendly perms
webhook "changing perms"
sudo chown $username:$username $xzname
# test archive
webhook "testing archive $xzname"
xz -t $xzname
ret=$?
# if test fails
if [ $ret -gt 0 ]; then
webhook "FAIL! ARCHIVE DES NOT CHECK return code: $ret EXITING"
exit 1
# if test succeeds
else
webhook "Archive Test SUCCESS"
fi
# finish and notify
webhook "DONE\n\tdisk: $dadisk\n\timgname: $imgname ($imgsize)\n\txzname: $xzname ($xzsize)" true
# schedule reboot in 1 minute
sudo shutdown -r +1