ipv4-static 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "${LABEL}" ]; then
  33. args="${args} label ${LABEL}"
  34. fi
  35. if [ -n "${BROADCAST}" ]; then
  36. args="${args} broadcast ${BROADCAST}"
  37. fi
  38. case "${2}" in
  39. up)
  40. if [ "$(ip addr show ${1} 2>/dev/null | grep ${IP}/)" = "" ]; then
  41. log_info_msg "Adding IPv4 address ${IP} to the ${1} interface..."
  42. ip addr add ${args} dev ${1}
  43. evaluate_retval
  44. else
  45. log_warning_msg "Cannot add IPv4 address ${IP} to ${1}. Already present."
  46. fi
  47. ;;
  48. down)
  49. if [ "$(ip addr show ${1} 2>/dev/null | grep ${IP}/)" != "" ]; then
  50. log_info_msg "Removing IPv4 address ${IP} from the ${1} interface..."
  51. ip addr del ${args} dev ${1}
  52. evaluate_retval
  53. fi
  54. if [ -n "${GATEWAY}" ]; then
  55. # Only remove the gateway if there are no remaining ipv4 addresses
  56. if [ "$(ip addr show ${1} 2>/dev/null | grep 'inet ')" != "" ]; then
  57. log_info_msg "Removing default gateway..."
  58. ip route del default
  59. evaluate_retval
  60. fi
  61. fi
  62. ;;
  63. *)
  64. echo "Usage: ${0} [interface] {up|down}"
  65. exit 1
  66. ;;
  67. esac
  68. # End /lib/services/ipv4-static