3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 # Functions which perform our networking setup
30 # Function which creates a kde4 .desktop file for the PC-BSD net tray
31 create_desktop_nettray()
34 echo "#!/usr/bin/env xdg-open
36 Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
39 Type=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
40 chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
44 # Function which checks is a nic is wifi or not
48 ifconfig ${NIC} | grep -q "802.11" 2>/dev/null
57 # Function to get the first available wired nic, used for setup
60 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
61 # start by getting a list of nics on this system
62 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
63 if [ -e "${TMPDIR}/.niclist" ]
67 NIC="`echo $line | cut -d ':' -f 1`"
74 done < ${TMPDIR}/.niclist
82 # Function which simply enables plain dhcp on all detected nics
85 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
86 # start by getting a list of nics on this system
87 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
88 if [ -e "${TMPDIR}/.niclist" ]
90 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
94 NIC="`echo $line | cut -d ':' -f 1`"
95 DESC="`echo $line | cut -d ':' -f 2`"
96 echo_log "Setting $NIC to DHCP on the system."
100 # We have a wifi device, setup a wlan* entry for it
101 WLAN="wlan${WLANCOUNT}"
102 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
103 echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
105 WLANCOUNT=$((WLANCOUNT+1))
107 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
111 done < ${TMPDIR}/.niclist
116 # Function which detects available nics, and enables dhcp on them
123 # Function which saves a manual nic setup to the installed system
129 get_value_from_cfg netSaveIP
132 if [ "$NETIP" = "DHCP" ]
134 echo_log "Setting $NIC to DHCP on the system."
135 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
139 # If we get here, we have a manual setup, lets do so now
142 IFARGS="inet ${NETIP}"
144 # Check if we have a netmask to set
145 get_value_from_cfg netSaveMask
147 if [ -n "${NETMASK}" ]
149 IFARGS="${IFARGS} netmask ${NETMASK}"
153 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
154 echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
156 # Check if we have a default router to set
157 get_value_from_cfg netSaveDefaultRouter
159 if [ -n "${NETROUTE}" ]
161 echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
164 # Check if we have a nameserver to enable
165 get_value_from_cfg netSaveNameServer
167 if [ -n "${NAMESERVER}" ]
169 echo "nameserver ${NAMESERVER}" >${FSMNT}/etc/resolv.conf
174 # Function which determines if a nic is active / up
177 ifconfig ${1} | grep -q "status: active" 2>/dev/null
178 if [ $? -eq 0 ] ; then
186 # Function which detects available nics, and runs DHCP on them until
190 # start by getting a list of nics on this system
191 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
194 NIC="`echo $line | cut -d ':' -f 1`"
195 DESC="`echo $line | cut -d ':' -f 2`"
197 is_nic_active "${NIC}"
198 if [ $? -eq 0 ] ; then
199 echo_log "Trying DHCP on $NIC $DESC"
200 dhclient ${NIC} >/dev/null 2>/dev/null
201 if [ $? -eq 0 ] ; then
202 # Got a valid DHCP IP, we can return now
207 done < ${TMPDIR}/.niclist
211 # Get the mac address of a target NIC
214 FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
218 # Function which performs the manual setup of a target nic in the cfg
224 # Check that this NIC exists
225 rc_halt "ifconfig ${NIC}"
227 get_value_from_cfg netIP
230 if [ "$NETIP" = "DHCP" ]
232 echo_log "Enabling DHCP on $NIC"
233 rc_halt "dhclient ${NIC}"
237 # If we get here, we have a manual setup, lets do so now
240 rc_halt "ifconfig ${NIC} ${NETIP}"
242 # Check if we have a netmask to set
243 get_value_from_cfg netMask
245 if [ -n "${NETMASK}" ]
247 rc_halt "ifconfig ${NIC} netmask ${NETMASK}"
250 # Check if we have a default router to set
251 get_value_from_cfg netDefaultRouter
253 if [ -n "${NETROUTE}" ]
255 rc_halt "route add default ${NETROUTE}"
258 # Check if we have a nameserver to enable
259 get_value_from_cfg netNameServer
261 if [ -n "${NAMESERVER}" ]
263 echo "nameserver ${NAMESERVER}" >/etc/resolv.conf
270 # Function which parses the cfg and enables networking per specified
273 # Check if we have any networking requested
274 get_value_from_cfg netDev
281 if [ "$NETDEV" = "AUTO-DHCP" ]
285 enable_manual_nic ${NETDEV}
291 # Function which checks the cfg and enables the specified networking on
292 # the installed system
293 save_networking_install()
296 # Check if we have any networking requested to save
297 get_value_from_cfg netSaveDev
304 if [ "$NETDEV" = "AUTO-DHCP" ]
308 save_manual_nic ${NETDEV}