Gokrazy: simplify image build and add run command using qemu

This commit is contained in:
andig 2022-11-21 15:22:16 +01:00
parent 900e550d3c
commit 636e0d54ad
3 changed files with 111 additions and 9 deletions

1
.gitignore vendored
View file

@ -29,3 +29,4 @@ cmd/wip
fly.toml
.idea
vendor/*
QEMU_EFI.fd

View file

@ -18,7 +18,6 @@ PLATFORM := linux/amd64,linux/arm64,linux/arm/v6
# gokrazy image
IMAGE_FILE := evcc_$(TAG_NAME).image
IMAGE_ROOTFS := evcc_$(TAG_NAME).rootfs
IMAGE_OPTIONS := -hostname evcc -http_port 8080 github.com/gokrazy/serial-busybox github.com/gokrazy/breakglass github.com/gokrazy/mkfs github.com/gokrazy/wifi github.com/evcc-io/evcc
# deb
@ -105,23 +104,22 @@ apt-release::
)
# gokrazy image
image::
gokrazy::
go install github.com/gokrazy/tools/cmd/gokr-packer@main
mkdir -p flags/github.com/gokrazy/breakglass
echo "-forward=private-network" > flags/github.com/gokrazy/breakglass/flags.txt
mkdir -p flags/github.com/evcc-io/evcc
echo "--sqlite /perm/evcc.db" > flags/github.com/evcc-io/evcc/flags.txt
echo "--sqlite=/perm/evcc.db" > flags/github.com/evcc-io/evcc/flags.txt
mkdir -p buildflags/github.com/evcc-io/evcc
echo "$(BUILD_TAGS),gokrazy" > buildflags/github.com/evcc-io/evcc/buildflags.txt
echo "-ldflags=$(LD_FLAGS)" >> buildflags/github.com/evcc-io/evcc/buildflags.txt
gokr-packer -overwrite=$(IMAGE_FILE) -target_storage_bytes=1258299392 $(IMAGE_OPTIONS)
gzip -f $(IMAGE_FILE)
gokr-packer -hostname evcc -http_port 8080 -overwrite=$(IMAGE_FILE) -target_storage_bytes=1258299392 $(IMAGE_OPTIONS)
# gzip -f $(IMAGE_FILE)
image-rootfs::
gokr-packer -overwrite_root=$(IMAGE_ROOTFS) $(IMAGE_OPTIONS)
gzip -f $(IMAGE_ROOTFS)
gokrazy-run::
MACHINE=arm64 IMAGE_FILE=$(IMAGE_FILE) ./packaging/gokrazy/run.sh
image-update::
gokrazy-update::
gokr-packer -update yes $(IMAGE_OPTIONS)
soc::

103
packaging/gokrazy/run.sh Executable file
View file

@ -0,0 +1,103 @@
#!/usr/bin/env bash
imagefile="${IMAGE_FILE:=drive.img}"
arch="${MACHINE:=amd64}"
os=""
if [ "$(uname)" == "Darwin" ]; then
os="darwin"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
os="linux"
fi
case $arch in
# arm64 emulator with kernel auto loading
arm64)
args=(
-name gokrazy-arm64
-m 3G
-smp 2
-M virt,highmem=off
-cpu cortex-a72
-nographic
-drive file=${imagefile},format=raw
-bios ./packaging/gokrazy/QEMU_EFI.fd
)
if [[ "$os" == "darwin" ]]; then
args+=(-nic vmnet-shared)
else
args+=(-netdev user,id=net0,hostfwd=tcp::8080-:80,hostfwd=tcp::2222-:22)
args+=(-device e1000,netdev=net0)
fi
qemu-system-aarch64 "${args[@]}"
;;
# raspi3b emulator with manual kernel loading
# requires kernel (./vmlinuz) and dtb file extraction from the drive.img,
# to be used as an arg (with -dtb, -kernel and -append)
# to instruct the vm on how to load the kernel
raspi3b)
if [[ "$os" == "darwin" ]]; then
args+=(-nic vmnet-shared)
fi
# Only works with qemu === v5.2.0
# because for lower versions networking is missing.
# It was introduced in qemu v5.2.0 via usb networking (-device usb-net), but it is very very slow at the point that gokrazy network updates fail.
# For qemu versions >= v6.0.0, the /gokrazy/init process crashes early at gokrazy.Boot(). To be investigated.
qemu_version="$(qemu-system-aarch64 --version | sed -nr 's/^.*version\s([.0-9]*).*$/\1/p')"
if [[ "$qemu_version" != "5.2.0" ]]; then
echo "error: incompatible qemu-system-aarch64 version: $qemu_version. gokrazy on raspi3b can only run on 5.2.0"
exit 1;
fi
# Extract the kernel (vmlinuz) and the dtb file from the drive.img first
./extract_kernel.sh
args=(
-name gokrazy-arm64-raspi3b
-m 1024
-no-reboot
-M raspi3b
-append "console=tty1 console=ttyAMA0,115200 dwc_otg.fiq_fsm_enable=0 root=/dev/mmcblk0p2 init=/gokrazy/init rootwait panic=10 oops=panic"
-dtb ./bcm2710-rpi-3-b-plus.dtb
-nographic
-serial mon:stdio
-drive file=${imagefile},format=raw
-kernel vmlinuz
-netdev user,id=net0,hostfwd=tcp::8080-:80,hostfwd=tcp::2222-:22
-device usb-net,netdev=net0
)
qemu-system-aarch64 "${args[@]}"
;;
# amd64 emulator with kernel auto loading
amd64)
args=(
-name gokrazy-amd64
-m 3G
-smp $(nproc)
-usb
-nographic
-serial mon:stdio
-boot order=d
-drive file=${imagefile},format=raw
)
if [[ "$os" == "darwin" ]]; then
args+=(-nic vmnet-shared)
else
args+=(-netdev user,id=net0,hostfwd=tcp::8080-:80,hostfwd=tcp::2222-:22)
args+=(-device e1000,netdev=net0)
fi
qemu-system-x86_64 "${args[@]}"
;;
*)
echo -n "unsupported arch ${arch}"
exit
;;
esac