#!/bin/bash # Advanced Duplicate tmpfs Cleanup and Consolidation Script # Fixes overlapping and duplicate tmpfs mounts intelligently 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 } analyze_duplicates() { log "Analyzing tmpfs mount duplicates and inefficiencies..." echo "" # Get all cache-related tmpfs mounts local cache_mounts=$(mount | grep tmpfs | grep -E "(cache|apt|thumbnails|browser)" | sort) echo "๐Ÿ” Current cache-related tmpfs mounts:" echo "$cache_mounts" | while IFS= read -r line; do local mount_point=$(echo "$line" | awk '{print $3}') local size=$(echo "$line" | grep -o 'size=[^,)]*' | cut -d= -f2) local usage=$(df -h "$mount_point" | tail -1 | awk '{print $3 " used of " $2}') echo " ๐Ÿ“ $mount_point ($size) - $usage" done echo "" # Identify specific duplicates log "๐Ÿšจ Identified duplicates and inefficiencies:" # Thumbnail duplicates if mount | grep -q "/home/.*/.cache/thumbnails" && mount | grep -q "/tmp/tmpfs-cache/thumbnails"; then error " Thumbnail cache: 2 separate 256MB mounts (512MB total waste)" THUMBNAIL_DUPLICATE=true else THUMBNAIL_DUPLICATE=false fi # APT/Package duplicates if mount | grep -q "/var/cache/apt" && mount | grep -q "/tmp/tmpfs-cache/packages"; then error " APT/Package cache: 2 separate 2GB mounts (2GB waste)" APT_DUPLICATE=true else APT_DUPLICATE=false fi # Browser inefficiency if mount | grep -q "/home/.*/.cache/.*Brave" && mount | grep -q "/tmp/tmpfs-cache/browser"; then error " Browser cache: Direct Brave mount + unused optimization mount (2GB waste)" BROWSER_DUPLICATE=true else BROWSER_DUPLICATE=false fi # Calculate total waste local total_waste=0 [[ $THUMBNAIL_DUPLICATE == true ]] && total_waste=$((total_waste + 256)) [[ $APT_DUPLICATE == true ]] && total_waste=$((total_waste + 2048)) [[ $BROWSER_DUPLICATE == true ]] && total_waste=$((total_waste + 2048)) echo "" if [[ $total_waste -gt 0 ]]; then error "๐Ÿ’ธ Total RAM waste: ${total_waste}MB (~$((total_waste/1024))GB)" echo "" return 1 else success "โœ… No duplicates found - system is optimized!" return 0 fi } fix_thumbnail_duplicates() { if [[ $THUMBNAIL_DUPLICATE != true ]]; then return 0 fi log "๐Ÿ”ง Fixing thumbnail cache duplicates..." # Check which thumbnail mount has data local user_mount="/home/rwiegand/.cache/thumbnails" local cache_mount="/tmp/tmpfs-cache/thumbnails" if [[ -d "$user_mount" ]]; then local user_usage=$(du -sh "$user_mount" 2>/dev/null | cut -f1) log " User thumbnail cache usage: $user_usage" # Keep the user mount, remove the cache mount if mountpoint -q "$cache_mount"; then log " Unmounting unused cache mount: $cache_mount" umount "$cache_mount" fi # Remove the empty directory [[ -d "$cache_mount" ]] && rmdir "$cache_mount" 2>/dev/null || true success " Consolidated to: $user_mount (saved 256MB)" fi } fix_apt_duplicates() { if [[ $APT_DUPLICATE != true ]]; then return 0 fi log "๐Ÿ”ง Fixing APT/package cache duplicates..." local apt_mount="/var/cache/apt" local cache_mount="/tmp/tmpfs-cache/packages" # Check if /var/cache/apt is a bind mount to the cache if mount | grep "$apt_mount" | grep -q "bind"; then log " APT cache is properly bind-mounted - keeping current setup" else # Both are separate mounts - consolidate them log " Consolidating APT cache mounts..." # Unmount the redundant cache mount if mountpoint -q "$cache_mount"; then log " Unmounting redundant cache mount: $cache_mount" umount "$cache_mount" fi # If apt mount exists and cache mount is free, bind mount it if mountpoint -q "$apt_mount" && [[ -d "$cache_mount" ]]; then log " Creating bind mount: $cache_mount -> $apt_mount" mount --bind "$apt_mount" "$cache_mount" fi success " Consolidated APT cache (saved 2GB)" fi } fix_browser_duplicates() { if [[ $BROWSER_DUPLICATE != true ]]; then return 0 fi log "๐Ÿ”ง Fixing browser cache duplicates..." local brave_mount="/home/rwiegand/.cache/BraveSoftware" local cache_mount="/tmp/tmpfs-cache/browser" # Check Brave cache usage if [[ -d "$brave_mount" ]]; then local brave_usage=$(du -sh "$brave_mount" 2>/dev/null | cut -f1) log " Brave cache usage: $brave_usage" # Since Brave already has a direct mount, remove the unused optimization mount if mountpoint -q "$cache_mount"; then log " Unmounting unused browser optimization mount: $cache_mount" umount "$cache_mount" fi # Remove the empty directory [[ -d "$cache_mount" ]] && rmdir "$cache_mount" 2>/dev/null || true success " Kept optimized Brave mount (saved 2GB)" fi } show_optimized_status() { echo "" success "๐ŸŽ‰ Optimization Complete!" echo "" log "๐Ÿ“Š Current optimized tmpfs mounts:" mount | grep tmpfs | grep -E "(cache|apt|thumbnails|browser)" | while IFS= read -r line; do local mount_point=$(echo "$line" | awk '{print $3}') local size=$(echo "$line" | grep -o 'size=[^,)]*' | cut -d= -f2) local usage=$(df -h "$mount_point" | tail -1 | awk '{print $3 " / " $2}') echo " โœ… $mount_point ($size) - $usage" done echo "" local total_tmpfs=$(df | grep tmpfs | awk '{sum+=$2} END {print sum/1024/1024}') log "๐Ÿ“ˆ Total tmpfs usage: ${total_tmpfs}GB" echo "" log "๐Ÿ’ก System is now optimally configured without duplicates!" } main() { echo "๐Ÿงน Advanced tmpfs Duplicate Cleanup & Consolidation" echo "==================================================" echo "" check_root if analyze_duplicates; then show_optimized_status exit 0 fi echo "" read -p "Do you want to fix these duplicates and consolidate the mounts? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then log "Cleanup cancelled" exit 0 fi log "๐Ÿ”ง Starting consolidation process..." echo "" fix_thumbnail_duplicates fix_apt_duplicates fix_browser_duplicates show_optimized_status log "๐Ÿ”„ The system will maintain these optimizations after reboot" } main "$@"