OSDN Git Service

init: set SRC=iso earlier
[android-x86/bootable-newinstaller.git] / initrd / init
1 #!/bin/busybox sh
2 #
3 # By Chih-Wei Huang <cwhuang@linux.org.tw>
4 # and Thorsten Glaser <tg@mirbsd.org>
5 #
6 # Last updated 2018/01/26
7 #
8 # License: GNU Public License
9 # We explicitely grant the right to use the scripts
10 # with Android-x86 project.
11 #
12
13 PATH=/sbin:/bin:/system/bin:/system/xbin; export PATH
14
15 # auto installation
16 [ -n "$AUTO_INSTALL" ] && INSTALL=1
17
18 # configure debugging output
19 if [ -n "$DEBUG" -o -n "$INSTALL" ]; then
20         LOG=/tmp/log
21         set -x
22 else
23         LOG=/dev/null
24         test -e "$LOG" || busybox mknod $LOG c 1 3
25 fi
26 exec 2>> $LOG
27
28 # early boot
29 if test x"$HAS_CTTY" != x"Yes"; then
30         # initialise /proc and /sys
31         busybox mount -t proc proc /proc
32         busybox mount -t sysfs sys /sys
33         # let busybox install all applets as symlinks
34         busybox --install -s
35         # spawn shells on tty 2 and 3 if debug or installer
36         if test -n "$DEBUG" || test -n "$INSTALL"; then
37                 # ensure they can open a controlling tty
38                 mknod /dev/tty c 5 0
39                 # create device nodes then spawn on them
40                 mknod /dev/tty2 c 4 2 && openvt
41                 mknod /dev/tty3 c 4 3 && openvt
42         fi
43         if test -z "$DEBUG" || test -n "$INSTALL"; then
44                 echo 0 0 0 0 > /proc/sys/kernel/printk
45         fi
46         # initialise /dev (first time)
47         mkdir -p /dev/block
48         echo /sbin/mdev > /proc/sys/kernel/hotplug
49         mdev -s
50         # re-run this script with a controlling tty
51         exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
52 fi
53
54 # now running under a controlling tty; debug output from stderr into log file
55 # boot up Android
56
57 error()
58 {
59         echo $*
60         return 1
61 }
62
63 try_mount()
64 {
65         RW=$1; shift
66         if [ "${ROOT#*:/}" != "$ROOT" ]; then
67                 # for NFS roots, use nolock to avoid dependency to portmapper
68                 mount -o $RW,noatime,nolock $@
69                 return $?
70         fi
71         case $(blkid $1) in
72                 *TYPE=*ntfs*)
73                         mount.ntfs-3g -o rw,force $@
74                         ;;
75                 *TYPE=*)
76                         mount -o $RW,noatime $@
77                         ;;
78                 *)
79                         return 1
80                         ;;
81         esac
82 }
83
84 check_root()
85 {
86         if [ "`dirname $1`" = "/dev" ]; then
87                 [ -e $1 ] || return 1
88                 blk=`basename $1`
89                 [ ! -e /dev/block/$blk ] && ln $1 /dev/block
90                 dev=/dev/block/$blk
91         else
92                 dev=$1
93         fi
94         try_mount ro $dev /mnt || return 1
95         if [ -n "$iso" -a -e /mnt/$iso ]; then
96                 mount --move /mnt /iso
97                 mkdir /mnt/iso
98                 mount -o loop /iso/$iso /mnt/iso
99         fi
100         if [ -e /mnt/$SRC/$RAMDISK ]; then
101                 zcat /mnt/$SRC/$RAMDISK | cpio -id > /dev/null
102         elif [ -b /dev/$RAMDISK ]; then
103                 zcat /dev/$RAMDISK | cpio -id > /dev/null
104         else
105                 return 1
106         fi
107         if [ -e /mnt/$SRC/system.sfs ]; then
108                 mount -o loop,noatime /mnt/$SRC/system.sfs /sfs
109                 mount -o loop,noatime /sfs/system.img system
110         elif [ -e /mnt/$SRC/system.img ]; then
111                 remount_rw
112                 mount -o loop,noatime /mnt/$SRC/system.img system
113         elif [ -d /mnt/$SRC/system ]; then
114                 remount_rw
115                 mount --bind /mnt/$SRC/system system
116         elif [ -z "$SRC" -a -e /mnt/build.prop ]; then
117                 mount --bind /mnt system
118         else
119                 rm -rf *
120                 return 1
121         fi
122         mkdir -p mnt
123         echo " found at $1"
124         rm /sbin/mke2fs
125         hash -r
126 }
127
128 remount_rw()
129 {
130         # "foo" as mount source is given to workaround a Busybox bug with NFS
131         # - as it's ignored anyways it shouldn't harm for other filesystems.
132         mount -o remount,rw foo /mnt
133 }
134
135 debug_shell()
136 {
137         if [ -x system/bin/sh ]; then
138                 echo Running MirBSD Korn Shell...
139                 USER="($1)" system/bin/sh -l 2>&1
140         else
141                 echo Running busybox ash...
142                 sh 2>&1
143         fi
144 }
145
146 echo -n Detecting Android-x86...
147
148 [ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE`
149 [ -z "$RAMDISK" ] && RAMDISK=ramdisk.img || RAMDISK=${RAMDISK##/dev/}
150
151 for c in `cat /proc/cmdline`; do
152         case $c in
153                 iso-scan/filename=*)
154                         SRC=iso
155                         eval `echo $c | cut -b1-3,18-`
156                         ;;
157                 *)
158                         ;;
159         esac
160 done
161
162 mount -t tmpfs tmpfs /android
163 cd /android
164 while :; do
165         for device in ${ROOT:-/dev/[hmnsv][dmrv][0-9a-z]*}; do
166                 check_root $device && break 2
167                 mountpoint -q /mnt && umount /mnt
168         done
169         sleep 1
170         echo -n .
171 done
172
173 ln -s mnt/$SRC /src
174 ln -s android/system /
175 ln -s ../system/lib/firmware ../system/lib/modules /lib
176
177 if [ -n "$INSTALL" ]; then
178         zcat /src/install.img | ( cd /; cpio -iud > /dev/null )
179 fi
180
181 if [ -x system/bin/ln -a \( -n "$DEBUG" -o -n "$BUSYBOX" \) ]; then
182         mv /bin /lib .
183         sed -i 's|\( PATH.*\)|\1:/bin|' init.environ.rc
184         rm /sbin/modprobe
185         busybox mv /sbin/* sbin
186         rmdir /sbin
187         ln -s android/bin android/lib android/sbin /
188         hash -r
189 fi
190
191 # load scripts
192 for s in `ls /scripts/* /src/scripts/*`; do
193         test -e "$s" && source $s
194 done
195
196 # ensure keyboard driver is loaded
197 if [ -n "$INSTALL" -o -n "$DEBUG" ]; then
198         busybox modprobe -a atkbd hid-apple
199         auto_detect &
200 fi
201
202 if [ 0$DEBUG -gt 0 ]; then
203         echo -e "\nType 'exit' to continue booting...\n"
204         debug_shell debug-found
205 fi
206
207 # A target should provide its detect_hardware function.
208 # On success, return 0 with the following values set.
209 # return 1 if it wants to use auto_detect
210 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
211
212 [ -n "$INSTALL" ] && do_install
213
214 load_modules
215 mount_data
216 mount_sdcard
217 setup_tslib
218 setup_dpi
219 post_detect
220
221 if [ 0$DEBUG -gt 1 ]; then
222         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
223         echo -e "Type 'exit' to enter Android...\n"
224
225         debug_shell debug-late
226         SETUPWIZARD=${SETUPWIZARD:-0}
227 fi
228
229 [ "$SETUPWIZARD" = "0" ] && echo "ro.setupwizard.mode=DISABLED" >> default.prop
230
231 [ -n "$DEBUG" ] && SWITCH=${SWITCH:-chroot}
232
233 # We must disable mdev before switching to Android
234 # since it conflicts with Android's init
235 echo > /proc/sys/kernel/hotplug
236
237 export ANDROID_ROOT=/system
238
239 exec ${SWITCH:-switch_root} /android /init
240
241 # avoid kernel panic
242 while :; do
243         echo
244         echo '  Android-x86 console shell. Use only in emergencies.'
245         echo
246         debug_shell fatal-err
247 done