a23c5982a4d3697427ca89370a5bdd58c90cdc96
[platform/core/system/initrd-recovery.git] / src / initrd-recovery / init
1 #!/bin/bash
2
3 export PATH=/usr/bin:/bin:/usr/sbin:/sbin
4
5 INFORM_PATH=/mnt/inform
6 HAL_PATH=/mnt/hal
7
8 SYNC="/bin/sync"
9 MKDIR="/bin/mkdir"
10 MOUNT="/bin/mount"
11 UMOUNT="/bin/umount"
12 DIRNAME="/bin/dirname"
13 REBOOT="/sbin/reboot"
14 BLKID="/usr/sbin/blkid"
15 CP="/bin/cp"
16 BOW_RESTORE="/sbin/bow-restore"
17
18 #------------------------------------------------
19 #       mount_partitions
20 #------------------------------------------------
21 mount_partitions() {
22     "$MOUNT" -t proc none /proc
23     "$MOUNT" -t sysfs none /sys
24     "$MOUNT" -t smackfs smackfs /smack
25     "$MOUNT" -t tmpfs tmpfs /run -o rw,nosuid,nodev,mode=755
26     "$MOUNT" -t tmpfs tmpfs /tmp -o mode=1777,smackfsroot=*
27
28     "$MKDIR" /dev/pts
29     "$MOUNT" -t devpts devpts /dev/pts
30 }
31
32 #------------------------------------------------
33 #       mount_inform
34 #------------------------------------------------
35 mount_inform() {
36     PART_INFORM=$("$BLKID" -L "inform" -o device)
37     if [ "z$PART_INFORM" != "z" ]; then
38         "$MKDIR" -p ${INFORM_PATH}
39         "$MOUNT" -t ext4 ${PART_INFORM} ${INFORM_PATH}
40     fi
41 }
42
43 #------------------------------------------------
44 #       mkdir_p_parent
45 #------------------------------------------------
46 mkdir_p_parent() {
47     dst_dir=`"$DIRNAME" "$1"`
48     if [ ! -d "$dst_dir" -a ! -L "$dst_dir" ]; then
49         "$MKDIR" -p "$dst_dir"
50     fi
51 }
52
53 #------------------------------------------------
54 #       get partition id
55 #------------------------------------------------
56 get_partition_id() {
57     P_SLOT=$([[ $(</proc/cmdline) =~ partition_ab=([ab]) ]]; echo ${BASH_REMATCH[1]})
58     P_SUFFIX=""
59
60     if [ "${P_SLOT}" != "" ]; then
61         P_SUFFIX="_${P_SLOT}"
62         echo "Using A/B slot: ${P_SLOT}"
63     fi
64
65     PART_ROOTFS=`/sbin/blkid -t PARTLABEL=rootfs${P_SUFFIX} -o device`
66     if [ x$PART_ROOTFS = "x" ]
67     then
68         PART_ROOTFS=`/sbin/blkid -L rootfs`
69     fi
70
71     PART_SYSTEM_DATA=`/sbin/blkid -t PARTLABEL=system-data -o device`
72     if [ x$PART_SYSTEM_DATA = "x" ]
73     then
74         PART_SYSTEM_DATA=`/sbin/blkid -L system-data`
75     fi
76
77     PART_RAMDISK=`/sbin/blkid -t PARTLABEL=ramdisk${P_SUFFIX} -o device`
78     if [ x$PART_RAMDISK = "x" ]
79     then
80         PART_RAMDISK=`/sbin/blkid -L ramdisk`
81     fi
82
83     PART_HAL=`/sbin/blkid -t PARTLABEL=hal${P_SUFFIX} -o device`
84     if [ x$PART_HAL = "x" ]
85     then
86         PART_HAL=`/sbin/blkid -L hal`
87     fi
88
89     PART_USER=$("$BLKID" --match-token PARTLABEL=user -o device || "$BLKID" --match-token LABEL=user -o device)
90 }
91
92 #------------------------------------------------
93 #       copy_hal_data
94 #------------------------------------------------
95 copy_hal_data() {
96     if [ ! -e "/hal/.hal_list" ]; then
97         return
98     fi
99
100     need_copy=0
101     while read file
102     do
103         dst="/hal/${file}"
104
105         if [ ! -e $dst ]; then
106             need_copy=1
107             break
108         fi
109     done < /hal/.hal_list
110
111     PART_HAL=$("$BLKID" -t PARTLABEL="hal${P_SUFFIX}" -o device)
112     if [ "$need_copy" = "1" -a "z$PART_HAL" != "z" ]; then
113         #mount hal partition
114         "$MKDIR" -p ${HAL_PATH}
115         "$MOUNT" ${PART_HAL} ${HAL_PATH} -o ro
116
117         #Load list and copy
118         while read file
119         do
120             src="${HAL_PATH}/$file"
121             dst="/hal/${file}"
122
123             mkdir_p_parent $dst
124
125             "$CP" -f "$src" "$dst"
126
127         done < /hal/.hal_list
128
129         #Done
130         "$UMOUNT" ${HAL_PATH}
131     fi
132 }
133
134 #------------------------------------------------
135 #       do_reboot
136 #------------------------------------------------
137 do_reboot() {
138     echo "Reboot"
139     "$SYNC"
140     "$REBOOT"
141     while [ 1 ]
142     do
143         sleep 1
144         echo "."
145     done
146 }
147
148 #------------------------------------------------
149 #       restore_checkpoint
150 #------------------------------------------------
151 restore_checkpoint() {
152     # Any existing checkpoint means that the RW update was unexpectedly
153     # aborded, so we must roll back the changes and update again.
154     echo "Checkpoint restore"
155     PART="${1}"
156     FS_TYPE="$(blkid -o value -s TYPE "${PART}")"
157     if [ "$FS_TYPE" = "ext4" ]; then
158         "$BOW_RESTORE" "${PART}"
159     else
160         echo "Checkpoint restore error: unknown filesystem ${FS_TYPE} on ${PART}"
161     fi
162 }
163
164 #------------------------------------------------
165 #       restore_partitions
166 #------------------------------------------------
167 restore_partitions() {
168     echo "Restore partitions"
169     . /usr/libexec/upgrade-support/upgrade-common.inc
170     if [[ "${P_SLOT}" == "" ]]
171     then
172         # We have only one slot, so it is likely that rootfs also has
173         # a checkpoint
174         restore_checkpoint "${PART_ROOTFS}"
175         if [ ! "z${PART_HAL}" = "z" ]; then
176             restore_checkpoint "${PART_HAL}"
177         fi
178     fi
179     if [ ! "z${PART_USER}" = "z" ]; then
180         restore_checkpoint "${PART_USER}"
181     fi
182     if [ ! "z${PART_SYSTEM_DATA}" = "z" ]; then
183         restore_checkpoint "${PART_SYSTEM_DATA}"
184     fi
185 }
186
187 #------------------------------------------------
188 #       Main Routine Start
189 #------------------------------------------------
190 echo "You entered into /sbin/init on initrd"
191
192 mount_partitions
193 mount_inform
194 get_partition_id
195 restore_partitions
196 copy_hal_data
197
198 cd /
199
200 # Manually parse /proc/cmdline to avoid additional tools on image
201 read cmdline </proc/cmdline
202 echo "Kernel command line: $cmdline"
203 set -- $cmdline
204 while [ $# -gt 0 ]; do
205     key="${1%%=*}"
206     if [ "$key" = "bootmode" ]; then
207         BOOT_MODE="${1#*=}"
208         break;
209     fi
210     shift
211 done
212
213 if [ "z$BOOT_MODE" = "z" ]; then
214     echo "BOOT_MODE was NOT defined!!"
215     echo "Do reboot!!"
216     do_reboot
217 fi
218 echo "BOOTMODE is ${BOOT_MODE}"
219
220 if [ -f /sbin/${BOOT_MODE}-init ]; then
221     exec "/sbin/${BOOT_MODE}-init"
222 else
223     echo "no ${BOOT_MODE}-init!!"
224     echo "Do reboot!!"
225     do_reboot
226 fi