| 1 | #!/bin/bash |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: networking ifupdown |
| 4 | # Required-Start: mountkernfs $local_fs urandom |
| 5 | # Required-Stop: $local_fs |
| 6 | # Default-Start: S |
| 7 | # Default-Stop: 0 6 |
| 8 | # Short-Description: Raise network interfaces. |
| 9 | # Description: Prepare /run/network directory, ifstate file and raise network interfaces, or take them down. |
| 10 | ### END INIT INFO |
| 11 | |
| 12 | RUN_DIR="/run/network" |
| 13 | IFSTATE_LOCKFILE="${RUN_DIR}/ifstatelock" |
| 14 | |
| 15 | STATE_DIR="/var/tmp/network" |
| 16 | IFSTATE_FILE="${STATE_DIR}/ifstatenew" |
| 17 | |
| 18 | NAME=networking |
| 19 | SCRIPTNAME=/etc/init.d/$NAME |
| 20 | |
| 21 | [ -x /sbin/ifup ] || exit 0 |
| 22 | [ -x /sbin/ifdown ] || exit 0 |
| 23 | |
| 24 | . /lib/lsb/init-functions |
| 25 | |
| 26 | CONFIGURE_INTERFACES=yes |
| 27 | |
| 28 | EXTRA_ARGS= |
| 29 | |
| 30 | [ -f /etc/default/networking ] && . /etc/default/networking |
| 31 | |
| 32 | [ "$VERBOSE" = yes ] && EXTRA_ARGS=-v |
| 33 | [ "$DEBUG" = yes ] && EXTRA_ARGS="$EXTRA_ARGS -d" |
| 34 | [ "$SYSLOG" = yes ] && EXTRA_ARGS="$EXTRA_ARGS --syslog" |
| 35 | |
| 36 | gen_examples() { |
| 37 | # Generate sample interfaces file. The interfaces files are |
| 38 | # generated under /usr/share/doc/ifupdown2/examples/ |
| 39 | # |
| 40 | |
| 41 | # generate files only at boot |
| 42 | [ -f ${IFSTATE_LOCKFILE} ] && return |
| 43 | |
| 44 | python_ifupdown2_docdir="/usr/share/doc/ifupdown2" |
| 45 | swpfile=${python_ifupdown2_docdir}"/examples/swp_defaults" |
| 46 | bridgedefaultfile=${python_ifupdown2_docdir}"/examples/bridge_untagged_default" |
| 47 | interfaces_gen_script=${python_ifupdown2_docdir}"/examples/generate_interfaces.py" |
| 48 | |
| 49 | [ ! -e $interfaces_gen_script ] && return |
| 50 | ret=$($interfaces_gen_script -s 2>&1 >$swpfile) |
| 51 | ret=$($interfaces_gen_script -b 2>&1 >$bridgedefaultfile) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | perf_options() { |
| 56 | # At bootup lets set perfmode |
| 57 | [ -f ${IFSTATE_LOCKFILE} ] && echo -n "" && return |
| 58 | |
| 59 | echo -n "--perfmode" |
| 60 | } |
| 61 | |
| 62 | process_exclusions() { |
| 63 | set -- $EXCLUDE_INTERFACES |
| 64 | exclusions="" |
| 65 | for d |
| 66 | do |
| 67 | exclusions="-X $d $exclusions" |
| 68 | done |
| 69 | echo $exclusions |
| 70 | } |
| 71 | |
| 72 | check_network_file_systems() { |
| 73 | [ -e /proc/mounts ] || return 0 |
| 74 | |
| 75 | if [ -e /etc/iscsi/iscsi.initramfs ]; then |
| 76 | log_warning_msg "not deconfiguring network interfaces: iSCSI root is mounted." |
| 77 | exit 0 |
| 78 | fi |
| 79 | |
| 80 | while read DEV MTPT FSTYPE REST; do |
| 81 | case $DEV in |
| 82 | /dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*) |
| 83 | log_warning_msg "not deconfiguring network interfaces: network devices still mounted." |
| 84 | exit 0 |
| 85 | ;; |
| 86 | esac |
| 87 | case $FSTYPE in |
| 88 | nfs|nfs4|smbfs|ncp|ncpfs|cifs|coda|ocfs2|gfs|pvfs|pvfs2|fuse.httpfs|fuse.curlftpfs) |
| 89 | log_warning_msg "not deconfiguring network interfaces: network file systems still mounted." |
| 90 | exit 0 |
| 91 | ;; |
| 92 | esac |
| 93 | done < /proc/mounts |
| 94 | } |
| 95 | |
| 96 | check_network_swap() { |
| 97 | [ -e /proc/swaps ] || return 0 |
| 98 | |
| 99 | while read DEV MTPT FSTYPE REST; do |
| 100 | case $DEV in |
| 101 | /dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*) |
| 102 | log_warning_msg "not deconfiguring network interfaces: network swap still mounted." |
| 103 | exit 0 |
| 104 | ;; |
| 105 | esac |
| 106 | done < /proc/swaps |
| 107 | } |
| 108 | |
| 109 | ifup_hotplug () { |
| 110 | if [ -d /sys/class/net ] |
| 111 | then |
| 112 | ifaces=$(for iface in $(ifquery --list --allow=hotplug 2>/dev/null) |
| 113 | do |
| 114 | link=${iface##:*} |
| 115 | link=${link##.*} |
| 116 | if [ -e "/sys/class/net/$link" ] && [ "$(cat /sys/class/net/$link/operstate)" = up ] |
| 117 | then |
| 118 | echo "$iface" |
| 119 | fi |
| 120 | done) |
| 121 | if [ -n "$ifaces" ] |
| 122 | then |
| 123 | ifup $ifaces "$@" || true |
| 124 | fi |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | ifupdown_init() { |
| 129 | # remove state file at boot |
| 130 | [ ! -e ${IFSTATE_LOCKFILE} ] && rm -f ${IFSTATE_FILE} |
| 131 | [ ! -e /run/network ] && mkdir -p /run/network &>/dev/null |
| 132 | [ ! -e /etc/network/run ] && \ |
| 133 | ln -sf /run/network /etc/network/run &>/dev/null |
| 134 | } |
| 135 | |
| 136 | case "$1" in |
| 137 | start) |
| 138 | gen_examples |
| 139 | ifupdown_init |
| 140 | if [ "$CONFIGURE_INTERFACES" = no ] |
| 141 | then |
| 142 | log_action_msg "Not configuring network interfaces, see /etc/default/networking" |
| 143 | exit 0 |
| 144 | fi |
| 145 | set -f |
| 146 | exclusions=$(process_exclusions) |
| 147 | perfoptions=$(perf_options) |
| 148 | log_action_begin_msg "Configuring network interfaces" |
| 149 | ifup -a $EXTRA_ARGS $exclusions $perfoptions |
| 150 | log_action_end_msg $? |
| 151 | ;; |
| 152 | |
| 153 | stop) |
| 154 | if [ "$SKIP_DOWN_AT_SYSRESET" = "yes" ]; then |
| 155 | shutdown_or_reboot=$(runlevel 2>/dev/null | \ |
| 156 | /usr/bin/tr -s " " | \ |
| 157 | /usr/bin/cut -d " " \ |
| 158 | -f1- --output-delimiter=$'\n' | \ |
| 159 | /bin/grep -e "0" -e "6") |
| 160 | if [ -n "$shutdown_or_reboot" ]; then |
| 161 | log_action_begin_msg "Deconfiguring network interfaces..skip" |
| 162 | log_action_end_msg 0 |
| 163 | exit 0 |
| 164 | fi |
| 165 | fi |
| 166 | ifupdown_init |
| 167 | check_network_file_systems |
| 168 | check_network_swap |
| 169 | exclusions=$(process_exclusions) |
| 170 | |
| 171 | log_action_begin_msg "Deconfiguring network interfaces" |
| 172 | ifdown -a $EXTRA_ARGS $exclusions |
| 173 | log_action_end_msg $? |
| 174 | ;; |
| 175 | |
| 176 | reload) |
| 177 | |
| 178 | ifupdown_init |
| 179 | log_action_begin_msg "Reloading network interfaces configuration" |
| 180 | |
| 181 | ifreload -a $EXTRA_ARGS |
| 182 | log_action_end_msg $? |
| 183 | ;; |
| 184 | |
| 185 | reload-currently-up) |
| 186 | |
| 187 | ifupdown_init |
| 188 | log_action_begin_msg "Reloading currently up network interfaces configuration" |
| 189 | |
| 190 | ifreload --currently-up $EXTRA_ARGS |
| 191 | log_action_end_msg $? |
| 192 | ;; |
| 193 | |
| 194 | force-reload) |
| 195 | |
| 196 | ifupdown_init |
| 197 | |
| 198 | log_action_begin_msg "Reloading network interfaces configuration" |
| 199 | ifreload -f -a $EXTRA_ARGS |
| 200 | log_action_end_msg $? |
| 201 | ;; |
| 202 | |
| 203 | restart) |
| 204 | ifupdown_init |
| 205 | |
| 206 | set -f |
| 207 | exclusions=$(process_exclusions) |
| 208 | log_action_begin_msg "Reconfiguring network interfaces" |
| 209 | ifdown -a $EXTRA_ARGS $exclusions || true |
| 210 | ifup -a $EXTRA_ARGS $exclusions |
| 211 | log_action_end_msg $? |
| 212 | ;; |
| 213 | |
| 214 | *) |
| 215 | echo "Usage: /etc/init.d/networking {start|stop|reload|restart|force-reload}" |
| 216 | exit 1 |
| 217 | ;; |
| 218 | esac |
| 219 | |
| 220 | exit 0 |
| 221 | |
| 222 | # vim: noet ts=8 |