#!/bin/bash # NAS Mount Setup Script for Fedora 42 # This script helps set up persistent mounting of your NAS at 192.168.68.51 set -e # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Configuration NAS_IP="192.168.68.51" MOUNT_POINT="/mnt/share/media" CREDENTIALS_FILE="/etc/nas-credentials" FSTAB_BACKUP="/etc/fstab.backup.$(date +%Y%m%d_%H%M%S)" # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running as root check_root() { if [[ $EUID -eq 0 ]]; then log_error "This script should not be run as root. Please run as a regular user." log_info "The script will use sudo when needed." exit 1 fi } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." # Check if required packages are installed if ! rpm -q cifs-utils >/dev/null 2>&1; then log_error "cifs-utils package is not installed" log_info "Install it with: sudo dnf install cifs-utils" exit 1 fi if ! rpm -q nfs-utils >/dev/null 2>&1; then log_error "nfs-utils package is not installed" log_info "Install it with: sudo dnf install nfs-utils" exit 1 fi # Check network connectivity if ! ping -c 1 "$NAS_IP" >/dev/null 2>&1; then log_error "Cannot reach NAS at $NAS_IP" exit 1 fi log_success "All prerequisites met" } # Test SMB shares test_smb_shares() { log_info "Testing SMB shares on $NAS_IP..." # First try to get shares without credentials (guest access) log_info "Trying guest access first..." if smbclient -L "$NAS_IP" -N 2>/dev/null; then return 0 fi log_info "Guest access failed. Authentication required." echo "Please enter your NAS username:" read -r username if [[ -n "$username" ]]; then echo "Enter password for $username:" read -rs password echo "" log_info "Listing SMB shares with authentication..." if smbclient -L "$NAS_IP" -U "$username%$password" 2>/dev/null; then # Store credentials for later use SMB_USERNAME="$username" SMB_PASSWORD="$password" return 0 else log_warning "Authentication failed or no shares found" log_info "Please check your username and password" return 1 fi else log_error "Username is required for this NAS" return 1 fi } # Test NFS exports test_nfs_exports() { log_info "Testing NFS exports on $NAS_IP..." local exports exports=$(showmount -e "$NAS_IP" 2>/dev/null) if [[ -n "$exports" ]]; then echo "$exports" return 0 else log_warning "No NFS exports found or access denied" log_info "NFS exports are directory paths on the NAS that are shared via NFS protocol" log_info "Common examples: /volume1/media, /mnt/storage, /export/data" log_info "Your NAS appears to use SMB/CIFS instead of NFS" return 1 fi } # Setup SMB mount setup_smb_mount() { local share_name="$1" local username="$2" local password="$3" log_info "Setting up SMB mount for share: $share_name" # Create credentials file if authentication is needed if [[ -n "$username" ]]; then log_info "Creating credentials file at $CREDENTIALS_FILE" sudo tee "$CREDENTIALS_FILE" >/dev/null </dev/null log_success "Added mount entry to fstab" # Create systemd mount unit for better boot integration create_systemd_mount_unit "$fs_type" "$device" "$options" } # Create systemd mount unit create_systemd_mount_unit() { local fs_type="$1" local device="$2" local options="$3" local unit_name="mnt-share-media.mount" local unit_file="/etc/systemd/system/$unit_name" log_info "Creating systemd mount unit: $unit_name" sudo tee "$unit_file" >/dev/null </dev/null; then log_success "Write access confirmed" rm -f "$test_file" else log_warning "Mount is read-only or write access denied" fi # Show mount info df -h "$MOUNT_POINT" echo "" read -rp "Do you want to unmount for now? (Y/n): " unmount_now if [[ ! "$unmount_now" =~ ^[Nn]$ ]]; then sudo umount "$MOUNT_POINT" log_info "Unmounted. Use 'sudo mount $MOUNT_POINT' to mount manually." fi else log_error "Mount failed!" exit 1 fi } # Show usage instructions show_usage_instructions() { log_success "=== NAS Mount Setup Complete ===" echo "" log_info "Your NAS is now configured for persistent mounting." echo "" echo "Usage instructions:" echo "• Manual mount: sudo mount $MOUNT_POINT" echo "• Manual unmount: sudo umount $MOUNT_POINT" echo "• The mount will be available after reboot via systemd" echo "" echo "Files and locations:" echo "• Mount point: $MOUNT_POINT" echo "• Fstab backup: $FSTAB_BACKUP" if [[ -f "$CREDENTIALS_FILE" ]]; then echo "• Credentials file: $CREDENTIALS_FILE (secure, 600 permissions)" fi echo "• Systemd unit: /etc/systemd/system/mnt-share-media.mount" echo "" log_info "To enable automatic mounting on boot:" echo "sudo systemctl start mnt-share-media.mount" echo "" log_warning "Remember to test the mount after a reboot to ensure it works correctly." } # Main function main() { check_root check_prerequisites interactive_setup test_mount show_usage_instructions } # Run main function main "$@"