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