Skip to content

Instantly share code, notes, and snippets.

@Madh93
Last active October 12, 2024 23:01
Show Gist options
  • Save Madh93/6006d6a84019075e8eaa7080f6ef2d77 to your computer and use it in GitHub Desktop.
Save Madh93/6006d6a84019075e8eaa7080f6ef2d77 to your computer and use it in GitHub Desktop.
Raspberry Pi with Packer and QEMU

Raspberry Pi with Packer and QEMU

Creates a Raspberry Pi image using Packer and using QEMU to test it.

Tested with Raspberry Pi OS Lite 5.10 (March 4th 2021).

Requirements (Arch Linux)

yay -S curl go packer qemu qemu-arch-extra qemu-user-static-bin

Quick start

Install packer-builder-arm plugin

git clone https://github.com/mkaczanowski/packer-builder-arm
cd packer-builder-arm
go mod download
go build
mkdir -p ~/.packer.d/plugins
cp packer-builder-arm ~/.packer.d/plugins

Build image

sudo packer raspios-lite.pkr.hcl

Set user permissions

sudo chown $(whoami): raspberry-pi-os.img

Download the Raspberry Pi kernel

curl https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/kernel-qemu-5.4.51-buster -o kernel-qemu-5.4.51-buster
curl https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/versatile-pb-buster-5.4.51.dtb -o versatile-pb-buster-5.4.51.dtb

Run image with QEMU

qemu-system-arm -M versatilepb -cpu arm1176 -m 256 -drive "file=raspberry-pi-os.img,if=none,index=0,media=disk,format=raw,id=disk0" -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" -net "user,hostfwd=tcp::5022-:22" -dtb versatile-pb-buster-5.4.51.dtb -kernel kernel-qemu-5.4.51-buster -append 'root=/dev/vda2 panic=1' -no-reboot

alt text

References

packer {
required_version = "1.7.2"
}
####################
# Variables (TODO: https://github.com/hashicorp/packer/issues/10127)
####################
variable "raspios_remote_url" {
description = "Raspberry Pi OS zip remote URL"
type = string
default = "https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2021-03-25/2021-03-04-raspios-buster-armhf-lite.zip"
}
variable "image_size" {
description = "Image size"
type = string
default = "4G"
}
variable "qemu_arm_binary_path" {
description = "QEMU binary path"
type = string
default = "/usr/bin/qemu-arm-static"
}
####################
# Sources
####################
source "arm" "raspios" {
// Base remote file
file_urls = [var.raspios_remote_url]
file_checksum_url = "${var.raspios_remote_url}.sha256"
file_checksum_type = "sha256"
file_target_extension = "zip"
// Image
image_build_method = "reuse"
image_path = "raspberry-pi-os.img"
image_type = "dos"
image_size = var.image_size
image_chroot_env = ["PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"]
image_partitions {
name = "boot"
type = "c"
start_sector = "8192"
filesystem = "vfat"
size = "256M"
mountpoint = "/boot"
}
image_partitions {
name = "root"
type = "83"
start_sector = "532480"
filesystem = "ext4"
size = "0"
mountpoint = "/"
}
// QEMU
qemu_binary_source_path = var.qemu_arm_binary_path
qemu_binary_destination_path = var.qemu_arm_binary_path
}
build {
sources = ["source.arm.raspios"]
provisioner "shell" {
inline = [
"touch /hello_world",
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment