Create Virtual Machine from a template

From Sea of Fate
Jump to navigationJump to search

Introduction

This is not a deep dive in to ansible or similar but a simple quick and dirty guide to creating a new VM from a template and configuring some of the networking with cloud init.

Creating a New Template

To create a template it would be better if the source VM did not have loads of junk on it, so we start from scratch and make a new VM. Set the host name to be something to mach the template eg LST24.04 is Linux Server V24.04 (as we use Ubuntu as a default only give distro if it is something different).

  • LST24.04 is Linux Server version 24.04
  • LDT24.o4 is Linux Desktop version 24.02
  • W11P is Microsoft Windows 11 Pro

Run through the installation of the the OS and configure anything that may be required. When everything has been added and configured shutdown the VM and convert to template.

Linux Adfditional config

Some things will be required on every Linux like the qemu agent. other things like setting the max auth tries on SSH might be good setting up the additional user photoup and photoshare are other things that may save some time in the future.

sudo apt update
sudo apt install cloud-init
sudo apt qemu-guest-agent
sudo apt upgrade
sudu nano /etc/ssh/sshd_config

uncomment MaxAuthTries and change the 6 to 10. then save and exit

sudo systemctl restart ssh
sudo systemctl enable ssh
sudo systemctl status ssh
sudo groupadd -g 5001 photoshare
sudo useradd -u 4001 -g 5001 photoup
sudo passwd photoup

clearing the history would be good and any ssh keys and clesr package manage cache

history -c
sudo rm /etc/ssh/ssh_host_*
sudo apt clean

when all options have been configured and completed shutdown

sudo shutdown -h now

Creating VMs on Command

To create a new host from your template with the specified IP address, gateway, DNS server, and search domain, you can use the Proxmox command-line interface (CLI) with the `qm clone` command and appropriate Cloud-Init parameters.

Here's how you can do it:

Using the `qm clone` Command

qm clone <template_vmid> <new_vmid> --name <new_hostname> --ipconfig0 "ip=192.168.100.26/24,gw=192.168.100.1" --nameserver 192.168.110.11 --searchdomain seaoffate.local

Explanation:

  • qm clone <template_vmid> <new_vmid>:
   * This is the base command to clone a VM.
   * Replace `<template_vmid>` with the ID of your template VM.
   * Replace `<new_vmid>` with the desired ID for the new VM.
  • --name <new_hostname>:
   * Replace `<new_hostname>` with the desired hostname for the new VM.
  • --ipconfig0 "ip=192.168.100.26/24,gw=192.168.100.1":
   * This sets the IPv4 configuration for the first network interface (`eth0` or similar).
   * `ip=192.168.100.26/24` sets the IP address and netmask.
   * `gw=192.168.100.1` sets the gateway address.
  • --nameserver 192.168.110.11:
   * This sets the DNS server address.
  • --searchdomain seaoffate.local:
   * This sets the search domain.

Example

If your template VM ID is 100, and you want to create a new VM with ID 101 and hostname "newhost," the command would be:

qm clone 100 101 --name newhost --ipconfig0 "ip=192.168.100.26/24,gw=192.168.100.1" --nameserver 192.168.110.11 --searchdomain seaoffate.local

Important Notes

  • Cloud-Init: This command assumes that your template VM has Cloud-Init installed and configured.
  • Network Interface Name: The network interface name might vary. If your VM uses a different interface name (e.g., `ens3`), you might need to adjust the Cloud-Init configuration accordingly.
  • Testing: After creating the new VM, verify that the IP address, gateway, DNS server, and search domain are correctly configured.
  • Proxmox User Permissions: The user running this command must have sufficient permissions to create VMs.
  • Firewall: Ensure that your firewall allows network traffic to and from the new VM.
  • DHCP: Make sure that the IP address does not conflict with any IP addresses assigned by a DHCP server.

By using this command, you can easily create new VMs from your template with the desired network configuration.

Other Properties

