fangorn/sunshine-qemu
public
ref:main
#!/usr/bin/env bash
# Build the test-pattern guest disk image.
#
# Usage: build_guest.sh <output-image>
#
# Produces a 1 MiB raw disk whose first sector is the boot sector from pattern.S.
set -euo pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
output="${1:?usage: build_guest.sh <output-image>}"
work="$(mktemp -d)"
trap 'rm -rf "${work}"' EXIT
as --32 -o "${work}/pattern.o" "${script_dir}/pattern.S"
ld -m elf_i386 -Ttext 0x7c00 -e _start --oformat binary -o "${work}/pattern.bin" "${work}/pattern.o"
size="$(stat -c %s "${work}/pattern.bin")"
if [[ "${size}" != 512 ]]; then
echo "boot sector is ${size} bytes, expected 512" >&2
exit 1
fi
mkdir -p "$(dirname "${output}")"
truncate -s 1M "${work}/disk.img"
dd if="${work}/pattern.bin" of="${work}/disk.img" conv=notrunc status=none
mv "${work}/disk.img" "${output}"
echo "${output}"