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