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