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.

231 lines
6.7 KiB

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