ksmbd-tools: add package with hotplug.d script for auto sharing
authorRafał Miłecki <rafal@milecki.pl>
Wed, 10 Aug 2022 12:23:44 +0000 (14:23 +0200)
committerRosen Penev <rosenp@gmail.com>
Thu, 11 Aug 2022 21:05:45 +0000 (14:05 -0700)
One of common use cases for SMB3 server in routers is sharing hotplugged
drives. Users make many attempts setting that up which often are not
optimal.

This script handles it in the cleanest way by using:
1. hotplug.d mount subsystem
2. runtime config in the /var/run/config/

It provides a working basic solution that can be later adjusted by
modifying provided hotplug script.

A pretty much idential solution was part of the samba36 package. It was
added in the OpenWrt commit ef1efa756e0d0 ("samba36: add package with
hotplug.d script for auto sharing") as an answer for feature required by
the Rosinson company.

Cc: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
net/ksmbd-tools/Makefile
net/ksmbd-tools/files/ksmbd.hotplug [new file with mode: 0644]

index a4f118704c82f6fbc3ab1ca00f28053b8c8630d5..e8a7482f78488b601ed190d35c2fceef14fcb672 100644 (file)
@@ -84,6 +84,12 @@ define Package/ksmbd-avahi-service/description
   Ksmbd (smb/445) Daemon service via mDNS/DNS-SD.
 endef
 
+define Package/ksmbd-hotplug
+  $(call Package/ksmbd-tools/Default)
+  TITLE+= hotplug script for auto sharing
+  DEPENDS:=+blockd
+endef
+
 MESON_ARGS += \
        -Db_lto=true \
        -Dkrb5=disabled
@@ -116,6 +122,11 @@ define Package/ksmbd-avahi-service/install
        $(INSTALL_DATA) ./files/smb.service $(1)/etc/avahi/services/
 endef
 
+define Package/ksmbd-hotplug/install
+       $(INSTALL_DIR) $(1)/etc/hotplug.d/mount
+       $(INSTALL_CONF) ./files/ksmbd.hotplug $(1)/etc/hotplug.d/mount/60-ksmbd
+endef
+
 define Package/ksmbd-server/conffiles
 /etc/config/ksmbd
 /etc/ksmbd/smb.conf.template
@@ -130,3 +141,4 @@ endef
 $(eval $(call BuildPackage,ksmbd-server))
 $(eval $(call BuildPackage,ksmbd-utils))
 $(eval $(call BuildPackage,ksmbd-avahi-service))
+$(eval $(call BuildPackage,ksmbd-hotplug))
diff --git a/net/ksmbd-tools/files/ksmbd.hotplug b/net/ksmbd-tools/files/ksmbd.hotplug
new file mode 100644 (file)
index 0000000..114bfa9
--- /dev/null
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+
+device_get_vars() {
+       json_init
+       json_load "$(ubus call block info)"
+
+       json_select devices || return 1
+
+       json_get_keys keys
+       for key in $keys
+       do
+               json_select $key
+
+               json_get_var device device
+               [ "$device" = "$1" ] && {
+                       shift
+                       json_get_vars $@
+                       json_select ..
+                       json_select ..
+                       return 0
+               }
+
+               json_select ..
+       done
+
+       json_select ..
+
+       return 2
+}
+
+[ -f /var/run/config/ksmbd ] || {
+       mkdir -p /var/run/config && touch /var/run/config/ksmbd
+}
+
+[ "$ACTION" = "add" ] && {
+       device_get_vars $DEVICE label mount || {
+               logger -t ksmbd-hotplug "Failed to get $DEVICE info"
+               exit 1
+       }
+       [ -n "$mount" ] && {
+               uci -c /var/run/config batch <<-EOF
+                       set ksmbd.$DEVICE="share"
+                       set ksmbd.$DEVICE.name="${label:-$DEVICE}"
+                       set ksmbd.$DEVICE.path="$mount"
+                       set ksmbd.$DEVICE.browseable="yes"
+                       set ksmbd.$DEVICE.read_only="yes"
+                       set ksmbd.$DEVICE.guest_ok="yes"
+                       commit ksmbd
+               EOF
+               /etc/init.d/ksmbd reload
+       }
+}
+
+[ "$ACTION" = "remove" ] && {
+       uci -c /var/run/config batch <<-EOF
+               delete ksmbd.$DEVICE
+               commit ksmbd
+       EOF
+       /etc/init.d/ksmbd reload
+}