From 099493b62ce9891c8663bde4ef6f7beec805281e Mon Sep 17 00:00:00 2001 From: Ramakrishnan G Date: Thu, 22 May 2014 18:04:54 +0530 Subject: [PATCH] Add dhcp support for ramdisk element. This change adds support for retrieving the IP through DHCP if 'ip' was not provided as a kernel command-line argument by pxelinux. This is used when the kernel/ramdisk is booted by virtual media. Change-Id: I1097ce5b56ad40f2d6dc3181681d54f924ec7145 Closes-Bug: #1321563 --- elements/ramdisk/binary-deps.d/ramdisk | 1 + elements/ramdisk/element-deps | 1 + elements/ramdisk/extra-data.d/scripts/init | 8 ++-- elements/ramdisk/init.d/02-start-network | 39 +++++++++++++------ .../ramdisk/install.d/20-install-dhcp-client | 5 +-- elements/ramdisk/pkg-map | 10 +++++ 6 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 elements/ramdisk/binary-deps.d/ramdisk create mode 100644 elements/ramdisk/element-deps create mode 100644 elements/ramdisk/pkg-map diff --git a/elements/ramdisk/binary-deps.d/ramdisk b/elements/ramdisk/binary-deps.d/ramdisk new file mode 100644 index 00000000..5d468f9b --- /dev/null +++ b/elements/ramdisk/binary-deps.d/ramdisk @@ -0,0 +1 @@ +dhclient diff --git a/elements/ramdisk/element-deps b/elements/ramdisk/element-deps new file mode 100644 index 00000000..ef25eec6 --- /dev/null +++ b/elements/ramdisk/element-deps @@ -0,0 +1 @@ +pkg-map diff --git a/elements/ramdisk/extra-data.d/scripts/init b/elements/ramdisk/extra-data.d/scripts/init index fcdd0d3f..5503f534 100755 --- a/elements/ramdisk/extra-data.d/scripts/init +++ b/elements/ramdisk/extra-data.d/scripts/init @@ -40,10 +40,12 @@ mount -t proc proc /proc readonly _BOOTIF_=$(get_kernel_parameter BOOTIF) readonly _IP_=$(get_kernel_parameter ip) readonly BOOT_MAC_ADDRESS=$(echo "$_BOOTIF_" | sed -e "s/-/:/g" | sed -e "s/^01://g" | tr 'a-f' 'A-F') -readonly BOOT_IP_ADDRESS=$(echo "$_IP_" | cut -d':' -f1) readonly BOOT_SERVER=$(echo "$_IP_" | cut -d':' -f2) -readonly BOOT_NETMASK=$(echo "$_IP_" | cut -d':' -f4) -readonly BOOT_GATEWAY=$(echo "$_IP_" | cut -d':' -f3) + +# IP address and other parameters may be set later in the init script +BOOT_IP_ADDRESS=$(echo "$_IP_" | cut -d':' -f1) +BOOT_NETMASK=$(echo "$_IP_" | cut -d':' -f4) +BOOT_GATEWAY=$(echo "$_IP_" | cut -d':' -f3) readonly DISK=$(get_kernel_parameter disk) diff --git a/elements/ramdisk/init.d/02-start-network b/elements/ramdisk/init.d/02-start-network index be5f7e87..94b352ae 100644 --- a/elements/ramdisk/init.d/02-start-network +++ b/elements/ramdisk/init.d/02-start-network @@ -26,20 +26,35 @@ if [ $rv -ne 0 ]; then troubleshoot fi fi -ifconfig "$BOOT_INTERFACE" "$BOOT_IP_ADDRESS" netmask "$BOOT_NETMASK" -route del default || true -route add default gw $BOOT_GATEWAY -echo "pinging to boot server $BOOT_SERVER" -w=30 -while [ $w -gt 0 ]; do - ping -c 5 -q "$BOOT_SERVER" > /dev/null - if [ $? -eq 0 ]; then - break +# Check if boot IP address was specific or retrieve from DHCP +if [ -n "$BOOT_IP_ADDRESS" ]; then + ifconfig "$BOOT_INTERFACE" "$BOOT_IP_ADDRESS" netmask "$BOOT_NETMASK" + route del default || true + route add default gw $BOOT_GATEWAY +else + dhclient -1 "$BOOT_INTERFACE" + if [[ $? == 2 ]]; then + echo "Error getting IP address for $BOOT_INTERFACE with MAC \ + $BOOT_MAC_ADDRESS" + troubleshoot fi - sleep 1 - w=`expr $w - 5` -done + + BOOT_IP_ADDRESS=$(ifconfig "$BOOT_INTERFACE" | grep 'inet addr:' | \ + cut -d: -f2 | awk '{ print $1}') + BOOT_NETMASK=$(ifconfig "$BOOT_INTERFACE" | grep 'Mask:' | cut -d':' -f4) + BOOT_GATEWAY=$(route -n | grep "$BOOT_INTERFACE" | grep '^0.0.0.0' | \ + awk '{print $2}') + echo "obtained the following from dhcp: " + echo " ip address = $BOOT_IP_ADDRESS" + echo " netmask = $BOOT_NETMASK" + echo " gateway = $BOOT_GATEWAY" +fi + +if [ -n "$BOOT_SERVER" ]; then + echo "pinging to boot server $BOOT_SERVER" + wait_for 6 1 ping -c 5 -q "$BOOT_SERVER" > /dev/null +fi echo "network ready" diff --git a/elements/ramdisk/install.d/20-install-dhcp-client b/elements/ramdisk/install.d/20-install-dhcp-client index 98fc31d6..f65ef3bf 100755 --- a/elements/ramdisk/install.d/20-install-dhcp-client +++ b/elements/ramdisk/install.d/20-install-dhcp-client @@ -2,6 +2,5 @@ set -eux set -o pipefail -if [ "opensuse" = "$DISTRO_NAME" ]; then - install-packages dhcp-client -fi +DHCP_CLIENT=$(pkg-map --element ramdisk dhcp_client) +install-packages $DHCP_CLIENT diff --git a/elements/ramdisk/pkg-map b/elements/ramdisk/pkg-map new file mode 100644 index 00000000..fa51ee75 --- /dev/null +++ b/elements/ramdisk/pkg-map @@ -0,0 +1,10 @@ +{ + "family":{ + "debian":{ + "dhcp_client":"isc-dhcp-client" + } + }, + "default":{ + "dhcp_client":"dhclient" + } +} \ No newline at end of file