openbsd-rv64-qemu/makefile

101 lines
3.7 KiB
Makefile
Raw Normal View History

2024-04-29 23:59:31 +02:00
# Inspired by https://www.codemadness.org/openbsd-riscv64-vm.html
# mirror list: https://www.openbsd.org/ftp.html
mirror := https://ftp.lysator.liu.se/pub/OpenBSD/
2024-04-30 00:47:58 +02:00
# Version of OpenBSD to install
2024-04-29 23:59:31 +02:00
release := 7.5
release_short := $(shell echo ${release} | sed 's/\.//g')# Without dots (7.5 -> 75)
2024-04-30 00:47:58 +02:00
# Name of the disk image, named after the release
vmname := openBSD$(release_short)
diskname := $(vmname).qcow2
# Timestamp used for backup
UNIXTIME := $(shell date +%s)
# These are the packages that will be fetched from the OpenBSD mirror
2024-04-29 23:59:31 +02:00
opensbiname := opensbi-1.2.tgz
ubootname := u-boot-riscv64-2021.10p8.tgz
2024-04-30 00:47:58 +02:00
# PK used for release (not package, apparently) verification.
# Note that this is explicitly fetched from the OpenBSD mirror.
2024-04-29 23:59:31 +02:00
release_pubkey := openbsd-$(release_short)-base.pub
2024-04-30 00:47:58 +02:00
release_pubkey_url := https://ftp.openbsd.org/pub/OpenBSD/$(release)/$(release_pubkey)
# Specify how we want curl to behave
2024-04-29 23:59:31 +02:00
CURL := @curl --progress-bar -O
2024-04-30 00:47:58 +02:00
# The miniroot image is the installer image
2024-04-29 23:59:31 +02:00
minirootname := miniroot$(release_short).img
2024-04-30 00:47:58 +02:00
# QEMU arguments
QEMU_ARGS := -machine virt -nographic -m 2048M -smp 2
QEMU_ARGS += -bios share/opensbi/generic/fw_jump.bin
QEMU_ARGS += -kernel share/u-boot/qemu-riscv64_smode/u-boot.bin
QEMU_ARGS += -netdev user,id=net0,ipv6=off -device virtio-net-device,netdev=net0
QEMU_ARGS += -drive file=$(diskname),format=qcow2,id=hd0 -device virtio-blk-device,drive=hd0
# Fetch the public key used to verify the release
2024-04-29 23:59:31 +02:00
$(release_pubkey):
$(CURL) ${release_pubkey_url}
2024-04-30 00:47:58 +02:00
# Fetch the miniroot image and its checksums -> verify with signify
2024-04-29 23:59:31 +02:00
$(minirootname): $(release_pubkey)
$(CURL) ${mirror}/${release}/riscv64/${minirootname}
$(CURL) ${mirror}/${release}/riscv64/SHA256
$(CURL) ${mirror}/${release}/riscv64/SHA256.sig
signify -C -p ${release_pubkey} -x SHA256.sig $(minirootname)
2024-04-30 00:47:58 +02:00
# Allocate a qcow2 disk image, this is analogous to a hard drive
2024-04-29 23:59:31 +02:00
$(diskname):
qemu-img create -f qcow2 ${diskname} 10G
2024-04-30 00:47:58 +02:00
# Fetches the amd64 repository keys and checksums
2024-04-29 23:59:31 +02:00
repokeys: $(release_pubkey)
$(CURL) ${mirror}/${release}/packages/amd64/SHA256
$(CURL) ${mirror}/${release}/packages/amd64/SHA256.sig
2024-04-30 00:47:58 +02:00
# Verify the checksums of the packages in the current directory
2024-04-29 23:59:31 +02:00
checksums: repokeys
cksum -a sha256 --ignore-missing -c SHA256
2024-04-30 00:47:58 +02:00
# Fetch OpenSBI
2024-04-29 23:59:31 +02:00
$(opensbiname): repokeys
$(CURL) ${mirror}/${release}/packages/amd64/${opensbiname}
@# signify -C -p ${release_pubkey} -x SHA256.sig $(opensbiname)
@tar -xzf ${opensbiname} share/opensbi/generic/fw_jump.bin
2024-04-30 00:47:58 +02:00
# Fetch U-Boot
2024-04-29 23:59:31 +02:00
$(ubootname): repokeys
$(CURL) ${mirror}/${release}/packages/amd64/${ubootname}
@# signify -C -p ${release_pubkey} -x SHA256.sig $(ubootname)
@tar -xzf ${ubootname} share/u-boot/qemu-riscv64_smode/u-boot.bin
2024-04-30 00:47:58 +02:00
# Installer pulls all the pieces together and starts the VM with the installer attached as a drive (with correct boot order)
2024-04-29 23:59:31 +02:00
installer: $(minirootname) $(diskname) $(opensbiname) $(ubootname)
make checksums
@echo "Starting VM with installer..."
2024-04-30 00:47:58 +02:00
@qemu-system-riscv64 -drive file=$(minirootname),format=raw,id=cd0 -device virtio-blk-device,drive=cd0 $(QEMU_ARGS)
2024-04-29 23:59:31 +02:00
2024-04-30 00:47:58 +02:00
# Run doesn't fetch anything, it just starts the VM with the disk (qcow2) image
2024-04-29 23:59:31 +02:00
run:
@echo "Starting VM from disk image..."
2024-04-30 00:47:58 +02:00
@qemu-system-riscv64 $(QEMU_ARGS)
# Creates a compressed archive of the disk image
backup:
@echo "Backing up disk image..."
qemu-img convert -c -O qcow2 $(diskname) $(vmname)_$(UNIXTIME).qcow2
qemu-img info $(vmname)_$(UNIXTIME).qcow2
tar -czvf $(vmname)_$(UNIXTIME).tar.gz $(vmname)_$(UNIXTIME).qcow2
2024-04-29 23:59:31 +02:00
2024-04-30 00:47:58 +02:00
# Clean up the directory
2024-04-29 23:59:31 +02:00
clean:
rm -f ${minirootname} ${diskname} ${opensbiname} ${ubootname} ${release_pubkey} SHA256 SHA256.sig
rm -rf share
2024-04-30 00:47:58 +02:00
# Clean, but also remove backups and disk images
superclean: clean
rm -f $(vmname)_*.qcow2 $(vmname)_*.tar.gz
.PHONY: installer run clean backup