···11+#!/bin/sh
22+# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33+#
44+# Licensed under the Apache License, Version 2.0 (the "License"). You may
55+# not use this file except in compliance with the License. A copy of the
66+# License is located at
77+#
88+# http://aws.amazon.com/apache2.0/
99+#
1010+# or in the "license" file accompanying this file. This file is distributed
1111+# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212+# express or implied. See the License for the specific language governing
1313+# permissions and limitations under the License.
1414+1515+# Parameters:
1616+# 1. rw_root -- path where the read/write root is mounted
1717+# 2. work_dir -- path to the overlay workdir (must be on same filesystem as rw_root)
1818+# Overlay will be set up on /mnt, original root on /mnt/rom
1919+pivot() {
2020+ local rw_root work_dir
2121+ rw_root="$1"
2222+ work_dir="$2"
2323+ /bin/mount \
2424+ -o noatime,lowerdir=/,upperdir=${rw_root},workdir=${work_dir} \
2525+ -t overlay "overlayfs:${rw_root}" /mnt
2626+ pivot_root /mnt /mnt/rom
2727+}
2828+2929+# Overlay is configured under /overlay
3030+# Global variable $overlay_root is expected to be set to either:
3131+# "ram", which configures a tmpfs as the rw overlay layer (this is
3232+# the default, if the variable is unset)
3333+# - or -
3434+# A block device name, relative to /dev, in which case it is assumed
3535+# to contain an ext4 filesystem suitable for use as a rw overlay
3636+# layer. e.g. "vdb"
3737+do_overlay() {
3838+ local overlay_dir="/overlay"
3939+ if [ "$overlay_root" = ram ] ||
4040+ [ -z "$overlay_root" ]; then
4141+ /bin/mount -t tmpfs -o noatime,mode=0755 tmpfs /overlay
4242+ else
4343+ /bin/mount -t ext4 "/dev/$overlay_root" /overlay
4444+ fi
4545+ mkdir -p /overlay/root /overlay/work
4646+ pivot /overlay/root /overlay/work
4747+}
4848+4949+# If we're given an overlay, ensure that it really exists. Panic if not.
5050+if [ -n "$overlay_root" ] &&
5151+ [ "$overlay_root" != ram ] &&
5252+ [ ! -b "/dev/$overlay_root" ]; then
5353+ echo -n "FATAL: "
5454+ echo "Overlay root given as $overlay_root but /dev/$overlay_root does not exist"
5555+ exit 1
5656+fi
5757+5858+do_overlay
5959+6060+# invoke the actual system init program and proceed with the boot
6161+# process.
6262+exec /sbin/init $@