blob: 4c2a98a96fa08e5450609edc6286e1905bd502c2 [file] [log] [blame]
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06001#!/bin/sh
2
Milton Miller54d882e2016-02-05 12:07:53 -06003fslist="proc sys dev run"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06004rodir=run/initramfs/ro
5rwdir=run/initramfs/rw
6upper=$rwdir/cow
7work=$rwdir/work
8
9cd /
Milton Miller54d882e2016-02-05 12:07:53 -060010mkdir -p $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060011mount dev dev -tdevtmpfs
12mount sys sys -tsysfs
13mount proc proc -tproc
14if ! grep run proc/mounts
15then
16 mount tmpfs run -t tmpfs -o mode=755,nodev
17fi
18
19mkdir -p $rodir $rwdir
20
21cp -rp init shutdown update whitelist bin sbin usr lib etc var run/initramfs
22
23# To start a interactive shell with job control at this point, run
24# getty 38400 ttyS4
25
26findmtd() {
27 m=$(grep -xl "$1" /sys/class/mtd/*/name)
28 m=${m%/name}
29 m=${m##*/}
30 echo $m
31}
32
Milton Miller06ccb1a2016-02-05 13:04:29 -060033debug_takeover() {
34 echo "$@"
35 test -n "$@" && echo Enter password to try to manually fix.
36 cat << HERE
37After fixing run exit to continue this script, or reboot -f to retry, or
38touch /takeover and exit to become PID 1 allowing editing of this script.
39HERE
40
41 while ! sulogin && ! test -f /takeover
42 do
43 echo getty failed, retrying
44 done
45
46 # Touch /takeover in the above getty to become pid 1
47 if test -e /takeover
48 then
49 cat << HERE
50
51Takeover of init requested. Executing /bin/sh as PID 1.
52When finished exec new init or cleanup and run reboot -f.
53
54Warning: No job control! Shell exit will panic the system!
55HERE
56 export PS1=init#\
57 exec /bin/sh
58 fi
59}
60
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060061env=$(findmtd u-boot-env)
62if test -n $env
63then
64 ln -s /dev/$env /run/mtd:u-boot-env
65 cp /run/mtd:u-boot-env /run/fw_env
66fi
67
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060068rofs=$(findmtd rofs)
69rwfs=$(findmtd rwfs)
70
Milton Miller54d882e2016-02-05 12:07:53 -060071rodev=/dev/mtdblock${rofs#mtd}
72rwdev=/dev/mtdblock${rwfs#mtd}
73
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060074rofst=squashfs
75rwfst=ext4
Milton Miller54d882e2016-02-05 12:07:53 -060076roopts=ro
77rwopts=rw
78
79init=/sbin/init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060080
81echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
82
Milton Miller06ccb1a2016-02-05 13:04:29 -060083if grep -w debug-init-sh /proc/cmdline
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060084then
Milton Miller06ccb1a2016-02-05 13:04:29 -060085 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060086fi
87
Milton Miller06ccb1a2016-02-05 13:04:29 -060088if ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060089then
Milton Miller06ccb1a2016-02-05 13:04:29 -060090 msg="$(cat)" << HERE
91
92Mounting read-write $rwdev filesystem failed. Please fix and run
93 mount $rwdev $rwdir -t $rwfs -o $rwopts
94to to continue, or do change nothing to run from RAM for this boot.
95HERE
96 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060097fi
98
Milton Miller54d882e2016-02-05 12:07:53 -060099mount $rodev $rodir -t $rofst -o $roopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600100
101rm -rf $work
102mkdir -p $upper
103mkdir -p $work
104
105mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
106
Milton Miller06ccb1a2016-02-05 13:04:29 -0600107while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
108do
109 msg="$(cat)" << HERE
110
111Unable to confirm /sbin/init is an executable non-empty file
112in merged file system mounted at /root.
113
114Change Root test failed! Invoking emergency shell.
115HERE
116 debug_takeover "$msg"
117done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600118
Milton Miller54d882e2016-02-05 12:07:53 -0600119for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600120do
121 mount --move $f root/$f
122done
123
Milton Miller54d882e2016-02-05 12:07:53 -0600124# switch_root /root $init
125exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600126