]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/boot/bootrc
serial console stuff
[plan9front.git] / sys / src / 9 / boot / bootrc
1 #!/bin/rc
2
3 mntgen -s slash /mnt
4 bind /root /mnt/broot
5 unmount /root
6
7 bind -q '#p' /proc
8 for(i in S f k æ t b m)
9         bind -qa '#'^$i /dev
10
11 # bind in an ip interface
12 for(i in I l^(0 1 2 3))
13         bind -qa '#'$i /net
14
15 # reparse #ec variables, move to #e
16 for(i in init service sysname user nvram rootdir cfs bootdisk bootargs \
17         nobootprompt debugfactotum fs fsaddr auth authaddr console){
18         a=`{echo $$i}
19         $i=()
20         rm -f '#e'/$i '#ec'/$i
21         if(! ~ $#a 0)
22                 $i=$a
23 }
24
25 fn fatal {
26         echo $*
27         exit $"*
28 }
29
30 fn must {
31         $* || fatal $"*^': '^$status
32 }
33
34 fn ask {
35         echo -n $1
36         echo -n $2
37         if(! ~ $#3 0){
38                 echo -n ' ['
39                 echo -n $3
40                 echo -n '] '
41         }
42         $1=`{dd -bs 64 -count 1 >[2]/dev/null}
43         if(~ $#$1 0)
44                 $1=$3
45         if(~ $"$1 '!rc'){
46                 rc -i
47                 $1=()
48         }
49         if(~ $#$1 0)
50                 ask $*
51 }
52
53 mt=()
54 . /rc/lib/tcp.rc
55 . /rc/lib/local.rc
56
57 fn main{
58         mp=()
59         while(~ $#mp 0){
60                 if(~ $#nobootprompt 0){
61                         echo
62                         showlocaldevs
63                         ask bootargs ' is (tcp, local!device)' $"bootargs
64                 }
65                 if not bootargs=$nobootprompt
66                 nobootprompt=()
67                 mn=`{echo $bootargs | awk -F! '{print $1}'}
68                 ma=`{echo $bootargs | sed 's/[^!]*!?//'}
69                 switch(m$"mn){
70                 case $mt
71                         mp=m$mn
72                         mp=$$mp
73                 }
74         }
75
76         # config method
77         $mp(1) $ma
78
79         # authentication agent
80         if(! test -f /srv/factotum){
81                 x=(/boot/factotum -sfactotum)
82                 if(~ $service cpu){
83                         x=($x -S)
84                         if(~ -k $ff)
85                                 x=($x -k)
86                 }
87                 if not
88                         x=($x -u)
89                 if(! ~ $#authaddr 0)
90                         x=($x -a $authaddr)
91                 if(! ~ $#debugfactotum 0)
92                         x=($x -p)
93                 must $x
94         }
95         if not user=`{cat /dev/hostowner}
96
97         # connect method
98         $mp(2) $ma
99
100         # insert cfs in the pipeline
101         if(test -x /bin/cfs){
102                 if(~ $#bootdisk 1 && ~ $#cfs 0)
103                         cfs=$bootdisk/cache
104                 if(~ $#cfs 1 && ! ~ $cfs off && test -f $cfs){
105                         x=(/bin/cfs -s -f $cfs)
106                         if(~ -f $ff)
107                                 x=($x -r)
108                         {$x </srv/boot &} | echo 0 >/srv/cfs
109                         rm /srv/boot
110                         mv /srv/cfs /srv/boot
111                 }
112         }
113
114         # mount and change root in new enviroment and namespace
115         rfork ne
116
117         # mount root filesystem
118         if(~ $#rootdir 0)
119                 rootdir=/root
120         must mount -c /srv/boot $rootdir
121
122         # compile init command
123         if(~ $#init 0){
124                 init=/$cputype/init
125                 if(~ $service cpu)
126                         init=($init -c)
127                 if not
128                         init=($init -t)
129                 if(~ -m $ff)
130                         init=($init -m)
131         }
132
133         # remove enviroment variables
134         rm -f /env/^$mt /env/? /env/?? '/env/fn#'* 
135
136         # remove part of our temporary root
137         /mnt/broot/$cputype/bin/unmount /$cputype/bin /bin
138         /mnt/broot/$cputype/bin/unmount /rc/bin /bin
139         /mnt/broot/$cputype/bin/unmount /
140
141         # create the name space, mount the root fs
142         /mnt/broot/$cputype/bin/bind / /
143         /mnt/broot/$cputype/bin/mount -ac '#s/boot' /
144
145         # remove the remaining temporary root
146         /mnt/broot/$cputype/bin/unmount /mnt/broot
147
148         exec $init
149 }
150
151 # keyboard and serial console
152 if(test -x /bin/aux/kbdfs){
153         a=$console(1)
154         if(! ~ $#a 0)
155                 a=/dev/eia^$a
156         aux/kbdfs -s cons $a
157 }
158
159 # usb devices
160 nusbrc
161
162 # add partitions and binds
163 configlocal
164
165 # boot(8) command line arguments
166 ff=()
167 aa=()
168 while(! ~ $#* 0){
169         if(~ $1 -*){
170                 if(~ $1 -u*){
171                         if(~ $1 -u){
172                                 user=$2
173                                 shift
174                         }
175                         if not {
176                                 user=`{echo $1 | sed 's,^-u,,g'}
177                         }
178                 }
179                 if not {
180                         if(~ $1 -*f*)
181                                 ff=($ff -f)
182                         if(~ $1 -*k*)
183                                 ff=($ff -k)
184                         if(~ $1 -*m*)
185                                 ff=($ff -m)
186                 }
187                 shift
188         }
189         if not {
190                 while(! ~ $#* 0){
191                         aa=($aa $1)
192                         shift
193                 }
194         }
195 }
196 if(! ~ $#aa 0 && ~ $#bootargs 0 && ~ $#nobootprompt 0){
197         bootargs=$aa
198         nobootprompt=$aa
199 }
200
201 while(){
202         @{main}
203
204         # cleanup so it can be restarted
205         nobootprompt=()
206         user=()
207         rm -f /srv/^(cfs boot slashn cs dns)
208 } </dev/cons >/dev/cons >[2]/dev/cons