summaryrefslogtreecommitdiff
path: root/sys/src/9/boot/bootrc
blob: d3046727a330ef70f47df1375ccbbb6b045ee3d3 (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
#!/bin/rc

mntgen -s slash /mnt
bind /root /mnt/broot
unmount /root

bind -q '#d' /fd
bind -q '#p' /proc
for(i in S f k æ t b m)
	bind -qa '#'^$i /dev

# bind in an ip interface
for(i in I l^(0 1 2 3))
	bind -qa '#'$i /net

# usualy better than 1970
cat '#r/rtc' >/dev/time >[2]/dev/null

# reparse variables
for(i in `{ls -Qp /env}){
	switch($i){
	case '*'* 'fn#'* e820 apm0 apid ifs path pid prompt status ?
		# ignore these
	case *
		$i=`{echo $$i}
	}
}

fn sigint {
	status=interrupted
}

fn fatal {
	echo $*
	exit $"*
}

fn must {
	$* || fatal $"*^': '^$status
}

fn ask {
	echo -n $1
	echo -n $2
	if(! ~ $#3 0){
		echo -n '['
		echo -n $3
		echo -n '] '
	}
	$1=`{dd -bs 64 -count 1 >[2]/dev/null}
	if(~ $#$1 0)
		$1=$3
	if(~ $"$1 '!rc'){
		rc -i
		$1=()
	}
	if(~ $#$1 0)
		ask $*
}

mt=()

fn main{
	mp=()
	while(~ $#mp 0){
		if(~ $#nobootprompt 0){
			echo
			showlocaldevs
			ask bootargs ' is (tcp, il, local!device)' $"bootargs
		}
		if not bootargs=$nobootprompt
		nobootprompt=()
		mn=`{echo $bootargs | awk -F! '{print $1}'}
		ma=`{echo $bootargs | sed 's/[^!]*!?//'}
		switch(m$"mn){
		case $mt
			mp=m$mn
			mp=$$mp
		}
	}

	# authentication agent
	if(! test -f /srv/factotum){
		x=(/boot/factotum -n -sfactotum)
		if(~ $service cpu){
			x=($x -S)
			if(~ -k $ff)
				x=($x -k)
		}
		if not
			x=($x -u)
		if(! ~ $#debugfactotum 0)
			x=($x -p)
		must $x
	}

	# config method
	$mp(1) $ma

	# load keys from secstore if $auth or $secstore is not empty
	x=secstore
	if(~ $#$x 0) x=auth
	if(! ~ $#$x 0 && test -x /bin/auth/secstore && test -f /mnt/factotum/ctl){
		x=(auth/secstore -G factotum -s^$$x)
		if(~ $service cpu)
			$x -n >/mnt/factotum/ctl
		if(~ $status *readnvram* || ! ~ $service cpu)
			$x >/mnt/factotum/ctl
	}

	# connect method
	$mp(2) $ma

	# insert cfs in the pipeline
	if(test -x /bin/cfs){
		if(~ $#bootdisk 1 && ~ $#cfs 0)
			cfs=$bootdisk/cache
		if(~ $#cfs 1 && ! ~ $cfs off && test -f $cfs){
			x=(/bin/cfs -s -f $cfs)
			if(~ -f $ff)
				x=($x -r)
			{$x </srv/boot &} | echo 0 >/srv/cfs
			rm /srv/boot
			mv /srv/cfs /srv/boot
		}
	}

	# mount and change root in new enviroment and namespace
	rfork ne

	# mount root filesystem
	if(~ $#rootdir 0)
		rootdir=/root
	must mount -c /srv/boot $rootdir

	# compile init command
	if(~ $#init 0){
		init=/$cputype/init
		if(~ $service cpu)
			init=($init -c)
		if not
			init=($init -t)
		if(~ -m $ff)
			init=($init -m)
	}

	# remove enviroment variables
	rm -f /env/^$mt /env/? /env/?? '/env/fn#'* 

	# remove part of our temporary root
	/mnt/broot/$cputype/bin/unmount /$cputype/bin /bin
	/mnt/broot/$cputype/bin/unmount /rc/bin /bin
	/mnt/broot/$cputype/bin/unmount /

	# create the name space, mount the root fs
	/mnt/broot/$cputype/bin/bind / /
	/mnt/broot/$cputype/bin/mount -ac '#s/boot' /

	# remove the remaining temporary root
	/mnt/broot/$cputype/bin/unmount /mnt/broot

	exec $init
}

# keyboard and serial console
if(test -x /bin/aux/kbdfs){
	a=$console(1)
	if(! ~ $#a 0)
		a=/dev/eia^$a
	aux/kbdfs -q -s cons $a
}

# usb devices
if(test -x /bin/nusbrc && ! test -e /env/nousbrc)
	nusbrc

# wait for devices to settle down
if(~ $#usbwait 1)
	sleep $usbwait
if(~ $#usbwait 0)
	sleep 2

. /rc/lib/net.rc
. /rc/lib/local.rc

# add partitions and binds
configlocal

# boot(8) command line arguments
ff=()
aa=()
while(! ~ $#* 0){
	if(~ $1 -*){
		if(~ $1 -u*){
			if(~ $1 -u){
				user=$2
				shift
			}
			if not {
				user=`{echo $1 | sed 's,^-u,,g'}
			}
		}
		if not {
			if(~ $1 -*f*)
				ff=($ff -f)
			if(~ $1 -*k*)
				ff=($ff -k)
			if(~ $1 -*m*)
				ff=($ff -m)
		}
		shift
	}
	if not {
		while(! ~ $#* 0){
			aa=($aa $1)
			shift
		}
	}
}
if(! ~ $#aa 0 && ~ $#bootargs 0 && ~ $#nobootprompt 0){
	bootargs=$aa
	nobootprompt=$aa
}

while(){
	@{main}

	# subshell doesnt wait on interrupts
	while(~ $status interrupted){wait}

	# cleanup so it can be restarted
	nobootprompt=()
	rm -f /srv/^(cfs boot slashn cs dns)
} </dev/cons