Prepare, configure, and manage Firecracker microVMs in seconds!
virtualization linux microvm firecracker
at main 62 lines 2.1 kB view raw
1#!/bin/sh 2# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"). You may 5# not use this file except in compliance with the License. A copy of the 6# License is located at 7# 8# http://aws.amazon.com/apache2.0/ 9# 10# or in the "license" file accompanying this file. This file is distributed 11# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12# express or implied. See the License for the specific language governing 13# permissions and limitations under the License. 14 15# Parameters: 16# 1. rw_root -- path where the read/write root is mounted 17# 2. work_dir -- path to the overlay workdir (must be on same filesystem as rw_root) 18# Overlay will be set up on /mnt, original root on /mnt/rom 19pivot() { 20 local rw_root work_dir 21 rw_root="$1" 22 work_dir="$2" 23 /bin/mount \ 24 -o noatime,lowerdir=/,upperdir=${rw_root},workdir=${work_dir} \ 25 -t overlay "overlayfs:${rw_root}" /mnt 26 pivot_root /mnt /mnt/rom 27} 28 29# Overlay is configured under /overlay 30# Global variable $overlay_root is expected to be set to either: 31# "ram", which configures a tmpfs as the rw overlay layer (this is 32# the default, if the variable is unset) 33# - or - 34# A block device name, relative to /dev, in which case it is assumed 35# to contain an ext4 filesystem suitable for use as a rw overlay 36# layer. e.g. "vdb" 37do_overlay() { 38 local overlay_dir="/overlay" 39 if [ "$overlay_root" = ram ] || 40 [ -z "$overlay_root" ]; then 41 /bin/mount -t tmpfs -o noatime,mode=0755 tmpfs /overlay 42 else 43 /bin/mount -t ext4 "/dev/$overlay_root" /overlay 44 fi 45 mkdir -p /overlay/root /overlay/work 46 pivot /overlay/root /overlay/work 47} 48 49# If we're given an overlay, ensure that it really exists. Panic if not. 50if [ -n "$overlay_root" ] && 51 [ "$overlay_root" != ram ] && 52 [ ! -b "/dev/$overlay_root" ]; then 53 echo -n "FATAL: " 54 echo "Overlay root given as $overlay_root but /dev/$overlay_root does not exist" 55 exit 1 56fi 57 58do_overlay 59 60# invoke the actual system init program and proceed with the boot 61# process. 62exec /sbin/init $@