Skip to content

Instantly share code, notes, and snippets.

@atoktoto
Last active February 27, 2024 21:22
Show Gist options
  • Save atoktoto/27cb50c04c2ffa506f7c2811f1f5ffcd to your computer and use it in GitHub Desktop.
Save atoktoto/27cb50c04c2ffa506f7c2811f1f5ffcd to your computer and use it in GitHub Desktop.
Nu shell script for creating Proxmox templates with pre-baked qemu-guest-agent and docker
#!/bin/nu
let source_image_url = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
let source_image_stem = $source_image_url | url parse | get path | path parse | get stem
let source_image = $"($source_image_stem).img"
let agent_image = $"($source_image_stem)-agent.img"
let docker_image = $"($source_image_stem)-docker.img"
apt install libguestfs-tools -y
let no_image = ls | where name == $source_image | is-empty
if $no_image {
# download the image
wget $source_image_url
}
do --ignore-errors { rm $agent_image }
do --ignore-errors { rm $docker_image }
cp $source_image $agent_image
virt-customize -a $agent_image --install qemu-guest-agent,ncat,net-tools,bash-completion,iputils-ping,nano
# virt-filesystems --long -h --all -a jammy-server-cloudimg-amd64.img
# truncate -s 3G $docker_image
qemu-img create $docker_image 3G
virt-resize --align-first never --expand /dev/sda1 $agent_image $docker_image
virt-customize -a $docker_image --run-command 'grub-install /dev/sda'
virt-customize -a $docker_image --run docker.sh
virt-customize -a $docker_image --install docker-ce,docker-ce-cli,containerd.io,docker-buildx-plugin,docker-compose-plugin
def create_template [id, img] {
# create a new VM
qm create $id --memory 2048 --net0 virtio,bridge=vmbr0
# import the downloaded disk to local-lvm storage
qm importdisk $id $img local-lvm
# finally attach the new disk to the VM as scsi drive
qm set $id --scsihw virtio-scsi-pci --scsi0 $"local-lvm:vm-($id)-disk-0" ## TODO FIX ()
# configure a CDROM drive, used to pass the
qm set $id --ide2 local-lvm:cloudinit
# boot directly from the Cloud-Init image
qm set $id --boot c --bootdisk scsi0
# configure a serial console and use that as display
qm set $id --serial0 socket --vga serial0
qm set $id --agent enabled=1
qm set $id --ipconfig0 ip=dhcp
# transform VM into a template.
qm template $id
}
create_template 9000 $agent_image
create_template 9001 $docker_image
# deploy Cloud-Init Templates
#qm clone 9000 123 --name ubuntu-test
#qm set 123 --sshkey artkrz.pub
#qm set 123 --ipconfig0 ip=dhcp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment