Adriana Kobylak | 86164b2 | 2020-06-03 16:22:42 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # Get the value of the root env variable found in /proc/cmdline |
| 4 | get_root() { |
| 5 | local root="$(cat /proc/cmdline)" |
| 6 | root="${root##* root=PARTLABEL=}" |
| 7 | root="${root%% *}" |
| 8 | [ "${root}" != "" ] && echo "${root}" |
| 9 | } |
| 10 | |
| 11 | fslist="proc sys dev run" |
| 12 | rodir=/mnt/rofs |
| 13 | cd / |
| 14 | mkdir -p $fslist |
| 15 | mount dev dev -tdevtmpfs |
| 16 | mount sys sys -tsysfs |
| 17 | mount proc proc -tproc |
| 18 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 19 | |
Adriana Kobylak | b7dccc1 | 2020-08-17 12:40:23 -0500 | [diff] [blame] | 20 | # Wait up to 5s for the mmc device to appear. Continue even if the count is |
| 21 | # exceeded. A failure will be caught later like in the mount command. |
| 22 | mmcdev="/dev/mmcblk0" |
| 23 | count=0 |
| 24 | while [ $count -lt 5 ]; do |
| 25 | if [ -e "${mmcdev}" ]; then |
| 26 | break |
| 27 | fi |
| 28 | sleep 1 |
| 29 | count=$((count + 1)) |
| 30 | done |
| 31 | |
Adriana Kobylak | 86164b2 | 2020-06-03 16:22:42 -0500 | [diff] [blame] | 32 | # Move the secondary GPT to the end of the device if needed. Look for the GPT |
| 33 | # header signature "EFI PART" located 512 bytes from the end of the device. |
Adriana Kobylak | b7dccc1 | 2020-08-17 12:40:23 -0500 | [diff] [blame] | 34 | magic=$(tail -c 512 "${mmcdev}" | hexdump -C -n 8 | grep "EFI PART") |
Adriana Kobylak | 86164b2 | 2020-06-03 16:22:42 -0500 | [diff] [blame] | 35 | if test -z "${magic}"; then |
Adriana Kobylak | b7dccc1 | 2020-08-17 12:40:23 -0500 | [diff] [blame] | 36 | sgdisk -e "${mmcdev}" |
Adriana Kobylak | 86164b2 | 2020-06-03 16:22:42 -0500 | [diff] [blame] | 37 | partprobe |
| 38 | fi |
| 39 | |
| 40 | # There eMMC GPT labels for the rootfs are rofs-a and rofs-b, and the label for |
| 41 | # the read-write partition is rwfs. Run udev to make the partition labels show |
| 42 | # up. Mounting by label allows for partition numbers to change if needed. |
| 43 | udevd --daemon |
| 44 | udevadm trigger --type=devices --action=add |
| 45 | udevadm settle --timeout=10 |
| 46 | |
| 47 | mkdir -p $rodir |
| 48 | if ! mount /dev/disk/by-partlabel/"$(get_root)" $rodir -t ext4 -o ro; then |
| 49 | /bin/sh |
| 50 | fi |
Adriana Kobylak | c3d6958 | 2020-08-24 15:20:04 -0500 | [diff] [blame] | 51 | |
| 52 | rwfsdev="/dev/disk/by-partlabel/rwfs" |
| 53 | fsck.ext4 -p "${rwfsdev}" |
| 54 | if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then |
Adriana Kobylak | 86164b2 | 2020-06-03 16:22:42 -0500 | [diff] [blame] | 55 | /bin/sh |
| 56 | fi |
| 57 | |
| 58 | rm -rf $rodir/var/persist/etc-work/ |
| 59 | mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root |
| 60 | mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work |
| 61 | |
| 62 | for f in $fslist; do |
| 63 | mount --move $f $rodir/$f |
| 64 | done |
| 65 | |
| 66 | exec chroot $rodir /sbin/init |