Openwrt/package/network/services/samba36/files/samba.hotplug
Rafał Miłecki ef1efa756e samba36: add package with hotplug.d script for auto sharing
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>
2019-01-10 10:33:40 +01:00

57 lines
981 B
Bash

#!/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
}