Skip to content

Commit

Permalink
feat: bootloader logic + general template design
Browse files Browse the repository at this point in the history
- resolves #7 by adding a smarter logic for UEFI detection
  in `late-commands` in the user-data cloud-init file
- closes #9 by adding a `host_distro` variable and a map to
  set the OVMF Paths in the template to make it generic

Signed-off-by: Shantanoo 'Shan' Desai <shantanoo.desai@gmail.com>
  • Loading branch information
shantanoo-desai committed May 2, 2023
1 parent e79e101 commit 37e54b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Shantanoo 'Shan' Desai <sdes.softdev@gmail.com>
# Copyright 2023 Shantanoo 'Shan' Desai <sdes.softdev@gmail.com>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,22 +20,22 @@ JAMMY_VARS_FILE:=./vars/jammy.pkrvars.hcl
TEST_TEMPLATE_FILE:=./templates/test.pkr.hcl

test-focal:
PACKER_LOG=1 packer build -force -var-file=${FOCAL_VARS_FILE} ${TEST_TEMPLATE_FILE}
source /etc/os-release; PACKER_LOG=1 packer build -force -var host_distro=$${ID} -var-file=${FOCAL_VARS_FILE} ${TEST_TEMPLATE_FILE}

test-jammy:
PACKER_LOG=1 packer build -force -var-file=${JAMMY_VARS_FILE} ${TEST_TEMPLATE_FILE}
source /etc/os-release; PACKER_LOG=1 packer build -force -var host_distro=$${ID} -var-file=${JAMMY_VARS_FILE} ${TEST_TEMPLATE_FILE}

build-focal:
PACKER_LOG=1 packer build -force -var-file=${FOCAL_VARS_FILE} ${TEMPLATE_FILE}
source /etc/os-release; PACKER_LOG=1 packer build -force -var host_distro=$${ID} -var-file=${FOCAL_VARS_FILE} ${TEMPLATE_FILE}

build-jammy:
PACKER_LOG=1 packer build -force -var-file=${JAMMY_VARS_FILE} ${TEMPLATE_FILE}
source /etc/os-release; PACKER_LOG=1 packer build -force -var host_distro=$${ID} -var-file=${JAMMY_VARS_FILE} ${TEMPLATE_FILE}

validate-focal:
packer validate -var-file=${FOCAL_VARS_FILE} ${TEMPLATE_FILE}
source /etc/os-release; packer validate -var host_distro=$${ID} -var-file=${FOCAL_VARS_FILE} ${TEMPLATE_FILE}

validate-jammy:
packer validate -var-file=${JAMMY_VARS_FILE} ${TEMPLATE_FILE}
source /etc/os-release; packer validate -var host_distro=$${ID} -var-file=${JAMMY_VARS_FILE} ${TEMPLATE_FILE}

validate-packer: validate-focal validate-jammy

Expand Down
8 changes: 5 additions & 3 deletions http/user-data
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ autoinstall:
install-server: true
allow-pw: true
late-commands:
- "sudo apt update && sudo apt install -y efibootmgr"
- "sudo efibootmgr"
- "sudo efibootmgr -o 0007,0001,0000,0002,0003,0004,0005,0006"
- |
if [ -d /sys/firmware/efi ]; then
apt-get install -y efibootmgr
efibootmgr -o $(efibootmgr | perl -n -e '/Boot(.+)\* ubuntu/ && print $1')
fi
user-data:
preserve_hostname: false
hostname: packerubuntu
Expand Down
17 changes: 12 additions & 5 deletions templates/ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Shantanoo 'Shan' Desai <sdes.softdev@gmail.com>
# Copyright 2023 Shantanoo 'Shan' Desai <sdes.softdev@gmail.com>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,17 @@ variable "vm_template_name" {
default = "packerubuntu"
}

variable "host_distro" {
type = string
default = "manjaro"
}

locals {
vm_name = "${var.vm_template_name}-${var.ubuntu_version}"
output_dir = "output/${local.vm_name}"
ovmf_prefix = {
"manjaro" = "x64/"
}
}

source "qemu" "custom_image" {
Expand Down Expand Up @@ -60,10 +68,9 @@ source "qemu" "custom_image" {
disk_size = "30G"
disk_compression = true

# Use the UEFI Bootloader OVMF file on the Build Machine
qemuargs = [
["-bios", "/usr/share/OVMF/OVMF_CODE.fd"]
]
efi_firmware_code = "/usr/share/OVMF/${lookup(local.ovmf_prefix, var.host_distro, "")}OVMF_CODE.fd"
efi_firmware_vars = "/usr/share/OVMF/${lookup(local.ovmf_prefix, var.host_distro, "")}OVMF_VARS.fd"
efi_boot = true

# Final Image will be available in `output/packerubuntu-*/`
output_directory = "${local.output_dir}"
Expand Down

0 comments on commit 37e54b4

Please sign in to comment.