]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
Added dhcp support to the liveramfs.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Aug 2009 18:37:16 +0000 (20:37 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Aug 2009 18:37:16 +0000 (20:37 +0200)
src/mkinitramfs/functions
src/mkinitramfs/mkliveramfs

index b89ab6097b5aa63b06bdddf26cdc8e877bc3857f..e92b41fbd804ac499aae3936645b84c62a9d25dc 100644 (file)
@@ -685,3 +685,167 @@ done
 
 vecho "kernel commandline: $(cat /proc/cmdline)"
 EOF
+
+if [ "${WITH_NET}" = "1" ]; then
+       MODULES="$MODULES af_packet"
+       for module in /lib/modules/$KERNEL/kernel/drivers/net/{,*/}*; do
+               MODULES="$MODULES $(basename ${module/.ko})"
+       done
+
+       install arping curl dhclient ip ping sha1sum /etc/nsswitch.conf \
+               /usr/lib/libnsl.so /usr/lib/libnss_{dns,files}.so
+       mkdir -p $TMPDIR/var/lib/dhclient 2>/dev/null
+
+       cat > sbin/dhclient-script <<'EOF'
+#!/bin/sh
+
+PATH=/usr/sbin:/sbin:/usr/bin:/bin
+
+function ip_encode() {
+       IFS=.
+
+       local int=0
+       for field in $1; do
+               int=$(( $(( $int << 8 )) | $field ))
+       done
+
+       echo $int
+       unset IFS
+}
+
+function mask_to_cidr() {
+       local mask
+       mask=$(ip_encode $1)
+       local cidr
+       cidr=0
+       local x
+       x=$(( 128 << 24 )) # 0x80000000
+
+       while [ $(( $x & $mask )) -ne 0 ]; do
+               [ $mask -eq $x ] && mask=0 || mask=$(( $mask << 1 ))
+               cidr=$(($cidr + 1))
+       done
+
+       if [ $(( $mask & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
+               echo "Invalid net mask: $1" >&2
+       else
+               echo $cidr
+       fi
+}
+
+function ip_in_subnet() {
+       local netmask
+       netmask=$(_netmask $2)
+       [ $(( $(ip_encode $1) & $netmask)) = $(( $(ip_encode ${2%/*}) & $netmask )) ]
+}
+
+function _netmask() {
+       local vlsm
+       vlsm=${1#*/}
+       [ $vlsm -eq 0 ] && echo 0 || echo $(( -1 << $(( 32 - $vlsm )) ))
+}
+
+dhconfig() {
+       if [ -n "${old_ip_address}" ] &&
+                       [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
+               # IP address changed.  Bringing down the interface will delete all
+               # routes, and clear the ARP cache.
+               ip -family inet addr flush dev ${interface} >/dev/null 2>&1
+               ip -family inet link set dev ${interface} down
+    fi
+
+       if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
+                       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
+                       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
+                       [ ! "${old_network_number}" = "${new_network_number}" ] ||
+                       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
+                       [ ! "${old_routers}" = "${new_routers}" ] ||
+                       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
+               ip -family inet addr add ${new_ip_address}/${new_prefix} \
+                       broadcast ${new_broadcast_address} dev ${interface}
+
+               if [ -n "${new_interface_mtu}" ]; then
+                       ip link set ${interface} mtu ${new_interface_mtu}
+               fi
+               
+               for router in ${new_routers}; do
+                       if ! ip_in_subnet ${router} ${new_ip_address}/${new_prefix}; then
+                               continue
+                       fi
+                       ip route replace default via ${router}
+                       break
+               done
+       fi
+
+       if [ "${reason}" = "RENEW" ] && \
+                       [ "${new_domain_name}" = "${old_domain_name}" ] && \
+                       [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
+               return
+       fi
+
+    if [ -n "${new_domain_name}" ] || [ -n "${new_domain_name_servers}" ] || \
+                       [ -n "${new_domain_search}" ]; then
+
+               echo "; generated by $0" > /etc/resolv.conf
+
+               if [ -n "${new_domain_search}" ]; then
+                       echo "search ${new_domain_search//\\032/ }" >> /etc/resolv.conf
+               elif [ -n "${new_domain_name}" ]; then
+                       echo "search ${new_domain_name//\\032/ }" >> /etc/resolv.conf
+               fi
+
+               for nameserver in ${new_domain_name_servers} ; do
+                       echo "nameserver ${nameserver}" >> /etc/resolv.conf
+               done
+    fi
+       
+       if [ -n "${new_host_name}" ]; then
+               hostname ${new_host_name}
+       fi
+}
+
+new_prefix=$(mask_to_cidr ${new_subnet_mask})
+
+case "${reason}" in
+       MEDIUM)
+               exit 0
+               ;;
+
+       PREINIT)
+               ip -family inet addr flush dev ${interface} >/dev/null 2>&1
+               ip -family inet link set ${interface} up
+               exit 0
+               ;;
+
+       ARPCHECK|ARPSEND)
+               if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] || \
+                               arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
+                       exit 0
+               else
+                       exit 1
+               fi
+               ;;
+
+       BOUND|RENEW|REBIND|REBOOT)
+               dhconfig
+               exit 0
+        ;;
+
+       EXPIRE|FAIL|RELEASE|STOP)
+               if [ -n "${old_ip_address}" ]; then
+                       # Shut down interface, which will delete routes and clear arp cache.
+                       ip -family inet addr flush dev ${interface} >/dev/null 2>&1
+                       ip -family inet link set ${interface} down
+               fi
+               exit 0
+               ;;
+
+       *)
+               echo "Unhandled state: ${reason}" >&2
+               exit 1  
+               ;;
+esac
+
+EOF
+       chmod 755 sbin/dhclient-script
+fi
index dc9ab509d65b69a3a33f59a039eb1c9893654cb6..ac9210072b113263d8f99859b13d4023f080a539 100644 (file)
 # Modules needed by the live system
 MODULES="$MODULES aufs squashfs loop vfat ehci-hcd ohci-hcd uhci-hcd usb-storage"
 
-# NFS
-if [ "$WITH_NET" = "1" ]; then
-       MODULES="$MODULES af_packet"
-       for module in /lib/modules/$KERNEL/kernel/drivers/net/{,*/}*; do
-               MODULES="$MODULES $(basename ${module/.ko})"
-       done
-       install arping curl dhclient dhclient-script ping sha1sum /etc/nsswitch.conf \
-               /usr/lib/libnsl.so /usr/lib/libnss_{dns,files}.so
-       mkdir -p $TMPDIR/var/lib/dhclient
-fi
-
 # Add all storage modules
 for module in /lib/modules/$KERNEL/kernel/drivers/{ata,message/fusion,pcmcia,scsi{,/*}}/*; do
        MODULES="$MODULES $(basename ${module/.ko})"