OSDN Git Service

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