summaryrefslogtreecommitdiff
path: root/packages/altboot/files/altboot-menu/Advanced/40-bootNFS
blob: c3fb7cedc442668dec7e12b78e3382d4e9dd1062 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# !/bin/sh
#
# Copyright Matthias Hentges (c) 2005
#
# License: GPL (see http://www.gnu.org/licenses/gpl.txt for a copy of the GPL)

M_TITLE="Boot from NFS"

die() {
	echo -e "ERROR: $1" >/dev/tty0
	exec $SH_SHELL </dev/tty0 >/dev/tty0 2>&1
}

# This function is activated by init.altboot by calling this script with the "run" option
run_module() {

	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"

	# Mount /proc, etc
	init_rootfs

	# Needed for NFS
	/etc/init.d/portmap start >/dev/null 2>&1 || die "/etc/init.d/portmap start failed!"
	
	# For some reason NFS mounts hang if /e/i/networking is not run.
	# For the time beeing I'm too lazy to investigate ;)
	/etc/init.d/networking start >/dev/null 2>&1 || die "/etc/init.d/networking start failed!"
	
	sleep 2
	
	# After the PCMCIA service is started, an inserted WLAN card should automatically
	# activate itself.
	
	if test -x /etc/init.d/pcmcia
	then
		/etc/init.d/pcmcia start >/dev/null 2>&1 || die "/etc/init.d/pcmcia start failed!"
	else	
		# With kernel 2.6.16+ udev is used 
		/etc/init.d/udev start >/dev/null 2>&1 || die "/etc/init.d/udev start failed!"
		
		/etc/init.d/udev stop
	fi
	
	nfs_host="`cat /etc/fstab | grep -v ^# | grep nfs | awk '{print $1}'|sed -n "s/\(.*\)\:\(.*\)/\1/p" `"
	nfs_mounts="`cat /etc/fstab | grep -v ^# | grep nfs | awk '{print $1}'`"
	nfs_mountpoints="`cat /etc/fstab | grep -v ^# | grep nfs | awk '{print $2}'`"	
	
	WLAN_NIC="`iwconfig 2>/dev/null | grep ESSID | awk '{print $1}'`"
	
	if test -z "$nfs_host"
	then
		die "${C_RED}No configured NFS drives found in /etc/fstab$C_RESET"
		
	fi
	
	# WLAN with DHCP needs some time to get a lease, set up the routing, etc.	
	echo -n "Waiting for WLAN"
	cnt=0
	while true
	do
		if (ping -c 1 $nfs_host) >/dev/null 2>&1
		then
			echo " found"
			break
		else
			if test "$cnt" = 30 -o "$cnt" = 60
			then
				echo ""
				echo "WARNING: WLAN didn't activate in $cnt seconds!"
				
				if test "$cnt" = 30
				then
					echo "Restarting udhcpc"
					killall udhcpc
					
					udhcpc -i "$WLAN_NIC" -H `cat /etc/hostname` >/dev/null 2>&1
				else
					die "Failed to activate WLAN!"
					break			
				fi
			else
				echo -n "."
				let cnt=$cnt+1
			fi
		fi
		sleep 1
	done				
	
	if test "` echo "$nfs_mountpoints" |wc -l | tr -d " "`" -gt 1
	then
		echo -e "Please select your NFS root:\n"

		cnt=1
		for nfs_mount in $nfs_mountpoints
		do
			echo -e "\t[$cnt] $nfs_mount"
			let cnt=$cnt+1
		done
		
		echo ""
		
		while test -z "$selection"
		do
			stty echo
			echo -n "Boot NFS root: "
			
			if test "$AUTOBOOT" != "yes"
			then
				read junk < /dev/tty1
			else
				if test -e /etc/.altboot-bootNFS-source.last
				then
					junk="`cat /etc/.altboot-bootNFS-source.last`"
					test -z "$junk" && read junk < /dev/tty1 || echo "$junk (autoboot)"
				else
					read junk < /dev/tty1
				fi
			fi	
				
			
			cnt=1
			for nfs_mount in $nfs_mounts
			do
				if test "$junk" = "$cnt"
				then
					selection="$nfs_mount"
					echo "$junk" > /etc/.altboot-bootNFS-source.last
				fi
				let cnt=$cnt+1
			done			
			
		done
	else
		test -z "$nfs_mounts" && die "No NFS mounts configured in /etc/fstab!"	
		selection="$nfs_mounts"
	fi
	
	mkdir -p /media/nfsroot || die "mkdir -p /media/nfsroot failed!"
	
	echo -n "Mounting NFS root..."
	
	if ( mount | grep -q "/media/nfsroot" )
	then
		echo "/media/nfsroot already used, tying to umount..."
		umount /media/image
		losetup -d /dev/loop0
		umount /media/nfsroot || die "umount failed!"
	fi
		
	mount -t nfs "$selection" /media/nfsroot && echo ok || die "mount -t nfs "$selection" /media/nfsroot failed!"
	
	# Use configured resolv.conf in the pivoted rootfs
	#echo -n "Copying resolv.conf..."
	#cp /etc/resolv.conf /media/nfsroot/etc && echo ok || echo "FAILED"
	
	check_target "/media/nfsroot" bootNFS
	
}	

case "$1" in
title)	echo "$M_TITLE";;
run)	run_module "$2";;
esac