proxmox-template

Are you tired of manually creating VMs in Proxmox every time you need a new Ubuntu server? I’ll show you how to create a Ubuntu 24.04 Cloud-Init template in Proxmox using a simple Makefile in this guide. This automation will save you valuable time and ensure consistency across your VM deployments.

Deep Dive into the Makefile

Let’s break down each component of our Makefile to understand exactly what’s happening at each step.

GIT: https://github.com/thiagousa/youtube/blob/main/31/Makefile

Variables Configuration

  • VM_ID: Unique identifier for your VM in Proxmox (must be unique across your cluster)
  • VM_NAME: Descriptive name for your template
  • IMAGE_URL: Direct link to Ubuntu’s cloud image
  • IMAGE_NAME: Local filename for the downloaded image
  • STORAGE: Proxmox storage location (default is ‘local’)
  • DISK_SIZE: Final size of the VM disk after expansion
  • BRIDGE: Network bridge for VM connectivity (default is vmbr0)

Step 0: Installing Required Tools

This step installs libguestfs-tools, which provides essential utilities for manipulating disk images. The -y flag automatically accepts installation prompts.

Step 1: Preparing the Directory

Creates a ‘template’ directory for our working files. The -p flag prevents errors if the directory already exists.

Step 2: Downloading the Cloud Image

  • Depends on 1_prepare_dir to ensure the directory exists
  • Changes to the template directory
  • Downloads the Ubuntu cloud image using wget
  • Uses $(IMAGE_URL) variable for the download link

Step 3: Customizing the Image

  • Depends on 2_download_image to ensure the image is available
  • Uses virt-customize to modify the image
  • Installs qemu-guest-agent for better VM management capabilities
  • The --add flag specifies which image to modify

Step 4: Creating the VM

  • Creates a new VM with specific hardware configurations:
  • --numa 0: Disables NUMA (Non-Uniform Memory Access)
  • --ostype l26: Specifies Linux 2.6/3.x/4.x kernel
  • --cpu cputype=host: Uses host CPU type for best performance
  • --cores 4: Allocates 4 CPU cores
  • --sockets 2: Sets 2 CPU sockets
  • --memory 6144: Allocates 6GB RAM
  • --net0: Configures the first network interface with virtio driver

Step 5: Importing the Disk

  • Imports the customized cloud image as a disk for the VM
  • Uses the specified storage location
  • Creates a new disk with the VM ID

Step 6: Configuring the VM

Multiple important configurations:

  1. Sets up virtio-scsi-pci storage controller and attaches the imported disk
  2. Configures Cloud-Init drive on IDE2
  3. Sets boot order to use the SCSI disk
  4. Configures serial console
  5. Enables QEMU guest agent

Step 7: Resizing the Disk

  • Expand the disk to the specified size (50G)
  • Uses the + to indicate additional space rather than absolute size

Step 8: Creating the Template

  • Converts the configured VM into a template
  • After this step, the VM can’t be started but can be cloned

Step 9: Cleanup Option

  • Optional cleanup step to remove the working directory
  • Useful for saving space after template creation

Phony Targets

  • Declares all targets as phony
  • Ensures Make runs commands even if a file with the same name exists
  • Prevents conflicts with actual files/directories

Template Usage Tips

After creating your template, you can clone it using:

For Cloud-Init configuration, you can set parameters like:

The process will execute all steps automatically, creating your template.

Creating VMs from the Template

To create a new VM from your template:

  1. Go to your Proxmox web interface
  2. Right-click the template
  3. Select “Clone”
  4. Choose “Linked Clone” for space efficiency or “Full Clone” for independent VMs
  5. Configure Cloud-Init options (network, SSH keys, etc.)
  6. Start your new VM

Benefits of Using This Approach

  1. Consistency: Every VM created from this template will have the same base configuration
  2. Speed: Create new VMs in seconds instead of minutes
  3. Automation: Eliminate manual steps and reduce human error
  4. Resource Efficiency: Linked clones save storage space
  5. Maintainability: It is easy to update the template when needed

Conclusion

Using this automated approach with Make and Cloud-Init can streamline your VM creation process in Proxmox. The template system provides a solid foundation for deploying consistent, well-configured Ubuntu servers in your home lab or production environment.

Feel free to modify the Makefile variables to suit your needs, such as changing the VM ID, memory allocation, or disk size. Happy virtualization!

Share This Tutorial:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *