pb-udhcpc: Look for more boot options
authorJeremy Kerr <jk@ozlabs.org>
Mon, 6 May 2013 07:54:23 +0000 (15:54 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 9 May 2013 00:47:54 +0000 (08:47 +0800)
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
utils/pb-udhcpc

index ff231780829630dd4823c7c56396acb8ab760e1a..808f12ab51ec5ab90a2fd63741c5e5468f81bb3d 100644 (file)
@@ -9,16 +9,96 @@
 PBOOT_USER_EVENT_SOCKET="/tmp/petitboot.ev"
 log="/var/log/petitboot/pb-udhcpc.log"
 
+resolve_url() {
+       file="$1"
+
+       # URL? use as-is.
+       tmp=${file#://*}
+       if [ "$tmp" != "$file" ]
+       then
+               echo "$file"
+       fi
+
+       # Otherwise, TFTP using an appropriate host. Start with the
+       # DHCP 'tftp' option:
+       host=${tftp}
+
+       # next, try the DHCP next-server-address
+       [ -z "$host" ] && host=${siaddr}
+
+       # finally, use the DHCP server we got this lease from:
+       [ -z "$host" ] && host=${serverid}
+
+       echo "tftp://$host/$file"
+}
+
+do_pxe() {
+       basedir=$1
+
+       params="conf@/net/${interface} method=dhcp"
+
+       # first, try by MAC
+       mac=$(cat /sys/class/net/$interface/address)
+       pb-event $params url=$basedir/$mac
+
+       # try decreasing fragments of IP lease
+       ip_hex=$(printf '%02X%02X%02X%02X' $(echo $ip | tr '.' ' '))
+       for i in $(seq 8 -1 1)
+       do
+               frag=${hex_ip:0:$i}
+               pb-event $params url=$basedir/$frag
+       done
+
+       # last, use default
+       pb-event $params url=$basedir/default
+}
+
 pb_add () {
+
+       # Look for an explicit config file location in the DHCP config-file
+       # parameter
+       if [ -n ${conffile} ]
+       then
+               url=$(resolve_url ${conffile})
+               pb-event conf@/net/${interface} url=$url method=dhcp
+               return
+       fi
+
+       # Otherwise, we'll need the boot-file parameter. Looks like udhcpc
+       # will give us different names, depending if the parameter was in
+       # the header, or specified by options
+       [ -n "$bootfile" ] && bootfile=${boot_file}
+
+       if [ -z "$bootfile" ]
+       then
+               return
+       fi
+
+       # PXE behaviour is to download the config file based on a file
+       # structure relative to the pxelinux binary
+       file=${bootfile}
+       [ -z "$file" ] && file=${boot_file}
+       if [ -n "$file" ]
+       then
+               basedir=${file%%/*}
+               do_pxe $basedir
+       fi
+
+       # Finally, add an option for the boot_file parameter
        k_server_ip=${rootpath%%:*}
        k_root_dir=${rootpath#*:}
 
-       [ ${k_server_ip} != ${rootpath} ] || k_server_ip=${serverid}
+       args=
+       if [ -n $rootpath ]
+       then
+               [ ${k_server_ip} != ${rootpath} ] || k_server_ip=${serverid}
+               args="root=/dev/nfs ip=any nfsroot=${k_server_ip}:${k_root_dir}"
+       fi
 
        pb-event add@/net/${interface} \
                name=netboot \
                image=tftp://${siaddr}/${boot_file} \
-               args="root=/dev/nfs ip=any nfsroot=${k_server_ip}:${k_root_dir}"
+               args="$args"
 }
 
 pb_remove () {