mount-smb.sh
· 8.6 KiB · Bash
Raw
#!/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
| 1 | #!/bin/bash |
| 2 | |
| 3 | # 颜色定义 |
| 4 | RED='\033[0;31m' |
| 5 | GREEN='\033[0;32m' |
| 6 | YELLOW='\033[1;33m' |
| 7 | BLUE='\033[0;34m' |
| 8 | NC='\033[0m' # No Color |
| 9 | |
| 10 | # 打印带颜色的消息 |
| 11 | print_info() { |
| 12 | echo -e "${BLUE}[INFO]${NC} $1" |
| 13 | } |
| 14 | |
| 15 | print_success() { |
| 16 | echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 17 | } |
| 18 | |
| 19 | print_warning() { |
| 20 | echo -e "${YELLOW}[WARNING]${NC} $1" |
| 21 | } |
| 22 | |
| 23 | print_error() { |
| 24 | echo -e "${RED}[ERROR]${NC} $1" |
| 25 | } |
| 26 | |
| 27 | # 检查是否以 root 运行 |
| 28 | check_root() { |
| 29 | if [ "$EUID" -ne 0 ]; then |
| 30 | print_error "请使用 sudo 运行此脚本" |
| 31 | echo "示例: sudo ./mount_fnnas.sh" |
| 32 | exit 1 |
| 33 | fi |
| 34 | } |
| 35 | |
| 36 | # 检查 cifs 支持 |
| 37 | check_cifs_support() { |
| 38 | print_info "检查系统是否支持 CIFS/SMB..." |
| 39 | |
| 40 | # 检查内核是否支持 cifs |
| 41 | if lsmod | grep -q cifs; then |
| 42 | print_success "CIFS 模块已加载" |
| 43 | return 0 |
| 44 | fi |
| 45 | |
| 46 | # 尝试加载 cifs 模块 |
| 47 | print_info "尝试加载 CIFS 内核模块..." |
| 48 | if modprobe cifs 2>/dev/null; then |
| 49 | print_success "CIFS 模块加载成功" |
| 50 | return 0 |
| 51 | fi |
| 52 | |
| 53 | print_error "系统不支持 CIFS/SMB 文件系统" |
| 54 | print_info "请确保内核支持 cifs,或安装 cifs-utils 包" |
| 55 | return 1 |
| 56 | } |
| 57 | |
| 58 | # 检查并安装必要软件包 |
| 59 | check_packages() { |
| 60 | print_info "检查必要软件包..." |
| 61 | |
| 62 | # 检查是否安装了 cifs-utils |
| 63 | if ! command -v mount.cifs &> /dev/null; then |
| 64 | print_warning "cifs-utils 未安装,正在安装..." |
| 65 | apt update -qq |
| 66 | apt install -y cifs-utils |
| 67 | if [ $? -eq 0 ]; then |
| 68 | print_success "cifs-utils 安装成功" |
| 69 | else |
| 70 | print_error "cifs-utils 安装失败" |
| 71 | return 1 |
| 72 | fi |
| 73 | else |
| 74 | print_success "cifs-utils 已安装" |
| 75 | fi |
| 76 | return 0 |
| 77 | } |
| 78 | |
| 79 | # 获取挂载信息 |
| 80 | get_mount_info() { |
| 81 | echo "" |
| 82 | echo "=========================================" |
| 83 | echo " 飞牛 NAS SMB 挂载工具" |
| 84 | echo "=========================================" |
| 85 | echo "" |
| 86 | |
| 87 | # 获取 NAS IP 地址 |
| 88 | read -p "请输入飞牛 NAS 的 IP 地址 (例如: 192.168.1.100): " nas_ip |
| 89 | while [[ -z $nas_ip ]]; do |
| 90 | print_error "IP 地址不能为空" |
| 91 | read -p "请输入飞牛 NAS 的 IP 地址: " nas_ip |
| 92 | done |
| 93 | |
| 94 | # 获取共享文件夹名称 |
| 95 | read -p "请输入共享文件夹名称 (例如: myshare): " share_name |
| 96 | while [[ -z $share_name ]]; do |
| 97 | print_error "共享文件夹名称不能为空" |
| 98 | read -p "请输入共享文件夹名称: " share_name |
| 99 | done |
| 100 | |
| 101 | # 获取用户名 |
| 102 | read -p "请输入 SMB 用户名 [默认: guest]: " username |
| 103 | username=${username:-guest} |
| 104 | |
| 105 | # 获取密码 |
| 106 | read -s -p "请输入密码 (留空则为无密码): " password |
| 107 | echo "" |
| 108 | |
| 109 | # 挂载点 |
| 110 | read -p "请输入本地挂载点路径 [默认: /mnt/fnnas]: " mount_point |
| 111 | mount_point=${mount_point:-/mnt/fnnas} |
| 112 | |
| 113 | # 询问是否开机自动挂载 |
| 114 | echo "" |
| 115 | read -p "是否配置开机自动挂载?(y/n) [默认: n]: " auto_mount |
| 116 | auto_mount=${auto_mount:-n} |
| 117 | } |
| 118 | |
| 119 | # 测试连接 |
| 120 | test_connection() { |
| 121 | print_info "测试与 NAS 的连接..." |
| 122 | if ping -c 3 -W 2 "$nas_ip" > /dev/null 2>&1; then |
| 123 | print_success "网络连接正常" |
| 124 | return 0 |
| 125 | else |
| 126 | print_error "无法 ping 通 $nas_ip" |
| 127 | read -p "是否继续尝试挂载?(y/n): " continue_anyway |
| 128 | if [[ $continue_anyway != "y" ]]; then |
| 129 | return 1 |
| 130 | fi |
| 131 | fi |
| 132 | } |
| 133 | |
| 134 | # 创建挂载点 |
| 135 | create_mount_point() { |
| 136 | if [ ! -d "$mount_point" ]; then |
| 137 | print_info "创建挂载点: $mount_point" |
| 138 | mkdir -p "$mount_point" |
| 139 | if [ $? -eq 0 ]; then |
| 140 | print_success "挂载点创建成功" |
| 141 | else |
| 142 | print_error "挂载点创建失败" |
| 143 | return 1 |
| 144 | fi |
| 145 | else |
| 146 | print_info "挂载点已存在: $mount_point" |
| 147 | fi |
| 148 | return 0 |
| 149 | } |
| 150 | |
| 151 | # 执行挂载 |
| 152 | do_mount() { |
| 153 | local source="//$nas_ip/$share_name" |
| 154 | |
| 155 | print_info "正在挂载 $source 到 $mount_point" |
| 156 | |
| 157 | # 构建挂载选项 |
| 158 | local mount_options="uid=$(logname 2>/dev/null || echo 1000),gid=$(logname 2>/dev/null || echo 1000),iocharset=utf8,vers=3.0" |
| 159 | |
| 160 | if [ -n "$password" ]; then |
| 161 | # 有密码的情况 |
| 162 | mount -t cifs "$source" "$mount_point" -o username="$username",password="$password",$mount_options |
| 163 | else |
| 164 | # 无密码(guest)的情况 |
| 165 | mount -t cifs "$source" "$mount_point" -o username="$username",password="",$mount_options |
| 166 | fi |
| 167 | |
| 168 | if [ $? -eq 0 ]; then |
| 169 | print_success "挂载成功!" |
| 170 | print_info "挂载点: $mount_point" |
| 171 | return 0 |
| 172 | else |
| 173 | print_error "挂载失败" |
| 174 | print_info "可能的原因:" |
| 175 | echo " 1. SMB 协议版本不匹配(尝试 vers=2.0 或 vers=1.0)" |
| 176 | echo " 2. 用户名或密码错误" |
| 177 | echo " 3. 共享文件夹名称不正确" |
| 178 | echo " 4. 防火墙阻止了连接" |
| 179 | return 1 |
| 180 | fi |
| 181 | } |
| 182 | |
| 183 | # 配置开机自动挂载 |
| 184 | setup_auto_mount() { |
| 185 | if [[ $auto_mount != "y" ]]; then |
| 186 | print_info "跳过开机自动挂载配置" |
| 187 | return 0 |
| 188 | fi |
| 189 | |
| 190 | print_info "配置开机自动挂载..." |
| 191 | |
| 192 | # 创建凭据文件 |
| 193 | local cred_file="/etc/samba/fnnas_credentials" |
| 194 | echo "username=$username" > "$cred_file" |
| 195 | if [ -n "$password" ]; then |
| 196 | echo "password=$password" >> "$cred_file" |
| 197 | else |
| 198 | echo "password=" >> "$cred_file" |
| 199 | fi |
| 200 | chmod 600 "$cred_file" |
| 201 | print_success "凭据文件已创建: $cred_file" |
| 202 | |
| 203 | # 备份 fstab |
| 204 | cp /etc/fstab /etc/fstab.backup |
| 205 | print_info "已备份 /etc/fstab 到 /etc/fstab.backup" |
| 206 | |
| 207 | # 添加到 fstab |
| 208 | local source="//$nas_ip/$share_name" |
| 209 | local uid=$(logname 2>/dev/null | xargs id -u 2>/dev/null || echo 1000) |
| 210 | local gid=$(logname 2>/dev/null | xargs id -g 2>/dev/null || echo 1000) |
| 211 | local fstab_entry="$source $mount_point cifs credentials=$cred_file,uid=$uid,gid=$gid,iocharset=utf8,vers=3.0,_netdev 0 0" |
| 212 | |
| 213 | echo "$fstab_entry" >> /etc/fstab |
| 214 | |
| 215 | print_success "已添加到 /etc/fstab" |
| 216 | print_info "现在可以运行 'mount -a' 测试自动挂载配置" |
| 217 | } |
| 218 | |
| 219 | # 显示挂载信息 |
| 220 | show_mount_info() { |
| 221 | echo "" |
| 222 | echo "=========================================" |
| 223 | echo " 挂载信息总结" |
| 224 | echo "=========================================" |
| 225 | echo -e "NAS 地址: ${GREEN}$nas_ip${NC}" |
| 226 | echo -e "共享名称: ${GREEN}$share_name${NC}" |
| 227 | echo -e "用户名: ${GREEN}$username${NC}" |
| 228 | echo -e "挂载点: ${GREEN}$mount_point${NC}" |
| 229 | echo -e "开机自动挂载: ${GREEN}$([[ $auto_mount == "y" ]] && echo "是" || echo "否")${NC}" |
| 230 | echo "=========================================" |
| 231 | |
| 232 | # 显示当前挂载情况 |
| 233 | echo "" |
| 234 | print_info "当前挂载的文件系统:" |
| 235 | df -h | grep -E "Filesystem|$mount_point" || echo " 未找到挂载点" |
| 236 | } |
| 237 | |
| 238 | # 主函数 |
| 239 | main() { |
| 240 | check_root |
| 241 | check_cifs_support |
| 242 | if [ $? -ne 0 ]; then |
| 243 | exit 1 |
| 244 | fi |
| 245 | |
| 246 | check_packages |
| 247 | if [ $? -ne 0 ]; then |
| 248 | exit 1 |
| 249 | fi |
| 250 | |
| 251 | get_mount_info |
| 252 | test_connection |
| 253 | if [ $? -ne 0 ]; then |
| 254 | print_error "连接测试失败,退出" |
| 255 | exit 1 |
| 256 | fi |
| 257 | |
| 258 | create_mount_point |
| 259 | if [ $? -ne 0 ]; then |
| 260 | exit 1 |
| 261 | fi |
| 262 | |
| 263 | do_mount |
| 264 | if [ $? -eq 0 ]; then |
| 265 | setup_auto_mount |
| 266 | show_mount_info |
| 267 | |
| 268 | # 询问是否测试写入 |
| 269 | echo "" |
| 270 | read -p "是否在挂载点创建测试文件?(y/n) [默认: n]: " test_write |
| 271 | if [[ $test_write == "y" ]]; then |
| 272 | touch "$mount_point/test_$(date +%s).txt" 2>/dev/null |
| 273 | if [ $? -eq 0 ]; then |
| 274 | print_success "测试文件创建成功,挂载点可写" |
| 275 | else |
| 276 | print_warning "无法创建测试文件,请检查权限" |
| 277 | fi |
| 278 | fi |
| 279 | else |
| 280 | # 尝试不同的 SMB 版本 |
| 281 | echo "" |
| 282 | read -p "是否尝试使用 SMB 2.0 协议重试?(y/n): " retry_v2 |
| 283 | if [[ $retry_v2 == "y" ]]; then |
| 284 | print_info "尝试使用 SMB 2.0 协议..." |
| 285 | local source="//$nas_ip/$share_name" |
| 286 | local mount_options="uid=$(logname 2>/dev/null || echo 1000),gid=$(logname 2>/dev/null || echo 1000),iocharset=utf8,vers=2.0" |
| 287 | |
| 288 | if [ -n "$password" ]; then |
| 289 | mount -t cifs "$source" "$mount_point" -o username="$username",password="$password",$mount_options |
| 290 | else |
| 291 | mount -t cifs "$source" "$mount_point" -o username="$username",password="",$mount_options |
| 292 | fi |
| 293 | |
| 294 | if [ $? -eq 0 ]; then |
| 295 | print_success "使用 SMB 2.0 挂载成功!" |
| 296 | else |
| 297 | print_error "仍然失败,请检查网络和共享设置" |
| 298 | fi |
| 299 | fi |
| 300 | fi |
| 301 | } |
| 302 | |
| 303 | # 运行主函数 |
| 304 | main |