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."

View file

@ -4,12 +4,29 @@
BIN_DIR=$(dirname "$(readlink -f "$0")")
DIR=$1
if [[ -z "$DIR" ]]; then
echo "Usage: $0 <directory>"
exit 1
fi
test -d "$DIR"
if [[ $? -ne 0 ]]; then
echo Directory "$DIR" does not exist!
exit -1
exit -1
fi
echo "You need support for FUSE, SquashFS, and OverlayFS."
echo "Checking your system for these requirements..."
for fs in fuse squashfs overlay; do
if ! grep -q "$fs" /proc/filesystems; then
echo "Loading $fs kernel module..."
if ! sudo modprobe "$fs"; then
echo "Error: $fs is not supported and could not be loaded."
exit 1
fi
fi
done
DIRSIZE=$(du -s "$DIR")
DIR=$(readlink -f "$DIR")
@ -32,57 +49,93 @@ 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_UPPER = \"$OVERLAY_UPPER\""
echo "> OVERLAY_LOWER= \"$OVERLAY_LOWER\""
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..."
echo "We already have an existing image, updating it..."
mount-squash-image "$DIR"
DIRSIZE=$(du -s "$DIR")
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"
echo "Copy created, now unmount and swap everything."
umount-squash-image "$DIR"
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"
# Make the dirs for the overlay
mkdir -p "$OVERLAY_UPPER"
mkdir -p "$OVERLAY_LOWER"
mkdir -p "$OVERLAY_WORK"
mkdir -p "$OVERLAY_TARG"
# 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 "Uncompressed: $DIRSIZE"
echo " Compressed: $(du -s ${OVERLAY_ROOT}.img)"
echo "========================================================================="
# Service content embedded
SERVICE_CONTENT="[Unit]
Description=SquashFS Mount for %I
After=local-fs.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=${BIN_DIR}/mount-squash-image %I
ExecStop=${BIN_DIR}/umount-squash-image %I
[Install]
WantedBy=multi-user.target"
echo "$SERVICE_CONTENT" | sudo tee /etc/systemd/system/squash-mount@.service > /dev/null
sudo systemctl daemon-reload
ESC_PATH=$(systemd-escape -p "$DIR")
SERVICE_NAME="squash-mount@-$ESC_PATH"
read -p "Do you want to auto-enable the mount service ($SERVICE_NAME)? [y/N] " yn
case $yn in
[Yy]* )
echo "Enabling and starting service..."
sudo systemctl enable --now "$SERVICE_NAME"
sudo systemctl stop "$SERVICE_NAME"
sudo systemctl start "$SERVICE_NAME"
;;
* )
echo "To enable the service manually, run: sudo systemctl enable --now $SERVICE_NAME"
;;
esac
echo ""
echo "Your SquashFS-backed folder is ready for use."
echo "To mount it manually, either cd into it or use mount-squash-image."
echo ""
echo "Should you wish to update the contents of the image with your changes made"
echo "inside of the folder, simply run make-squash-image again on the same"
echo "folder to update the image."
echo
echo "To disable auto-mounting upon cd, please remove the"
echo ".needs_mount file to prevent that."
echo

View file

@ -1,9 +1,8 @@
#!/bin/bash
echo $@
DIR="$@"
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename $DIR)
DIR_SHORT=$(basename "$DIR")
BIN_DIR=$(dirname "$(readlink -f "$0")")
mkdir -p "$DIR"
OVERLAY_ROOT=$(readlink -f "${DIR}/..")/.squashfs/${DIR_SHORT}
@ -12,9 +11,28 @@ OVERLAY_LOWER=${OVERLAY_ROOT}/lower
OVERLAY_WORK=${OVERLAY_ROOT}/work
OVERLAY_TARG=$DIR
umount-squash-image $@ 1>/dev/null 2>/dev/null
if [ ! -f "${OVERLAY_ROOT}.img" ]; then
echo "Error: SquashFS image ${OVERLAY_ROOT}.img not found." >&2
exit 1
fi
"$BIN_DIR"/umount-squash-image "$@" 1>/dev/null 2>/dev/null
sudo mount "${OVERLAY_ROOT}.img" "$OVERLAY_LOWER" -t squashfs -o loop
sudo mount -t overlay -o lowerdir="$OVERLAY_LOWER",upperdir="$OVERLAY_UPPER",workdir="$OVERLAY_WORK" none "$OVERLAY_TARG"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "Error: Failed to mount squashfs image (exit code $EXIT_CODE)." >&2
exit $EXIT_CODE
fi
echo "SquashFS filesystem is mounted and ready,"
sudo mount -t overlay -o lowerdir="$OVERLAY_LOWER",upperdir="$OVERLAY_UPPER",workdir="$OVERLAY_WORK" none "$OVERLAY_TARG"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "Error: Failed to mount overlay (exit code $EXIT_CODE)." >&2
# Cleanup: unmount the lower squashfs if overlay mount fails
sudo umount "$OVERLAY_LOWER" 2>/dev/null
exit $EXIT_CODE
fi
echo "SquashFS filesystem is mounted and ready."
exit 0

2
own
View file

@ -14,4 +14,4 @@ then
exit 2
fi
sudo chown -R $(whoami):$(whoami) "$@"
sudo chown -R $(whoami):$(whoami) "$@" 1>/dev/null 2>/dev/null

View file

@ -1,8 +1,7 @@
#!/bin/bash
DIR="$@"
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename $DIR)
mkdir -p "$DIR"
DIR_SHORT=$(basename "$DIR")
OVERLAY_ROOT=$(readlink -f "${DIR}/..")/.squashfs/${DIR_SHORT}
OVERLAY_UPPER=${OVERLAY_ROOT}/upper
@ -10,9 +9,8 @@ OVERLAY_LOWER=${OVERLAY_ROOT}/lower
OVERLAY_WORK=${OVERLAY_ROOT}/work
OVERLAY_TARG=$DIR
sudo umount -R "$OVERLAY_ROTO"
sudo umount -R "$OVERLAY_LOWER"
sudo umount -R "$OVERLAY_TARG"
sudo umount -R "$DIR"
sudo umount -R "$OVERLAY_TARG" 2>/dev/null
sudo umount -R "$OVERLAY_LOWER" 2>/dev/null
echo "SquashFS filesystem has been unmounted."
exit 0