Saltstack Official Linux Formula
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

137 lines
2.8KB

  1. #!/bin/sh
  2. SYSFS_NETCONSOLE="/sys/kernel/config/netconsole"
  3. NETCONSOLE_CONF="/etc/default/netconsole.conf"
  4. NETCONSOLE_PORT="514"
  5. netconsole_remove() {
  6. for sysfsnc in "${SYSFS_NETCONSOLE}/${interface:-}-"*
  7. do
  8. if [ -e "${sysfsnc}" ]
  9. then
  10. logger -t netconsole "remove ${sysfsnc}"
  11. rmdir "${sysfsnc}"
  12. fi
  13. done
  14. }
  15. netconsole_remote_mac()
  16. {
  17. neigh()
  18. {
  19. ip -4 -o neigh show to "${remote_ip}" dev "${interface}" | cut -d\ -f3
  20. }
  21. remote_mac="$(neigh)"
  22. if [ -n "${remote_mac:-}" ] && [ "${remote_mac:-}" != "INCOMPLETE" ]
  23. then
  24. if [ "${remote_mac:-}" != "FAILED" ]
  25. then
  26. echo "${remote_mac:-}"
  27. return 0
  28. fi
  29. else
  30. if ping -n -q -c 1 -w 1 -I "${interface}" "${remote_ip}" >/dev/null && remote_mac="$(neigh)" && [ -n "${remote_mac:-}" ]
  31. then
  32. echo "${remote_mac:-}"
  33. return 0
  34. fi
  35. fi
  36. return 1
  37. }
  38. netconsole_add() {
  39. netconsole() {
  40. iface="${1:-}"
  41. remote_ip="${2:-}"
  42. remote_mac="${3:-}"
  43. if [ "${iface:-}" = "${interface:-}" ] && [ -n "${remote_ip:-}" ]
  44. then
  45. logger -t netconsole "from ${new_ip_address:-}@${interface:-}"
  46. else
  47. return 1
  48. fi
  49. if [ -n "${remote_mac}" ] || remote_mac="$(netconsole_remote_mac)"
  50. then
  51. logger -t netconsole "to ${remote_ip} ${remote_mac}"
  52. else
  53. return 1
  54. fi
  55. sysfsnc="${SYSFS_NETCONSOLE}/${interface}-${remote_ip}"
  56. if [ -e "${sysfsnc}" ] && [ -z "${old_ip_address:-}" ]
  57. then
  58. old_ip_address="$(cat "${sysfsnc}/local_ip")"
  59. fi
  60. if [ "${old_ip_address:-}" != "${new_ip_address:-}" ] || ! [ -e "${sysfsnc}" ]
  61. then
  62. logger -t netconsole "setup netconsole"
  63. else
  64. return 1
  65. fi
  66. mkdir -p "${sysfsnc}"
  67. if [ "$(cat "${sysfsnc}/enabled")" != "0" ]
  68. then
  69. echo "0" > "${sysfsnc}/enabled"
  70. fi
  71. if [ -n "${new_ip_address:-}" ]
  72. then
  73. echo "${new_ip_address}" > "${sysfsnc}/local_ip"
  74. fi
  75. echo "${interface}" > "${sysfsnc}/dev_name"
  76. echo "${remote_mac}" > "${sysfsnc}/remote_mac"
  77. echo "${remote_ip}" > "${sysfsnc}/remote_ip"
  78. echo "${PORT:-${NETCONSOLE_PORT}}" > "${sysfsnc}/remote_port"
  79. echo "1" > "${sysfsnc}/enabled"
  80. return 0
  81. }
  82. if [ -f "${NETCONSOLE_CONF}" ]
  83. then
  84. modprobe netconsole
  85. mountpoint -q /sys/kernel/config || mount none -t configfs /sys/kernel/config
  86. if [ -e "${SYSFS_NETCONSOLE}" ]
  87. then
  88. (
  89. set -x
  90. set +e
  91. . "${NETCONSOLE_CONF}"
  92. ) ||:
  93. fi
  94. fi
  95. }
  96. netconsole_setup() {
  97. case ${reason:-} in
  98. BOUND|RENEW|REBIND|REBOOT)
  99. netconsole_add
  100. ;;
  101. EXPIRE|FAIL|RELEASE|STOP)
  102. netconsole_remove
  103. ;;
  104. PREINIT) : ;;
  105. *)
  106. if [ "${ADDRFAM:-}" = "inet" ] && [ "${METHOD:-}" = "static" ]
  107. then
  108. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  109. interface="${IFACE:-}"
  110. new_ip_address="${IF_ADDRESS:-}"
  111. case ${MODE:-} in
  112. start)
  113. netconsole_add
  114. ;;
  115. stop)
  116. netconsole_remove
  117. ;;
  118. *) : ;;
  119. esac
  120. fi
  121. esac
  122. }
  123. netconsole_setup