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