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.

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