Skip to content

Commit

Permalink
Add Debian x86_64 cloud image harness
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorific committed Jun 23, 2024
1 parent b15aed2 commit eae3a58
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 0 deletions.
13 changes: 13 additions & 0 deletions debian/cloud/scripts/cloud-init-wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# https://bugs.launchpad.net/cloud-init/+bug/1890528
# cloud-init can return an exit status on wait other than 0
# so eat the exit status for now so it doesn't error packer
cloud_init_status=0
cloud-init status --wait || cloud_init_status=$?

if [ "$cloud_init_status" = "0" ]; then
echo "cloud-init succeeded"
else
echo "cloud-init exit=${cloud_init_status}"
fi
6 changes: 6 additions & 0 deletions debian/cloud/scripts/qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh -eux
export DEBIAN_FRONTEND=noninteractive

echo "==> Install qemu guest agent"
apt-get install -y qemu-guest-agent
apt-get -y autoremove
147 changes: 147 additions & 0 deletions debian/cloud/x86_64/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Debian x86_64 cloud images

## Debian 12 UEFI

```
cd debian/cloud/x86_64
packer init .
PACKER_LOG=1 packer build \
-var-file debian-12-x86_64.pkrvars.hcl \
debian.pkr.hcl
```

```
$ sudo qemu-img convert \
-f qcow2 \
-O qcow2 \
output-debian-12-x86_64/debian-12-x86_64.qcow2 \
/var/lib/libvirt/images/debian-12-x86_64.qcow2
$ sudo qemu-img resize \
-f qcow2 \
/var/lib/libvirt/images/debian-12-x86_64.qcow2 \
32G
```

```
$ sudo apt-get install libosinfo-bin
$ osinfo-query os
```

```
virt-install \
--connect qemu:///system \
--name debian-12 \
--boot uefi \
--memory 4096 \
--vcpus 2 \
--os-variant debian12 \
--disk /var/lib/libvirt/images/debian-12-x86_64.qcow2,bus=virtio \
--network network=host-network,model=virtio \
--graphics spice \
--noautoconsole \
--console pty,target_type=serial \
--import \
--debug
virsh console debian-12
# login with packer user
# Check networking - you may notice that the network interface is down and
# the name of the interface generated in netplan doesn't match. If not
# correct, can regenerate with cloud-init
# ip reports that enp1s0 is down
$ ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
enp1s0 DOWN
# Make cloud-init regenerate the network configuration
sudo rm /var/lib/cloud/data/instance-id
sudo cloud-init init --local
$ sudo reboot
# Verify image boots with the networking enabled
$ ip --brief a
lo UNKNOWN 127.0.0.1/8 ::1/128
enp1s0 UP 10.63.46.11/22 metric 100 fe80::5054:ff:fe04:483/64
# Verify cloud-init is disabled
$ cloud-init status
status: disabled
```

```
$ virsh shutdown debian-12
$ virsh undefine debian-12 --nvram --remove-all-storage
```

## Debian 12 BIOS

```
cd debian/cloud/x86_64
packer init .
PACKER_LOG=1 packer build \
-var-file debian-12-bios-x86_64.pkrvars.hcl \
debian.pkr.hcl
```

```
$ sudo qemu-img convert \
-f qcow2 \
-O qcow2 \
output-debian-12-bios-x86_64/debian-12-bios-x86_64.qcow2 \
/var/lib/libvirt/images/debian-12.qcow2
$ sudo qemu-img resize \
-f qcow2 \
/var/lib/libvirt/images/debian-12.qcow2 \
32G
```

