OpenWrt中开启usb存储和samba服务

在从官网安装的WNDR3800 15.05.1版本OpenWrt中, 不带usb存储支持以及samba, 需要另外安装

参考的文章

http://www.geektalks.org/setup-usb-drive-and-sambanas-on-openwrt-from-scratch/ 
https://wiki.openwrt.org/doc/howto/usb.essentials USB Basic Support 
https://wiki.openwrt.org/doc/howto/usb.storage   USB Storage 
https://wiki.openwrt.org/doc/uci/fstab fstab Configuration 
https://wiki.openwrt.org/doc/techref/block_mount#block-mount_binary_package Mount Block Storage

安装过程中需要安装的包有

#Those are USB drivers
kmod-usb-core
kmod-usb2
kmod-usb-storage
kmod-usb-storage-extras
block-mount
kmod-usb-uhci
kmod-usb-ohci
#Those are file system support, you may choose only the ones you are using:
kmod-fs-exfat
kmod-fs-ext4
kmod-fs-ntfs
kmod-fs-vfat #IO charset, IMPORTANT
kmod-nls-cp437
kmod-nls-iso8859-1 #This one is for auto mount
block-mount #These are samba server and web page configuration software:
luci-app-samba
luci-i18n-samba-en
samba36-server 

1. 启用usb支持 

# 每次重启后, 软件包都要重新update
opkg update

安装 kmod-usb-uhci 或者 kmod-usb-ohci, 如果前者不能安装, 系统会有提示

opkg install kmod-usb-uhci
insmod usbcore
insmod uhci
#在执行后两个命令时, 一个会提示已经完成, 一个会报不存在, 可能教程里的命令未随版本更新

opkg update
opkg install kmod-usb-ohci
insmod usb-ohci

安装usb2

opkg install kmod-usb2
insmod ehci-hcd

2. 启用usb存储

需要安装这些包: kmod-usb-storage, 各种格式 kmod-fs-<fs>, block-mount, kmod-scsi-core

# 查看可以安装的fs list
opkg list |grep kmod-fs # 安装e2fsprogs
opkg install e2fsprogs # 安装
opkg install kmod-usb-storage block-mount kmod-fs-ext4 kmod-fs-vfat
# block-mount 会报一个错, 这个是/etc/init.d/fstab中报的, 可以忽略

启用对usb存储的自动挂载

主要是通过 block detect 产生的 fstab 配置来实现的, 各配置项的说明

# mount swap devices that don't have their own config section
option anon_swap ''
# mount block devices that don't have their own config section
option anon_mount ''
# automatically mount swap devices when they appear
option auto_swap ''
# automatically mount block devices when they appear
option auto_mount ''
# wait X seconds before trying to mount root devices on boot
option delay_root ''
# run e2fsck on device prior to a mount. e2fsck is used to check the ext2/ext3/ext4 family of file systems
option check_fs ''

fstab配置也可以在luci界面中进行编辑

root@OpenWrt:~# block detect > /etc/config/fstab
root@OpenWrt:~# cat /etc/config/fstab
config 'global'
option anon_swap ''
option anon_mount ''
option auto_swap ''
option auto_mount ''
option delay_root ''
option check_fs '' config 'mount'
option target '/mnt/sda1'
option uuid '40e0-6302'
option enabled '' root@OpenWrt:~# /etc/init.d/fstab enable

注: 最后一个 option enabled 改为'1' 就会自动挂载, 重启后, 用df -h就能看到usb存储了.

注: 可以将config 'mount'部分删除, 将 option anon_mount 和 option check_fs 改为 '1' 这样在启动时会自动发现usb存储并挂载

住: ntfs格式的移动硬盘不能自动加载, 需要启动后, 通过这样的命令进行挂载

root@WNDR3800:~# ntfs-3g /dev/sda1 /mnt/sda1/
root@WNDR3800:~# ntfs-3g /dev/sda2 /mnt/sda2/

配置samba

主要是通过两个配置文件实现的, 一个是 /etc/config/samba,

# vi /etc/config/samba
config samba
option name 'WNDR3800'
option workgroup 'WORKGROUP'
option description 'OpenWrt'
option homes ''

config sambashare
      option name 'sda1'
      option path '/mnt/sda1/'
      option read_only 'no'
      option guest_ok 'yes'
      option 'create_mask' '0700'
      option 'dir_mask' '0700'

注: option name 和 option path, 如果直接使用 'mnt' 和 '/mnt/' 会导致服务器对剩余空间大小判断错误而无法写入稍微大一点的文件.

