nbd: add nbd-client init script
authorMarcin Jurkowski <marcin1j@gmail.com>
Tue, 1 Sep 2015 22:27:36 +0000 (00:27 +0200)
committerMarcin Jurkowski <marcin1j@gmail.com>
Wed, 2 Sep 2015 00:03:38 +0000 (02:03 +0200)
Adds init.d and config files for nbd-client. Each section holds
parameters of one block device, where section name (eg. nbd0) is NBD
device name.

Signed-off-by: Marcin Jurkowski <marcin1j@gmail.com>
net/nbd/Makefile
net/nbd/files/nbd-client.conf [new file with mode: 0644]
net/nbd/files/nbd-client.init [new file with mode: 0644]

index dbe61c7c48909cb0405fc3d9c10c7e542e4f428e..aea9e4bfbf1fafa6d9859a318b5d6a1fe79ed04c 100644 (file)
@@ -60,6 +60,14 @@ endef
 define Package/nbd/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/nbd-client $(1)/usr/sbin/
+       $(INSTALL_DIR) $(1)/etc/config
+       $(INSTALL_CONF) ./files/nbd-client.conf $(1)/etc/config/nbd-client
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_BIN) ./files/nbd-client.init $(1)/etc/init.d/nbd-client
+endef
+
+define Package/nbd/conffiles
+/etc/config/nbd-client
 endef
 
 $(eval $(call BuildPackage,nbd))
diff --git a/net/nbd/files/nbd-client.conf b/net/nbd/files/nbd-client.conf
new file mode 100644 (file)
index 0000000..e355c61
--- /dev/null
@@ -0,0 +1,9 @@
+config nbd-client nbd0
+       option enabled '0'
+       option server '127.0.0.1'
+       option port 10809
+       option sdp 0
+       option swap 0
+       option persist 0
+       option blocksize 1024
+       option exportname foo
diff --git a/net/nbd/files/nbd-client.init b/net/nbd/files/nbd-client.init
new file mode 100644 (file)
index 0000000..e10c220
--- /dev/null
@@ -0,0 +1,69 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2015 OpenWrt.org
+
+START=90
+STOP=10
+USE_PROCD=1
+
+append_arg() {
+       local cfg="$1"
+       local var="$2"
+       local opt="$3"
+       local def="$4"
+       local val
+
+       config_get val "$cfg" "$var"
+       [ -n "$val" -o -n "$def" ] && procd_append_param command $opt "${val:-$def}"
+}
+
+append_bool() {
+       local cfg="$1"
+       local var="$2"
+       local opt="$3"
+       local def="$4"
+       local val
+
+       config_get_bool val "$cfg" "$var" "$def"
+       [ "$val" = 1 ] && procd_append_param command "$opt"
+}
+
+start_instance() {
+       local cfg="$1"
+       local enabled
+
+       config_get_bool enabled "$cfg" 'enabled' '0'
+       [ "$enabled" = 0 ] && return 1
+
+       procd_open_instance
+
+       procd_set_param command /usr/sbin/nbd-client
+
+       append_arg "$cfg" server
+       append_arg "$cfg" port
+       # device path
+       procd_append_param command "/dev/$cfg"
+       procd_append_param command -nofork
+       append_bool "$cfg" sdp "-sdp"
+       append_bool "$cfg" swap "-swap"
+       append_bool "$cfg" persist "-persist"
+       append_arg "$cfg" blocksize "-block-size"
+       append_arg "$cfg" timeout "-timeout"
+       append_arg "$cfg" exportname "-name"
+
+       procd_close_instance
+}
+
+service_triggers() {
+       procd_add_reload_trigger "nbd-client"
+}
+
+start_service() {
+       config_load nbd-client
+       config_foreach start_instance nbd-client
+}
+
+stop_service() {
+       for dev in /dev/nbd*; do
+               nbd-client -d $dev 1>/dev/null 2>&1
+       done
+}