nbd-client.init 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2015 OpenWrt.org
  3. START=90
  4. STOP=10
  5. USE_PROCD=1
  6. append_arg() {
  7. local cfg="$1"
  8. local var="$2"
  9. local opt="$3"
  10. local def="$4"
  11. local val
  12. config_get val "$cfg" "$var"
  13. [ -n "$val" -o -n "$def" ] && procd_append_param command $opt "${val:-$def}"
  14. }
  15. append_bool() {
  16. local cfg="$1"
  17. local var="$2"
  18. local opt="$3"
  19. local def="$4"
  20. local val
  21. config_get_bool val "$cfg" "$var" "$def"
  22. [ "$val" = 1 ] && procd_append_param command "$opt"
  23. }
  24. start_instance() {
  25. local cfg="$1"
  26. local enabled
  27. config_get_bool enabled "$cfg" 'enabled' '0'
  28. [ "$enabled" = 0 ] && return 1
  29. procd_open_instance
  30. procd_set_param command /usr/sbin/nbd-client
  31. append_arg "$cfg" server
  32. append_arg "$cfg" port
  33. # device path
  34. procd_append_param command "/dev/$cfg"
  35. procd_append_param command -nofork
  36. append_bool "$cfg" sdp "-sdp"
  37. append_bool "$cfg" swap "-swap"
  38. append_bool "$cfg" persist "-persist"
  39. append_arg "$cfg" blocksize "-block-size"
  40. append_arg "$cfg" timeout "-timeout"
  41. append_arg "$cfg" exportname "-name"
  42. procd_close_instance
  43. }
  44. service_triggers() {
  45. procd_add_reload_trigger "nbd-client"
  46. }
  47. start_service() {
  48. config_load nbd-client
  49. config_foreach start_instance nbd-client
  50. }
  51. stop_service() {
  52. for dev in /dev/nbd*; do
  53. nbd-client -d $dev 1>/dev/null 2>&1
  54. done
  55. }