aboutsummaryrefslogtreecommitdiff
blob: 88a2aa777495a36d937b20d1e4efd9e7bce063da (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/ash

# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later

export PATH="/usr/sbin:/usr/bin:/sbin:/bin"

#// Path, basic vars
#//--------------------------------------------------------------------------------

BasicSetup() {
	#// Copyright year, Build date in YYYYMMDD format, and in MMDDYYYY to make busybox 'date' happy
	MYDATE="$(cat /etc/build_date)"
	CPYYEAR="$(echo ${MYDATE} | cut -c 1-4)"
	BBDATE="$(echo ${MYDATE} | cut -c 5-8)$(echo ${MYDATE} | cut -c 1-4)"
	DISDATE="$(echo ${MYDATE} | cut -c 7-8) $(echo ${MYDATE} | cut -c 5-6) $(echo ${MYDATE} | cut -c 1-4)"

	. /etc/initrd.defaults
	# Clean input/output
	exec >${CONSOLE} <${CONSOLE} 2>&1
}
#//--------------------------------------------------------------------------------



#// Startup Tasks
#//--------------------------------------------------------------------------------

StartUp() {
	if [ ! -f "/tmp/.startup" ]; then
		#// Mount proc && sys
		mount proc	/proc		-t proc			# /proc
		mount sys	/sys		-t sysfs		# /sys
		mount mdev	/dev		-t tmpfs  -o size=800k	# /dev for mdev

		#// Let busybox build its applets
		/bin/busybox --install -s

		#// Create additional mount points
		mkdir -m 0755 /dev/pts
		mkdir		/dev/shm
		mkdir -p	/mnt/cdrom
		mkdir		/mnt/floppy
		mkdir		/mnt/gentoo
		mkdir		/tmp

		#// Mount remaining filesystems
		mount tmp	/tmp		-t tmpfs		# /tmp
		mount devpts	/dev/pts 	-t devpts -o gid=5,mode=0620	# /dev/pts
		mount shm	/dev/shm	-t tmpfs -o size=512k	# /dev/shm

		#// Create mtab
		ln -sf	/proc/mounts		/etc/mtab		# mtab (symlink -> /proc/mounts)

		#// Start mdev
		echo "/sbin/mdev" > /proc/sys/kernel/hotplug		# mdev handles hotplug events
		/sbin/mdev -s						# have mdev populate /dev

		#// Create standard (non-mdev) devices
		if [ ! -e /dev/md0 ]
		then
			makedevs	/dev/md		b 9 0 0 7
		fi

		if [ ! -e /dev/tty0 ]
		then
			makedevs 	/dev/tty	c 4 0 0 12
		fi

		# We probably don't need any of these anymore with mdev
#		makedevs	/dev/ptyp	c 2 0 0 9
#		makedevs	/dev/ttyp	c 3 0 0 9
#		makedevs	/dev/ttyq	c 3 16 0 9
#		makedevs	/dev/ttyS	c 4 64 0 3
#		mknod		/dev/console	c 5 1
#		mknod		/dev/kmsg	c 1 11
#		mknod		/dev/null	c 1 3
#		mknod		/dev/tty	c 5 0
#		mknod		/dev/urandom	c 1 9
#		ln -s		/dev/urandom	/dev/random
#		mknod		/dev/zero	c 1 5

		#// Create std* devices
		ln -snf /proc/self/fd /dev/fd
		ln -snf /proc/self/fd/0 /dev/stdin
		ln -snf /proc/self/fd/1 /dev/stdout
		ln -snf /proc/self/fd/2 /dev/stderr

		#// Make some misc directories
		mkdir	/var/log
		mkdir	/var/run

		#// Start a minimal logger
		klogd
		syslogd

		#// Hostname
		hostname netboot

		if [ -n "$(which dropbear 2>/dev/null)" ]
		then
			# Setup dropbear (sshd)
			echo
			mkdir /etc/dropbear
			echo ">>> Generating RSA hostkey ..."
			dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
			echo
			echo ">>> Generating DSS hostkey ..."
			dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
			echo
			dropbear
		fi

		#// Misc tasks
		chmod +x /bin/net-setup
		chmod +x /bin/ashlogin
	fi
}

#//--------------------------------------------------------------------------------



#// Informative Message (copied from Gentoo /sbin/functions.sh)
#//--------------------------------------------------------------------------------

#// show an informative message (with a newline)
einfo() {
	printf " * %b\n" "$*"
}

#//--------------------------------------------------------------------------------



#// Determine Mips Machine Type
#//--------------------------------------------------------------------------------

DetectMips() {
	MYARCH="MIPS"
	MACHINFO="$(cat /proc/cpuinfo | grep "system type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
	CPUINFO="$(cat /proc/cpuinfo | grep "cpu model" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
	case "${MACHINFO}" in
		"SGI Indy")			MACHTYPE="SGI Indy"		;;	# Indy R4x00/R5000
		"SGI Indigo2")
			case "${CPUINFO}" in
				R4*)		MACHTYPE="SGI Indigo2"		;;	# I2 R4x00
				R8*)		MACHTYPE="SGI Indigo2 Power"	;;	# I2 R8000
				R10*)		MACHTYPE="SGI Indigo2 Impact"	;;	# I2 R10000
			esac
			;;
		"SGI O2"|"SGI IP32")		MACHTYPE="SGI O2"		;;	# O2 R5K/RM5K2/RM7K/R10K/R12K
		"SGI Octane"|"SGI IP30")	MACHTYPE="SGI Octane"		;;	# Octane R10K/R12K/R14K
		"SGI Origin"|"SGI IP27")	MACHTYPE="SGI Origin"		;;	# Origin 200/2000 R10K/R12K
		"MIPS Cobalt"|*RaQ*|*Qube*)	MACHTYPE="Cobalt Microserver"	;;	# Cobalt Qube/RaQ (1/2)
		*)				MACHTYPE="Unknown MIPS"		;;	# ???
	esac
}

