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 |
|
---|
17 | if [ -z ${1} ] ; then
|
---|
18 | log_failure_msg "No ifconfig file specified"
|
---|
19 | exit 1
|
---|
20 | fi
|
---|
21 | IFCONFIG=$1
|
---|
22 | . ${IFCONFIG}
|
---|
23 |
|
---|
24 | if [ -z "${IP}" ]; then
|
---|
25 | log_failure_msg "\nIP variable missing from ${IFCONFIG}, cannot continue."
|
---|
26 | exit 1
|
---|
27 | fi
|
---|
28 |
|
---|
29 | # If there are multiple ${PHYS}, then use the first.
|
---|
30 | test -z $IPV4_IF && IPV4_IF="$(echo ${PHYS} | awk '{print $1}')"
|
---|
31 |
|
---|
32 | if [ -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 |
|
---|
37 | elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then
|
---|
38 | log_failure_msg "\nPREFIX and PEER both specified in ${IFCONFIG}, cannot continue."
|
---|
39 | exit 1
|
---|
40 |
|
---|
41 | elif [ -n "${PREFIX}" ]; then
|
---|
42 | args="${args} ${IP}/${PREFIX}"
|
---|
43 |
|
---|
44 | elif [ -n "${PEER}" ]; then
|
---|
45 | args="${args} ${IP} peer ${PEER}"
|
---|
46 | fi
|
---|
47 |
|
---|
48 | if [ -n "${BROADCAST}" ]; then
|
---|
49 | args="${args} broadcast ${BROADCAST}"
|
---|
50 | fi
|
---|
51 |
|
---|
52 | case "${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 | ;;
|
---|
101 | esac
|
---|
102 |
|
---|
103 | # End /lib/services/ipv4-static
|
---|