summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDieter Plaetinck <dieter@plaetinck.be>2010-12-28 22:02:04 +0100
committerDieter Plaetinck <dieter@plaetinck.be>2010-12-28 22:11:56 +0100
commit407da88514dafeab77e683ee50029f55c4a32424 (patch)
tree896eeaac7a21ba3b1a3631bcdbbe744d89b0d29c
parent25f7a0471340e8d6c2b37e3c75aa7d0ae209d09d (diff)
Refactor network config generation code
Make interactive_runtime_network() and target_configure_network() able to be run multiple times with different settings, and make it clear what's going on in target_configure_network().
-rw-r--r--src/core/libs/lib-misc.sh12
-rw-r--r--src/core/libs/lib-network.sh35
-rw-r--r--src/core/libs/lib-ui-interactive.sh11
3 files changed, 42 insertions, 16 deletions
diff --git a/src/core/libs/lib-misc.sh b/src/core/libs/lib-misc.sh
index 3af5977..b647f2b 100644
--- a/src/core/libs/lib-misc.sh
+++ b/src/core/libs/lib-misc.sh
@@ -140,3 +140,15 @@ target_configure_initial_keymap_font ()
[ -n "$var_KEYMAP" ] && sed -i "s/^KEYMAP=.*/KEYMAP=\"`basename $var_KEYMAP .map.gz`\"/" ${var_TARGET_DIR}/etc/rc.conf
[ -n "$var_CONSOLEFONT" ] && sed -i "s/^CONSOLEFONT=.*/CONSOLEFONT=\"${var_CONSOLEFONT/\.*/}\"/" ${var_TARGET_DIR}/etc/rc.conf
}
+
+# apped string after last line matching regex in a file.
+# $1 regex
+# $2 string (can contain "\n", "\t" etc)
+# $3 file
+append_after_last ()
+{
+ [ -r "$3" -a -w "$3" ] || return 1
+ line_no=$(sed -ne "$1=" "$3" | tail -n 1)
+ [ -n "$line_no" ] || return 1
+ sed -i ${line_no}a"$2" "$3"
+}
diff --git a/src/core/libs/lib-network.sh b/src/core/libs/lib-network.sh
index aa961ce..1499dbf 100644
--- a/src/core/libs/lib-network.sh
+++ b/src/core/libs/lib-network.sh
@@ -7,27 +7,38 @@
target_configure_network()
{
source $RUNTIME_DIR/aif-network-settings 2>/dev/null || return 1
+
+ IFO=${INTERFACE_PREV:-eth0} # old iface: a previously entered one, or the arch default
+ IFN=${INTERFACE:-eth0} # new iface: a specified one, or the arch default
+
+ # comment out any existing uncommented entries, whether specified by us, or arch defaults.
+ for var in eth0 $IFO INTERFACES gateway ROUTES
+ do
+ sed -i "s/^$var=/#$var=/" ${var_TARGET_DIR}/etc/rc.conf || return 1
+ done
+ sed -i "s/^nameserver/#nameserver/" ${var_TARGET_DIR}/etc/resolv.conf || return 1
+ if [ -f ${var_TARGET_DIR}/etc/profile.d/proxy.sh ]
+ then
+ sed -i "s/^export/#export/" ${var_TARGET_DIR}/etc/profile.d/proxy.sh || return 1
+ fi
+
if [ "$DHCP" = 0 ] ; then
- sed -i "s/#eth0=\"eth0/eth0=\"eth0/g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s/^eth0=\"dhcp/#eth0=\"dhcp/g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#eth0=\"eth0#${INTERFACE:-eth0}=\"${INTERFACE:-eth0}#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#${INTERFACE:-eth0} 192.168.0.2#${INTERFACE:-eth0} ${IPADDR:-192.168.0.2}#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#netmask 255.255.255.0#netmask ${SUBNET:-255.255.255.0}#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#broadcast 192.168.0.255#broadcast ${BROADCAST:-192.168.0.255}#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
+ local line="$IFN=\"$IFN ${IPADDR:-192.168.0.2} netmask ${SUBNET:-255.255.255.0} broadcast ${BROADCAST:-192.168.0.255}\""
+ append_after_last "/$IFO\|eth0/" "$line" ${var_TARGET_DIR}/etc/rc.conf || return 1
+
if [ -n "$GW" ]; then
- sed -i "s#gw 192.168.0.1#gw $GW#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#!gateway#gateway#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
+ append_after_last "/gateway/" "gateway=\"default gw $GW\"" ${var_TARGET_DIR}/etc/rc.conf || return 1
+ append_after_last "/ROUTES/" "ROUTES=(gateway)" ${var_TARGET_DIR}/etc/rc.conf || return 1
fi
if [ -n "$DNS" ]
then
echo "nameserver $DNS" >> ${var_TARGET_DIR}/etc/resolv.conf || return 1
fi
else
- sed -i "s/^eth0=\"eth0/#eth0=\"eth0/g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s/#eth0=\"dhcp/eth0=\"dhcp/g" ${var_TARGET_DIR}/etc/rc.conf || return 1
- sed -i "s#eth0=\"dhcp#${INTERFACE:-eth0}=\"dhcp#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
+ append_after_last "/$IFO\|eth0/" "$IFN=\"dhcp\"" ${var_TARGET_DIR}/etc/rc.conf || return 1
fi
- sed -i "s#INTERFACES=(eth0)#INTERFACES=(${INTERFACE:-eth0})#g" ${var_TARGET_DIR}/etc/rc.conf || return 1
+
+ append_after_last "/$IFO\|eth0/" "INTERFACES=($IFN)" ${var_TARGET_DIR}/etc/rc.conf || return 1
if [ -n "$PROXY_HTTP" ]; then
echo "export http_proxy=$PROXY_HTTP" >> ${var_TARGET_DIR}/etc/profile.d/proxy.sh || return 1
diff --git a/src/core/libs/lib-ui-interactive.sh b/src/core/libs/lib-ui-interactive.sh
index 0b3abb4..864ee62 100644
--- a/src/core/libs/lib-ui-interactive.sh
+++ b/src/core/libs/lib-ui-interactive.sh
@@ -786,15 +786,16 @@ If any previous configuration you've done until now (like fancy filesystems) req
# args: none
# returns: 1 on failure
interactive_runtime_network() {
- INTERFACE=""
- DHCP=""
local ifaces
ifaces=$(ifconfig -a |grep "Link encap:Ethernet"|sed 's/ \+Link encap:Ethernet \+HWaddr \+/ /g')
[ -z "$ifaces" ] && show_warning "No network interfaces?" "Cannot find any ethernet interfaces. This usually means udev was\nunable to load the module and you must do it yourself. Switch to\nanother VT, load the appropriate module, and run this step again." && return 1
+ INTERACE_PREV=$INTERFACE
+ unset INTERFACE DHCP IPADDR SUBNET BROADCAST GW DNS PROXY_HTTP PROXY_FTP
ask_option no "Interface selection" "Select a network interface" required $ifaces || return 1
INTERFACE=$ANSWER_OPTION
+ [ "$INTERFACE" = "$INTERFACE_PREV" ] && INTERFACE_PREV=
if ask_yesno "Do you want to use DHCP?"
then
@@ -826,7 +827,7 @@ interactive_runtime_network() {
[ -n "$GW" ] && default_dns="$GW"
[ -z "$GW" ] && default_dns="$(sed 's/\.[^.]*$/\.1/' <<< $IPADDR)"
ask_string "Enter your DNS server IP" "$default_dns" || return 1
- DNS=$ANSWER_STRING
+ DNS=$ANSWER_STRING
ask_string "Enter your HTTP proxy server, for example:\nhttp://name:port\nhttp://ip:port\nhttp://username:password@ip:port\n\n Leave the field empty if no proxy is needed to install." "" 0 || return 1
PROXY_HTTP=$ANSWER_STRING
ask_string "Enter your FTP proxy server, for example:\nhttp://name:port\nhttp://ip:port\nhttp://username:password@ip:port\n\n Leave the field empty if no proxy is needed to install." "" 0 || return 1
@@ -858,7 +859,9 @@ interactive_runtime_network() {
fi
echo "nameserver $DNS" >/etc/resolv.conf
fi
- echo "INTERFACE=$INTERFACE
+
+ echo "INTERFACE_PREV=$INTERFACE_PREV
+ INTERFACE=$INTERFACE
DHCP=$DHCP
IPADDR=$IPADDR
SUBNET=$SUBNET