#//--------------------------------------------------------------------------------



#// Determine Sparc Machine Type
#//--------------------------------------------------------------------------------

DetectSparc() {
	MYARCH="SPARC"
	MACHINFO="$(cat /proc/cpuinfo | grep "type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"

	case "${MACHINFO}" in
		sun4u)			MACHTYPE="Sun UltraSparc"	;;		# Sparc64
		sun4c|sun4d|sun4m)	MACHTYPE="Sun Sparc32"		;;		# Sparc32
		*)			MACHTYPE="Unknown SPARC"	;;		# ???
	esac
}

#//--------------------------------------------------------------------------------



#// Determine Ppc Machine Type
#//--------------------------------------------------------------------------------

DetectPpc() {
	MACHINFO="$(cat /proc/cpuinfo | grep "machine" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"

	case "${ARCHINFO}" in
		ppc)
			MYARCH="PPC"
			case "${MACHINFO}" in
				PowerMac*)	MACHTYPE="Apple PowerMac"	;;	# PowerMac
				PowerBook*)	MACHTYPE="Apple PowerBook"	;;	# PowerBook
				"CHRP Pegasos")	MACHTYPE="Pegasos"		;;	# Pegasos
				CHRP*|PReP)	MACHTYPE="IBM PPC-Based"	;;	# IBM PPC
				Amiga)		MACHTYPE="Amiga"		;;	# Amiga
				*)		MACHTYPE="Unknown PPC"		;;	# ???
			esac
		;;

		ppc64)
			MYARCH="PPC64"
			case "${MACHINFO}" in
				PowerMac*)	MACHTYPE="Apple G5"		;;	# Apple G5
				CHRP*|PReP)	MACHTYPE="IBM PPC-Based"	;;	# IBM PPC
				*iSeries*)	MACHTYPE="iSeries (Old)"	;;	# Old iSeries
				*)		MACHTYPE="Unknown PPC64"	;;	# ???
			esac
		;;
	esac
}

#//--------------------------------------------------------------------------------

#// Determine hppa Machine Type
#//--------------------------------------------------------------------------------

DetectHppa() {
	case "${ARCHINFO}" in
		parisc)
			MYARCH="HPPA"
		;;
		parisc64)
			MYARCH="HPPA (64bit kernel)"
		;;
	esac

	MACHTYPE=$(grep 'model' /proc/cpuinfo | cut -d '/' -f 3 | head -n 1 | sed -e 's/ //g')
}