```
virt-install \
--connect qemu:///system \
--name debian-12 \
--memory 4096 \
--vcpus 2 \
--os-variant debian12 \
--disk /var/lib/libvirt/images/debian-12.qcow2,bus=virtio \
--network network=host-network,model=virtio \
--graphics spice \
--noautoconsole \
--console pty,target_type=serial \
--import \
--debug
virsh console debian-12
# login with packer user
# Check networking - you may notice that the network interface is down and
# the name of the interface generated in netplan doesn't match. If not
# correct, can regenerate with cloud-init
# ip reports that enp1s0 is down
$ ip --brief a
lo UNKNOWN 127.0.0.1/8 ::1/128
enp1s0 DOWN
# NOTE: Because Ubuntu 20.04 has a version of cloud-init earlier than 23.4
# it does not have the "clean" parameter, instead regenerate the netplan
# config with the following
# Make cloud-init regenerate the network configuration
sudo rm /var/lib/cloud/data/instance-id
sudo cloud-init init --local
$ sudo reboot
# Verify cloud-init is disabled
$ cloud-init status
status: disabled
```

```
$ virsh shutdown debian-12
$ virsh undefine debian-12 --nvram --remove-all-storage
```
4 changes: 4 additions & 0 deletions debian/cloud/x86_64/debian-12-bios-x86_64.pkrvars.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
efi_boot = false
iso_checksum = "file:https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS"
iso_url = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
vm_name = "debian-12-bios-x86_64"
6 changes: 6 additions & 0 deletions debian/cloud/x86_64/debian-12-x86_64.pkrvars.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
efi_boot = true
efi_firmware_code = "/usr/share/OVMF/OVMF_CODE.fd"
efi_firmware_vars = "/usr/share/OVMF/OVMF_VARS.fd"
iso_checksum = "file:https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS"
iso_url = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
vm_name = "debian-12-x86_64"
108 changes: 108 additions & 0 deletions debian/cloud/x86_64/debian.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
packer {
required_plugins {
qemu = {
version = "~> 1"
source = "github.com/hashicorp/qemu"
}
}
}

variable "efi_boot" {
type = bool
default = false
}

variable "efi_firmware_code" {
type = string
default = null
}

variable "efi_firmware_vars" {
type = string
default = null
}

variable "ssh_username" {
type = string
default = "packer"
}

variable "ssh_password" {
type = string
default = "packer"
}

variable "vm_name" {
type = string
default = "debian-12-x86_64"
}

source "file" "user_data" {
content = <<EOF
#cloud-config
user: ${var.ssh_username}
password: ${var.ssh_password}
chpasswd: { expire: False }
ssh_pwauth: True
EOF
target = "boot-${var.vm_name}/user-data"
}

source "file" "meta_data" {
content = <<EOF
instance-id: debian-cloud
local-hostname: debian-cloud
EOF
target = "boot-${var.vm_name}/meta-data"
}

build {
sources = ["sources.file.user_data", "sources.file.meta_data"]

provisioner "shell-local" {
inline = ["genisoimage -output boot-${var.vm_name}/cidata.iso -input-charset utf-8 -volid cidata -joliet -r boot-${var.vm_name}/user-data boot-${var.vm_name}/meta-data"]
}
}

variable "iso_checksum" {
type = string
default = "file:https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS"
}

variable "iso_url" {
type = string
default = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
}

source "qemu" "debian" {
disk_compression = true
disk_image = true
disk_size = "16G"
iso_checksum = var.iso_checksum
iso_url = var.iso_url
qemuargs = [
["-cdrom", "boot-${var.vm_name}/cidata.iso"]
]
output_directory = "output-${var.vm_name}"
shutdown_command = "echo '${var.ssh_password}' | sudo -S shutdown -P now"
ssh_password = var.ssh_password
ssh_timeout = "120s"
ssh_username = var.ssh_username
vm_name = "${var.vm_name}.qcow2"
efi_boot = var.efi_boot
efi_firmware_code = var.efi_firmware_code
efi_firmware_vars = var.efi_firmware_vars
}

build {
sources = ["source.qemu.debian"]

# cloud-init may still be running when we start executing scripts
# To avoid race conditions, make sure cloud-init is done first
provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -S -E sh -eux '{{ .Path }}'"
scripts = [
"../scripts/cloud-init-wait.sh",
]
}
}

0 comments on commit eae3a58

Please sign in to comment.