Skip to content

Commit

Permalink
Add Efi support (#95)
Browse files Browse the repository at this point in the history
* increase memory for install

* add efi support

* fix virtualbox efi setting not save and builder with efi mod name correct for vagrant file

* add vagrant remove function

* update README
  • Loading branch information
ifurther authored Jan 26, 2024
1 parent b1c1210 commit 0dfb4cb
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 16 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ BUILDER ?= virtualbox-iso.virtualbox
VERSION ?= 23.05
ARCH ?= x86_64
REPO ?= nixbox/nixos
USE_EFI ?= false
REPO_NAME = $(word 1, $(subst /, ,${REPO}))
BOX_NAME = $(word 2, $(subst /, ,${REPO}))
BUILD_PROVIDER = $(word 2, $(subst ., ,${BUILDER}))
BUILD_PROVIDER = $(word 1, $(subst -, ,$(word 2, $(subst ., ,${BUILDER}))))

ifeq ($(USE_EFI),true)
BUILDER=${BUILDER}-efi
endif

all: help

Expand Down Expand Up @@ -36,6 +41,9 @@ vagrant-plugin:
vagrant-add: vagrant-plugin ## Add vagrant box
@test -f nixos-${VERSION}-${BUILDER}-${ARCH}.box && ARCH=${ARCH} vagrant box add --force nixbox-${ARCH} nixos-${VERSION}-${BUILDER}-${ARCH}.box

vagrant-remove: vagrant-plugin ## Remove vagrant box
@vagrant box remove nixbox-${ARCH}

vagrant-up: ## Try builded vagrant box
@ARCH="${ARCH}" vagrant up --provider ${BUILD_PROVIDER}

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ First install [packer](http://packer.io) and

Four packer builders are currently supported:

- Virtualbox (`BUILDER=virtualbox-iso.virtualbox`)
- qemu / libvirt (`BUILDER=qemu.qemu`)
- VMware (`BUILDER=vmware-iso.vmware`)
- Hyper-V (`BUILDER=hyperv-iso.hyperv`)
- BIOS
- Virtualbox (`BUILDER=virtualbox-iso.virtualbox`)
- qemu / libvirt (`BUILDER=qemu.qemu`)
- VMware (`BUILDER=vmware-iso.vmware`)
- Hyper-V (`BUILDER=hyperv-iso.hyperv`)
- UEFI
- Virtualbox (`BUILDER=virtualbox-iso.virtualbox-efi`)
- qemu / libvirt (`BUILDER=qemu.qemu-efi`)

Have a look at the different `make build` target to build your image.

Expand Down
Binary file added efi_data/OVMF_CODE_4M.ms.fd
Binary file not shown.
Binary file added efi_data/OVMF_VARS_4M.ms.fd
Binary file not shown.
71 changes: 66 additions & 5 deletions nixos.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ variable "boot_wait" {
default = "120s"
}

variable "qemu_accelerator" {
type = string
default = "kvm"
}

variable "cloud_repo" {
type = string
default = "nixbox/nixos"
Expand Down Expand Up @@ -131,6 +136,30 @@ source "qemu" "qemu" {
ssh_username = "nixos"
}

source "qemu" "qemu-efi" {
boot_command = [
"mkdir -m 0700 .ssh<enter>",
"curl http://{{ .HTTPIP }}:{{ .HTTPPort }}/install_ed25519.pub > .ssh/authorized_keys<enter>",
"sudo systemctl start sshd<enter>"
]
boot_wait = var.boot_wait
disk_interface = "virtio-scsi"
disk_size = var.disk_size
format = "qcow2"
headless = true
http_directory = "scripts"
iso_checksum = var.iso_checksum
iso_url = local.iso_url
qemuargs = [["-m", var.memory]]
shutdown_command = "sudo shutdown -h now"
machine_type = "q35"
ssh_port = 22
ssh_private_key_file = "./scripts/install_ed25519"
ssh_username = "nixos"
efi_firmware_code = "./efi_data/OVMF_CODE_4M.ms.fd"
#efi_firmware_vars = "./efi_data/OVMF_VARS_4M.ms.fd"
}

source "virtualbox-iso" "virtualbox" {
boot_command = [
"mkdir -m 0700 .ssh<enter>",
Expand All @@ -152,6 +181,28 @@ source "virtualbox-iso" "virtualbox" {
vboxmanage = [["modifyvm", "{{ .Name }}", "--memory", var.memory, "--vram", "128", "--clipboard", "bidirectional"]]
}

source "virtualbox-iso" "virtualbox-efi" {
boot_command = [
"mkdir -m 0700 .ssh<enter>",
"echo '{{ .SSHPublicKey }}' > .ssh/authorized_keys<enter>",
"sudo systemctl start sshd<enter>"
]
boot_wait = "55s"
disk_size = var.disk_size
format = "ova"
guest_additions_mode = "disable"
guest_os_type = "Linux_64"
headless = true
http_directory = "scripts"
iso_checksum = var.iso_checksum
iso_url = local.iso_url
iso_interface = "sata"
shutdown_command = "sudo shutdown -h now"
ssh_port = 22
ssh_username = "nixos"
vboxmanage = [["modifyvm", "{{ .Name }}", "--memory", var.memory, "--vram", "128", "--clipboard", "bidirectional", "--firmware", "EFI"]]
}

source "vmware-iso" "vmware" {
boot_command = [
"mkdir -m 0700 .ssh<enter>",
Expand All @@ -176,7 +227,9 @@ build {
sources = [
"source.hyperv-iso.hyperv",
"source.qemu.qemu",
"source.qemu.qemu-efi",
"source.virtualbox-iso.virtualbox",
"source.virtualbox-iso.virtualbox-efi",
"source.vmware-iso.vmware"
]

Expand All @@ -188,14 +241,22 @@ build {
post-processors {
post-processor "vagrant" {
keep_input_artifact = false
only = ["virtualbox-iso.virtualbox", "qemu.qemu", "hyperv-iso.hyperv"]
only = ["virtualbox-iso.virtualbox", "qemu.qemu", "hyperv-iso.hyperv", "virtualbox-iso.virtualbox-efi", "qemu.qemu-efi"]
output = "nixos-${var.version}-${var.builder}-${var.arch}.box"
}
post-processor "vagrant-cloud" {
access_token = "${var.cloud_token}"
box_tag = "${var.cloud_repo}"
version = "${var.version}"
architecture = "${lookup(var.vagrant_cloud_arch, var.arch, "amd64")}"
only = ["virtualbox-iso.virtualbox", "qemu.qemu", "hyperv-iso.hyperv"]
access_token = "${var.cloud_token}"
box_tag = "${var.cloud_repo}"
version = "${var.version}"
architecture = "${lookup(var.vagrant_cloud_arch, var.arch, "amd64")}"
}
post-processor "vagrant-cloud" {
only = ["virtualbox-iso.virtualbox-efi", "qemu.qemu-efi"]
access_token = "${var.cloud_token}"
box_tag = "${var.cloud_repo}"
version = "${var.version}-efi"
architecture = "${lookup(var.vagrant_cloud_arch, var.arch, "amd64")}"
}
}
}
11 changes: 5 additions & 6 deletions scripts/grub-efi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
{
boot.loader = {
efi = {
canTouchEfiVariables = true;
efiSysMountPoint = "/boot/efi"; # ← use the same mount point here.
canTouchEfiVariables = false;
efiSysMountPoint = "/boot/efi";
};
# Use the GRUB 2 boot loader.
grub = {
enable = true;
grub = {
efiSupport = true;
device = "nodev";
};
efiSupport = true;
device = "nodev";
efiInstallAsRemovable = true;
};
};
}
31 changes: 31 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh -e

export MACHINE_TYPE=$([ -d /sys/firmware/efi/efivars ] && echo "UEFI" || echo "Legacy")

# Partition disk
if [ $MACHINE_TYPE == "Legacy" ];then
cat <<FDISK | fdisk /dev/sda
n
Expand All @@ -13,17 +15,46 @@ w
FDISK

elif [ $MACHINE_TYPE == "UEFI" ];then

parted /dev/sda -- mklabel gpt
parted /dev/sda -- mkpart root ext4 512MB 100%
parted /dev/sda -- mkpart ESP fat32 1MB 512MB
parted /dev/sda -- set 2 esp on
fi

# Create filesystem
if [ $MACHINE_TYPE == "Legacy" ];then

mkfs.ext4 -j -L nixos /dev/sda1

elif [ $MACHINE_TYPE == "UEFI" ];then

mkfs.fat -F 32 -n esp /dev/sda2
mkfs.ext4 -L nixos /dev/sda1

fi

# Mount filesystem
mount LABEL=nixos /mnt
if [ $MACHINE_TYPE == "UEFI" ];then
mkdir -p /mnt/boot/efi
if [ -e /dev/disk/by-label/esp ];then
mount /dev/disk/by-label/esp /mnt/boot/efi
else
mount /dev/sda2 /mnt/boot/efi
fi
fi

# Setup system
nixos-generate-config --root /mnt

curl -sf "$PACKER_HTTP_ADDR/vagrant.nix" > /mnt/etc/nixos/vagrant.nix
if [ $MACHINE_TYPE == "Legacy" ];then
curl -sf "$PACKER_HTTP_ADDR/grub-bios.nix" > /mnt/etc/nixos/bootloader.nix
elif [ $MACHINE_TYPE == "UEFI" ];then
curl -sf "$PACKER_HTTP_ADDR/grub-efi.nix" > /mnt/etc/nixos/bootloader.nix
fi
curl -sf "$PACKER_HTTP_ADDR/vagrant-hostname.nix" > /mnt/etc/nixos/vagrant-hostname.nix
curl -sf "$PACKER_HTTP_ADDR/vagrant-network.nix" > /mnt/etc/nixos/vagrant-network.nix
curl -sf "$PACKER_HTTP_ADDR/builders/$PACKER_BUILDER_TYPE.nix" > /mnt/etc/nixos/hardware-builder.nix
Expand Down

0 comments on commit 0dfb4cb

Please sign in to comment.