]> git.ozlabs.org Git - petitboot/blob - utils/pb-udhcpc
dd291b9a0fa61441f072b2b75251abdf160056b5
[petitboot] / utils / pb-udhcpc
1 #!/bin/sh
2 #
3 # Petitboot udhcpc user script.  Should be run by udhcpc when
4 # there is a change in the dhcp configuration.  For more info
5 # see the udhcpc man page and the Linux kernel source file
6 # Documentation/filesystems/nfsroot.txt.
7 #
8
9 PBOOT_USER_EVENT_SOCKET="/tmp/petitboot.ev"
10 log="/var/log/petitboot/pb-udhcpc.log"
11
12 resolve_url() {
13         file="$1"
14
15         # URL? use as-is.
16         tmp=${file%://*}
17         if [ "$tmp" != "$file" ]
18         then
19                 echo "$file"
20                 return
21         fi
22
23         # Otherwise, TFTP using an appropriate host. Start with the
24         # DHCP 'tftp' option:
25         host=${tftp}
26
27         # next, try the DHCP next-server-address
28         [ -z "$host" ] && host=${siaddr}
29
30         # finally, use the DHCP server we got this lease from:
31         [ -z "$host" ] && host=${serverid}
32
33         echo "tftp://$host/$file"
34 }
35
36 do_pxe() {
37         basedir=$1
38
39         params="conf@${interface} method=dhcp"
40
41         # first, try by MAC
42         mac=$(tr ':' '-' < /sys/class/$interface/address)
43         pb-event $params url=$basedir/01-$mac
44
45         # try decreasing fragments of IP lease
46         ip_hex=$(printf '%02X%02X%02X%02X' $(echo $ip | tr '.' ' '))
47         for i in $(seq 8 -1 1)
48         do
49                 frag=${ip_hex:0:$i}
50                 pb-event $params url=$basedir/$frag
51         done
52
53         # last, use default
54         pb-event $params url=$basedir/default
55 }
56
57 pb_add () {
58
59         # Look for an explicit config file location in the DHCP config-file
60         # parameter
61         if [ -n "${conffile}" ]
62         then
63                 url=$(resolve_url ${conffile})
64                 pb-event conf@${interface} url=$url method=dhcp
65                 return
66         fi
67
68         # Otherwise, we'll need the boot-file parameter. Looks like udhcpc
69         # will give us different names, depending if the parameter was in
70         # the header, or specified by options
71         [ -z "$bootfile" ] && bootfile=${boot_file}
72
73         if [ -z "$bootfile" ]
74         then
75                 return
76         fi
77
78         # PXE behaviour is to download the config file based on a file
79         # structure relative to the pxelinux binary
80         file=${bootfile}
81         [ -z "$file" ] && file=${boot_file}
82         if [ -n "$file" ]
83         then
84                 basedir=${file%%/*}
85                 do_pxe $basedir
86         fi
87
88         # Finally, add an option for the boot_file parameter
89         k_server_ip=${rootpath%%:*}
90         k_root_dir=${rootpath#*:}
91
92         args=
93         if [ -n "$rootpath" ]
94         then
95                 [ ${k_server_ip} != ${rootpath} ] || k_server_ip=${serverid}
96                 args="root=/dev/nfs ip=any nfsroot=${k_server_ip}:${k_root_dir}"
97         fi
98
99         pb-event add@${interface} \
100                 name=netboot \
101                 image=tftp://${siaddr}/${boot_file} \
102                 args="$args"
103 }
104
105 pb_remove () {
106         pb-event remove@${interface} name=netboot
107 }
108
109 case "$1" in
110 bound)
111         pb_add
112         ;;
113 deconfig)
114         pb_remove
115         ;;
116 *)
117         ;;
118 esac
119
120 printf "--- $1 ---\n" >> ${log}
121 set >> ${log}
122 printf "---------------\n" >> ${log}