Skip to content

Instantly share code, notes, and snippets.

@smoser
Created May 20, 2024 21:09
Show Gist options
  • Save smoser/cfadd142dd2b710befd4377facd05bc2 to your computer and use it in GitHub Desktop.
Save smoser/cfadd142dd2b710befd4377facd05bc2 to your computer and use it in GitHub Desktop.
boot a tarball or oci image ref

boot a tarball or oci imag ref

Get yourself a kernel

# kernel from vmlinuz on ubuntu
kdist=/boot/$(uname -r)
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
sudo cat "$kdist" > kernel

Now convert a oci or local tarball to kernel flavor cpio. this script adds a very basic 'init'

fakeroot ./create-initramfs my-initramfs base.tar

boot it

qemu-system-x86_64 -enable-kvm -m 2048 -nographic \
    -kernel ./kernel -initrd ./my-initramfs \
    -append "console=ttyS0 this is my cmdline"
#!/bin/sh
TEMP_D=""
Usage() {
cat <<EOF
${0##*/} out.cpio tar-or-ociref [init]
EOF
}
winit() {
cat <<"EOF"
#!/bin/sh
# vi: ts=4 expandtab
msg() {
local d="" written=false
for d in /dev/ttyS0 /dev/tty1; do
[ -c "$d" ] || continue
echo "$@" >"$d" 2>&1 && written=true;
done
[ "$written" = "true" ] && return 0
echo "$@"
}
panic() {
msg "FATAL:" "$@"
}
msg "==== MINI INITRAMFS INIT ===="
mkdir -p /proc /dev /tmp /sys
mount -t devtmpfs /dev /dev ||
panic "failed mount devtmpfs"
mount -t proc /proc /proc ||
panic "failed mount proc"
mount -t sysfs /sys /sys ||
panic "failed mount /sys"
echo "6 4 1 7" >/proc/sys/kernel/printk
read cmdline < /proc/cmdline ||
panic "failed to read cmdline"
msg "KERNEL COMMAND LINE:" "$cmdline"
echo o >/proc/sysrq-trigger
read || panic "Read returned $?"
panic "Read returned success; What to do now?"
exit 1
EOF
}
fail() { echo "$@" 1>&2; exit 1; }
cleanup() { [ "$TEMP_D" = "" ] || rm -Rf "$TEMP_D"; }
stderr() { echo "$@" 1>&2; }
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage ; exit 1; }
[ $# -gt 1 -a $# -lt 4 ] || fail "got $# expect 2 or 3"
trap cleanup EXIT
out=$1
input=$2
init=${3:-builtin}
[ "$(id -u)" = "0" ] || fail "have to be root. or use fakeroot"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || fail "failed mktemp -d"
initf=""
if [ "$init" = "builtin" ]; then
initf="${TEMP_D}/init"
winit > "$initf"
elif [ -n "$init" ]; then
[ -f "$initf" ] || fail "init '$initf' not a file"
initf="$init"
fi
case "$input" in
cgr.dev/*)
tar="${TEMP_D}/tmp.tar"
stderr "downloading $input to $tar"
crane export "$input" "$tar" || fail "crane export failed"
;;
*)
[ -f "$input" ] || fail "$input: didn't look like a tarball"
tar="$input"
;;
esac
rootd="${TEMP_D}/rootfs"
flist="${TEMP_D}/files"
mkdir "$rootd"
tar -C "$rootd" -xf "$tar" || fail "failed etract $tar"
if [ -n "$init" ]; then
cp "$initf" "$rootd/init" &&
chmod 755 "$rootd/init" || fail "failed to write $init"
fi
( cd "$rootd" && find . > "$flist" ) || fail "failed find files"
( cd "$rootd" && cpio --format=newc -o ) < "$flist" |
gzip --to-stdout > "$out" ||
fail "failed to create update cpio"
stderr "wrote $out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment