OSDN Git Service

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