#!/bin/bash # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 打印带颜色的消息 print_info() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # 检查是否以 root 运行 check_root() { if [ "$EUID" -ne 0 ]; then print_error "请使用 sudo 运行此脚本" echo "示例: sudo ./mount_fnnas.sh" exit 1 fi } # 检查 cifs 支持 check_cifs_support() { print_info "检查系统是否支持 CIFS/SMB..." # 检查内核是否支持 cifs if lsmod | grep -q cifs; then print_success "CIFS 模块已加载" return 0 fi # 尝试加载 cifs 模块 print_info "尝试加载 CIFS 内核模块..." if modprobe cifs 2>/dev/null; then print_success "CIFS 模块加载成功" return 0 fi print_error "系统不支持 CIFS/SMB 文件系统" print_info "请确保内核支持 cifs,或安装 cifs-utils 包" return 1 } # 检查并安装必要软件包 check_packages() { print_info "检查必要软件包..." # 检查是否安装了 cifs-utils if ! command -v mount.cifs &> /dev/null; then print_warning "cifs-utils 未安装,正在安装..." apt update -qq apt install -y cifs-utils if [ $? -eq 0 ]; then print_success "cifs-utils 安装成功" else print_error "cifs-utils 安装失败" return 1 fi else print_success "cifs-utils 已安装" fi return 0 } # 获取挂载信息 get_mount_info() { echo "" echo "=========================================" echo " 飞牛 NAS SMB 挂载工具" echo "=========================================" echo "" # 获取 NAS IP 地址 read -p "请输入飞牛 NAS 的 IP 地址 (例如: 192.168.1.100): " nas_ip while [[ -z $nas_ip ]]; do print_error "IP 地址不能为空" read -p "请输入飞牛 NAS 的 IP 地址: " nas_ip done # 获取共享文件夹名称 read -p "请输入共享文件夹名称 (例如: myshare): " share_name while [[ -z $share_name ]]; do print_error "共享文件夹名称不能为空" read -p "请输入共享文件夹名称: " share_name done # 获取用户名 read -p "请输入 SMB 用户名 [默认: guest]: " username username=${username:-guest} # 获取密码 read -s -p "请输入密码 (留空则为无密码): " password echo "" # 挂载点 read -p "请输入本地挂载点路径 [默认: /mnt/fnnas]: " mount_point mount_point=${mount_point:-/mnt/fnnas} # 询问是否开机自动挂载 echo "" read -p "是否配置开机自动挂载?(y/n) [默认: n]: " auto_mount auto_mount=${auto_mount:-n} } # 测试连接 test_connection() { print_info "测试与 NAS 的连接..." if ping -c 3 -W 2 "$nas_ip" > /dev/null 2>&1; then print_success "网络连接正常" return 0 else print_error "无法 ping 通 $nas_ip" read -p "是否继续尝试挂载?(y/n): " continue_anyway if [[ $continue_anyway != "y" ]]; then return 1 fi fi } # 创建挂载点 create_mount_point() { if [ ! -d "$mount_point" ]; then print_info "创建挂载点: $mount_point" mkdir -p "$mount_point" if [ $? -eq 0 ]; then print_success "挂载点创建成功" else print_error "挂载点创建失败" return 1 fi else print_info "挂载点已存在: $mount_point" fi return 0 } # 执行挂载 do_mount() { local source="//$nas_ip/$share_name" print_info "正在挂载 $source 到 $mount_point" # 构建挂载选项 local mount_options="uid=$(logname 2>/dev/null || echo 1000),gid=$(logname 2>/dev/null || echo 1000),iocharset=utf8,vers=3.0" if [ -n "$password" ]; then # 有密码的情况 mount -t cifs "$source" "$mount_point" -o username="$username",password="$password",$mount_options else # 无密码(guest)的情况 mount -t cifs "$source" "$mount_point" -o username="$username",password="",$mount_options fi if [ $? -eq 0 ]; then print_success "挂载成功!" print_info "挂载点: $mount_point" return 0 else print_error "挂载失败" print_info "可能的原因:" echo " 1. SMB 协议版本不匹配(尝试 vers=2.0 或 vers=1.0)" echo " 2. 用户名或密码错误" echo " 3. 共享文件夹名称不正确" echo " 4. 防火墙阻止了连接" return 1 fi } # 配置开机自动挂载 setup_auto_mount() { if [[ $auto_mount != "y" ]]; then print_info "跳过开机自动挂载配置" return 0 fi print_info "配置开机自动挂载..." # 创建凭据文件 local cred_file="/etc/samba/fnnas_credentials" echo "username=$username" > "$cred_file" if [ -n "$password" ]; then echo "password=$password" >> "$cred_file" else echo "password=" >> "$cred_file" fi chmod 600 "$cred_file" print_success "凭据文件已创建: $cred_file" # 备份 fstab cp /etc/fstab /etc/fstab.backup print_info "已备份 /etc/fstab 到 /etc/fstab.backup" # 添加到 fstab local source="//$nas_ip/$share_name" local uid=$(logname 2>/dev/null | xargs id -u 2>/dev/null || echo 1000) local gid=$(logname 2>/dev/null | xargs id -g 2>/dev/null || echo 1000) local fstab_entry="$source $mount_point cifs credentials=$cred_file,uid=$uid,gid=$gid,iocharset=utf8,vers=3.0,_netdev 0 0" echo "$fstab_entry" >> /etc/fstab print_success "已添加到 /etc/fstab" print_info "现在可以运行 'mount -a' 测试自动挂载配置" } # 显示挂载信息 show_mount_info() { echo "" echo "=========================================" echo " 挂载信息总结" echo "=========================================" echo -e "NAS 地址: ${GREEN}$nas_ip${NC}" echo -e "共享名称: ${GREEN}$share_name${NC}" echo -e "用户名: ${GREEN}$username${NC}" echo -e "挂载点: ${GREEN}$mount_point${NC}" echo -e "开机自动挂载: ${GREEN}$([[ $auto_mount == "y" ]] && echo "是" || echo "否")${NC}" echo "=========================================" # 显示当前挂载情况 echo "" print_info "当前挂载的文件系统:" df -h | grep -E "Filesystem|$mount_point" || echo " 未找到挂载点" } # 主函数 main() { check_root check_cifs_support if [ $? -ne 0 ]; then exit 1 fi check_packages if [ $? -ne 0 ]; then exit 1 fi get_mount_info test_connection if [ $? -ne 0 ]; then print_error "连接测试失败,退出" exit 1 fi create_mount_point if [ $? -ne 0 ]; then exit 1 fi do_mount if [ $? -eq 0 ]; then setup_auto_mount show_mount_info # 询问是否测试写入 echo "" read -p "是否在挂载点创建测试文件?(y/n) [默认: n]: " test_write if [[ $test_write == "y" ]]; then touch "$mount_point/test_$(date +%s).txt" 2>/dev/null if [ $? -eq 0 ]; then print_success "测试文件创建成功,挂载点可写" else print_warning "无法创建测试文件,请检查权限" fi fi else # 尝试不同的 SMB 版本 echo "" read -p "是否尝试使用 SMB 2.0 协议重试?(y/n): " retry_v2 if [[ $retry_v2 == "y" ]]; then print_info "尝试使用 SMB 2.0 协议..." local source="//$nas_ip/$share_name" local mount_options="uid=$(logname 2>/dev/null || echo 1000),gid=$(logname 2>/dev/null || echo 1000),iocharset=utf8,vers=2.0" if [ -n "$password" ]; then mount -t cifs "$source" "$mount_point" -o username="$username",password="$password",$mount_options else mount -t cifs "$source" "$mount_point" -o username="$username",password="",$mount_options fi if [ $? -eq 0 ]; then print_success "使用 SMB 2.0 挂载成功!" else print_error "仍然失败,请检查网络和共享设置" fi fi fi } # 运行主函数 main