Created
January 5, 2026 05:02
-
-
Save Alex4386/9b40250aa9a6e64d53125b07e7f22d66 to your computer and use it in GitHub Desktop.
flatten iso for symlinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ================= CONFIGURATION ================= | |
| SOURCE_DIR="/mnt/pool_name/dataset_name/iso" | |
| PROXMOX_DIR="${SOURCE_DIR}/template/iso" | |
| # true = Always use "Folder-Filename.iso" | |
| # false = Only use prefix if filenames collide | |
| APPEND_DIRNAME_ALWAYS=false | |
| # ================================================= | |
| mkdir -p "$PROXMOX_DIR" | |
| # Temporary file to track which links are valid during this run | |
| VALID_LINKS_LOG=$(mktemp) | |
| echo "Starting Sync..." | |
| echo "Source: $SOURCE_DIR" | |
| echo "Target: $PROXMOX_DIR" | |
| echo "Mode: APPEND_DIRNAME_ALWAYS=$APPEND_DIRNAME_ALWAYS" | |
| # 1. PRE-CALCULATE DUPLICATES (If needed) | |
| if [ "$APPEND_DIRNAME_ALWAYS" = false ]; then | |
| echo "Scanning for duplicates..." | |
| # Get list of filenames that appear more than once | |
| DUPLICATES=$(find "$SOURCE_DIR" -type f -name "*.iso" -not -path "${PROXMOX_DIR}/*" -exec basename {} \; | sort | uniq -d) | |
| fi | |
| # 2. SYNC LOOP (Create & Update) | |
| # We process every source ISO and ensure its link exists and points to the right place. | |
| find "$SOURCE_DIR" -type f -name "*.iso" -not -path "${PROXMOX_DIR}/*" | while read filepath; do | |
| filename=$(basename "$filepath") | |
| parentdir=$(basename "$(dirname "$filepath")") | |
| # --- DETERMINE LINK NAME --- | |
| use_prefix=false | |
| if [ "$APPEND_DIRNAME_ALWAYS" = true ]; then | |
| use_prefix=true | |
| elif echo "$DUPLICATES" | grep -Fxq "$filename"; then | |
| use_prefix=true | |
| fi | |
| if [ "$use_prefix" = true ]; then | |
| linkname="${parentdir}-${filename}" | |
| else | |
| linkname="${filename}" | |
| fi | |
| # Track that this link name is valid | |
| echo "$linkname" >> "$VALID_LINKS_LOG" | |
| # --- CALCULATE RELATIVE PATH --- | |
| # Python is safest for reliable relative path math on TrueNAS | |
| target_rel_path=$(python3 -c "import os.path; print(os.path.relpath('$filepath', '$PROXMOX_DIR'))") | |
| full_link_path="${PROXMOX_DIR}/${linkname}" | |
| # --- CHECK STATE & UPDATE --- | |
| # Check if link exists and where it points | |
| if [ -L "$full_link_path" ]; then | |
| current_target=$(readlink "$full_link_path") | |
| if [ "$current_target" == "$target_rel_path" ]; then | |
| # Link exists and is correct. Do nothing. | |
| continue | |
| fi | |
| echo "Updating changed link: $linkname" | |
| else | |
| echo "Creating new link: $linkname" | |
| fi | |
| # Create/Overwrite the link (-f forces overwrite) | |
| ln -sf "$target_rel_path" "$full_link_path" | |
| done | |
| # 3. CLEANUP ORPHANS | |
| # Delete any symlink in PROXMOX_DIR that was not added to our "VALID_LINKS_LOG" | |
| echo "Cleaning up orphans..." | |
| # We loop through existing links in the target dir | |
| find "$PROXMOX_DIR" -maxdepth 1 -type l -name "*.iso" | while read link_obj; do | |
| link_basename=$(basename "$link_obj") | |
| # Grep the temp file to see if this link was processed in Step 2 | |
| # grep -F (fixed string) -x (exact match) -q (quiet) | |
| if ! grep -Fxq "$link_basename" "$VALID_LINKS_LOG"; then | |
| echo "Removing orphan/stale link: $link_basename" | |
| rm "$link_obj" | |
| fi | |
| done | |
| # Cleanup temp file | |
| rm "$VALID_LINKS_LOG" | |
| echo "Sync Complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment