Openwrt下适配Samba

关于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.mk

PKG_NAME:=samba
PKG_VERSION:=3.6.25
PKG_RELEASE:=1.1

PKG_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:=76da2fa64edd94a0188531e7ecb27c4e

PKG_LICENSE:=GPLv3
PKG_LICENSE_FILES:=COPYING

PKG_MAINTAINER:=Felix Fietkau <nbd@openwrt.org>

PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

MAKE_PATH:=source3
CONFIGURE_PATH:=source3

PKG_BUILD_BIN:=$(PKG_BUILD_DIR)/$(MAKE_PATH)/bin

define Package/samba36-server
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Samba 3.6 SMB/CIFS server
  URL:=http://www.samba.org/
  DEPENDS:=+USE_EGLIBC:librt
endef

define Package/samba36-client
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Samba 3.6 SMB/CIFS client
  URL:=http://www.samba.org/
  DEPENDS:=+libreadline +libncurses
endef

define Package/samba36-server/config
	config PACKAGE_SAMBA_MAX_DEBUG_LEVEL
		int "Maximum level of compiled-in debug messages"
		depends on PACKAGE_samba36-server || PACKAGE_samba36-client
		default -1

endef

define Package/samba36-server/description
 The Samba software suite is a collection of programs that implements the
 SMB protocol for UNIX systems, allowing you to serve files and printers to
 Windows, NT, OS/2 and DOS clients. This protocol is sometimes also referred
 to as the LanManager or Netbios protocol.
endef

TARGET_CFLAGS += -DMAX_DEBUG_LEVEL=$(CONFIG_PACKAGE_SAMBA_MAX_DEBUG_LEVEL) -D__location__=\\\"\\\" -ffunction-sections -fdata-sections
TARGET_LDFLAGS += -Wl,--gc-sections

CONFIGURE_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=no

CONFIGURE_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_domain

MAKE_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/sbin
	ln -sf samba_multicall $(1)/usr/sbin/smbd
	ln -sf samba_multicall $(1)/usr/sbin/nmbd
	ln -sf samba_multicall $(1)/usr/sbin/smbpasswd
endef

define 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 samba
	option '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 = yes
	deadtime = 30
	domain master = yes
	encrypt passwords = true
	enable core files = no
	guest account = nobody
	guest ok = yes
	local master = yes
	load printers = no
	map to guest = Bad User
	max protocol = SMB2
	min receivefile size = 16384
	null passwords = yes
	obey pam restrictions = yes
	os level = 20
	passdb backend = smbpasswd
	preferred master = yes
	printable = no
	security = user
	smb encrypt = disabled
	smb passwd file = /etc/samba/smbpasswd
	socket options = TCP_NODELAY IPTOS_LOWDELAY
	syslog = 2
	use sendfile = yes
	writeable = yes
	server signing = mandatory
	client signing = mandatory

samba.init:
samba初始化脚本,samba服务启动和关闭需要调用该脚本

#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org

START=60

smb_header() {
	local interface
	config_get interface $1 interface "loopback lan"

	# resolve interfaces
	local interfaces=$(
		. /lib/functions/network.sh

		local net
		for net in $interface; do
			local device
			network_get_device device "$net" && {
				local subnet
				network_get_subnet  subnet "$net" && echo -n "$subnet "
				network_get_subnet6 subnet "$net" && echo -n "$subnet "
			}

			echo -n "${device:-$net} "
		done
	)

	local name workgroup description charset
	local 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/etc
	sed -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.conf

	local homes
	config_get_bool homes $1 homes 0
	[ $homes -gt 0 ] && {
		cat <<EOT >> /var/etc/smb.conf

[homes]
	comment     = Home Directories
	browsable   = no
	read only   = no
	create mode = 0750
EOT
	}

	[ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
}

smb_add_share() {
	local name
	local path
	local users
	local read_only
	local guest_ok
	local create_mask
	local dir_mask
	local browseable
	local enabled

	config_get name $1 name
	config_get path $1 path
	config_get users $1 users
	config_get read_only $1 read_only
	config_get guest_ok $1 guest_ok
	config_get create_mask $1 create_mask
	config_get dir_mask $1 dir_mask
	config_get browseable $1 browseable
	config_get enabled $1 enabled

	[ -z "$name" -o -z "$path" ] && return
	if [ "$enabled" = "0" ]; then
		exit 0
	fi

	echo -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 samba
	config_foreach smb_header samba
	config_foreach smb_add_share sambashare
	
	service_stop /usr/sbin/smbd
	service_stop /usr/sbin/nmbd
	
	sleep 1
	
	service_start /usr/sbin/smbd -D
	service_start /usr/sbin/nmbd -D
}

stop() {
	service_stop /usr/sbin/smbd
	service_stop /usr/sbin/nmbd
}

驱动适配好Type A后,在/etc/hotplug.d/block下可以修改热插拔挂载脚本:
将设备名为sda开头的USB设备挂载到你预设的目录下(注意与uci中配置的一致),在 Linux 中 通常会将文件写入缓存中,而不是立即写入磁盘。使用sync参数实现数据的立即刷新到磁盘,确保数据的安全性和一致性。如果是samba4.0以后的版本可以尝试使用strict sync = yes配置项。

/sbin/block hotplug

udisk_folder=/tmp/samba
echo "action:$ACTION,device:$DEVICENAME,devname:$DEVNAME" > /dev/kmsg


case "$ACTION" in
	add)
		for i in $(ls /dev/ | grep '^sd[a-z][1-9]*$')
		do
			mkdir  $udisk_folder
			chmod 777 $udisk_folder
			mount -o sync -o fmask=0000 -o dmask=0000  -o iocharset=utf8  /dev/$i $udisk_folder
		done
		
		[ "$DEVTYPE" = "partition" -o "$DEVTYPE" = "disk" ] && {
		[ "${DEVNAME:0:2}" = "sd" ] && {
			touch /tmp/smb.flag
			/etc/init.d/samba restart
			echo "add dev $DEVNAME" > /dev/kmsg
			}
		}
	;;
	remove)
		[ "$DEVTYPE" = "partition" -o "$DEVTYPE" = "disk" ] && {
		[ "${DEVNAME:0:2}" = "sd" ] && {
			rm -rf /tmp/smb.flag
			/etc/init.d/samba stop
			echo "remove dev $DEVNAME" > /dev/kmsg
			}
		}
		sleep 1
		umount -l $udisk_folder
		rm -rf $udisk_folder
		
	;;
esac

Ref:
https://www.kancloud.cn/limata/threepandas/756923

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ryan爱吃糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值