所以.. 可以一下多配置几个, 这样接移动硬盘时, 就不用再改了, 比如这样

onfig samba
option name 'WNDR3800'
option workgroup 'WORKGROUP'
option description 'OpenWrt'
option homes '' config sambashare
option read_only 'no'
option guest_ok 'yes'
option create_mask ''
option dir_mask ''
option name 'sda1'
option path '/mnt/sda1/' config sambashare
option read_only 'no'
option guest_ok 'yes'
option create_mask ''
option dir_mask ''
option name 'sda2'
option path '/mnt/sda2/' config sambashare
option name 'sda3'
option path '/mnt/sda3/'
option read_only 'no'
option guest_ok 'yes'
option create_mask ''
option dir_mask '' config sambashare
option name 'sda4'
option path '/mnt/sda4/'
option read_only 'no'
option guest_ok 'yes'
option create_mask ''
option dir_mask ''

另一个是 /etc/samba/smb.conf, 这是samba服务的标准配置文件, 下面是配置的例子

[global]
netbios name = |NAME|
workgroup = |WORKGROUP|
server string = |DESCRIPTION|
syslog =
encrypt passwords = true
passdb backend = smbpasswd
obey pam restrictions = yes
socket options = TCP_NODELAY
unix charset = utf-
preferred master = yes
os level =
security = user
guest account = nobody
# For safety, disable root user
# invalid users = root
smb passwd file = /etc/samba/smbpasswd

然后 /etc/init.d/samba enable 并重启路由器就可以了

启动后, samba会自动更新smb.conf, 将在/etc/config/samba下设置的 [mnt] 附加到文件的后面

用 ext4 格式化u盘

# 查看原文件格式df -T参数
df -h -T # format, 不格式化为ext4不能在Mount Points的添加界面中看到
mkfs.ext4 /dev/sda1
# 或者
mke2fs /dev/sda1 # 如果不行就先挂成swap, 再取消挂载, 再格式化
mkswap /dev/sda1
swapon /dev/sda1
swapoff /dev/sda1 # 创建挂载点
mkdir -p /mnt/usb # 设置挂载点为可写
chmod a+w /mnt/usb
chown -R nobody /mnt/usb

在Luci界面中,

先通过System -> Mount Points 将U盘挂载到 /mnt/usb

再通过Services -> Network Shares 中添加 Shared Directories 将 /mnt/usb 挂载为usb, 勾选Allow Guest

在其它设备中, 检查是否可以连接并且写入. 然后重启路由器, 再次确认是否可以连接并且写入.

在启动日志中查看usb设备的识别情况

root@WNDR3800:~# dmesg
[ 11.460000] uhci_hcd: USB Universal Host Controller Interface driver
[ 11.470000] usb-storage -:1.0: USB Mass Storage device detected
[ 11.480000] scsi host0: usb-storage -:1.0
[ 11.490000] usbcore: registered new interface driver usb-storage
[ 11.740000] block: attempting to load /tmp/jffs_cfg/upper/etc/config/fstab
[ 11.760000] block: extroot: not configured
[ 11.760000] mount_root: switching to jffs2 overlay
[ 11.800000] eth0: link down
[ 11.820000] procd: - early -
[ 11.820000] procd: - watchdog -
[ 12.490000] scsi :::: Direct-Access Generic- SD/MMC 1.00 PQ: ANSI: CCS
[ 12.560000] procd: - ubus -
[ 13.170000] sd :::: [sda] -byte logical blocks: (15.4 GB/14.4 GiB)
[ 13.200000] sd :::: [sda] Write Protect is off
[ 13.200000] sd :::: [sda] Mode Sense:
[ 13.200000] sd :::: [sda] No Caching mode page found
[ 13.210000] sd :::: [sda] Assuming drive cache: write through
[ 13.240000] sda: sda1
[ 13.250000] sd :::: [sda] Attached SCSI removable disk
[ 13.610000] procd: - init -
[ 15.110000] NET: Registered protocol family
[ 15.120000] ntfs: driver 2.1. [Flags: R/O MODULE].

Update 2017-10-15

对于非usb类型的存储, 例如sd card/tf card, 其在/dev中自动检测得到的名称并非sda, 而是类似于mmcblk0这样的设备, 这时候block detect能正常工作, 但是/etc/config/samba里面配置的路径必须设置为(根据/dev/mmcblk0创建后的分区名, 一般为 mmcblk0p1)

      option name 'mmcblk0p1'
option path '/mnt/mmcblk0p1/'

这样, fstab才能开机时自动检测并挂载

上一篇:配置Samba共享服务器


下一篇:Scala控制抽象