#//--------------------------------------------------------------------------------
#// Discover if the network is already running for us or not
#//--------------------------------------------------------------------------------

DetectNetwork() {
	if [ ! -f "/tmp/.startup" ]; then
		#// If this image is loaded via NFS Root, chances are the network is autoconfigured for us
		if [ ! -z "$(ifconfig | grep "eth0")" ]; then
			MYIP="$(ifconfig | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1 | head -n 1)"
			MYGW="$(route | grep default | cut -d" " -f10)"
		fi
	fi
}

#//--------------------------------------------------------------------------------



#// For those in the Church of the SubGenius...
#//--------------------------------------------------------------------------------

SubGenius() {
	BUILDDATE="Build Date: $(date -d ${BBDATE} +"%B %d, %Y")"
	for CMDLINE in $(cat /proc/cmdline); do
		if [ "${CMDLINE}" = "discord" ]; then
			BUILDDATE="$(ddate +'Built on %{%A, the %e day of %B%} in the YOLD %Y. %NCelebrate %H!' ${DISDATE})"
		fi
	done
}
#//--------------------------------------------------------------------------------



#// Basic Startup Stuff
#//--------------------------------------------------------------------------------

GenMotd() {
	cat <<-EOF >/etc/motd


	Gentoo Linux; http://www.gentoo.org/
	 Copyright 2001-${CPYYEAR} Gentoo Foundation; Distributed under the GPL

	 Gentoo/${MYARCH} Netboot for ${MACHTYPE} Systems
	 ${BUILDDATE}

EOF

	#// If this is the initial startup, then display some messages, otherwise just execute a shell for the user
	if [ ! -f "/tmp/.startup" ]; then
		if [ -z "${MYIP}" ]; then
			(
			einfo "To configure networking (eth0), do the following:"
			echo
			einfo "For Static IP:"
			einfo "/bin/net-setup <IP Address> <Gateway Address>"
			echo
			einfo "For Dynamic IP:"
			einfo "/bin/net-setup dhcp"
			echo
			) > /etc/motd2
		else
			(
			echo
			einfo "Network interface eth0 has been started:"
			einfo "  IP Address: ${MYIP}"
			einfo "  Gateway:    ${MYGW}"
			echo
			einfo "An sshd server is available on port 22.  Please set a root"
			einfo "password via \"passwd\" before using."
			echo
			) > /etc/motd2
		fi
	fi
}

#//--------------------------------------------------------------------------------



#// Display Motd
#//--------------------------------------------------------------------------------

DisplayMotd() {
	cat /etc/motd
	[ -f "/etc/motd2" ] && cat /etc/motd2
}

#//--------------------------------------------------------------------------------



#// Launch Shell
#//--------------------------------------------------------------------------------

LaunchShell() {
	#// Completed Startup
	touch /tmp/.startup

	SERIAL_TTY=$(sed -e 's/.*console=\(tty[a-zA-Z0-9]*\).*/\1/' /proc/cmdline)
	if [ "${SERIAL_TTY:0:3}" = "tty" -a "${SERIAL_TTY:0:4}" != "tty0" ]
	then
		# A serial tty has been specified
		getty -n -l /bin/ashlogin 38400 ${SERIAL_TTY}
	else
		for i in 2 3 4 5 6; do
			getty -n -l /bin/ashlogin 38400 tty${i} &
		done

		# If we can't autodetect a serial console and start a getty there, we'll just run
		# ash on /dev/console
		/bin/ash
	fi
}

#//--------------------------------------------------------------------------------



#// Main
#//--------------------------------------------------------------------------------

BasicSetup
StartUp

#// Detect Arch
ARCHINFO="$(uname -m)"
case "${ARCHINFO}" in
	mips*)		DetectMips	;;
	sparc*)		DetectSparc
		mount -t openpromfs openprom /proc/openprom
	;;
	ppc*)		DetectPpc	;;
	parisc*)	DetectHppa	;;
	*)			MACHTYPE=$ARCHINFO	;;
esac

DetectNetwork
SubGenius
GenMotd
DisplayMotd
LaunchShell

#//--------------------------------------------------------------------------------