本教程介绍如何在 Armbian 系统上实现 USB 设备的自动挂载和 Samba 共享功能。通过这个脚本,你可以实现插入 USB 设备后自动挂载并创建网络共享,支持 Windows 网络发现功能。
功能特点
自动检测并挂载 USB 存储设备
支持多种文件系统:NTFS、FAT32、exFAT、ext4
自动配置 Samba 共享
支持 Windows 网络发现(通过 wsdd2)
即插即用,无需手动操作
支持匿名访问和完全读写权限
自动日志记录
智能依赖检查和安装
前期准备
1. 更换镜像源(可选,但推荐)
编辑 sources.list 文件:
1
nano /etc/apt/sources.list
选择适合你系统的镜像源:
📦 Ubuntu 系统(点击展开)
1 2 3 4
# 清华大学 Ubuntu 镜像源 deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ noble main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ noble-updates main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ noble-backports main restricted universe multiverse
📦 Debian 系统(点击展开)
1 2 3 4
# 清华大学 Debian 镜像源 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
💡 提示:选择对应你系统的镜像源复制即可,不确定是哪个系统可以通过 lsb_release -a 命令查看
# 创建基本配置 cat > "$SMB_CONF" << EOF [global] workgroup = WORKGROUP netbios name = USB-SHARE wins support = yes local master = yes preferred master = yes server string = USB Share Server dns proxy = no log file = /var/log/samba/log.%m max log size = 1000 server role = standalone server map to guest = Bad User usershare allow guests = yes EOF
# 确保日志目录存在 mkdir -p /var/log/samba
# 设置适当的权限 chmod 644 "$SMB_CONF" fi
# 检查并添加全局配置 if ! grep -q "wins support = yes""$SMB_CONF"; then # 备份原配置 cp"$SMB_CONF""${SMB_CONF}.bak"
# 添加全局配置到文件开头 cat > "$SMB_CONF" << EOF [global] workgroup = WORKGROUP netbios name = USB-SHARE wins support = yes local master = yes preferred master = yes server string = USB Share Server dns proxy = no log file = /var/log/samba/log.%m max log size = 1000 server role = standalone server map to guest = Bad User usershare allow guests = yes EOF# 追加原配置文件的其余部分(排除原有的 [global] 部分) sed '/\[global\]/,/\[/!d;/\[global\]/d;/\[/!d'"${SMB_CONF}.bak" >> "$SMB_CONF" fi
# 检查共享是否已存在 if ! grep -q "^\[$share_name\]""$SMB_CONF"; then # 添加共享配置 cat >> "$SMB_CONF" << EOF [$share_name] path = $mount_point browseable = yes read only = no guest ok = yes create mask = 0777 directory mask = 0777 force user = nobody force group = nogroup comment = USB Share - $share_name EOF# 重启 Samba 服务 systemctl restart smbd nmbd fi }
# 挂载设备
mount_device() { local device="$1" local uuid="$2" local mount_point="$MOUNT_BASE/$(basename "$device")"
# 创建挂载点 mkdir -p "$mount_point"
# 检查是否已经挂载 if ! mountpoint -q "$mount_point"; then # 尝试挂载 mount -U "$uuid""$mount_point" -o rw,users,umask=000
if [ $? -eq 0 ]; then echo"成功挂载设备 $device 到 $mount_point" | tee -a "$LOG_FILE" # 设置Samba共享 setup_samba_share "$mount_point" else echo"挂载设备 $device 失败" | tee -a "$LOG_FILE" rmdir"$mount_point" fi fi
}
# 检查并安装依赖
install_dependencies() { local dependencies=("samba""samba-common-bin""ntfs-3g""exfat-fuse""exfatprogs""jq""wsdd2") local packages_to_install=()
for package in"${dependencies[@]}"; do # 使用 dpkg-query 检查包是否已安装 if ! dpkg-query -W -f='${Status}'"$package" 2>/dev/null | grep -q "ok installed"; then echo"需要安装: $package" | tee -a "$LOG_FILE" packages_to_install+=("$package") fi done
# 只有当有包需要安装时才执行安装 if [ ${#packages_to_install[@]} -gt 0 ]; then echo"开始安装缺失的包..." | tee -a "$LOG_FILE" # 先尝试直接安装 if ! apt-get install -y "${packages_to_install[@]}"; then # 如果安装失败,则更新源后重试 echo"直接安装失败,更新软件源后重试..." | tee -a "$LOG_FILE" apt-get update && apt-get install -y "${packages_to_install[@]}" fi else echo"所有依赖包已安装" | tee -a "$LOG_FILE" fi