<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mike1237</title>
    <description>The latest articles on DEV Community by mike1237 (@mike1237).</description>
    <link>https://dev.to/mike1237</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131577%2F6c72591a-dbac-46fd-81ff-66ef43610902.png</url>
      <title>DEV Community: mike1237</title>
      <link>https://dev.to/mike1237</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mike1237"/>
    <language>en</language>
    <item>
      <title>Create Proxmox cloud-init templates for use with Packer</title>
      <dc:creator>mike1237</dc:creator>
      <pubDate>Fri, 08 Oct 2021 18:30:17 +0000</pubDate>
      <link>https://dev.to/mike1237/create-proxmox-cloud-init-templates-for-use-with-packer-193a</link>
      <guid>https://dev.to/mike1237/create-proxmox-cloud-init-templates-for-use-with-packer-193a</guid>
      <description>&lt;p&gt;My latest project has been to re-create my homelab using cloud native principles such as Infrastructure-as-Code and Dev(Git)Ops.&lt;/p&gt;

&lt;p&gt;After much research and deliberation, I've concluded the best way for me to achieve this is to separate my local hardware from the software which will run on it. Essentially, I'll be treating my hardware as a "local cloud", and create VM Images that I will deploy running my application stack. (Azure Stack HCI has the right idea here!)&lt;/p&gt;

&lt;p&gt;When developing and testing on cloud providers, they always have a base image available to use as a starting off point. Not so for my local cloud!&lt;/p&gt;

&lt;p&gt;In this article, I'll demonstrate how to create a cloud-init enabled Ubuntu 20.04 LTS base image to use on Proxmox VE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cloud Native Image
&lt;/h2&gt;

&lt;p&gt;There are several guides out there which utilize the "proxmox-iso" packer builder to build a base image from an ISO file. Modern Linux distributions are increasingly moving away from this install method and preseed files. Rather, disk images are provided with the OS pre-installed, and configuration is performed via cloud-init. We will create a Proxmox KVM base image using Ubuntu's KVM cloud image. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Since the Packer Proxmox builder doesn't support LXD images nor templates, we need to create a KVM template.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Proxmox Script
&lt;/h2&gt;

&lt;p&gt;The Proxmox API doesn't appear to offer the full functionality provided by the native shell commands to create a template, so we will run a script via SSH.&lt;br&gt;
This gist contains the script, and I'll step through it below. &lt;a href="https://gist.github.com/mike1237/cce83a74f898b11c2cec911204568cf9"&gt;https://gist.github.com/mike1237/cce83a74f898b11c2cec911204568cf9&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Download the image
&lt;/h3&gt;

&lt;p&gt;We are downloading the kvm disk image. Note that this is a qcow2 image format with an extension of .img&lt;br&gt;
Promxox doesn't like this so we rename the disk image to .qcow2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SRC_IMG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img"&lt;/span&gt;
&lt;span class="nv"&gt;IMG_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"focal-server-cloudimg-amd64-disk-kvm.qcow2"&lt;/span&gt;
wget &lt;span class="nt"&gt;-O&lt;/span&gt; &lt;span class="nv"&gt;$IMG_NAME&lt;/span&gt; &lt;span class="nv"&gt;$SRC_IMG&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Add QEMU Guest Agent
&lt;/h3&gt;

&lt;p&gt;The Ubuntu 20.04 image we are going to use does not include the &lt;code&gt;qemu-guest-agent&lt;/code&gt; package which is needed for the Guest VM to report its IP details back to Proxmox. This is required for Packer to communicate with the VM after cloning. the template. &lt;code&gt;libguestfs-tools&lt;/code&gt; will allow us to embed &lt;code&gt;qemu-guest-agent&lt;/code&gt; into the image. You can also add any additional packages you'd like in your base image. Personally, I prefer to customize this base image later with packer so that the packages can live in source control.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apt update
apt &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; libguestfs-tools
virt-customize &lt;span class="nt"&gt;--install&lt;/span&gt; qemu-guest-agent &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nv"&gt;$IMG_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Create a VM in Proxmox with required settings and convert to template
&lt;/h3&gt;

