Skip to content

Commit

Permalink
Move cidata generation to subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
automat committed Jun 9, 2024
1 parent b392c82 commit 45e3b4a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
http/
output-*/
boot-*/
87 changes: 87 additions & 0 deletions ubuntu/cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Ubuntu cloud images

## Ubuntu 20.04 UEFI virtual firmware

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

```
$ sudo cp boot-ubuntu-20.04-x86_64/cidata.iso \
/var/lib/libvirt/boot/ubuntu-server-2004-cloud-init.iso
$ sudo qemu-img convert \
-f qcow2 \
-O qcow2 \
output-ubuntu-20.04-x86_64/ubuntu-20.04-x86_64.qcow2 \
/var/lib/libvirt/images/ubuntu-server-2004.qcow2
$ sudo qemu-img resize \
-f qcow2 \
/var/lib/libvirt/images/ubuntu-server-2004.qcow2 \
32G
```

```
virt-install \
--connect qemu:///system \
--name ubuntu-server-2004 \
--boot uefi \
--memory 4096 \
--vcpus 2 \
--os-variant ubuntu22.04 \
--disk /var/lib/libvirt/images/ubuntu-server-2004.qcow2,bus=virtio \
--disk /var/lib/libvirt/boot/ubuntu-server-2004-cloud-init.iso,device=cdrom \
--network network=host-network,model=virtio \
--graphics spice \
--noautoconsole \
--console pty,target_type=serial \
--import \
--debug
virsh console ubuntu-server-2004
# login with ubuntu user
$ cloud-init status --wait
status: done
# Disable cloud-init
$ sudo touch /etc/cloud/cloud-init.disabled
$ cloud-init status
status: disabled
$ sudo shutdown -h now
$ virsh domblklist ubuntu-server-2004
Target Source
-------------------------------------------------------------------
vda /var/lib/libvirt/images/ubuntu-server-2004.qcow2
sda /var/lib/libvirt/boot/ubuntu-server-2004-cloud-init.iso
$ virsh change-media ubuntu-server-2004 sda --eject
Successfully ejected media.
$ sudo rm /var/lib/libvirt/boot/ubuntu-server-2004-cloud-init.iso
$ virsh edit ubuntu-server-2004
# remove entry for the cloud-init.iso
<!--
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/var/lib/libvirt/boot/ubuntu-server-2004-cloud-init.iso'/>
<target dev='sda' bus='sata'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
-->
# Verify image boots without cloud-init iso being mounted
```

```
$ virsh shutdown ubuntu-server-2004
$ virsh undefine ubuntu-server-2004 --nvram --remove-all-storage
```
20 changes: 10 additions & 10 deletions ubuntu/cloud/x86_64/ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ variable "ssh_password" {
default = "packer"
}

variable "vm_name" {
type = string
default = "ubuntu-22.04-x86_64"
}

source "file" "user_data" {
content = <<EOF
#cloud-config
Expand All @@ -31,22 +36,22 @@ password: ${var.ssh_password}
chpasswd: { expire: False }
ssh_pwauth: True
EOF
target = "user-data"
target = "boot-${var.vm_name}/user-data"
}

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

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

provisioner "shell-local" {
inline = ["genisoimage -output cidata.iso -input-charset utf-8 -volid cidata -joliet -r user-data meta-data"]
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"]
}
}

Expand All @@ -60,26 +65,21 @@ variable "iso_url" {
default = "http://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
}

variable "vm_name" {
type = string
default = "ubuntu-22.04-x86_64"
}

source "qemu" "ubuntu" {
disk_compression = true
disk_image = true
disk_size = "16G"
iso_checksum = var.iso_checksum
iso_url = var.iso_url
qemuargs = [
["-cdrom", "cidata.iso"]
["-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
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
Expand Down

0 comments on commit 45e3b4a

Please sign in to comment.