Skip to content

Instantly share code, notes, and snippets.

@rikka0w0
Last active October 30, 2023 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikka0w0/ef417b89ab2d5321fe0c8b9cd16bfda1 to your computer and use it in GitHub Desktop.
Save rikka0w0/ef417b89ab2d5321fe0c8b9cd16bfda1 to your computer and use it in GitHub Desktop.
[L4D2] Use squashfs to save disk. This is an script that auto mounts images and create symbolic links.
#!/bin/bash
# To make squashfs images, run:
# $ mksquashfs *.vpk /tmp/maps.squashfs -comp xz -all-root
# xz has the best compression ratio, lz4 is faster.
# The destination system should have losetup and kmod-loop to mount squashfs in kernel.
# Run this script as root.
# Debian/Ubuntu prerequisite: apt install squashfuse
# OpenVZ: make sure /dev/fuse exists, otherwise see https://iserversupport.com/forum/virtualization/enable-fuse-on-openvz/
# Point to the L4D2 addons directory
L4D2_ADDON_DIR=
# Specifies where the VPKs and squashfs images are located
IMG_DIR=$(pwd)
# The owner of the symbolic links
OWNER=rikka
# Specifies where to mount the squashfs images
MOUNT_DIR=$(pwd)/mounts
# ln command options
LN_OPTIONS="-srv"
# Unlink old maps
for map in "$L4D2_ADDON_DIR"/*.vpk; do
map_filename=$(basename "$map")
echo "Unlinking $map_filename"
unlink "$map" || true
done
# Make sure the directory exists and is empty
if [ -d "$MOUNT_DIR" ]; then
# Attempt to unmount old mount points
for mount_point in "$MOUNT_DIR"/*; do
if [ -d "$mount_point" ]; then
umount "$mount_point" || true
fi
done
# Remove old mount points and empty folders
find "$MOUNT_DIR"/* -maxdepth 1 -type d -empty -delete 2>/dev/null
if [[ -d "$MOUNT_DIR" && -z "$(ls -A "$MOUNT_DIR")" ]]; then
echo "All old mount points removed."
else
echo "Mount folder(\"$MOUNT_DIR\") is not empty after umount!"
exit -1
fi
fi
# Check if we are running the clean command only
if [[ $1 == "clean" ]]; then
exit 0
fi
echo ""
echo "Mounting new images"
for image_path in "$IMG_DIR"/*.squashfs
do
image_filename=$(basename -s .squashfs "$image_path")
if [[ $image_filename == '*' ]]; then
echo "No squashfs image (*.squashfs) found!"
break
fi
echo "Mounting \"$(basename "$image_path")\":"
mount_path="$MOUNT_DIR/$image_filename"
sudo -u "$OWNER" mkdir -p "$mount_path"
if squashfuse -o allow_other "$image_path" "$mount_path"; then
sudo -u "$OWNER" ln $LN_OPTIONS "$mount_path"/*.vpk "$L4D2_ADDON_DIR"
fi
echo ""
done
echo ""
echo "Mount sparse maps"
find "$IMG_DIR"/*.vpk -maxdepth 1 -type f 1>/dev/null 2>/dev/null && sudo -u "$OWNER" ln $LN_OPTIONS "$IMG_DIR"/*.vpk "$L4D2_ADDON_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment