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.

547 lines
14 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2007-2015 OpenWrt.org
  3. START=70
  4. STOP=01
  5. USE_PROCD=1
  6. KEEPALIVED_CONF=/tmp/keepalived.conf
  7. INDENT_1="\t"
  8. INDENT_2="${INDENT_1}${INDENT_1}"
  9. INDENT_3="${INDENT_1}${INDENT_1}${INDENT_1}"
  10. INDENT_4="${INDENT_1}${INDENT_1}${INDENT_1}${INDENT_1}"
  11. config_section_open() {
  12. local tag="$1"
  13. local name="$2"
  14. printf '%s' "$tag" >> "$KEEPALIVED_CONF"
  15. [ -n "$name" ] && printf ' %s' "$name" >> "$KEEPALIVED_CONF"
  16. printf ' {\n' >> "$KEEPALIVED_CONF"
  17. }
  18. config_section_close() {
  19. printf '}\n\n' >> "$KEEPALIVED_CONF"
  20. }
  21. config_foreach_wrapper() {
  22. local section="$1"
  23. local function="$1"
  24. # Convention is that 'function' and 'section' are the same
  25. config_foreach "$function" "$section"
  26. }
  27. print_elems_indent() {
  28. local config="$1"
  29. shift
  30. local indent="$1"
  31. shift
  32. [ -z "$indent" ] && indent="$INDENT_1"
  33. for opt in "$@"; do
  34. local "$opt"
  35. local optval
  36. local no_val=0
  37. if [ "${opt:0:7}" = "no_val_" ]; then
  38. opt="${opt:7}"
  39. no_val=1
  40. fi
  41. config_get "$opt" "$config" "$opt"
  42. eval optval=\$"$opt"
  43. [ -z "$optval" ] && continue
  44. printf '%b%s' "$indent" "$opt" >> "$KEEPALIVED_CONF"
  45. [ "$no_val" = "0" ] && {
  46. local words=0
  47. words="$(echo "$optval" | wc -w)"
  48. if [ "$words" -gt 1 ]; then
  49. printf ' "%s"' "$optval" >> "$KEEPALIVED_CONF"
  50. else
  51. printf ' %s' "$optval" >> "$KEEPALIVED_CONF"
  52. fi
  53. }
  54. printf '\n' >> "$KEEPALIVED_CONF"
  55. done
  56. unset optval
  57. }
  58. print_list_indent() {
  59. local lst="$1"
  60. local indent="$2"
  61. local lst_elems
  62. [ -z "$indent" ] && indent="$INDENT_1"
  63. eval lst_elems=\$"$lst"
  64. [ -z "$lst_elems" ] && return 0
  65. printf '%b%s {\n' "$indent" "$lst" >> "$KEEPALIVED_CONF"
  66. for e in $lst_elems; do
  67. printf '%b%s\n' "${indent}${INDENT_1}" "$e">> "$KEEPALIVED_CONF"
  68. done
  69. printf '%b}\n' "$indent" >> "$KEEPALIVED_CONF"
  70. }
  71. print_notify() {
  72. local type="$1"
  73. shift
  74. local name="$1"
  75. shift
  76. for notify in "$@"; do
  77. printf '%b%s' "${INDENT_1}" "$notify">> "$KEEPALIVED_CONF"
  78. notify="$(echo "$notify" | tr 'a-z' 'A-Z')"
  79. printf ' "/bin/busybox env -i ACTION=%s TYPE=%s NAME=%s /sbin/hotplug-call keepalived"\n' "$notify" "$type" "$name" >> "$KEEPALIVED_CONF"
  80. done
  81. }
  82. global_defs() {
  83. local linkbeat_use_polling notification_email
  84. config_get alt_config_file "$1" alt_config_file
  85. [ -z "$alt_config_file" ] || return 0
  86. config_get_bool linkbeat_use_polling "$1" linkbeat_use_polling 0
  87. [ "$linkbeat_use_polling" -gt 0 ] && printf 'linkbeat_use_polling\n\n' >> "$KEEPALIVED_CONF"
  88. config_get notification_email "$1" notification_email
  89. print_list_indent notification_email
  90. print_elems_indent "$1" "$INDENT_1" \
  91. notification_email_from \
  92. smtp_server \
  93. smtp_connect_timeout \
  94. router_id \
  95. vrrp_mcast_group4 \
  96. vrrp_mcast_group6 \
  97. vrrp_startup_delay
  98. }
  99. print_ipaddress_indent() {
  100. local section="$1"
  101. local curr_ipaddr="$2"
  102. local indent="$3"
  103. local address device scope name
  104. config_get name "$section" name
  105. [ "$name" != "$curr_ipaddr" ] && return 0
  106. config_get address "$section" address
  107. config_get device "$section" device
  108. config_get scope "$section" scope
  109. # Default indent
  110. [ -z "$indent" ] && indent="$INDENT_1"
  111. # If no address exit
  112. [ -z "$address" ] && return 0
  113. if [ -z "$device" ]; then
  114. printf '%b%s' "$indent" "$address" >> "$KEEPALIVED_CONF"
  115. else
  116. # Add IP address/netmask and device
  117. printf '%b%s dev %s' "$indent" "$address" "$device">> "$KEEPALIVED_CONF"
  118. # Add scope
  119. [ -n "$scope" ] && printf ' scope %s' "$scope" >> "$KEEPALIVED_CONF"
  120. fi
  121. printf '\n' >> "$KEEPALIVED_CONF"
  122. }
  123. static_ipaddress() {
  124. local address
  125. config_get address "$1" address
  126. for a in $address; do
  127. config_foreach print_ipaddress_indent ipaddress "$a"
  128. done
  129. }
  130. print_route_indent() {
  131. local section="$1"
  132. local curr_route="$2"
  133. local indent="$3"
  134. local name blackhole address src_addr gateway device scope table
  135. config_get name "$section" name
  136. [ "$name" != "$curr_route" ] && return 0
  137. config_get_bool blackhole "$section" blackhole 0
  138. config_get address "$section" address
  139. config_get src_addr "$section" src_addr
  140. config_get gateway "$section" gateway
  141. config_get device "$section" device
  142. config_get table "$section" table
  143. # If no address exit
  144. [ -z "$address" ] && return 0
  145. # Default indent
  146. [ -z "$indent" ] && indent="$INDENT_1"
  147. [ "$blackhole" -gt 0 ] && {
  148. printf '%bblackhole %s\n' "$indent" "$address" >> "$KEEPALIVED_CONF"
  149. return 0
  150. }
  151. # Add src addr or address
  152. if [ -n "$src_addr" ]; then
  153. printf '%bsrc %s %s' "$indent" "$src_addr" "$address" >> "$KEEPALIVED_CONF"
  154. else
  155. [ -z "$device" ] && return 0
  156. printf '%b%s' "$indent" "$address" >> "$KEEPALIVED_CONF"
  157. fi
  158. # Add route/gateway
  159. [ -n "$gateway" ] && printf ' via %s' "$gateway" >> "$KEEPALIVED_CONF"
  160. # Add device
  161. printf ' dev %s' "$device" >> "$KEEPALIVED_CONF"
  162. # Add scope
  163. [ -n "$scope" ] && printf ' scope %s' "$scope" >> "$KEEPALIVED_CONF"
  164. # Add table
  165. [ -n "$table" ] && printf ' table %s' "$table" >> "$KEEPALIVED_CONF"
  166. printf '\n' >> "$KEEPALIVED_CONF"
  167. }
  168. print_track_elem_indent() {
  169. local section="$1"
  170. local curr_track_elem="$2"
  171. local indent="$3"
  172. local name value
  173. config_get name "$section" name
  174. [ "$name" != "$curr_track_elem" ] && return 0
  175. config_get value "$section" value
  176. config_get weight "$section" weight
  177. [ -z "$value" ] && return 0
  178. printf '%b%s' "$indent" "$value" >> "$KEEPALIVED_CONF"
  179. [ -n "$weight" ] && printf ' weight %s' "$weight" >> "$KEEPALIVED_CONF"
  180. printf '\n' >> "$KEEPALIVED_CONF"
  181. }
  182. static_routes() {
  183. local route
  184. config_get route "$1" route
  185. for r in $route; do
  186. config_foreach print_route_indent route "$r"
  187. done
  188. }
  189. # Count 'vrrp_instance' with the given name ; called by vrrp_instance_check()
  190. vrrp_instance_name_count() {
  191. local name
  192. config_get name "$1" name
  193. [ "$name" = "$2" ] && count="$((count + 1))"
  194. }
  195. # Check if there's a 'vrrp_instance' section with the given name
  196. vrrp_instance_check() {
  197. local count="0"
  198. local name="$1"
  199. config_foreach vrrp_instance_name_count vrrp_instance "$name"
  200. [ $count -gt 0 ] && return 0 || return 1
  201. }
  202. vrrp_sync_group() {
  203. local group name
  204. local valid_group
  205. # No name for group, exit
  206. config_get name "$1" name
  207. [ -z "$name" ] && return 0
  208. # No members for group, exit
  209. config_get group "$1" group
  210. [ -z "$group" ] && return 0
  211. # Check if we have 'vrrp_instance's defined for
  212. # each member and remove names with not vrrp_instance defined
  213. for m in $group; do
  214. vrrp_instance_check "$m" && valid_group="$valid_group $m"
  215. done
  216. [ -z "$valid_group" ] && return 0
  217. config_section_open "vrrp_sync_group" "$name"
  218. group="$valid_group"
  219. print_list_indent group
  220. print_elems_indent "$1" "$INDENT_1" no_val_smtp_alert no_val_global_tracking
  221. print_notify "GROUP" "$name" notify_backup notify_master \
  222. notify_fault notify
  223. config_section_close
  224. }
  225. vrrp_instance() {
  226. local name auth_type auth_pass
  227. config_get name "$1" name
  228. [ -z "$name" ] && return 0
  229. config_section_open "vrrp_instance" "$name"
  230. config_get auth_type "$1" auth_type
  231. config_get auth_pass "$1" auth_pass
  232. [ -n "$auth_type" ] && [ -n "$auth_pass" ] && {
  233. printf '%bauthentication {\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  234. printf '%bauth_type %s\n' "${INDENT_2}" "$auth_type" >> "$KEEPALIVED_CONF"
  235. printf '%bauth_pass %s\n' "${INDENT_2}" "$auth_pass" >> "$KEEPALIVED_CONF"
  236. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  237. }
  238. print_elems_indent "$1" "$INDENT_1" state interface \
  239. mcast_src_ip unicast_src_ip virtual_router_id version priority \
  240. advert_int preempt_delay debug \
  241. lvs_sync_daemon_interface garp_master_delay garp_master_refresh \
  242. garp_master_repeat garp_master_refresh_repeat \
  243. no_val_vmac_xmit_base no_val_native_ipv6 no_val_accept \
  244. no_val_dont_track_primary no_val_smtp_alert no_val_nopreempt \
  245. no_val_use_vmac
  246. print_notify "INSTANCE" "$name" notify_backup notify_master \
  247. notify_fault notify_stop
  248. # Handle virtual_ipaddress & virtual_ipaddress_excluded lists
  249. for opt in virtual_ipaddress virtual_ipaddress_excluded; do
  250. config_get "$opt" "$1" "$opt"
  251. eval optval=\$$opt
  252. [ -z "$optval" ] && continue
  253. printf '%b%s {\n' "${INDENT_1}" "$opt" >> "$KEEPALIVED_CONF"
  254. for a in $optval; do
  255. config_foreach print_ipaddress_indent ipaddress "$a" "$INDENT_2"
  256. done
  257. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  258. done
  259. # Handle virtual_routes
  260. for opt in virtual_routes; do
  261. config_get "$opt" "$1" "$opt"
  262. eval optval=\$$opt
  263. [ -z "$optval" ] && continue
  264. printf '%b%s {\n' "${INDENT_1}" "$opt" >> "$KEEPALIVED_CONF"
  265. for r in $optval; do
  266. config_foreach print_route_indent route "$r" "$INDENT_2"
  267. done
  268. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  269. done
  270. # Handle track_script lists
  271. for opt in track_script; do
  272. config_get "$opt" "$1" "$opt"
  273. eval optval=\$$opt
  274. [ -z "$optval" ] && continue
  275. printf '%b%s {\n' "${INDENT_1}" "$opt" >> "$KEEPALIVED_CONF"
  276. for t in $optval; do
  277. printf '%b%s\n' "${INDENT_2}" "$optval" >> "$KEEPALIVED_CONF"
  278. done
  279. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  280. done
  281. # Handle track_interface lists
  282. for opt in track_interface; do
  283. config_get "$opt" "$1" "$opt"
  284. eval optval=\$$opt
  285. [ -z "$optval" ] && continue
  286. printf '%b%s {\n' "${INDENT_1}" "$opt" >> "$KEEPALIVED_CONF"
  287. for t in $optval; do
  288. config_foreach print_track_elem_indent track_interface "$t" "$INDENT_2"
  289. done
  290. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  291. done
  292. # Handle simple lists of strings (with no spaces in between)
  293. for opt in unicast_peer; do
  294. config_get "$opt" "$1" "$opt"
  295. print_list_indent "$opt"
  296. done
  297. unset optval
  298. config_section_close
  299. }
  300. vrrp_script() {
  301. local name
  302. config_get name "$1" name
  303. [ -z "$name" ] && return 0
  304. config_section_open "vrrp_script" "$name"
  305. print_elems_indent "$1" "$INDENT_1" script interval weight fall rise
  306. config_section_close
  307. }
  308. url() {
  309. local url="$2"
  310. local name path digest
  311. config_get name "$1" name
  312. [ "$url" = "$name" ] || return 0
  313. config_get path "$1" path
  314. config_get digest "$1" digest
  315. [ -n "$digest" ] && [ -n "$path" ] && {
  316. printf '%burl {\n' "${INDENT_3}" >> "$KEEPALIVED_CONF"
  317. printf '%bpath %s\n' "${INDENT_4}" "$path" >> "$KEEPALIVED_CONF"
  318. printf '%bdigest %s\n' "${INDENT_4}" "$digest" >> "$KEEPALIVED_CONF"
  319. printf '%b}\n' "${INDENT_3}" >> "$KEEPALIVED_CONF"
  320. }
  321. }
  322. url_list() {
  323. config_foreach url url "$1"
  324. }
  325. real_server() {
  326. local server="$2"
  327. local enabled name weight ipaddr port check
  328. config_get_bool enabled "$1" enabled 1
  329. [ "$enabled" -eq 1 ] || return 0
  330. config_get name "$1" name
  331. [ "$server" = "$name" ] || return 0
  332. config_get weight "$1" weight
  333. [ -n "$weight" ] || return 0
  334. config_get ipaddr "$1" ipaddr
  335. config_get port "$1" port
  336. config_get check "$1" check
  337. [ -n "$ipaddr" ] && [ -n "$port" ] && {
  338. printf '%breal_server %s %d {\n' "${INDENT_1}" "$ipaddr" "$port" >> "$KEEPALIVED_CONF"
  339. printf '%bweight %d\n' "${INDENT_2}" "$weight" >> "$KEEPALIVED_CONF"
  340. case "$check" in
  341. TCP_CHECK)
  342. printf '%b%s {\n' "${INDENT_2}" "$check" >> "$KEEPALIVED_CONF"
  343. print_elems_indent "$1" "$INDENT_3" connect_timeout \
  344. connect_port
  345. printf '%b}\n' "${INDENT_2}" >> "$KEEPALIVED_CONF"
  346. ;;
  347. MISC_CHECK)
  348. printf '%b%s {\n' "${INDENT_2}" "$check" >> "$KEEPALIVED_CONF"
  349. print_elems_indent "$1" "$INDENT_3" misc_path
  350. printf '%b}\n' "${INDENT_2}" >> "$KEEPALIVED_CONF"
  351. ;;
  352. HTTP_GET | SSL_GET)
  353. printf '%b%s {\n' "${INDENT_2}" "$check" >> "$KEEPALIVED_CONF"
  354. print_elems_indent "$1" "$INDENT_3" connect_timeout \
  355. connect_port nb_get_retry delay_before_retry
  356. # Handle url list
  357. config_list_foreach "$1" url url_list
  358. printf '%b}\n' "${INDENT_2}" >> "$KEEPALIVED_CONF"
  359. ;;
  360. esac
  361. printf '%b}\n' "${INDENT_1}" >> "$KEEPALIVED_CONF"
  362. }
  363. }
  364. real_server_list() {
  365. config_foreach real_server real_server "$1"
  366. }
  367. virtual_server() {
  368. local enabled ipaddr port lb_algo sorry_server_ip sorry_server_port
  369. config_get_bool enabled "$1" enabled 1
  370. [ "$enabled" -eq 1 ] || return 0
  371. config_get ipaddr "$1" ipaddr
  372. [ -z "$ipaddr" ] && return 0
  373. config_get port "$1" port
  374. [ -z "$port" ] && return 0
  375. config_section_open "virtual_server" "$ipaddr $port"
  376. print_elems_indent "$1" "$INDENT_1" fwmark delay_loop \
  377. lb_kind persistence_timeout persistence_granularity \
  378. virtualhost protocol
  379. config_get lb_algo "$1" lb_algo
  380. [ -z "$lb_algo" ] && lb_algo="rr"
  381. modprobe ip_vs_${lb_algo} 1>/dev/null 2>&1
  382. printf '%blb_algo %s\n' "${INDENT_1}" "${lb_algo}" >> "$KEEPALIVED_CONF"
  383. config_get sorry_server_ip "$1" sorry_server_ip
  384. config_get sorry_server_port "$1" sorry_server_port
  385. [ -n "$sorry_server_ip" ] && [ -n "$sorry_server_port" ] && {
  386. printf '%bsorry_server %s %s\n' "${INDENT_1}" "$sorry_server_ip" "$sorry_server_port" >> "$KEEPALIVED_CONF"
  387. }
  388. # Handle real_server list
  389. config_list_foreach "$1" real_server real_server_list
  390. config_section_close
  391. }
  392. process_config() {
  393. local alt_config_file
  394. rm -f "$KEEPALIVED_CONF"
  395. # First line
  396. printf '! Configuration file for keepalived (autogenerated via init script)\n' > "$KEEPALIVED_CONF"
  397. printf '! Written %s\n\n' "$(date +'%c')" >> "$KEEPALIVED_CONF"
  398. [ -f /etc/config/keepalived ] || return 0
  399. config_load 'keepalived'
  400. config_section_open "global_defs"
  401. config_foreach_wrapper global_defs
  402. config_section_close
  403. # If "alt_config_file" specified, use that instead
  404. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  405. rm -f "$KEEPALIVED_CONF"
  406. # Symlink "alt_config_file" since it's a bit easier and safer
  407. ln -s "$alt_config_file" "$KEEPALIVED_CONF"
  408. return 0
  409. }
  410. config_section_open "static_ipaddress"
  411. config_foreach_wrapper static_ipaddress
  412. config_section_close
  413. config_section_open "static_routes"
  414. config_foreach_wrapper static_routes
  415. config_section_close
  416. config_foreach_wrapper vrrp_script
  417. config_foreach_wrapper vrrp_sync_group
  418. config_foreach_wrapper vrrp_instance
  419. config_foreach_wrapper virtual_server
  420. return 0
  421. }
  422. service_triggers() {
  423. procd_add_reload_trigger "keepalived"
  424. }
  425. reload_service() {
  426. process_config
  427. #SIGHUP is used by keepalived to do init.d reload
  428. procd_send_signal keepalived
  429. }
  430. start_service() {
  431. procd_open_instance
  432. procd_set_param command /usr/sbin/keepalived
  433. procd_append_param command -n # don't daemonize, procd will handle that for us
  434. procd_append_param command -f "$KEEPALIVED_CONF"
  435. process_config
  436. # set auto respawn behavior
  437. procd_set_param respawn
  438. procd_close_instance
  439. }