Update 20-smb

This commit is contained in:
kiddin9 authored and GitHub committed 2026-10-04 00:14:19 +08:00
1 parent f1193c9bf7
commit 7ee0fc91ed
1 file changed
+96 -88
@@ -1,112 +1,120 @@
#!/bin/sh #!/bin/sh
. /lib/functions.sh . /lib/functions.sh
. /lib/functions/service.sh . /lib/functions/service.sh
global=0 global=0
config_file="/etc/config/samba4" config_file="/etc/config/samba4"
wait_for_init() { # 若 Samba 服务本身未开机自启,则无需处理
for i in `seq 30` /etc/init.d/samba4 enabled 2>/dev/null || exit 0
do
if ubus call service list '{"name":"samba4"}' 2>/dev/null | grep -q "samba4"; then
sleep 2
return
fi
sleep 1
done
}}
smb_handle() {
local path
config_get path "$1" path
if [ -n "$path" ]; then
case "$path" in
"$2" | "$2"/*) global=1 ;;
esac
case "$2" in
"$path" | "$path"/*) global=1 ;;
esac
fi
}
chk_en() { chk_en() {
config_get_bool autoshare "$1" autoshare 1 config_get_bool autoshare "$1" autoshare 1
[ "$autoshare" -eq 0 ] && exit [ "$autoshare" -eq 0 ] && exit 0
} }
config_load samba4 config_load samba4
config_foreach chk_en samba4 config_foreach chk_en samba4
device=`basename "$DEVPATH"` wait_for_init() {
# 1. 开机引导期等待 procd 就绪
for _ in $(seq 30); do
[ -e /tmp/procd.done ] && break
sleep 1
done
# 2. 确认 samba4 实例已注入 procd
for _ in $(seq 30); do
if ubus call service list '{"name":"samba4"}' 2>/dev/null | grep -q "samba4"; then
sleep 1
return 0
fi
sleep 1
done
}
smb_handle() {
local path
config_get path "$1" path
[ -z "$path" ] && return
case "$path" in
"$2" | "$2"/*) global=1 ;;
esac
case "$2" in
"$path" | "$path"/*) global=1 ;;
esac
}
device="${DEVPATH##*/}"
case "$ACTION" in case "$ACTION" in
add) add)
case "$device" in
sd*|md*|hd*|mmcblk*|nvme*) ;;
*) return 0 ;;
esac
case "$device" in path="/dev/$device"
sd*);; wait_for_init
md*);;
hd*);;
mmcblk*);;
nvme*);;
*) return;;
esac
path="/dev/$device" grep -v '/boot\|/opt' /proc/mounts | while read -r j; do
str="${j%% *}"
if [ "$str" = "$path" ]; then
strr="${j#* }"
target="${strr%% *}"
global=0
config_load samba4
config_foreach smb_handle sambashare "$target"
wait_for_init # 提取共享名:优先提取 /mnt/ 后的目录名,异常情况下取末级路径名
name="${target#*/mnt/}"
name="${name#/}"
[ -z "$name" ] || [ "$name" = "$target" ] && name="${target##*/}"
grep -v '/boot\|/opt' /proc/mounts | while read -r j if [ "$global" -eq 0 ]; then
do printf "\n\nconfig sambashare\n" >> "$config_file"
str=${j%% *} printf "\toption auto '1'\n" >> "$config_file"
if [ "$str" = "$path" ];then printf "\toption name '%s'\n" "$name" >> "$config_file"
strr=${j#* } printf "\toption path '%s'\n" "$target" >> "$config_file"
target=${strr%% *} printf "\toption read_only 'no'\n" >> "$config_file"
global=0 printf "\toption guest_ok 'yes'\n" >> "$config_file"
printf "\toption create_mask '0666'\n" >> "$config_file"
printf "\toption dir_mask '0777'\n" >> "$config_file"
printf "\toption device '%s'\n" "$device" >> "$config_file"
printf "\toption force_root '1'\n" >> "$config_file"
printf "\toption inherit_owner 'yes'\n" >> "$config_file"
config_load samba4 /etc/init.d/samba4 reload
config_foreach smb_handle sambashare "$target" return 0
fi
name=${target#*/mnt/} fi
name=$(echo "$name" | sed -e "s/^\///") done
;;
if [ "$global" -eq 0 ] ;then
echo -e "\n\nconfig sambashare" >> $config_file
echo -e "\toption auto '1'" >> $config_file
echo -e "\toption name '$name'" >> $config_file
echo -e "\toption path '$target'" >> $config_file
echo -e "\toption read_only 'no'" >> $config_file
echo -e "\toption guest_ok 'yes'" >> $config_file
echo -e "\toption create_mask '0666'" >> $config_file
echo -e "\toption dir_mask '0777'" >> $config_file
echo -e "\toption device '$device'" >> $config_file
echo -e "\toption force_root '1'" >> $config_file
/etc/init.d/samba4 reload
return
fi
fi
done
;;
remove) remove)
i=0 changed=0
while true i=0
do while true; do
if ! uci -q get samba4.@sambashare[$i] >/dev/null; then if ! uci -q get "samba4.@sambashare[$i]" >/dev/null; then
break break
fi fi
dev=$(uci -q get samba4.@sambashare[$i].device) dev=$(uci -q get "samba4.@sambashare[$i].device")
if [ "$dev" = "$device" ]; then auto=$(uci -q get "samba4.@sambashare[$i].auto")
auto=$(uci -q get samba4.@sambashare[$i].auto)
if [ "$auto" = "1" ]; then if [ "$dev" = "$device" ] && [ "$auto" = "1" ]; then
uci delete samba4.@sambashare[$i] uci -q delete "samba4.@sambashare[$i]"
uci commit samba4 changed=1
/etc/init.d/samba4 reload # 删除当前节点后后续元素索引前移,保持 i 不变继续扫描
return continue
fi fi
fi i=$((i + 1))
let i+=1 done
done
;; if [ "$changed" -eq 1 ]; then
uci commit samba4
/etc/init.d/samba4 reload
fi
;;
esac esac