summaryrefslogtreecommitdiffstats
path: root/utils/pb-udhcpc
blob: bac16dad5dc322b8c20cd0008b9dd6fdcb7c1ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
#
# Petitboot udhcpc user script.  Should be run by udhcpc when
# there is a change in the dhcp configuration.  For more info
# see the udhcpc man page and the Linux kernel source file
# Documentation/filesystems/nfsroot.txt.
#

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"
		return
	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@${interface} method=dhcp"

	# first, try by MAC
	mac=$(tr ':' '-' < /sys/class/$interface/address)
	pb-event $params url=$basedir/01-$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=${ip_hex: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@${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
	[ -z "$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#*:}

	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@${interface} \
		name=netboot \
		image=tftp://${siaddr}/${boot_file} \
		args="$args"
}

pb_remove () {
	pb-event remove@${interface} name=netboot
}

case "$1" in
bound | renew)
	pb_add
	;;
deconfig)
	pb_remove
	;;
*)
	;;
esac

printf "--- $1 ---\n" >> ${log}
set >> ${log}
printf "---------------\n" >> ${log}
OpenPOWER on IntegriCloud