Ticket #3053: ipv4-static

File ipv4-static, 2.9 KB (added by qrux, 13 years ago)

/lib/services/ipv4-static

Line 
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
15. /lib/lsb/init-functions
16
17if [ -z ${1} ] ; then
18 log_failure_msg "No ifconfig file specified"
19 exit 1
20fi
21IFCONFIG=$1
22. ${IFCONFIG}
23
24if [ -z "${IP}" ]; then
25 log_failure_msg "\nIP variable missing from ${IFCONFIG}, cannot continue."
26 exit 1
27fi
28
29# If there are multiple ${PHYS}, then use the first.
30test -z $IPV4_IF && IPV4_IF="$(echo ${PHYS} | awk '{print $1}')"
31
32if [ -z "${PREFIX}" -a -z "${PEER}" ]; then
33 log_warning_msg "\nPREFIX variable missing from ${IFCONFIG}, assuming 24."
34 PREFIX=24
35 args="${args} ${IP}/${PREFIX}"
36
37elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then
38 log_failure_msg "\nPREFIX and PEER both specified in ${IFCONFIG}, cannot continue."
39 exit 1
40
41elif [ -n "${PREFIX}" ]; then
42 args="${args} ${IP}/${PREFIX}"
43
44elif [ -n "${PEER}" ]; then
45 args="${args} ${IP} peer ${PEER}"
46fi
47
48if [ -n "${BROADCAST}" ]; then
49 args="${args} broadcast ${BROADCAST}"
50fi
51
52case "${2}" in
53 up)
54 if [ "$(ip addr show ${IPV4_IF} 2>/dev/null | grep ${IP})" == "" ]; then
55
56 # Cosmetic output not needed for multiple services
57 if ! $(echo ${SERVICE} | grep -q " "); then
58 log_info_msg2 "\n" # Terminate the previous message
59 fi
60
61 log_info_msg "Adding IPv4 address ${IP} to the ${IPV4_IF} interface..."
62 ip addr add ${args} dev ${IPV4_IF}
63 evaluate_retval
64
65 if [ -n "${GATEWAY}" ]; then
66 if ip route | grep -q default; then
67 log_warning_msg "\nGateway already setup; skipping."
68 else
69 log_info_msg "Setting up default gateway..."
70 ip route add default via ${GATEWAY} dev ${IPV4_IF}
71 evaluate_retval
72 fi
73 fi
74 else
75 msg="Cannot add IPv4 address ${IP} to ${IPV4_IF}. Already present."
76 log_warning_msg "$msg"
77 fi
78 ;;
79
80 down)
81 if [ "$(ip addr show ${IPV4_IF} 2>/dev/null | grep ${IP})" != "" ]; then
82 log_info_msg "Removing IPv4 address ${IP} from the ${IPV4_IF} interface..."
83 ip addr del ${args} dev ${IPV4_IF}
84 evaluate_retval
85 fi
86
87 if [ -n "${GATEWAY}" ]; then
88 # Only remove the gateway if ther are no remaining ipv4 addresses
89 if [ "$(ip addr show ${IPV4_IF} 2>/dev/null | grep 'inet ')" != "" ]; then
90 log_info_msg "Removing default gateway..."
91 ip route del default
92 evaluate_retval
93 fi
94 fi
95 ;;
96
97 *)
98 echo "Usage: ${0} <interface> {up|down}"
99 exit 1
100 ;;
101esac
102
103# End /lib/services/ipv4-static