#!/bin/bash # Cleanup script for duplicate tmpfs mounts # This script safely removes duplicate tmpfs-cache mounts set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log() { echo -e "${BLUE}[INFO]${NC} $1" } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warn() { echo -e "${YELLOW}[WARNING]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" } check_root() { if [[ $EUID -ne 0 ]]; then error "This script must be run as root" echo "Usage: sudo $0" exit 1 fi } cleanup_duplicate_tmpfs() { log "Checking for duplicate tmpfs-cache mounts..." # Get all tmpfs-cache mount points local all_mounts=$(mount | grep "tmpfs.*tmpfs-cache" | awk '{print $3}') local unique_mounts=$(echo "$all_mounts" | sort -u) local total_count=$(echo "$all_mounts" | wc -l) local unique_count=$(echo "$unique_mounts" | wc -l) if [[ $total_count -eq $unique_count ]]; then success "No duplicate mounts found. System is clean!" echo "" echo "Current tmpfs-cache mounts:" echo "$unique_mounts" | while read mount_point; do size=$(mount | grep "tmpfs.*tmpfs-cache" | grep " $mount_point " | head -1 | grep -o 'size=[^,)]*' | cut -d= -f2 || echo "unknown") echo " โœ… $mount_point ($size)" done return 0 fi warn "Found $total_count total mounts but only $unique_count unique paths" error "Duplicate mounts detected!" echo "" echo "Duplicate analysis:" echo "$all_mounts" | sort | uniq -c | while read count path; do if [[ $count -gt 1 ]]; then error " $path: mounted $count times" else log " $path: mounted once (OK)" fi done echo "" read -p "Do you want to clean up these duplicate mounts? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then log "Cleanup cancelled" return 0 fi log "Cleaning up duplicate tmpfs mounts..." # Unmount all tmpfs-cache mounts echo "$all_mounts" | while read mount_point; do if mountpoint -q "$mount_point"; then log " Unmounting: $mount_point" umount "$mount_point" 2>/dev/null || warn " Failed to unmount $mount_point" fi done # Remove the cache directory structure if [[ -d /tmp/tmpfs-cache ]]; then log "Removing /tmp/tmpfs-cache directory" rm -rf /tmp/tmpfs-cache fi success "Cleanup complete!" echo "" log "You can now run the optimizer to recreate clean tmpfs mounts:" echo " sudo ./one-button-optimizer.sh" } show_current_status() { echo "๐Ÿ” Current tmpfs Mount Status" echo "============================" echo "" # Show all tmpfs mounts local all_tmpfs=$(mount -t tmpfs | wc -l) log "Total tmpfs mounts on system: $all_tmpfs" # Show tmpfs-cache specific mounts local cache_mounts=$(mount | grep "tmpfs.*tmpfs-cache" | wc -l) if [[ $cache_mounts -gt 0 ]]; then log "tmpfs-cache mounts: $cache_mounts" echo "" mount | grep "tmpfs.*tmpfs-cache" | while read line; do mount_point=$(echo "$line" | awk '{print $3}') size=$(echo "$line" | grep -o 'size=[^,)]*' | cut -d= -f2 || echo "unknown") echo " $mount_point ($size)" done else log "No tmpfs-cache mounts found" fi echo "" } main() { echo "๐Ÿงน tmpfs Duplicate Mount Cleanup Tool" echo "=====================================" echo "" check_root show_current_status cleanup_duplicate_tmpfs } main "$@"