&lt;p&gt;For best performance, virtio 'hardware' should be used. Additionally, cloud-init requires a serial console and cloudinit IDE (CDROM) drive. We will set the network config to DHCP so that we get an IP address. Lastly, we will expand the template disk image size so we have space to install items later. It appears packer doesn't support doing this later.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;TEMPL_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"ubuntu2004-cloud"&lt;/span&gt;
&lt;span class="nv"&gt;VMID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"9000"&lt;/span&gt;
&lt;span class="nv"&gt;MEM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"512"&lt;/span&gt;
&lt;span class="nv"&gt;DISK_SIZE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"32G"&lt;/span&gt;
&lt;span class="nv"&gt;DISK_STOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"local-lvm"&lt;/span&gt;
&lt;span class="nv"&gt;NET_BRIDGE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vmbr0"&lt;/span&gt;
qm create &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="nv"&gt;$TEMPL_NAME&lt;/span&gt; &lt;span class="nt"&gt;--memory&lt;/span&gt; &lt;span class="nv"&gt;$MEM&lt;/span&gt; &lt;span class="nt"&gt;--net0&lt;/span&gt; virtio,bridge&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$NET_BRIDGE&lt;/span&gt;
qm importdisk &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nv"&gt;$IMG_NAME&lt;/span&gt; &lt;span class="nv"&gt;$DISK_STOR&lt;/span&gt;
qm &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--scsihw&lt;/span&gt; virtio-scsi-pci &lt;span class="nt"&gt;--scsi0&lt;/span&gt; &lt;span class="nv"&gt;$DISK_STOR&lt;/span&gt;:vm-&lt;span class="nv"&gt;$VMID&lt;/span&gt;&lt;span class="nt"&gt;-disk-0&lt;/span&gt;
qm &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--ide2&lt;/span&gt; &lt;span class="nv"&gt;$DISK_STOR&lt;/span&gt;:cloudinit
qm &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--boot&lt;/span&gt; c &lt;span class="nt"&gt;--bootdisk&lt;/span&gt; scsi0
qm &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--serial0&lt;/span&gt; socket &lt;span class="nt"&gt;--vga&lt;/span&gt; serial0
qm &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$VMID&lt;/span&gt; &lt;span class="nt"&gt;--ipconfig0&lt;/span&gt; &lt;span class="nv"&gt;ip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dhcp
qm resize &lt;span class="nv"&gt;$VMID&lt;/span&gt; scsi0 &lt;span class="nv"&gt;$DISK_SIZE&lt;/span&gt;
qm template &lt;span class="nv"&gt;$VMID&lt;/span&gt;
&lt;span class="c"&gt;# Remove downloaded image&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nv"&gt;$IMG_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Packer template
&lt;/h2&gt;

&lt;p&gt;Now that we have our cloud-init enabled image on Proxmox, we can use Packer to create a template based off of this template.&lt;br&gt;
Ensure to set the &lt;code&gt;scsi_controller="virtio-scsi-pci"&lt;/code&gt; and &lt;code&gt;qemu_agent=true&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I'd recommend adding the Proxmox variables to a var file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;packer build &lt;span class="nt"&gt;--var-file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./proxmox.pkvars.hcl &lt;span class="nt"&gt;--var&lt;/span&gt; &lt;span class="s2"&gt;"proxox_template_name=test-output-template"&lt;/span&gt; &lt;span class="nt"&gt;--var&lt;/span&gt; &lt;span class="s2"&gt;"proxmox_source_template=ubuntu2004-cloud"&lt;/span&gt; base.pkr.hcl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Now that you've created a template using packer from the base template, you can use Terraform to deploy that VM!&lt;br&gt;
I'm still working on that myself :)&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466"&gt;https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466&lt;/a&gt;&lt;br&gt;
&lt;a href="https://whattheserver.com/proxmox-cloud-init-os-template-creation/"&gt;https://whattheserver.com/proxmox-cloud-init-os-template-creation/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/"&gt;https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pve.proxmox.com/wiki/Cloud-Init_Support"&gt;https://pve.proxmox.com/wiki/Cloud-Init_Support&lt;/a&gt;&lt;br&gt;
&lt;a href="https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/"&gt;https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>packer</category>
      <category>proxmox</category>
      <category>iac</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
