关于Samba
samba是Linux和Unix平台的一款实现了SMB协议的软件,可以使得windows平台的主机也可以访问Linux和Unix平台主机共享出来的资源。
SMB协议(Server Message Block)是由微软开发的一种软件程序级的网络传输协议,基于NetBIOS,主要功用是使得一个网络上的机器共享计算机文件、打印机、串行端口和通讯等资源,使用C/S架构运行,主要作用在Windows平台。
samba服务监听的协议端口:
- UDP协议 端口号:137 NetBIOS名字服务 服务名称:nmbd
- UDP协议 端口号:138 NetBIOS数据包服务
- TCP协议 端口号:139 文件共享和打印机共享 服务名称:smbd
- TCP协议 端口号:445 NetBIOS在Windows2000及以后版本使用此端口
Openwrt下直接安装samba服务
终端输入
opkg update
opkg install samba
安装完成后,重启 OpenWrt 路由器以使配置生效。
Openwrt下USB TypeA编译适配Samba
可以到Openwrt官网搜索samba相关内容进行学习
以下以samba3.6为例表述我自己在openwrt下的操作步骤:
- 首先需要下载对应的tar包到openwrt源码根目录的dl文件夹下
- makefile等文件放在package/network/services/samba36目录下
samba36
├─ files
├─ patches
└─ Makefile - 随后根据个人需求对配置文件做修改,其中samba.config是uci配置文件,samba.conf.template是配置模板,samba.init是samba初始化脚本
Makefile:默认配置即可
# Copyright (C) 2007-2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#include $(TOPDIR)/rules.mkPKG_NAME:=samba
PKG_VERSION:=3.6.25
PKG_RELEASE:=1.1PKG_SOURCE_URL:=http://ftp.samba.org/pub/samba \http://ftp.samba.org/pub/samba/stable
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_MD5SUM:=76da2fa64edd94a0188531e7ecb27c4ePKG_LICENSE:=GPLv3
PKG_LICENSE_FILES:=COPYINGPKG_MAINTAINER:=Felix Fietkau <nbd@openwrt.org>PKG_BUILD_PARALLEL:=1include $(INCLUDE_DIR)/package.mkMAKE_PATH:=source3
CONFIGURE_PATH:=source3PKG_BUILD_BIN:=$(PKG_BUILD_DIR)/$(MAKE_PATH)/bindefine Package/samba36-serverSECTION:=netCATEGORY:=NetworkTITLE:=Samba 3.6 SMB/CIFS serverURL:=http://www.samba.org/DEPENDS:=+USE_EGLIBC:librt
endefdefine Package/samba36-clientSECTION:=netCATEGORY:=NetworkTITLE:=Samba 3.6 SMB/CIFS clientURL:=http://www.samba.org/DEPENDS:=+libreadline +libncurses
endefdefine Package/samba36-server/configconfig PACKAGE_SAMBA_MAX_DEBUG_LEVELint "Maximum level of compiled-in debug messages"depends on PACKAGE_samba36-server || PACKAGE_samba36-clientdefault -1endefdefine Package/samba36-server/descriptionThe Samba software suite is a collection of programs that implements theSMB protocol for UNIX systems, allowing you to serve files and printers toWindows, NT, OS/2 and DOS clients. This protocol is sometimes also referredto as the LanManager or Netbios protocol.
endefTARGET_CFLAGS += -DMAX_DEBUG_LEVEL=$(CONFIG_PACKAGE_SAMBA_MAX_DEBUG_LEVEL) -D__location__=\\\"\\\" -ffunction-sections -fdata-sections
TARGET_LDFLAGS += -Wl,--gc-sectionsCONFIGURE_VARS += \ac_cv_lib_attr_getxattr=no \ac_cv_search_getxattr=no \ac_cv_file__proc_sys_kernel_core_pattern=yes \libreplace_cv_HAVE_C99_VSNPRINTF=yes \libreplace_cv_HAVE_GETADDRINFO=yes \libreplace_cv_HAVE_IFACE_IFCONF=yes \LINUX_LFS_SUPPORT=yes \samba_cv_CC_NEGATIVE_ENUM_VALUES=yes \samba_cv_HAVE_GETTIMEOFDAY_TZ=yes \samba_cv_HAVE_IFACE_IFCONF=yes \samba_cv_HAVE_KERNEL_OPLOCKS_LINUX=yes \samba_cv_HAVE_SECURE_MKSTEMP=yes \samba_cv_HAVE_WRFILE_KEYTAB=no \samba_cv_USE_SETREUID=yes \samba_cv_USE_SETRESUID=yes \samba_cv_have_setreuid=yes \samba_cv_have_setresuid=yes \ac_cv_header_zlib_h=no \samba_cv_zlib_1_2_3=noCONFIGURE_ARGS += \--exec-prefix=/usr \--prefix=/ \--disable-avahi \--disable-cups \--disable-pie \--disable-relro \--disable-static \--disable-swat \--disable-shared-libs \--with-codepagedir=/etc/samba \--with-configdir=/etc/samba \--with-included-iniparser \--with-included-popt \--with-lockdir=/var/lock \--with-logfilebase=/var/log \--with-nmbdsocketdir=/var/nmbd \--with-piddir=/var/run \--with-privatedir=/etc/samba \--with-sendfile-support \--without-acl-support \--without-cluster-support \--without-ads \--without-krb5 \--without-ldap \--without-pam \--without-winbind \--without-libtdb \--without-libtalloc \--without-libnetapi \--without-libsmbclient \--without-libsmbsharemodes \--without-libtevent \--without-libaddns \--with-shared-modules=pdb_tdbsam,pdb_wbc_sam,idmap_nss,nss_info_template,auth_winbind,auth_wbc,auth_domainMAKE_FLAGS += DYNEXP= PICFLAG= MODULES=define Package/samba36-server/install$(INSTALL_DIR) $(1)/etc/config$(INSTALL_DATA) ./files/samba.config $(1)/etc/config/samba$(INSTALL_DIR) $(1)/etc/samba$(INSTALL_DATA) ./files/smb.conf.template $(1)/etc/samba$(INSTALL_DATA) $(PKG_BUILD_DIR)/codepages/lowcase.dat $(1)/etc/samba$(INSTALL_DATA) $(PKG_BUILD_DIR)/codepages/upcase.dat $(1)/etc/samba$(INSTALL_DATA) $(PKG_BUILD_DIR)/codepages/valid.dat $(1)/etc/samba$(INSTALL_DIR) $(1)/etc/init.d$(INSTALL_BIN) ./files/samba.init $(1)/etc/init.d/samba$(INSTALL_DIR) $(1)/usr/sbin$(INSTALL_BIN) $(PKG_BUILD_BIN)/samba_multicall $(1)/usr/sbinln -sf samba_multicall $(1)/usr/sbin/smbdln -sf samba_multicall $(1)/usr/sbin/nmbdln -sf samba_multicall $(1)/usr/sbin/smbpasswd
endefdefine Package/samba36-client/install$(INSTALL_DIR) $(1)/usr/sbin$(INSTALL_BIN) $(PKG_BUILD_BIN)/smbclient $(1)/usr/sbin$(INSTALL_BIN) $(PKG_BUILD_BIN)/nmblookup $(1)/usr/sbin
endef$(eval $(call BuildPackage,samba36-client))
$(eval $(call BuildPackage,samba36-server))
samba.config:
uci配置文件,可以设置共享名,共享路径等信息
config sambaoption 'name' 'OpenWrt'option 'workgroup' 'WORKGROUP'option 'description' 'OpenWrt'option 'homes' '1'config sambashare 'sdcard'option path '/tmp/samba'option guest_ok 'yes'option create_mask '777'option dir_mask '777'option name 'USB-Drive'option read_only 'no'option enabled '1'
samba.conf.template:
samba配置文件模板,具体可以参考这篇samba配置文件详解的文章
[global]netbios name = |NAME| display charset = |CHARSET|interfaces = |INTERFACES|server string = |DESCRIPTION|unix charset = |CHARSET|workgroup = |WORKGROUP|browseable = yesdeadtime = 30domain master = yesencrypt passwords = trueenable core files = noguest account = nobodyguest ok = yeslocal master = yesload printers = nomap to guest = Bad Usermax protocol = SMB2min receivefile size = 16384null passwords = yesobey pam restrictions = yesos level = 20passdb backend = smbpasswdpreferred master = yesprintable = nosecurity = usersmb encrypt = disabledsmb passwd file = /etc/samba/smbpasswdsocket options = TCP_NODELAY IPTOS_LOWDELAYsyslog = 2use sendfile = yeswriteable = yesserver signing = mandatoryclient signing = mandatory
samba.init:
samba初始化脚本,samba服务启动和关闭需要调用该脚本
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.orgSTART=60smb_header() {local interfaceconfig_get interface $1 interface "loopback lan"# resolve interfaceslocal interfaces=$(. /lib/functions/network.shlocal netfor net in $interface; dolocal devicenetwork_get_device device "$net" && {local subnetnetwork_get_subnet subnet "$net" && echo -n "$subnet "network_get_subnet6 subnet "$net" && echo -n "$subnet "}echo -n "${device:-$net} "done)local name workgroup description charsetlocal hostname="$(uci_get system.@system[0].hostname)"config_get name $1 name "${hostname:-OpenWrt}"config_get workgroup $1 workgroup "${hostname:-OpenWrt}"config_get description $1 description "Samba on ${hostname:-OpenWrt}"config_get charset $1 charset "UTF-8"mkdir -p /var/etcsed -e "s#|NAME|#$name#g" \-e "s#|WORKGROUP|#$workgroup#g" \-e "s#|DESCRIPTION|#$description#g" \-e "s#|INTERFACES|#$interfaces#g" \-e "s#|CHARSET|#$charset#g" \/etc/samba/smb.conf.template > /var/etc/smb.conflocal homesconfig_get_bool homes $1 homes 0[ $homes -gt 0 ] && {cat <<EOT >> /var/etc/smb.conf[homes]comment = Home Directoriesbrowsable = noread only = nocreate mode = 0750
EOT}[ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
}smb_add_share() {local namelocal pathlocal userslocal read_onlylocal guest_oklocal create_masklocal dir_masklocal browseablelocal enabledconfig_get name $1 nameconfig_get path $1 pathconfig_get users $1 usersconfig_get read_only $1 read_onlyconfig_get guest_ok $1 guest_okconfig_get create_mask $1 create_maskconfig_get dir_mask $1 dir_maskconfig_get browseable $1 browseableconfig_get enabled $1 enabled[ -z "$name" -o -z "$path" ] && returnif [ "$enabled" = "0" ]; thenexit 0fiecho -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf[ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf[ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf[ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf[ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf[ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf[ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
}start() {config_load sambaconfig_foreach smb_header sambaconfig_foreach smb_add_share sambashareservice_stop /usr/sbin/smbdservice_stop /usr/sbin/nmbdsleep 1service_start /usr/sbin/smbd -Dservice_start /usr/sbin/nmbd -D
}stop() {service_stop /usr/sbin/smbdservice_stop /usr/sbin/nmbd
}
驱动适配好Type A后,在/etc/hotplug.d/block下可以修改热插拔挂载脚本:
将设备名为sda开头的USB设备挂载到你预设的目录下(注意与uci中配置的一致),在 Linux 中 通常会将文件写入缓存中,而不是立即写入磁盘。使用sync参数实现数据的立即刷新到磁盘,确保数据的安全性和一致性。如果是samba4.0以后的版本可以尝试使用strict sync = yes配置项。
/sbin/block hotplugudisk_folder=/tmp/samba
echo "action:$ACTION,device:$DEVICENAME,devname:$DEVNAME" > /dev/kmsgcase "$ACTION" inadd)for i in $(ls /dev/ | grep '^sd[a-z][1-9]*$')domkdir $udisk_folderchmod 777 $udisk_foldermount -o sync -o fmask=0000 -o dmask=0000 -o iocharset=utf8 /dev/$i $udisk_folderdone[ "$DEVTYPE" = "partition" -o "$DEVTYPE" = "disk" ] && {[ "${DEVNAME:0:2}" = "sd" ] && {touch /tmp/smb.flag/etc/init.d/samba restartecho "add dev $DEVNAME" > /dev/kmsg}};;remove)[ "$DEVTYPE" = "partition" -o "$DEVTYPE" = "disk" ] && {[ "${DEVNAME:0:2}" = "sd" ] && {rm -rf /tmp/smb.flag/etc/init.d/samba stopecho "remove dev $DEVNAME" > /dev/kmsg}}sleep 1umount -l $udisk_folderrm -rf $udisk_folder;;
esac
Ref:
https://www.kancloud.cn/limata/threepandas/756923