ipv4-static 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. ########################################################################
  3. # Begin /lib/services/ipv4-static
  4. #
  5. # Description : IPV4 Static Boot Script
  6. #
  7. # Authors : Nathan Coulson - nathan@linuxfromscratch.org
  8. # Kevin P. Fleming - kpfleming@linuxfromscratch.org
  9. # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
  10. #
  11. # Version : LFS 7.0
  12. #
  13. ########################################################################
  14. . /lib/lsb/init-functions
  15. . ${IFCONFIG}
  16. if [ -z "${IP}" ]; then
  17. log_failure_msg "\nIP variable missing from ${IFCONFIG}, cannot continue."
  18. exit 1
  19. fi
  20. if [ -z "${PREFIX}" -a -z "${PEER}" ]; then
  21. log_warning_msg "\nPREFIX variable missing from ${IFCONFIG}, assuming 24."
  22. PREFIX=24
  23. args="${args} ${IP}/${PREFIX}"
  24. elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then
  25. log_failure_msg "\nPREFIX and PEER both specified in ${IFCONFIG}, cannot continue."
  26. exit 1
  27. elif [ -n "${PREFIX}" ]; then
  28. args="${args} ${IP}/${PREFIX}"
  29. elif [ -n "${PEER}" ]; then
  30. args="${args} ${IP} peer ${PEER}"
  31. fi
  32. if [ -n "${BROADCAST}" ]; then
  33. args="${args} broadcast ${BROADCAST}"
  34. fi
  35. case "${2}" in
  36. up)
  37. if [ "$(ip addr show ${1} | grep ${IP})" == "" ]; then
  38. log_info_msg2 "\n" # Terminate the previous message
  39. log_info_msg "Adding IPv4 address ${IP} to the ${1} interface..."
  40. ip addr add ${args} dev ${1}
  41. evaluate_retval
  42. if [ -n "${GATEWAY}" ]; then
  43. if ip route | grep -q default; then
  44. log_warning_msg "\nGateway already setup; skipping."
  45. else
  46. log_info_msg "Setting up default gateway..."
  47. ip route add default via ${GATEWAY} dev ${1}
  48. evaluate_retval
  49. fi
  50. fi
  51. else
  52. msg="Cannot add IPv4 address ${IP} to ${1}. Already present."
  53. log_warning_msg "$msg"
  54. fi
  55. ;;
  56. down)
  57. if [ "$(ip addr show ${1} | grep ${IP})" != "" ]; then
  58. log_info_msg "Removing IPv4 address ${IP} from the ${1} interface..."
  59. ip addr del ${args} dev ${1}
  60. evaluate_retval
  61. fi
  62. if [ -n "${GATEWAY}" ]; then
  63. # Only remove the gateway if ther are no remaining ipv4 addresses
  64. if [ "$(ip addr show ${1} | grep 'inet ')" != "" ]; then
  65. log_info_msg "Removing default gateway..."
  66. ip route del default
  67. evaluate_retval
  68. fi
  69. fi
  70. ;;
  71. *)
  72. echo "Usage: ${0} [interface] {up|down}"
  73. exit 1
  74. ;;
  75. esac
  76. # End /lib/services/ipv4-static