OSDN Git Service

init: use mdev as the hotplug tool
[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 2011/02/25
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         else
40                 echo 0 0 0 0 > /proc/sys/kernel/printk
41         fi
42         # initialise /dev (first time)
43         echo /sbin/mdev > /proc/sys/kernel/hotplug
44         mdev -s
45         # re-run this script with a controlling tty
46         exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
47 fi
48
49 # now running under a controlling tty; debug output from stderr into log file
50 # boot up Android
51
52 try_mount()
53 {
54         RW=$1; shift
55         # FIXME: any way to mount ntfs gracefully?
56         mount -o $RW $@ || mount.ntfs-3g -o rw,force $@
57 }
58
59 remount_rw()
60 {
61         mount -o remount,rw /mnt
62 }
63
64 debug_shell()
65 {
66         if which mksh >/dev/null 2>&1; then
67                 echo Running MirBSD Korn Shell...
68                 USER="($1)" mksh -l 2>&1
69         else
70                 echo Running busybox ash...
71                 sh 2>&1
72         fi
73 }
74
75 echo -n Detecting Android-x86...
76
77 while :; do
78         for device in /dev/sr* /dev/sd[a-z]*; do
79                 try_mount ro $device /mnt || continue
80                 cd /mnt/$SRC
81                 if [ ! -e ramdisk.img -o ! \( -e system.sfs -o -e system.img -o -d system \) ]; then
82                         cd /
83                         umount /mnt
84                         continue
85                 fi
86                 mount -t tmpfs tmpfs /android
87                 cd /android
88                 zcat /mnt/$SRC/ramdisk.img | cpio -id > /dev/null
89                 if [ -e /mnt/$SRC/system.sfs ]; then
90                         mount -o loop /mnt/$SRC/system.sfs /sfs
91                         mount -o loop /sfs/system.img system
92                 elif [ -e /mnt/$SRC/system.img ]; then
93                         mount -o loop /mnt/$SRC/system.img system
94                 else
95                         remount_rw
96                         mount --bind /mnt/$SRC/system system
97                 fi
98                 mkdir cache mnt mnt/sdcard
99                 mount -t tmpfs tmpfs cache
100                 echo " found at $device"
101                 break
102         done
103         mountpoint -q /android && break
104         sleep 1
105         echo -n .
106 done
107
108 ln -s mnt/$SRC /src
109 ln -s android/system /
110 ln -s ../system/lib/modules /lib
111 ln -s ../system/lib/firmware /lib
112
113 if [ -n "$INSTALL" ]; then
114         cd /
115         zcat /src/install.img | cpio -iud > /dev/null
116 fi
117
118 if [ -n "$DEBUG" -o -n "$BUSYBOX" ]; then
119         mv /bin /lib .
120         system/bin/ln -s android/lib /lib
121         system/bin/ln -s android/bin /bin
122         sed -i 's|\(PATH *\)\(/sbin\)|\1/bin:\2|' init.rc
123         mv /sbin/* sbin
124         rmdir /sbin
125         ln -s android/sbin /
126 fi
127
128 if [ -n "$DEBUG" ]; then
129         echo -e "\nType 'exit' to continue booting...\n"
130         debug_shell debug-found
131 fi
132
133 # load scripts
134 for s in `ls /scripts/* /src/scripts/*`; do
135         test -e "$s" && source $s
136 done
137
138 # A target should provide its detect_hardware function.
139 # On success, return 0 with the following values set.
140 #
141 # FB0DEV: framebuffer driver
142 # LANDEV: lan driver
143 # WIFDEV: wifi driver
144 # SNDDEV: sound driver
145 # CAMDEV: camera driver
146 # PREDEV: any module the drivers depend on but can't be loaded automatically
147 # EXTMOD: any other module
148
149 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
150
151 [ -n "$INSTALL" ] && do_install
152
153 load_modules
154 mount_data
155 mount_sdcard
156 setup_tslib
157 post_detect
158
159 if [ -n "$DEBUG" ]; then
160         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
161         echo -e "Type 'exit' to enter Android...\n"
162
163         debug_shell debug-late
164         SWITCH=chroot
165 fi
166
167 # We must disable mdev before switching to Android
168 # since it conflicts with Android's init
169 echo > /proc/sys/kernel/hotplug
170
171 exec ${SWITCH:-switch_root} /android /init
172
173 # avoid kernel panic
174 while :; do
175         echo
176         echo '  Android-x86 console shell. Use only in emergencies.'
177         echo
178         debug_shell fatal-err
179 done