Skip to content

eBPF-based security platform

2.9 KB
Raw Blame
#!/bin/bash
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo

set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
. "$here/common.sh"

usage()
{
	cat <<'EOF'
usage: shirudo-gen-image --source <directory> --output <prefix>
       --keys-dir <operator-key-directory> [--kind config|runtime] [--size <MiB>]
       [--builder <shirudo-build>]

Build an ext4 image and detached signed dm-verity metadata on the operator.
Writes <prefix>.img, <prefix>.verity, <prefix>.roothash and <prefix>.p7s.
The source directory is the filesystem root; it is not the host rootfs.
No existing output file is replaced. The private key never enters the image.
EOF
}

parse_args()
{
	while [ $# -gt 0 ]; do
		case "$1" in
		--source) source_dir=${2:?}; shift 2 ;;
		--output) output=${2:?}; shift 2 ;;
		--keys-dir) keys_dir=${2:?}; shift 2 ;;
		--kind) kind=${2:?}; shift 2 ;;
		--size) size=${2:?}; shift 2 ;;
		--builder) builder=${2:?}; shift 2 ;;
		-h|--help) usage; exit 0 ;;
		*) usage >&2; die "unknown option: $1" ;;
		esac
	done
}

check_inputs()
{
	[ -n "$source_dir" ] && [ -d "$source_dir" ] ||
		die '--source needs a directory'
	check_image_options "$output" "$keys_dir" "$size"
	case "$kind" in config|runtime) ;; *) die '--kind must be config or runtime' ;; esac
	require_command veritysetup 'install veritysetup (cryptsetup-bin or cryptsetup)'
	require_command mkfs.ext4 'install e2fsprogs (mkfs.ext4)'
	require_command "$builder" 'shirudo-build not found; use --builder'
	source_dir=$(realpath "$source_dir")
	keys_dir=$(realpath "$keys_dir")
}

check_source()
{
	case "$keys_dir/" in "$source_dir/"*) die 'operator keys must be outside the image source' ;; esac
	if find "$source_dir" -type f -name signing_key.pem -print -quit | grep -q .; then
		die 'the image source contains a private signing key'
	fi
	if [ "$kind" = config ] && find "$source_dir" -type l -print -quit | grep -q .; then
		die 'config images must not contain symbolic links'
	fi
	if find "$source_dir" ! -type d ! -type f ! -type l -print -quit | grep -q .; then
		die 'the image source contains a special file'
	fi
}

build_filesystem()
{
	local files rc=0

	truncate -s "${size}M" "$work/filesystem.img"
	mkfs.ext4 -q -F -b 4096 -O '^has_journal' -E root_owner=0:0 \
		-d "$source_dir" "$work/filesystem.img" 2>"$work/mkfs.err" || rc=$?
	cat "$work/mkfs.err" >&2
	[ "$rc" -eq 0 ] && return
	if grep -q 'Could not allocate' "$work/mkfs.err"; then
		files=$(du -s -k --apparent-size "$source_dir" | cut -f1)
		die "$files KiB of files do not fit an image of $size MiB; raise --size"
	fi
	die 'mkfs.ext4 failed'
}

build_signed_image()
{
	"$here/shirudo-gen-verity" --builder "$builder" "$work/filesystem.img" -o "$output" \
		--kind "$kind" --keys-dir "$keys_dir"
}

main()
{
	parse_args "$@"
	check_inputs
	check_source
	work=$(mktemp -d)
	trap 'rm -rf "$work"' EXIT
	build_filesystem
	build_signed_image
}

source_dir=
output=
keys_dir=
kind=config
size=1
builder=${SHIRUDO_BUILD:-shirudo-build}
main "$@"