tizen: change mount option of root partion from RW to RO
[sdk/emulator/emulator-kernel.git] / ramfs / init
1 #!/bin/busybox sh
2 # SeokYeon Hwang (syeon.hwang@samsung.com)
3
4 cmdline() {
5     local value
6     value=" $(/bin/busybox cat /proc/cmdline) "
7     value="${value##* $1=}"
8     value="${value%% *}"
9     [ "$value" != "" ] && echo "$value"
10 }
11
12 rescue_shell() {
13     echo -e "${COLOR_RED}Error !!! Dropping you to a shell.${NO_COLOR}"
14     /bin/busybox --install -s /bin
15     echo -e "${COLOR_BLUE}Opennng a shell on '/dev/hvc0'.${NO_COLOR}"
16     sh </dev/hvc0 >/dev/hvc0 2>&1 &
17
18     if [ -z $CONSOLE ]; then
19         CONSOLE="console"
20     fi
21
22     echo -e "${COLOR_BLUE}Opening a shell on '/dev/${CONSOLE}'.${NO_COLOR}"
23     export CONSOLE
24     exec setsid sh -c 'exec sh < /dev/${CONSOLE} >/dev/${CONSOLE} 2>&1'
25 }
26
27 mount_fs() {
28     /bin/busybox mount -t $FSTYPE -o rw $1 $2 || rescue_shell
29 }
30
31 /bin/busybox mkdir -p /dev
32 /bin/busybox mount -t devtmpfs devtmpfs /dev
33
34 exec >/dev/console 2>&1
35
36 COLOR_BLUE="\033[1;34m" # light blue
37 COLOR_RED="\033[1;31m" # light red
38 NO_COLOR="\033[0m"
39
40 # for debugging...
41 #/bin/busybox ls -la /dev >> /dev/console
42
43 echo -e "${COLOR_BLUE}Preparing...${NO_COLOR}"
44 /bin/busybox mkdir -p /proc
45 /bin/busybox mount -t proc proc /proc
46 /bin/busybox mkdir -p /sys
47 /bin/busybox mount -t sysfs sys /sys
48
49 NEW_ROOT="/new_root"
50 INIT=$(cmdline init)
51 CONSOLE=$(cmdline console)
52 ROOT=$(cmdline root)
53 FSTYPE=$(cmdline fstype)
54
55 # set default fs type
56 if [ -z $FSTYPE]; then
57     FSTYPE=ext4
58 fi
59
60 # mount image...
61 echo -e "${COLOR_BLUE}Mount image...${NO_COLOR}"
62 /bin/busybox mkdir -p $NEW_ROOT
63 if [ -z $ROOT ]; then
64     # mount rootfs...
65     # find rootfs...
66     ROOT=$(/bin/busybox findfs LABEL=emulator-rootfs)
67     if [ -z $ROOT ]; then
68         # for legacy image...
69         ROOT=$(/bin/busybox findfs LABEL=platform)
70     fi
71     if [ -z $ROOT ]; then
72         # failsafe...
73         ROOT=/dev/vda
74     fi
75
76     mount_fs $ROOT $NEW_ROOT
77
78     # mount system data area...
79     SYSDATA=$(/bin/busybox findfs LABEL=emulator-sysdata)
80     if [ ! -z $SYSDATA ]; then
81         mount_fs $SYSDATA $NEW_ROOT/opt
82     fi
83     # mount user area...
84     USER=$(/bin/busybox findfs LABEL=emulator-user)
85     if [ ! -z $USER ]; then
86         mount_fs $USER $NEW_ROOT/opt/usr
87     fi
88 else
89     echo -e "${COLOR_BLUE}Mount ${ROOT}...${NO_COLOR}"
90     mount_fs $ROOT $NEW_ROOT
91 fi
92
93 # execute prerun scrpits...
94 if [ -r $NEW_ROOT/etc/emulator/prerun ]; then
95     echo -e "${COLOR_BLUE}Prerun...${NO_COLOR}"
96     . $NEW_ROOT/etc/emulator/prerun $NEW_ROOT
97 fi
98
99 # clean up...
100 /bin/busybox umount /proc
101 /bin/busybox umount /sys
102 #/bin/busybox umount /dev
103
104 echo -e "${COLOR_BLUE}Switching root...${NO_COLOR}"
105 if [ -z $INIT ]; then
106     INIT="/sbin/init"
107 fi
108
109 /bin/busybox mkdir -p $NEW_ROOT/dev
110 /bin/busybox mount -t devtmpfs devtmpfs $NEW_ROOT/dev
111 /bin/busybox mkdir -p $NEW_ROOT/sys
112 /bin/busybox mount -t sysfs sys $NEW_ROOT/sys
113
114 /bin/busybox mount -o remount,ro $NEW_ROOT
115
116 exec /bin/busybox switch_root -c /dev/console $NEW_ROOT $INIT