]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/boot/bootrc
kernel: introduce devswap #¶ to serve /dev/swap and handle swapfile encryption
[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 k æ 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 # usualy 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 $*
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                 x=(/boot/factotum -n -sfactotum)
90                 if(~ $service cpu){
91                         x=($x -S)
92                         if(~ -k $ff)
93                                 x=($x -k)
94                 }
95                 if not
96                         x=($x -u)
97                 if(! ~ $#debugfactotum 0)
98                         x=($x -p)
99                 must $x
100         }
101
102         # config method
103         $mp(1) $ma
104
105         # load keys from secstore if $auth or $secstore is not empty
106         x=secstore
107         if(~ $#$x 0) x=auth
108         if(! ~ $#$x 0 && test -x /bin/auth/secstore && test -f /mnt/factotum/ctl){
109                 x=(auth/secstore -G factotum -s^$$x)
110                 if(~ $service cpu)
111                         $x -n >/mnt/factotum/ctl
112                 if(~ $status *readnvram* || ! ~ $service cpu)
113                         $x >/mnt/factotum/ctl
114         }
115
116         # connect method
117         $mp(2) $ma
118
119         # insert cfs in the pipeline
120         if(test -x /bin/cfs){
121                 if(~ $#bootdisk 1 && ~ $#cfs 0)
122                         cfs=$bootdisk/cache
123                 if(~ $#cfs 1 && ! ~ $cfs off && test -f $cfs){
124                         x=(/bin/cfs -s -f $cfs)
125                         if(~ -f $ff)
126                                 x=($x -r)
127                         {$x </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 /srv/boot $rootdir
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                 if(~ -m $ff)
149                         init=($init -m)
150         }
151
152         # remove enviroment variables
153         rm -f '#e/'^$mt '#e/'? '#e/'?? '#e/fn#'* 
154
155         # remove part of our temporary root
156         /mnt/broot/$cputype/bin/unmount /$cputype/bin /bin
157         /mnt/broot/$cputype/bin/unmount /rc/bin /bin
158         /mnt/broot/$cputype/bin/unmount /
159
160         # create the name space, mount the root fs
161         /mnt/broot/$cputype/bin/bind / /
162         /mnt/broot/$cputype/bin/mount -ac '#s/boot' /
163
164         # remove the remaining temporary root
165         /mnt/broot/$cputype/bin/unmount /mnt/broot
166
167         exec $init
168 }
169
170 # keyboard and serial console
171 if(test -x /bin/aux/kbdfs){
172         a=$console(1)
173         if(! ~ $#a 0)
174                 a=/dev/eia^$a
175         aux/kbdfs -q -s cons $a
176 }
177
178 # usb devices
179 if(test -x /bin/nusbrc && ! test -e /env/nousbrc)
180         nusbrc
181
182 # load boot methods
183 fn showlocaldevs {}
184 fn configlocal {}
185 for(i in /rc/lib/*.rc){
186         . $i
187 }
188
189 # add partitions and binds
190 configlocal
191
192 while(){
193         @{main}
194
195         # subshell doesnt wait on interrupts
196         while(~ $status interrupted){wait}
197
198         # cleanup so it can be restarted
199         nobootprompt=()
200         rm -f /srv/^(cfs boot cs dns)
201 } </dev/cons