You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.3 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2015 OpenWrt.org
  3. START=60
  4. STOP=40
  5. USE_PROCD=1
  6. PROG=/usr/bin/nbd-server
  7. CONFIGFILE="/var/etc/nbd-server.conf"
  8. append_plain() {
  9. echo "$1" >> $CONFIGFILE
  10. }
  11. append_val_str() {
  12. local resultname="$1"
  13. local cfg="$2"
  14. local uciname="${3:-$1}"
  15. local value=
  16. config_get value "$cfg" "$uciname"
  17. if [ -z "$value" ]; then
  18. return
  19. fi
  20. append_plain "$resultname = $value"
  21. }
  22. append_val_bool() {
  23. local resultname="$1"
  24. local cfg="$2"
  25. local uciname="${3:-$1}"
  26. local value=
  27. config_get_bool value "$cfg" "$uciname"
  28. if [ -z "$value" ]; then
  29. return
  30. fi
  31. [ $value == 1 ] && value="true" || value="false"
  32. append_plain "$resultname = $value"
  33. }
  34. config_handle_generic() {
  35. local cfg="$1"
  36. append_plain "[generic]"
  37. append_val_str user "$cfg"
  38. append_val_str group "$cfg"
  39. append_val_str port "$cfg"
  40. append_val_str listenaddr "$cfg"
  41. append_val_bool allowlist "$cfg"
  42. append_val_str includedir "$cfg"
  43. append_val_bool oldstyle "$cfg"
  44. config_get_bool SERVICE_ENABLED "$cfg" enabled 0
  45. }
  46. config_handle_share() {
  47. local cfg="$1"
  48. append_plain
  49. append_plain "[$cfg]"
  50. append_val_str exportname "$cfg" filename
  51. append_val_str timeout "$cfg"
  52. append_val_str maxconnections "$cfg"
  53. append_val_str authfile "$cfg"
  54. append_val_str filesize "$cfg"
  55. append_val_str readonly "$cfg"
  56. append_val_str multifile "$cfg"
  57. append_val_str copyonwrite "$cfg"
  58. append_val_bool sparse_cow "$cfg"
  59. append_val_bool flush "$cfg"
  60. append_val_bool fua "$cfg"
  61. append_val_bool rotational "$cfg"
  62. append_val_bool sync "$cfg"
  63. append_val_bool discard "$cfg"
  64. append_val_str prerun "$cfg"
  65. append_val_str postrun "$cfg"
  66. append_val_str virtstyle "$cfg"
  67. append_val_str port "$cfg" oldstyle_port
  68. append_val_str listenaddr "$cfg" oldstyle_listenaddr
  69. }
  70. start_instance() {
  71. local cfg="$1"
  72. local enabled
  73. config_get_bool enabled "$cfg" 'enabled' '0'
  74. [ "$enabled" = 0 ] && return 1
  75. procd_open_instance
  76. procd_set_param command $PROG --config-file=$CONFIGFILE
  77. procd_set_param file $CONFIGFILE
  78. procd_close_instance
  79. }
  80. start_service() {
  81. config_load nbd-server
  82. mkdir -p $(dirname $CONFIGFILE)
  83. echo "# auto-generated config file from /etc/config/nbd-server" > $CONFIGFILE
  84. config_foreach config_handle_generic nbd-server
  85. config_foreach config_handle_share share
  86. config_foreach start_instance nbd-server
  87. }
  88. service_triggers() {
  89. procd_add_reload_trigger "nbd-server"
  90. }