lackadaisical/make-squash-image
2026-03-08 20:54:07 +01:00

88 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
# Lackadaisical squashfs tools
# Allows you to create a modifiable squashfs-backed image for a folder
BIN_DIR=$(dirname "$(readlink -f "$0")")
DIR=$1
test -d "$DIR"
if [[ $? -ne 0 ]]; then
echo Directory "$DIR" does not exist!
exit -1
fi
DIRSIZE=$(du -s "$DIR")
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename "$DIR")
echo Compressing "$DIR"
# Make basic dirs
mkdir -p "${DIR}"
mkdir -p "${DIR_SHORT}"
OVERLAY_ROOT=$(readlink -f "${DIR}/..")/.squashfs/${DIR_SHORT}
OVERLAY_UPPER=${OVERLAY_ROOT}/upper
OVERLAY_LOWER=${OVERLAY_ROOT}/lower
OVERLAY_WORK=${OVERLAY_ROOT}/work
OVERLAY_TARG=$DIR
RECREATE=false
echo "Overlay information"
echo "========================================================================="
echo "> DIR = \"$DIR\""
echo "> DIR_SHORT = \"$DIR_SHORT\""
echo "> OVERLAY_ROOT = \"$OVERLAY_ROOT\""
echo "> OVERLAY_UPPER = \"$OVERLAY_LOWER\""
echo "> OVERLAY_LOWER= \"$OVERLAY_UPPER\""
echo "> OVERLAY_WORK = \"$OVERLAY_WORK\""
echo "> OVERLAY_TARG = \"$OVERLAY_TARG\""
echo "========================================================================="
# Make the dirs for the overlay
mkdir -p "$OVERLAY_ROOT"
mkdir -p "$OVERLAY_UPPER"
mkdir -p "$OVERLAY_LOWER"
mkdir -p "$OVERLAY_WORK"
mkdir -p "$OVERLAY_TARG"
# Check for existing image
if test -f "${OVERLAY_ROOT}.img"; then
echo "We already have an existing image, updating i..."
RECREATE=true
fi
sudo mksquashfs "$DIR" "${OVERLAY_ROOT}.img.1" -noappend -comp xz
if [ $RECREATE == true ]; then
echo "Copy created, now unmount and swap everything."
sudo rm -rf "$OVERLAY_UPPER"
sudo rm -rf "$OVERLAY_LOWER"
sudo rm -rf "$OVERLAY_WORK"
rm -rf "${OVERLAY_ROOT}.img"
fi
mv "${OVERLAY_ROOT}.img.1" "${OVERLAY_ROOT}.img"
# Reset dir
sudo rm -rf "$DIR"
mkdir -p "$DIR"
touch "${DIR}/.needs_mount"
echo "Your SquashFS-backed folder is ready for use."
echo "To mount it, either cd into it or use mount-squash-image."
echo "We recomnnend setting up a cronjob for that."
echo ""
echo "Should you wish to update the contents of the image with your changes made"
echo "imside of the folder, simply run make-squash-image again on the same"
echo "folder to update the imaghe."
echo
echo "To disable auto-mounting upon cd, pleas remove the"
echo "'.needs_mount' file to prevent that."
echo "To enable mount at-login, add something aking to this to cron:"
echo "\"@reboot <path>/mount-squash-image <folder>\""
echo
echo "========================================================================="
echo "Storage data:"
echo "Uuncompressed: $DIRSIZE"
echo " Compressed: $(du -s ${OVERLAY_ROOT}.img)"
echo "========================================================================="