<===

ProNotes

2025-10-27 15:17:58
**`mount_menu.sh` — интерактивный монтер SquashFS**

- **Глобальные настройки**:  
  `SQSH_DIR` — где искать `*.sqsh`  
  `MOUNT_POINT` — куда монтировать  

- **Алгоритм**:  
  1. **Размонтировать** `MOUNT_POINT` через `sudo umount`.  
     Если занято — показать процессы (`lsof`) и выйти.  
  2. **Собрать** все `*.sqsh` из `SQSH_DIR`.  
     Нет файлов — выйти.  
  3. **Меню**: имена файлов + «Exit».  
     Стрелки + Enter.  
  4. **Монтировать**: `sudo mount -t squashfs <файл> <точка>`  
     Успех — выйти сразу.  
     Ошибка — вернуться в меню.  

Простой, гибкий, безопасный. Требует `sudo` и `lsof`/`fuser`.

-----------------

#!/bin/bash

# Global variables
SQSH_DIR="/dat/sqsh"           # Directory containing *.sqsh files
MOUNT_POINT="/mnt/sqsh"  # Mount point for squashfs files

# Function to unmount the mount point unconditionally
unmount_point() {
  echo "Trying to unmount $MOUNT_POINT..."
  sudo umount "$MOUNT_POINT" 2>/dev/null
  # Check if still mounted
  if mountpoint -q "$MOUNT_POINT"; then
    echo "Unmount failed. Checking if occupied..."
    occupied=$(lsof "$MOUNT_POINT" 2>/dev/null)
    if [ -n "$occupied" ]; then
      echo "Error: $MOUNT_POINT is occupied by processes:"
      echo "$occupied"
      echo "Please close any open files or processes and try again."
      exit 1
    else
      echo "Error: Failed to unmount $MOUNT_POINT for unknown reason."
      exit 1
    fi
  fi
  echo "$MOUNT_POINT is now unmounted and free."
}

# Get list of .sqsh files
get_sqsh_files() {
  sqsh_files=($(ls -1 "$SQSH_DIR"/*.sqsh 2>/dev/null))
  if [ ${#sqsh_files[@]} -eq 0 ]; then
    echo "No *.sqsh files found in $SQSH_DIR. Exiting."
    exit 1
  fi
}

# Array of menu items (will be populated dynamically) and commands
menu_items=()
commands=()

# Populate menu with .sqsh files and Exit
populate_menu() {
  for file in "${sqsh_files[@]}"; do
    basename=$(basename "$file")
    menu_items+=("$basename")
    commands+=("sudo mount -t squashfs $file $MOUNT_POINT")
  done
  menu_items+=("Exit")
  commands+=("exit 0")
}

# Current selected item
current=0

# Function to display the menu
display_menu() {
  clear
  echo "=== SquashFS Mount Menu ==="
  for i in "${!menu_items[@]}"; do
    if [ $i -eq $current ]; then
      echo "> ${menu_items[$i]}"
    else
      echo "  ${menu_items[$i]}"
    fi
  done
  echo "=========================="
  echo "Use arrow keys to navigate, Enter to select, Ctrl+C to quit"
}

# Function to handle key input
read_arrow() {
  read -s -n 1 key
  if [[ $key == $'\x1b' ]]; then
    read -s -n 2 key
    case $key in
      '[A') # Up arrow
        ((current--))
        if [ $current -lt 0 ]; then
          current=$((${#menu_items[@]} - 1))
        fi
        ;;
      '[B') # Down arrow
        ((current++))
        if [ $current -ge ${#menu_items[@]} ]; then
          current=0
        fi
        ;;
    esac
  elif [[ $key == "" ]]; then # Enter key
    execute_command
  fi
}

# Function to execute the selected command
execute_command() {
  clear
  echo "Executing: ${menu_items[$current]}"
  echo "-----------------------------"
  if [ "${commands[$current]}" == "exit 0" ]; then
    exit 0
  fi
  eval "${commands[$current]}"
  if [ $? -ne 0 ]; then
    echo "Error: Command failed! Check permissions or file."
    echo "-----------------------------"
    echo "Press any key to return to menu..."
    read -s -n 1
  else
    echo "Success: Mounted successfully."
    echo "-----------------------------"
    exit 0  # Exit immediately after successful mount
  fi
}

# Main logic
unmount_point
get_sqsh_files
populate_menu

# Main loop
while true; do
  display_menu
  read_arrow
done
← Previous Next →
Back to list