diff --git a/Makefile b/Makefile index 899a35a..3a8ae33 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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} diff --git a/README.md b/README.md index 2b3ab89..0e0f199 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/efi_data/OVMF_CODE_4M.ms.fd b/efi_data/OVMF_CODE_4M.ms.fd new file mode 100644 index 0000000..a180af5 Binary files /dev/null and b/efi_data/OVMF_CODE_4M.ms.fd differ diff --git a/efi_data/OVMF_VARS_4M.ms.fd b/efi_data/OVMF_VARS_4M.ms.fd new file mode 100644 index 0000000..87ef4c9 Binary files /dev/null and b/efi_data/OVMF_VARS_4M.ms.fd differ diff --git a/nixos.pkr.hcl b/nixos.pkr.hcl index e12ecc9..48a4588 100644 --- a/nixos.pkr.hcl +++ b/nixos.pkr.hcl @@ -64,6 +64,11 @@ variable "boot_wait" { default = "120s" } +variable "qemu_accelerator" { + type = string + default = "kvm" +} + variable "cloud_repo" { type = string default = "nixbox/nixos" @@ -131,6 +136,30 @@ source "qemu" "qemu" { ssh_username = "nixos" } +source "qemu" "qemu-efi" { + boot_command = [ + "mkdir -m 0700 .ssh", + "curl http://{{ .HTTPIP }}:{{ .HTTPPort }}/install_ed25519.pub > .ssh/authorized_keys", + "sudo systemctl start sshd" + ] + 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", @@ -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", + "echo '{{ .SSHPublicKey }}' > .ssh/authorized_keys", + "sudo systemctl start sshd" + ] + 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", @@ -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" ] @@ -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")}" } } } diff --git a/scripts/grub-efi.nix b/scripts/grub-efi.nix index 8bb4e59..368573b 100644 --- a/scripts/grub-efi.nix +++ b/scripts/grub-efi.nix @@ -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; }; }; } diff --git a/scripts/install.sh b/scripts/install.sh index 6ad1958..e9e2064 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -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 < /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