When creating a VM from a template in Proxmox, you can specify a wide range of parameters beyond just the IP address, hostname, and DNS. These parameters allow you to customize the VM's hardware, storage, networking, and other settings during the cloning process. These will override the templates setting so it is only neeassary to add the values that differ from the template

Here's a comprehensive list of what you can specify using the `qm clone` command and related options:

Basic VM Configuration
  • VM ID (`<new_vmid>`): The unique identifier for the new VM.
  • VM Name (`--name <new_hostname>`): The hostname or descriptive name for the VM.
  • Target Node (`--target <node>`): The Proxmox node where the VM will be created.
  • Full Clone (`--full`): Creates a full, independent copy of the VM's disk. (Default is linked clone)
Hardware Configuration
  • CPU Cores (`--cores <n>`): The number of CPU cores assigned to the VM.
  • Sockets (`--sockets <n>`): The number of CPU sockets assigned to the VM.
  • Memory (`--memory <n>`): The amount of RAM (in MB) assigned to the VM.
  • NUMA (`--numa <0|1>`): Enables or disables NUMA support.
  • CPU Type (`--cpu <type>`): The CPU type (e.g., `host`, `kvm64`, `Penryn`).
  • Machine Type (`--machine <type>`): The machine type (e.g., `pc-q35-7.1`).
  • BIOS (`--bios <type>`): The BIOS type (e.g., `seabios`, `ovmf`).
  • VGA (`--vga <type>`): The VGA adapter type (e.g., `std`, `virtio`).
  • Keyboard Layout (`--keyboard <layout>`): The keyboard layout (e.g., `en-us`, `de`).
Storage Configuration
  • Disk Size (`--disk <disk>[,size=<n>]`): Size of the new virtual disk.
  • Storage (`--storage <storage>`): The storage location for the VM's disks.
  • Disk Type (`--disk <disk>[,format=<format>]`): The disk format (e.g., `raw`, `qcow2`).
  • Cache (`--cache <mode>`): Disk cache mode (e.g., `none`, `writethrough`, `writeback`).
  • IO Thread (`--iothread <0|1>`): Enables or disables I/O threading.
Network Configuration
  • Network Bridge (`--net<n> bridge=<bridge>`): The network bridge to connect the VM to.
  • Network Model (`--net<n> model=<model>`): The network adapter model (e.g., `virtio`, `e1000`).
  • MAC Address (`--net<n> macaddr=<mac>`): The MAC address for the network interface.
  • VLAN ID (`--net<n> vlan=<vlan>`): The VLAN ID for the network interface.
  • IP Configuration (`--ipconfig<n> "ip=<ip>/<cidr>,gw=<gw>"`): IPv4 or IPv6 configuration.
  • Name Server (`--nameserver <ns>`): DNS server address.
  • Search Domain (`--searchdomain <domain>`): DNS search domain.
Cloud-Init Configuration
  • =User Data (`--ciuser <user>`)=: The username for Cloud-Init.
  • =Password (`--cipassword <password>`)=: The password for Cloud-Init.
  • =SSH Public Keys (`--sshkeys <keys>`)=: SSH public keys for Cloud-Init.
  • =DNS Search Domain (`--searchdomain <domain>`)=: DNS search domain.
  • =DNS Name Server (`--nameserver <ns>`)=: DNS name server.
Other Options
  • Start VM (`--start <0|1>`): Starts the VM after creation.
  • Description (`--description <text>`): Adds a description to the VM.
  • Tags (`--tags <tags>`): Adds tags to the VM.
Example with Multiple Options
qm clone 100 101 --name newvm --cores 4 --memory 4096 --storage local-lvm --disk 0,size=32G --net0 bridge=vmbr0,model=virtio --ipconfig0 "ip=192.168.100.27/24,gw=192.168.100.1" --nameserver 192.168.110.11 --searchdomain seaoffate.local --ciuser myuser --cipassword mypassword

This command clones VM 100 to VM 101, sets the name, CPU cores, memory, storage, disk size, network bridge, IP configuration, DNS, Cloud-Init user, and Cloud-Init password.

Remember to replace the placeholder values with your desired settings. Consult the Proxmox documentation for the most up-to-date information and options.