diff mbox series

[OpenWrt-Devel] samba36: add package with hotplug.d script for auto sharing

Message ID 20181226095128.489-1-zajec5@gmail.com
State Accepted
Delegated to: Rafał Miłecki
Headers show
Series [OpenWrt-Devel] samba36: add package with hotplug.d script for auto sharing | expand

Commit Message

Rafał Miłecki Dec. 26, 2018, 9:51 a.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

The new samba36-hotplug package provides a hotplug.d script for the
"mount" subsystem. It automatically shares every mounted block device.

It works by updating /var/run/config/samba file which:
1) Is read by procd init script
2) Gets wiped on reboot providing a consistent state
3) Can be safely updated without flash wearing or conflicting with user
   changes being made in /etc/config/samba

Cc: Rosy Song <rosysong@rosinson.com>
Cc: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
This patch requires pushing remaining patches to the fstools.git and
updating package/system/fstools/Makefile to make "mount" events work.
---
 package/network/services/samba36/Makefile          | 12 +++++
 .../network/services/samba36/files/samba.hotplug   | 56 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 package/network/services/samba36/files/samba.hotplug

Comments

Rafał Miłecki Dec. 27, 2018, 5:32 a.m. UTC | #1
On Thu, 27 Dec 2018 at 05:25, rosysong@rosinson.com
<rosysong@rosinson.com> wrote:
> Be care of the issue that the mount points(external usb sda1, sda2, ....) will lost when system firstboot.

That's right, fstools (block-mound / blockd) doesn't mount any
external drives by default. We have to expect users to handle *some*
configuration in order to get expected behavior.

I don't see any real solution for that. Doing things like
samba36-hotplug changing /etc/config/fstab would be too hacky. We
cannot say if user wants to specify mounts on his own or maybe just
use fstab.@global[0].anon_mount.
Rafał Miłecki Dec. 27, 2018, 8:22 a.m. UTC | #2
On 2018-12-27 08:50, rosysong@rosinson.com wrote:
> I added a while loop in samba.hotplug to solve the problem.
> The loop will stop until the file  */overlay/.fs_state*  is equal to 
> "2"  which indicates the filesystem is ready for everything.
> This really works fine for me.

OK, apparently we're talking about some different issues.

By default (e.g. after "firstboot") fstools won't mount any external
drives because:
1) fstab.@global[0].anon_mount is 0 by default
2) there won't be any mount entries in the /etc/config/fstab

So the chain looks like that:
1) fstools (block-mount / blockd) doen't mount anything
2) there is no "mount" hotplug.d event
3) /etc/hotplug.d/mount/60-samba doesn't get called

What your are describing doesn't sound like related to the above
problem. So please explain that issue you are seeing/fixing.
diff mbox series

Patch

diff --git a/package/network/services/samba36/Makefile b/package/network/services/samba36/Makefile
index b63c2eaad3..a08b51ba17 100644
--- a/package/network/services/samba36/Makefile
+++ b/package/network/services/samba36/Makefile
@@ -43,6 +43,12 @@  define Package/samba36-server
   DEPENDS:=+USE_GLIBC:librt $(ICONV_DEPENDS)
 endef
 
+define Package/samba36-hotplug
+  $(call Package/samba/Default)
+  TITLE+= hotplug script for auto sharing
+  DEPENDS:=+blockd
+endef
+
 define Package/samba36-client
   $(call Package/samba/Default)
   TITLE+= client
@@ -161,6 +167,11 @@  define Package/samba36-server/install
 	$(LN) samba_multicall $(1)/usr/sbin/smbpasswd
 endef
 
+define Package/samba36-hotplug/install
+	$(INSTALL_DIR) $(1)/etc/hotplug.d/mount
+	$(INSTALL_CONF) ./files/samba.hotplug $(1)/etc/hotplug.d/mount/60-samba
+endef
+
 define Package/samba36-client/install
 	$(INSTALL_DIR) $(1)/usr/sbin
 	$(INSTALL_BIN) $(PKG_BUILD_BIN)/smbclient $(1)/usr/sbin
@@ -174,5 +185,6 @@  endef
 
 $(eval $(call BuildPackage,samba36-client))
 $(eval $(call BuildPackage,samba36-server))
+$(eval $(call BuildPackage,samba36-hotplug))
 $(eval $(call BuildPackage,samba36-net))
 
diff --git a/package/network/services/samba36/files/samba.hotplug b/package/network/services/samba36/files/samba.hotplug
new file mode 100644
index 0000000000..e1ae64e655
--- /dev/null
+++ b/package/network/services/samba36/files/samba.hotplug
@@ -0,0 +1,56 @@ 
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+
+device_get_vars() {
+	json_init
+	json_load "$(ubus call block info)"
+
+	json_select devices
+
+	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 ..
+			break
+		}
+
+		json_select ..
+	done
+
+	json_select ..
+}
+
+[ -f /var/run/config/samba ] || {
+	mkdir -p /var/run/config && touch /var/run/config/samba
+}
+
+[ "$ACTION" = "add" ] && {
+	device_get_vars $DEVICE label mount
+	[ -n "$mount" ] && {
+		uci -c /var/run/config batch <<-EOF
+			set samba.$DEVICE="sambashare"
+			set samba.$DEVICE.name="$label"
+			set samba.$DEVICE.path="$mount"
+			set samba.$DEVICE.browseable="yes"
+			set samba.$DEVICE.read_only="yes"
+			set samba.$DEVICE.guest_ok="yes"
+			commit samba
+		EOF
+		/etc/init.d/samba reload
+	}
+}
+
+[ "$ACTION" = "remove" ] && {
+	uci -c /var/run/config batch <<-EOF
+		delete samba.$DEVICE
+		commit samba
+	EOF
+	/etc/init.d/samba reload
+}