OSDN Git Service

Merge remote-tracking branch 'x86/oreo-x86' into pie-x86
[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 system
109                 if [ -e system/system.img ]; then
110                         mount --move system /sfs
111                         mount -o loop,noatime /sfs/system.img system
112                 fi
113         elif [ -e /mnt/$SRC/system.img ]; then
114                 remount_rw
115                 mount -o loop,noatime /mnt/$SRC/system.img system
116         elif [ -s /mnt/$SRC/system/build.prop ]; then
117                 remount_rw
118                 mount --bind /mnt/$SRC/system system
119         elif [ -z "$SRC" -a -s /mnt/build.prop ]; then
120                 mount --bind /mnt system
121         else
122                 rm -rf *
123                 return 1
124         fi
125         mkdir -p mnt
126         echo " found at $1"
127         rm /sbin/mke2fs
128         hash -r
129 }
130
131 remount_rw()
132 {
133         # "foo" as mount source is given to workaround a Busybox bug with NFS
134         # - as it's ignored anyways it shouldn't harm for other filesystems.
135         mount -o remount,rw foo /mnt
136 }
137
138 debug_shell()
139 {
140         if [ -x system/bin/sh ]; then
141                 echo Running MirBSD Korn Shell...
142                 USER="($1)" system/bin/sh -l 2>&1
143         else
144                 echo Running busybox ash...
145                 sh 2>&1
146         fi
147 }
148
149 echo -n Detecting Android-x86...
150
151 [ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE`
152 [ -z "$RAMDISK" ] && RAMDISK=ramdisk.img || RAMDISK=${RAMDISK##/dev/}
153
154 for c in `cat /proc/cmdline`; do
155         case $c in
156                 iso-scan/filename=*)
157                         SRC=iso
158                         eval `echo $c | cut -b1-3,18-`
159                         ;;
160                 *)
161                         ;;
162         esac
163 done
164
165 mount -t tmpfs tmpfs /android
166 cd /android
167 while :; do
168         for device in ${ROOT:-/dev/[hmnsv][dmrv][0-9a-z]*}; do
169                 check_root $device && break 2
170                 mountpoint -q /mnt && umount /mnt
171         done
172         sleep 1
173         echo -n .
174 done
175
176 ln -s mnt/$SRC /src
177 ln -s android/system /
178 ln -s ../system/lib/firmware ../system/lib/modules /lib
179
180 if [ -n "$INSTALL" ]; then
181         zcat /src/install.img | ( cd /; cpio -iud > /dev/null )
182 fi
183
184 if [ -x system/bin/ln -a \( -n "$DEBUG" -o -n "$BUSYBOX" \) ]; then
185         mv -f /bin /lib .
186         sed -i 's|\( PATH.*\)|\1:/bin|' init.environ.rc
187         rm /sbin/modprobe
188         busybox mv /sbin/* sbin
189         rmdir /sbin
190         ln -s android/bin android/lib android/sbin /
191         hash -r
192 fi
193
194 # load scripts
195 for s in `ls /scripts/* /src/scripts/*`; do
196         test -e "$s" && source $s
197 done
198
199 # ensure keyboard driver is loaded
200 if [ -n "$INSTALL" -o -n "$DEBUG" ]; then
201         busybox modprobe -a atkbd hid-apple
202         auto_detect &
203 fi
204
205 if [ 0$DEBUG -gt 0 ]; then
206         echo -e "\nType 'exit' to continue booting...\n"
207         debug_shell debug-found
208 fi
209
210 # A target should provide its detect_hardware function.
211 # On success, return 0 with the following values set.
212 # return 1 if it wants to use auto_detect
213 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
214
215 [ -n "$INSTALL" ] && do_install
216
217 load_modules
218 mount_data
219 mount_sdcard
220 setup_tslib
221 setup_dpi
222 post_detect
223
224 if [ 0$DEBUG -gt 1 ]; then
225         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
226         echo -e "Type 'exit' to enter Android...\n"
227
228         debug_shell debug-late
229         SETUPWIZARD=${SETUPWIZARD:-0}
230 fi
231
232 [ "$SETUPWIZARD" = "0" ] && echo "ro.setupwizard.mode=DISABLED" >> default.prop
233
234 [ -n "$DEBUG" ] && SWITCH=${SWITCH:-chroot}
235
236 # We must disable mdev before switching to Android
237 # since it conflicts with Android's init
238 echo > /proc/sys/kernel/hotplug
239
240 export ANDROID_ROOT=/system
241
242 exec ${SWITCH:-switch_root} /android /init
243
244 # avoid kernel panic
245 while :; do
246         echo
247         echo '  Android-x86 console shell. Use only in emergencies.'
248         echo
249         debug_shell fatal-err
250 done