diff --git a/customscripts/pi_create_image_from_img b/customscripts/pi_create_image_from_img new file mode 100644 index 0000000..e8b15f5 --- /dev/null +++ b/customscripts/pi_create_image_from_img @@ -0,0 +1,50 @@ +#!/bin/bash +set -e # fail on any error +mountpoint=/mnt/img + +if [ -z "$1" ]; then + echo "Usage: pi_create_image_from_img path/to/file.img" + exit 1 +fi + +if [ ! -f "$1" ]; then + echo "$1 not found" + exit 1 +fi + +if [ ! -d "$mountpoint" ]; then + echo "$mountpoint not found, creating" + sudo mkdir -p "$mountpoint" +fi + +imgpath="$(realpath $1)" +imgdir="$(dirname $imgpath)" +xzpath="$imgpath.xz" +renamepath="${imgpath:0:-4}_$(date -I).img.xz" + +echo -e "\nsettings:\n\tmountpoint: $mountpoint\n\timgpath: $imgpath\n\timgdir: $imgdir\n\txzpath: $xzpath\n\trenamepath: $renamepath\n" +cd "$imgdir" + +echo "mapping $imgpath" +sudo kpartx -av "$imgpath" + +echo "mounting $imgpath" +sudo mount /dev/mapper/loop0p2 "$mountpoint" + +cd "$mountpoint" +echo "now edit the image pwd: $PWD" +read teeeemp + +echo "unmounting $mountpoint" +sudo umount "$mountpoint" + +echo "unmapping $imgpath" +sudo kpartx -dv "$imgpath" + +echo "shrinking $imgpath to $imgpath.xz" +sudo pishrink -Z -r -v -a "$imgpath" + +echo "renaming $xzpath to $renamepath" +mv "$xzpath" "$renamepath" + +# rm "$imgpath"