Commit 1904d000 authored by Martin Roukala's avatar Martin Roukala
Browse files

initscript: add a way to control whether to use a caching drive or not

This will soon be improved by allowing to select the exact drive.
parent 0227259f
......@@ -24,7 +24,7 @@ rebuild_container:
test: out/initramfs.linux_amd64.cpio
[ -f "$(KERNEL)" ] || (echo "ERROR: Set the KERNEL parameter, pointing to linux kernel with modules compiled in"; exit 1)
[ -f out/disk.img ] || fallocate -l 128M out/disk.img
qemu-system-x86_64 -drive file=out/disk.img,format=raw,if=virtio -nic user,model=virtio-net-pci -kernel $(KERNEL) -initrd out/initramfs.linux_amd64.cpio -nographic -m 256M -enable-kvm -append "console=ttyS0 b2c.container=docker://registry.hub.docker.com/library/hello-world"
qemu-system-x86_64 -drive file=out/disk.img,format=raw,if=virtio -nic user,model=virtio-net-pci -kernel $(KERNEL) -initrd out/initramfs.linux_amd64.cpio -nographic -m 256M -enable-kvm -append "console=ttyS0 b2c.container=docker://registry.hub.docker.com/library/hello-world b2c.cache_device=auto"
clean:
-rm out/initramfs.linux_amd64.cpio
......
......@@ -47,6 +47,9 @@ that needs to be executed. In `grub.cfg`, this could look like this:
Here is a list of options:
* b2c.container: Name of the container that should be executed at boot. Format: See [podman's image format](https://docs.podman.io/en/latest/markdown/podman-run.1.html#image)
* b2c.cache_device: Use a cache to store the image layers, thus saving on network usage:
* none (default): Do not use any cache, guaranteeing a fresh environment every time
* auto: Re-use a previously-setup drive, or pick a suitable one, and partition it! (**WILL ERASE YOUR DATA**)
## How to?
......
......@@ -54,6 +54,9 @@ function parse_cmdline {
b2c.insmods=*)
ARG_MODULES=$value
;;
b2c.cache_device=*)
ARG_CACHE_DEVICE=$value
;;
b2c.container=*)
ARG_CONTAINER=$value
;;
......@@ -88,15 +91,12 @@ function find_container_partition {
fi
}
function format_first_disk {
sr_disks_majors=`grep ' sr' /proc/devices | sed "s/^[ \t]*//" | cut -d ' ' -f 1 | tr '\n' ',' | sed 's/,$//'`
function format_disk {
if [ -n "$1" ]; then
parted --script $1 mklabel gpt
parted --script $1 mkpart primary ext4 2048s 100%
disk=`lsblk -ndlfp -e "$sr_disks_majors" | head -n 1`
if [ -n "$disk" ]; then
parted --script $disk mklabel gpt
parted --script $disk mkpart primary ext4 2048s 100%
container_partition=`lsblk -no PATH $disk | tail -n -1`
container_partition=`lsblk -no PATH $1 | tail -n -1`
mkfs.ext4 -F -L B2C_CACHE "$container_partition"
return 0
......@@ -109,28 +109,50 @@ function find_or_create_container_partition {
# See if we have an existing block device that would work
container_partition=`find_container_partition` && return 0
# Find a suitable disk
sr_disks_majors=`grep ' sr' /proc/devices | sed "s/^[ \t]*//" | cut -d ' ' -f 1 | tr '\n' ',' | sed 's/,$//'`
disk=`lsblk -ndlfp -e "$sr_disks_majors" | head -n 1`
# Find a disk, partition it, then format it as ext4
format_first_disk && return 0
format_disk $disk && return 0
return 1
}
function mount_container_partition {
function mount_cache_partition {
mkdir "$CONTAINER_MOUNTPOINT"
# Find a suitable cache partition
container_partition=""
find_or_create_container_partition || return 1
mount "$container_partition" "$CONTAINER_MOUNTPOINT"
case $ARG_CACHE_DEVICE in
none)
log "Do not use a partition cache"
return 0
;;
auto)
find_or_create_container_partition || return 1
;;
*)
log "Invalid caching option '$ARG_CACHE_DEVICE'. Defaulting to 'none'"
return 0
;;
esac
log "Selected the partition $container_partition as a cache"
if mount "$container_partition" "$CONTAINER_MOUNTPOINT"; then
status_str="DONE"
status=0
else
status_str="FAILED"
status=1
fi
log "Mounting the partition $container_partition to $CONTAINER_MOUNTPOINT: $status_str"
log "Mounting the partition $container_partition to $CONTAINER_MOUNTPOINT: DONE"
return $status
}
function setup_container_runtime {
# Mount the cache partition
container_partition=""
mount_container_partition
# HACK: I could not find a way to change the right parameter in podman's
# config, so make a symlink for now
[ -d "$CONTAINER_CACHE" ] || mkdir "$CONTAINER_CACHE"
......@@ -157,18 +179,26 @@ setup_busybox
#setup_mounts # To be continued to so we could boot without any go commands
setup_env
# Parse the kernel command line, in search of the b2c parameters
ARG_MODULES=""
ARG_CONTAINER=""
ARG_CACHE_DEVICE="none"
parse_cmdline
# Now that the early boot is over, let's log every command executed
set -x
setup_container_runtime
# Start the container
# Load the user-requested modules
load_modules $ARG_MODULES
# Connect to the network, now that the modules are loaded
connect
# Mount the cache partition
mount_cache_partition
# Start the container
setup_container_runtime
start_container $ARG_CONTAINER
# Emergency shell
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment