drive-dependencies.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/env bash
  2. # mac-ops :: drive-dependencies.sh
  3. # "Is it safe to eject this volume?" — find every reference to a volume
  4. # before you yank the cable / unmount / destroy a snapshot.
  5. #
  6. # Checks:
  7. # - Open files via lsof
  8. # - Spotlight index state
  9. # - Time Machine destination
  10. # - Photos / Music / TV library locations
  11. # - Helper-tool security-scoped bookmarks (best-effort)
  12. # - Symlinks pointing into the volume from common locations
  13. # - Background processes with cwd inside the volume
  14. set -u
  15. TARGET=""
  16. while [[ $# -gt 0 ]]; do
  17. case "$1" in
  18. -v|--volume) TARGET="$2"; shift 2 ;;
  19. --help|-h)
  20. cat <<EOF
  21. Usage: $0 -v <mount-point> [options]
  22. -v, --volume PATH Volume to check (e.g. /Volumes/Backup, /)
  23. --json, --redact, --quiet, --verbose Standard flags
  24. Verdict: "safe to eject" requires PASS on every check. Any FAIL/WARN means
  25. something would break or lose state on disconnect.
  26. EOF
  27. exit 0 ;;
  28. *) shift ;;
  29. esac
  30. done
  31. if [[ -z "$TARGET" ]]; then
  32. echo "Error: -v <mount-point> required (e.g. -v /Volumes/Backup)" >&2
  33. exit 2
  34. fi
  35. if [[ ! -d "$TARGET" ]]; then
  36. echo "Error: $TARGET is not a directory / not mounted" >&2
  37. exit 3
  38. fi
  39. source "$(dirname "$0")/_lib/common.sh"
  40. parse_common_flags "$@"
  41. maybe_filter_self "$@"
  42. note " Volume: $TARGET"
  43. # ----------------------------------------------------------------------------
  44. section "1. OPEN FILES (lsof)"
  45. # ----------------------------------------------------------------------------
  46. # `lsof +D` is recursive and VERY slow on large volumes (especially $HOME).
  47. # Use `lsof` without +D and grep by mount point — much faster, equivalent
  48. # accuracy for "is anything open under this path".
  49. target_real=$(cd "$TARGET" 2>/dev/null && pwd -P || echo "$TARGET")
  50. open_lines=$(lsof -F n 2>/dev/null | grep "^n${target_real}/" 2>/dev/null || true)
  51. open_count=$(printf '%s\n' "$open_lines" | grep -c . 2>/dev/null || echo 0)
  52. if [[ "$open_count" -gt 0 ]]; then
  53. log_fail "Open file handles" "$open_count — unmount will fail or corrupt"
  54. note " Top processes holding files (sample):"
  55. # lsof -F format is column-based; use plain lsof for the process listing
  56. lsof 2>/dev/null | awk -v t="$target_real" '$NF ~ "^"t"/"{print $1, $2}' | sort -u | head -10 | sed 's/^/ /'
  57. else
  58. log_pass "Open file handles" "0"
  59. fi
  60. # ----------------------------------------------------------------------------
  61. section "2. PROCESSES WITH CWD INSIDE VOLUME"
  62. # ----------------------------------------------------------------------------
  63. # Use lsof -c with -d cwd for current working directories
  64. cwd_procs=$(lsof -d cwd 2>/dev/null | awk -v t="$TARGET" '$NF ~ t {print $1, $2}' | sort -u)
  65. if [[ -n "$cwd_procs" ]]; then
  66. cwd_count=$(echo "$cwd_procs" | wc -l | tr -d ' ')
  67. log_warn "Processes with cwd inside" "$cwd_count"
  68. echo "$cwd_procs" | head -5 | sed 's/^/ /'
  69. else
  70. log_pass "Processes with cwd inside" "0"
  71. fi
  72. # ----------------------------------------------------------------------------
  73. section "3. SPOTLIGHT INDEX STATE"
  74. # ----------------------------------------------------------------------------
  75. spotlight_status=$(mdutil -s "$TARGET" 2>/dev/null | tail -1)
  76. note " $spotlight_status"
  77. case "$spotlight_status" in
  78. *"Indexing enabled"*) log_warn "Spotlight indexing" "enabled on this volume — eject may corrupt index" ;;
  79. *"Indexing disabled"*) log_pass "Spotlight indexing" "disabled" ;;
  80. *) log_info "Spotlight indexing" "$spotlight_status" ;;
  81. esac
  82. # ----------------------------------------------------------------------------
  83. section "4. TIME MACHINE DESTINATION CHECK"
  84. # ----------------------------------------------------------------------------
  85. tm_dest=$(tmutil destinationinfo 2>/dev/null | awk -F': *' '/Mount Point/{print $2}')
  86. if [[ "$tm_dest" == "$TARGET" ]] || [[ "$TARGET" == "$tm_dest"/* ]]; then
  87. log_fail "Time Machine destination" "this volume IS the TM target — eject will fail current/next backup"
  88. elif [[ -n "$tm_dest" ]]; then
  89. log_pass "Time Machine destination" "different volume ($tm_dest)"
  90. else
  91. log_pass "Time Machine destination" "none configured"
  92. fi
  93. # Recent TM activity touching this volume
  94. tm_active=$(tmutil currentphase 2>/dev/null)
  95. if [[ "$tm_active" != "BackupNotRunning" ]] && [[ -n "$tm_active" ]]; then
  96. log_warn "Time Machine current phase" "$tm_active — wait before eject"
  97. fi
  98. # ----------------------------------------------------------------------------
  99. section "5. MEDIA LIBRARY LOCATIONS"
  100. # ----------------------------------------------------------------------------
  101. # Photos library
  102. photos_lib=$(defaults read com.apple.Photos UserLibrarySelectionMethod 2>/dev/null || true)
  103. # Best-effort: check common Photos library paths under this volume
  104. photos_libs=$(find "$TARGET" -maxdepth 3 -name "Photos Library.photoslibrary" -type d 2>/dev/null | head -3)
  105. if [[ -n "$photos_libs" ]]; then
  106. log_warn "Photos library detected on volume" "$(echo "$photos_libs" | head -1)"
  107. fi
  108. # Music library
  109. music_libs=$(find "$TARGET" -maxdepth 3 -name "*.musiclibrary" -type d 2>/dev/null | head -3)
  110. if [[ -n "$music_libs" ]]; then
  111. log_warn "Music library detected on volume" "$(echo "$music_libs" | head -1)"
  112. fi
  113. # Final Cut / Logic / iMovie libraries
  114. fcp_libs=$(find "$TARGET" -maxdepth 3 \( -name "*.fcpbundle" -o -name "*.logicx" -o -name "*.imovielibrary" \) -type d 2>/dev/null | head -3)
  115. if [[ -n "$fcp_libs" ]]; then
  116. log_warn "Pro app library detected on volume" "$(echo "$fcp_libs" | head -1)"
  117. fi
  118. # ----------------------------------------------------------------------------
  119. section "6. SYMLINKS POINTING INTO VOLUME"
  120. # ----------------------------------------------------------------------------
  121. # Common places where symlinks land
  122. declare -a check_dirs=(
  123. "$HOME/Documents"
  124. "$HOME/Desktop"
  125. "$HOME/Movies"
  126. "$HOME/Music"
  127. "$HOME/Pictures"
  128. "$HOME/Library/Mobile Documents"
  129. )
  130. symlink_count=0
  131. for d in "${check_dirs[@]}"; do
  132. [[ -d "$d" ]] || continue
  133. found=$(find "$d" -maxdepth 2 -type l 2>/dev/null | while read -r link; do
  134. dest=$(readlink "$link")
  135. [[ "$dest" == "$TARGET"/* ]] && echo "$link -> $dest"
  136. done)
  137. if [[ -n "$found" ]]; then
  138. n=$(echo "$found" | wc -l | tr -d ' ')
  139. symlink_count=$((symlink_count + n))
  140. echo "$found" | head -3 | sed 's/^/ /'
  141. fi
  142. done
  143. if [[ "$symlink_count" -gt 0 ]]; then
  144. log_warn "Symlinks pointing into volume" "$symlink_count — they'll dangle on eject"
  145. else
  146. log_pass "Symlinks pointing into volume" "0"
  147. fi
  148. # ----------------------------------------------------------------------------
  149. section "7. PRIVILEGED HELPER / LAUNCH ITEMS REFERENCING VOLUME"
  150. # ----------------------------------------------------------------------------
  151. # Grep launchd plists for paths inside the target
  152. helper_refs=0
  153. for d in "$HOME/Library/LaunchAgents" /Library/LaunchAgents /Library/LaunchDaemons; do
  154. [[ -d "$d" ]] || continue
  155. matches=$(grep -l "$TARGET" "$d"/*.plist 2>/dev/null || true)
  156. if [[ -n "$matches" ]]; then
  157. helper_refs=$((helper_refs + $(echo "$matches" | wc -l | tr -d ' ')))
  158. echo "$matches" | head -3 | sed 's|^| |'
  159. fi
  160. done
  161. if [[ "$helper_refs" -gt 0 ]]; then
  162. log_warn "Launchd plists referencing volume" "$helper_refs — daemons will fail on eject"
  163. else
  164. log_pass "Launchd plists referencing volume" "0"
  165. fi
  166. # ----------------------------------------------------------------------------
  167. section "8. APP BOOKMARKS / RECENTS"
  168. # ----------------------------------------------------------------------------
  169. # Sandboxed apps store security-scoped bookmarks; we can't decode them without
  170. # the app, but we can list which apps have recents pointing at this volume.
  171. note " (App security-scoped bookmarks aren't directly inspectable — this is informational)"
  172. # ----------------------------------------------------------------------------
  173. emit_summary
  174. if [[ "$JSON_MODE" -eq 0 ]]; then
  175. echo
  176. if [[ "$FAIL_COUNT" -eq 0 ]] && [[ "$WARN_COUNT" -eq 0 ]]; then
  177. echo " ✓ Safe to eject $TARGET — no system references detected."
  178. elif [[ "$FAIL_COUNT" -gt 0 ]]; then
  179. echo " ✗ NOT safe to eject $TARGET — eject will fail or break the items above."
  180. else
  181. echo " ⚠ Ejecting will work, but the items above will dangle or stop working until remount."
  182. fi
  183. fi