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.

220 lines
5.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=25
  4. EXTRA_COMMANDS="uciadd ucidel"
  5. EXTRA_HELP="\
  6. uciadd Add default bridge configuration to network and firewall uci config
  7. ucidel Delete default bridge configuration from network and firewall uci config"
  8. DOCKERD_CONF="/tmp/dockerd/daemon.json"
  9. uci_quiet() {
  10. uci -q ${@} >/dev/null
  11. }
  12. json_add_array_string() {
  13. json_add_string "" "$1"
  14. }
  15. boot() {
  16. uciadd
  17. rc_procd start_service
  18. }
  19. uciupdate() {
  20. local net="$1"
  21. uci -q get network.docker >/dev/null || {
  22. logger -t "dockerd-init" -p warn "No network uci config section for docker default bridge (docker0) found"
  23. return
  24. }
  25. [ -z "$net" ] && {
  26. logger -t "dockerd-init" -p notice "Removing network uci config options for docker default bridge (docker0)"
  27. uci_quiet delete network.docker.netmask
  28. uci_quiet delete network.docker.ipaddr
  29. uci_quiet commit network
  30. return
  31. }
  32. eval "$(ipcalc.sh "$net")"
  33. logger -t "dockerd-init" -p notice "Updating network uci config option \"$net\" for docker default bridge (docker0)"
  34. uci_quiet set network.docker.netmask="$NETMASK"
  35. uci_quiet set network.docker.ipaddr="$IP"
  36. uci_quiet commit network
  37. }
  38. uciadd() {
  39. /etc/init.d/dockerd running && {
  40. echo "Please stop dockerd service first"
  41. exit 0
  42. }
  43. # Add network interface
  44. if ! uci -q get network.docker >/dev/null; then
  45. logger -t "dockerd-init" -p notice "Adding docker default interface to network uci config (docker)"
  46. uci_quiet add network interface
  47. uci_quiet rename network.@interface[-1]="docker"
  48. uci_quiet set network.docker.ifname="docker0"
  49. uci_quiet set network.docker.proto="static"
  50. uci_quiet set network.docker.auto="0"
  51. uci_quiet commit network
  52. fi
  53. # Add docker bridge device
  54. if ! uci -q get network.docker0 >/dev/null; then
  55. logger -t "dockerd-init" -p notice "Adding docker default bridge device to network uci config (docker0)"
  56. uci_quiet add network device
  57. uci_quiet rename network.@device[-1]="docker0"
  58. uci_quiet set network.docker0.type="bridge"
  59. uci_quiet set network.docker0.name="docker0"
  60. uci_quiet add_list network.docker0.ifname="docker0"
  61. uci_quiet commit network
  62. fi
  63. # Add firewall zone
  64. if ! uci -q get firewall.docker >/dev/null; then
  65. logger -t "dockerd-init" -p notice "Adding docker default firewall zone to firewall uci config (docker)"
  66. uci_quiet add firewall zone
  67. uci_quiet rename firewall.@zone[-1]="docker"
  68. uci_quiet set firewall.docker.network="docker"
  69. uci_quiet set firewall.docker.input="REJECT"
  70. uci_quiet set firewall.docker.output="ACCEPT"
  71. uci_quiet set firewall.docker.forward="REJECT"
  72. uci_quiet set firewall.docker.name="docker"
  73. uci_quiet commit firewall
  74. fi
  75. reload_config
  76. }
  77. ucidel() {
  78. /etc/init.d/dockerd running && {
  79. echo "Please stop dockerd service first"
  80. exit 0
  81. }
  82. logger -t "dockerd-init" -p notice "Deleting docker default bridge device from network uci config (docker0)"
  83. uci_quiet delete network.docker0
  84. uci_quiet commit network
  85. logger -t "dockerd-init" -p notice "Deleting docker default interface from network uci config (docker)"
  86. uci_quiet delete network.docker
  87. uci_quiet commit network
  88. logger -t "dockerd-init" -p notice "Deleting docker firewall zone from firewall uci config (docker)"
  89. uci_quiet delete firewall.docker
  90. uci_quiet commit firewall
  91. reload_config
  92. }
  93. process_config() {
  94. local alt_config_file data_root log_level bip
  95. rm -f "$DOCKERD_CONF"
  96. [ -f /etc/config/dockerd ] || {
  97. # Use the daemon default configuration
  98. DOCKERD_CONF=""
  99. return 0
  100. }
  101. config_load 'dockerd'
  102. config_get alt_config_file globals alt_config_file
  103. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  104. ln -s "$alt_config_file" "$DOCKERD_CONF"
  105. return 0
  106. }
  107. config_get data_root globals data_root "/opt/docker/"
  108. config_get log_level globals log_level "warn"
  109. config_get bip globals bip ""
  110. . /usr/share/libubox/jshn.sh
  111. json_init
  112. json_add_string "data-root" "$data_root"
  113. json_add_string "log-level" "$log_level"
  114. [ -z "$bip" ] || json_add_string "bip" "$bip"
  115. json_add_array "registry-mirrors"
  116. config_list_foreach globals registry_mirrors json_add_array_string
  117. json_close_array
  118. json_add_array "hosts"
  119. config_list_foreach globals hosts json_add_array_string
  120. json_close_array
  121. mkdir -p /tmp/dockerd
  122. json_dump > "$DOCKERD_CONF"
  123. uciupdate "$bip"
  124. }
  125. start_service() {
  126. local nofile=$(cat /proc/sys/fs/nr_open)
  127. process_config
  128. procd_open_instance
  129. procd_set_param stderr 1
  130. if [ -z "$DOCKERD_CONF" ]; then
  131. procd_set_param command /usr/bin/dockerd
  132. else
  133. procd_set_param command /usr/bin/dockerd --config-file="$DOCKERD_CONF"
  134. fi
  135. procd_set_param limits nofile="${nofile} ${nofile}"
  136. procd_close_instance
  137. }
  138. reload_service() {
  139. process_config
  140. procd_send_signal dockerd
  141. }
  142. service_triggers() {
  143. procd_add_reload_trigger 'dockerd'
  144. }
  145. ip4tables_remove_nat() {
  146. iptables -t nat -D OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
  147. iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
  148. iptables -t nat -F DOCKER
  149. iptables -t nat -X DOCKER
  150. }
  151. ip4tables_remove_filter() {
  152. # Chain DOCKER-USER is only present,
  153. # if bip option is NOT set, so >/dev/null 2>&1
  154. iptables -t filter -D FORWARD -j DOCKER-USER >/dev/null 2>&1
  155. iptables -t filter -D FORWARD -j DOCKER-ISOLATION-STAGE-1
  156. iptables -t filter -D FORWARD -o docker0 -j DOCKER
  157. iptables -t filter -F DOCKER
  158. iptables -t filter -F DOCKER-ISOLATION-STAGE-1
  159. iptables -t filter -F DOCKER-ISOLATION-STAGE-2
  160. # Chain DOCKER-USER is only present,
  161. # if bip option is NOT set, so >/dev/null 2>&1
  162. iptables -t filter -F DOCKER-USER >/dev/null 2>&1
  163. iptables -t filter -X DOCKER
  164. iptables -t filter -X DOCKER-ISOLATION-STAGE-1
  165. iptables -t filter -X DOCKER-ISOLATION-STAGE-2
  166. # Chain DOCKER-USER is only present,
  167. # if bip option is NOT set, so >/dev/null 2>&1
  168. iptables -t filter -X DOCKER-USER >/dev/null 2>&1
  169. }
  170. ip4tables_remove() {
  171. ip4tables_remove_nat
  172. ip4tables_remove_filter
  173. }
  174. stop_service() {
  175. if /etc/init.d/dockerd running; then
  176. service_stop "/usr/bin/dockerd"
  177. ip4tables_remove
  178. fi
  179. }