Created
April 20, 2022 01:41
-
-
Save xkisu/4afb229c065c30146669ae0301bb7032 to your computer and use it in GitHub Desktop.
Run Ubuntu Cloud image under QEMU and KVM with Nomad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://powersj.io/posts/ubuntu-qemu-cli/ | |
# https://github.com/hashicorp/nomad/issues/5688 | |
job "linux-vm" { | |
region = "global" | |
datacenters = ["dc1"] | |
type = "service" | |
group "vm" { | |
count = 1 | |
network { | |
port "http" {} | |
port "ssh" {} | |
} | |
# Task to generate the cloud-init metadata NoCloud drive in the allocation directory. | |
task "metadata" { | |
lifecycle { | |
hook = "prestart" | |
sidecar = false | |
} | |
template { | |
data = <<EOH | |
instance-id: iid-local01 | |
local-hostname: cloudimg | |
EOH | |
destination = "local/metadata.yaml" | |
} | |
template { | |
data = <<EOH | |
#cloud-config | |
password: password | |
chpasswd: | |
expire: False | |
ssh_pwauth: True | |
EOH | |
destination = "local/user-data.yaml" | |
} | |
driver = "docker" | |
config { | |
image = "ghcr.io/xkisu/docker-cloud-utils/cloud-utils:main" | |
work_dir = "/metadata/local/" | |
command = "cloud-localds" | |
# Output the image to the shared allocation directory. | |
args = ["/alloc/data/seed.img", "user-data.yaml", "metadata.yaml"] | |
# Mount the task directory to the container to allow us | |
# to read the metadata files generated from the templates | |
# and to give us a place to save the generated image to. | |
mount { | |
type = "bind" | |
source = "." # bind the task directory for this task | |
target = "/metadata/" | |
readonly = false | |
} | |
} | |
} | |
task "vm" { | |
driver = "qemu" | |
config { | |
image_path = "local/bionic-server-cloudimg-amd64.img" | |
# Use KVM acceleration | |
accelerator = "kvm" | |
# Attempt a gracefull shutdown via power button emulation | |
graceful_shutdown = true | |
args = [ | |
"-nographic", | |
"-device", "virtio-net-pci,netdev=net0", | |
# Tell QEMU to listen on the allocated ssh port and forward connections | |
"-netdev", "user,id=net0,hostfwd=tcp::${NOMAD_PORT_http}-:80,hostfwd=tcp::${NOMAD_PORT_ssh}-:22", | |
# Supply the cloud-init metadata (NOMAD_ALLOC_DIR env variable does not work here) | |
"-drive", "if=virtio,format=raw,file=/opt/nomad/data/alloc/${NOMAD_ALLOC_ID}/alloc/data/seed.img" | |
] | |
port_map = { | |
ssh = 22 | |
} | |
} | |
# Use an artifact to retrieve the image to run | |
artifact { | |
# https://cloud-images.ubuntu.com/bionic/current/ | |
source = "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img" | |
options { | |
# https://cloud-images.ubuntu.com/bionic/current/MD5SUMS | |
checksum = "md5:48d672e399b3905f412ea015633c5893" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment