ifup 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  2. ########################################################################
  3. # Begin /sbin/ifdown
  4. #
  5. # Description : Interface Up
  6. #
  7. # Authors : DJ Lucas - dj@linuxfromscratch.org
  8. #
  9. # Version : 00.02
  10. #
  11. ########################################################################
  12. . /lib/lsb/init-functions
  13. function get_args()
  14. {
  15. if test -z "${1}" ; then
  16. showhelp
  17. exit 1
  18. fi
  19. while test -n "${1}" ; do
  20. case "${1}" in
  21. -c | --configfile)
  22. check_arg $1 $2
  23. CONFIGFILE="${2}"
  24. shift 2
  25. ;;
  26. eth* | iw* | wlan*)
  27. INTERFACE="${1}"
  28. shift 1
  29. ;;
  30. -h | --help)
  31. showhelp
  32. exit 0
  33. ;;
  34. *)
  35. showhelp
  36. echo "ERROR: '${1}' unknown argument"
  37. echo ""
  38. exit 2
  39. ;;
  40. esac
  41. done
  42. }
  43. function check_arg()
  44. {
  45. echo "${2}" | grep -v "^-" > /dev/null
  46. if [ -z "${?}" -o ! -n "${2}" ]; then
  47. echo "Error: ${1} requires a valid argument."
  48. exit 2
  49. fi
  50. }
  51. function showhelp()
  52. {
  53. echo "`/usr/bin/basename ${0}` brings up a valid network interface."
  54. echo ""
  55. echo "Options:"
  56. echo " -c --configfile The path to an interface configuration file"
  57. echo " If no configuration file is given, all files"
  58. echo " listed in ${NETWORK_DEVICES}/ifconfig.<int> will"
  59. echo " be processed, regarless of the value of ONBOOT"
  60. echo " -h --help Show this help message and exit."
  61. echo ""
  62. echo "Examples:"
  63. echo " `/usr/bin/basename ${0}` eth0 -c ${NETWORK_DEVICES}/ifconfig.eth0/ipv4"
  64. echo " `/usr/bin/basename ${0}` eth0"
  65. echo ""
  66. echo ""
  67. }
  68. # Intialize empty variables so that the shell does not polute the script
  69. CONFIGFILE=""
  70. CONFIGDIR=""
  71. INTERFACE=""
  72. # Process command line arguments
  73. get_args ${@}
  74. # Handle common errors - No need to account for bootscripts, this should not
  75. # happen during boot or shutdown.
  76. if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
  77. echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
  78. echo ""
  79. exit 2
  80. fi
  81. if [ "${INTERFACE}x" == "x" ]; then
  82. echo "ERROR: No interface was given"
  83. echo ""
  84. exit 2
  85. else
  86. if ! grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null; then
  87. echo "ERROR: ${INTERFACE} is not a valid network interface."
  88. echo ""
  89. exit 2
  90. fi
  91. fi
  92. # If a configuration file is present, use it
  93. if [ "${CONFIGFILE}x" != "x" ]; then
  94. . "${CONFIGFILE}"
  95. if [ -x "/lib/network-services/${SERVICE}" ]; then
  96. # do the work
  97. # Check to make sure the interface is up
  98. link_status=`/sbin/ip link show "${INTERFACE}" | \
  99. grep -o "state UP"`
  100. if [ "${link_status}" != "state UP" ]; then
  101. message="Bringing up the ${INTERFACE} interface..."
  102. /sbin/ip link set ${INTERFACE} up
  103. evaluate_retval standard
  104. fi
  105. if IFCONFIG=${CONFIGFILE} \
  106. /lib/network-services/${SERVICE} ${INTERFACE} up; then
  107. mkdir -p "/run/network/ifconfig.${INTERFACE}"
  108. cp "${CONFIGFILE}" "/run/network/ifconfig.${INTERFACE}/"
  109. fi
  110. else
  111. echo "ERROR: Service '${SERVICE}' is not a valid service."
  112. echo ""
  113. exit 2
  114. fi
  115. # No interface configuration file was given
  116. else
  117. # Process all available interface configuration files
  118. CONFIGDIR="/etc/network/ifconfig.${INTERFACE}"
  119. if [ -d "${CONFIGDIR}" ]; then
  120. FILES=`ls "${CONFIGDIR}"`
  121. for CONFIGFILE in ${FILES}
  122. do
  123. (
  124. . "${CONFIGDIR}/${CONFIGFILE}"
  125. if [ -x "/lib/network-services/${SERVICE}" ]; then
  126. # Check to make sure the interface is up
  127. link_status=`/sbin/ip link show "${INTERFACE}" | \
  128. grep -o "state UP"`
  129. if [ "${link_status}" != "state UP" ]; then
  130. message="Bringing up the ${INTERFACE} interface..."
  131. /sbin/ip link set ${INTERFACE} up
  132. evaluate_retval standard
  133. fi
  134. if IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
  135. /lib/network-services/${SERVICE} ${INTERFACE} up; then
  136. mkdir -p "/run/network/ifconfig.${INTERFACE}"
  137. cp "${CONFIGDIR}/${CONFIGFILE}" \
  138. "/run/network/ifconfig.${INTERFACE}/"
  139. fi
  140. else
  141. echo "ERROR: Service '${SERVICE}' is not a valid service."
  142. echo ""
  143. exit 2
  144. fi
  145. )
  146. done
  147. fi
  148. fi