Pre-refactor

This commit is contained in:
Sam Hardeman 2026-03-13 00:37:28 +01:00
parent 84f55f82b8
commit 2e31283b61
5 changed files with 157 additions and 59 deletions

View file

@ -1,27 +1,56 @@
#!/bin/bash
DIR="$@"
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename $DIR)
# Lackadaisical squashfs tools - Destroy
BIN_DIR=$(dirname "$(readlink -f "$0")")
DIR="$1"
if [[ -f "$DIR/.needs_mount" ]];
then
echo "Not a SquashFS image, exiting."
exit 0
if [[ -z "$DIR" ]]; then
echo "Usage: $0 <directory>"
exit 1
fi
echo "Destroying the existing SquashFS image."
echo "Copying data from the original directory."
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename "$DIR")
echo "Using rsync... in 2 seconds."
sleep 2
rsync -avz --progress "$DIR" "/tmp/$DIR_SHORT.tmp"
if [[ ! -f "$DIR/.needs_mount" ]]; then
if ! mountpoint -q "$DIR"; then
echo "Error: $DIR does not appear to be a SquashFS-backed directory (missing .needs_mount)."
exit 1
fi
fi
umount-squash-image "$DIR"
# Disable systemd service
ESC_PATH=$(systemd-escape -p "$DIR")
SERVICE_NAME="squash-mount@-$ESC_PATH"
rm -rf "$DIR"
mv "/tmp/$DIR_SHORT.tmp" "$DIR"
echo "Disabling systemd service ($SERVICE_NAME)..."
sudo systemctl stop "$SERVICE_NAME" 2>/dev/null
sudo systemctl disable "$SERVICE_NAME" 2>/dev/null
rm -rf "$DIR/../.squashfs/$DIR_SHORT.img"
rm -rf "$DIR/../.squashfs/$DIR_SHORT"
# Ensure it is mounted to copy merged data
echo "Ensuring image is mounted to preserve data..."
"$BIN_DIR"/mount-squash-image "$DIR" 1>/dev/null 2>/dev/null
echo "Destroying SquashFS image and restoring data to normal directory..."
TEMP_DIR=$(mktemp -d)
# Copy data out (preserving all attributes)
sudo rsync -a "$DIR/" "$TEMP_DIR/"
# Unmount the overlay and squashfs
echo "Unmounting..."
"$BIN_DIR"/umount-squash-image "$DIR"
# Restore data to original location
echo "Restoring files..."
sudo rm -rf "$DIR"
sudo mv "$TEMP_DIR" "$DIR"
# Cleanup hidden image and overlay files
OVERLAY_ROOT=$(readlink -f "${DIR}/..")/.squashfs/${DIR_SHORT}
echo "Cleaning up ${OVERLAY_ROOT}..."
sudo rm -rf "${OVERLAY_ROOT}.img"
sudo rm -rf "${OVERLAY_ROOT}"
echo "Success: SquashFS image destroyed and data restored to $DIR."
exit 0
echo "Your SquashFS image was deleted."