| #!/usr/bin/env bash |
|
|
| usage() { |
| cat <<EOF |
| Usage: $0 -s </path/to/wdl> [-t </path/to/wdl-mount>] [-o <max-ring-overworld>] [-n <max-ring-nether>] [-e <max-ring-end>] [-r </path/to/ring-mount>] [-u] |
| |
| Options: |
| -s </path/to/wdl> Path to directory containing overworld, nether, and end directories (required) |
| -t </path/to/wdl-mount> Mount point for the final merged directory (default: /mnt/wdl) |
| -o <max-ring-overworld> Maximum ring for overworld (default: 33, set to 0 for none) |
| -n <max-ring-nether> Maximum ring for nether (default: 23, set to 0 for none) |
| -e <max-ring-end> Maximum ring for end (default: 8, set to 0 for none) |
| -r </path/to/ring-mount> Mount point for individual rings (default: /mnt/ring) |
| -u Use FUSE-based tools (squashfuse + mergerfs) instead of kernel (squashfs + overlayfs) |
| -h Show this help message |
| EOF |
| exit 1 |
| } |
|
|
| SRC="" |
| MNT="/mnt/wdl" |
| MAX_RING_OVERWORLD=33 |
| MAX_RING_NETHER=23 |
| MAX_RING_END=8 |
| RING_MOUNT="/mnt/ring" |
| USE_FUSE=0 |
|
|
| while getopts "s:t:o:n:e:r:uh" opt; do |
| case "$opt" in |
| s) SRC="$OPTARG" ;; |
| t) MNT="$OPTARG" ;; |
| o) MAX_RING_OVERWORLD="$OPTARG" ;; |
| n) MAX_RING_NETHER="$OPTARG" ;; |
| e) MAX_RING_END="$OPTARG" ;; |
| r) RING_MOUNT="$OPTARG" ;; |
| u) USE_FUSE=1 ;; |
| h) usage ;; |
| *) usage ;; |
| esac |
| done |
|
|
| if [[ -z "$SRC" ]]; then |
| echo "Error: -s </path/to/wdl> is required." >&2 |
| usage |
| fi |
|
|
| [[ -d "$SRC" ]] || { |
| echo "Error: $SRC is not a directory." >&2 |
| exit 1 |
| } |
|
|
| if [[ $USE_FUSE -eq 1 ]]; then |
| command -v mergerfs >/dev/null || { |
| echo "Error: mergerfs is not installed or is not in PATH. Did you mean to enable FUSE-mode (-u)?" >&2 |
| exit 1 |
| } |
| command -v squashfuse >/dev/null || { |
| echo "Error: squashfuse is not installed or is not in PATH. Did you mean to enable FUSE-mode (-u)?" >&2 |
| exit 1 |
| } |
| fi |
|
|
| validate_ring() { |
| local arg="$1" |
| local value="$2" |
|
|
| if [[ ! "$value" =~ ^[0-9]+$ ]]; then |
| echo "Error: -$arg expects a non-negative integer." >&2 |
| exit 1 |
| fi |
| } |
|
|
| validate_ring "o" "$MAX_RING_OVERWORLD" |
| validate_ring "n" "$MAX_RING_NETHER" |
| validate_ring "e" "$MAX_RING_END" |
|
|
| exit_code=0 |
| cleanup() { |
| umount "$MNT" 2>/dev/null |
| for d in "$RING_MOUNT/"*; do |
| [[ -d "$d" ]] && umount "$d" 2>/dev/null |
| done |
| exit "$exit_code" |
| } |
|
|
| trap 'exit_code=$?; cleanup' ERR INT TERM |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| LOWERS=() |
|
|
| mount_ring() { |
| local max=$1 |
| local prefix=$2 |
| local dim=$3 |
|
|
| for ((r=1; r<=max; r++)); do |
| local img="$SRC/$dim/ring$r.squashfs" |
| if [[ ! -f "$img" ]]; then |
| echo "Skipping missing ring $img" |
| continue |
| fi |
| local mnt_dir="$RING_MOUNT/$prefix$r" |
| mkdir -p "$mnt_dir" |
|
|
| if [[ $USE_FUSE -eq 1 ]]; then |
| squashfuse "$img" "$mnt_dir" |
| else |
| mount -t squashfs "$img" "$mnt_dir" |
| fi || { |
| echo "Mount failed for $dim ring$r. Check permissions, filesystem support, or try toggling FUSE-mode (-u)" >&2 |
| exit_code=1 |
| cleanup |
| } |
| LOWERS+=("$prefix$r") |
| done |
| } |
|
|
| mount_ring "$MAX_RING_OVERWORLD" "o" "overworld" |
| mount_ring "$MAX_RING_NETHER" "n" "nether" |
| mount_ring "$MAX_RING_END" "e" "end" |
|
|
| if ((${#LOWERS[@]} == 0)); then |
| echo "No rings were mounted." >&2 |
| exit 1 |
| fi |
|
|
| FINAL_LOWER=$(IFS=:; echo "${LOWERS[*]}") |
|
|
| mkdir -p "$MNT" |
| cd "$RING_MOUNT" || exit 1 |
|
|
| if [[ $USE_FUSE -eq 1 ]]; then |
| mergerfs -o ro "$FINAL_LOWER" "$MNT" |
| else |
| mount -t overlay overlay -o "lowerdir=$FINAL_LOWER,ro" "$MNT" |
| fi || { |
| echo "Mount failed for final merge at $MNT. Check permissions, filesystem support, or try toggling FUSE-mode (-u)" >&2 |
| exit_code=1 |
| cleanup |
| } |
|
|