Logo

dev-resources.site

for different kinds of informations.

How to Build and Manage Virtual Machines Using Proxmox CLI: A Step-by-Step Guide

Published at
1/5/2025
Categories
proxmox
itautomation
vmdeployment
commandlinetools
Author
sebos
Author
5 person written this
sebos
open
How to Build and Manage Virtual Machines Using Proxmox CLI: A Step-by-Step Guide

Recently, I needed to set up a series of virtual machines (VMs) in Proxmox for a project. One of the VMs was intended for high-end data processing, and I wasn’t entirely certain about the exact requirements at the outset. To address this, I decided to experiment with various configurations until I found the optimal setup. While the Proxmox web interface is excellent, I needed a quicker, more flexible way to make incremental changes without navigating through multiple steps in the GUI. As someone who spends most of their time in the command line, using the Proxmox qm command-line interface felt like a natural fit.

Table of Contents

  1. Building a Virtual Machine with Proxmox CLI
  2. Leveraging the Proxmox CLI
  3. Removing Existing VMs
  4. Creating the Base VM
  5. Configuring Storage
  6. Configuring Networking
  7. Why Not Use the Web Interface?
  8. Why Not Use a Clone or Template?
  9. Adapting the Script for Your Project

Leveraging the Proxmox CLI

Proxmox provides the qm command for managing VMs directly from the command line, offering functionality comparable to the web interface and, in some cases, even more flexibility. You can refer to the official qm man page for comprehensive details. To make my workflow more efficient and reusable, I began by setting up some variables in a script. These variables included information about the VM ID, name, storage locations, disk sizes, memory, and CPU configuration. Here's the script snippet:

#!/bin/bash
## Basic VM Info
VMID=990
NAME="ThisVM"

## Linux to Install
ISO_STORAGE="ISOs"           # Replace with your ISO storage name
ISO_FILE="OracleLinux-R9-U4-x86_64-dvd.iso" # Replace with your ISO filename

## Hard Drive Size
DISK_STORAGE="vm_storage"    # Replace with your disk storage name
SYSTEM_DISK="300G"
DATA_DISK="500G"

## Memory Size
MEMORY=32
MEMORY_SIZE=$(( MEMORY * 1024 ))

## Processors
CORES=7
SOCKETS=2

## Network
VLAN_TAG=20
INTERFACE="vmbr0"
Enter fullscreen mode Exit fullscreen mode

Removing Existing VMs

If the VM already existed, I made sure to remove it before proceeding. This allowed me to tweak configurations and rerun the script without issues. Of course, if you plan to use this code, ensure the VM is either backed up or no longer needed.

## If existing, remove
if qm list | awk '{print $1}' | grep -q "^$VMID$"; then
    qm stop $VMID
    qm destroy $VMID
fi
Enter fullscreen mode Exit fullscreen mode

Creating the Base VM

With the setup cleared, I moved on to creating the base VM using qm create and configuring its resources. Here’s the snippet for setting memory, CPUs, and enabling NUMA (Non-Uniform Memory Access):

## Create VM
qm create $VMID --name $NAME 

## Setup Memory and CPUs
qm set $VMID --memory ${MEMORY_SIZE}
qm set $VMID --balloon ${MEMORY_SIZE}
qm set $VMID --cpu cputype=host
qm set $VMID --cores ${CORES} --sockets ${SOCKETS} --numa 1
Enter fullscreen mode Exit fullscreen mode

Note: NUMA support helps optimize memory access on modern multi-CPU servers by aligning memory regions with specific processors. This can be particularly important for high-performance workloads.

Configuring Storage

Next, I set up the boot ISO and the VM’s storage drives. For this project, I added a secondary disk specifically for data storage:

## Install ISO and Hard Drives
qm set $VMID --cdrom $ISO_STORAGE:iso/$ISO_FILE

### OS Drive
pvesm alloc vm_storage $VMID vm-${VMID}-disk-0 300G
qm set $VMID --scsi0 vm_storage:vm-${VMID}-disk-0,iothread=1,cache=writeback

### Data Drive
pvesm alloc vm_storage $VMID vm-${VMID}-disk-1 500G
qm set $VMID --scsihw virtio-scsi-single 
qm set $VMID --scsi1 vm_storage:vm-${VMID}-disk-1,iothread=1

### Boot Order
qm set $VMID --boot order='ide2;scsi0'
Enter fullscreen mode Exit fullscreen mode

Configuring Networking

For networking, the project required a single network card on VLAN 20. Here’s the configuration I used:

## Network 
qm set $VMID --net0 virtio,bridge=${INTERFACE},tag=${VLAN_TAG},queues=4
Enter fullscreen mode Exit fullscreen mode

full code here

Why Not Use the Web Interface?

While there’s absolutely nothing wrong with the Proxmox web interface, for this project, I needed the flexibility to tinker and rebuild quickly. Writing the script allowed me to fine-tune the setup iteratively, and it ensured that future rebuilds would be consistent and straightforward.

Why Not Use a Clone or Template?

Cloning or using a VM template would certainly be a valid approach for creating similar VMs in the future. However, as someone who enjoys coding and experimenting, I found scripting this process to be both rewarding and practical. It also gave me complete control over every aspect of the VM’s configuration.

How might you adapt this script for your project? Whether you’re automating VM deployments or just testing out configurations, using the Proxmox CLI can save time and streamline your workflows.

proxmox Article's
30 articles in total
Favicon
How to Build and Manage Virtual Machines Using Proxmox CLI: A Step-by-Step Guide
Favicon
Efficient Proxmox Backups: How to Use NAKIVO Backup & Replication
Favicon
Introduction à Terraform avec Proxmox
Favicon
Proxmox Virtual Machine optimization
Favicon
Complete Data Protection Guide: Backing Up Proxmox Virtual Machines
Favicon
Proxmox Backup by NAKIVO: The Ultimate Solution for VM Data Protection
Favicon
Proxmox cpu affinity for VMs
Favicon
Self-Host a Joplin Sync Server in Proxmox
Favicon
A Detailed Guide to Securing Proxmox Virtual Machines via NAKIVO Backup & Replication
Favicon
How to recover and update Proxmox 8 firewall configuration in SQLite when you locked yourself out
Favicon
Proxmox: Resize Your Local (pve) Disk
Favicon
Network performance optimization with Nvidia ConnectX on Proxmox
Favicon
Learning Ansible, Proxmox and LXC, Part 1
Favicon
Proxmox HugePages for VMs
Favicon
Disable or Hide Local Storage on Proxmox VE
Favicon
Install Proxmox on any bare metal server
Favicon
Proxmox Network Storage: Firewall Rules
Favicon
How to Install Ubuntu 24.04 on Proxmox VE
Favicon
Install Ubuntu 24.04 on Proxmox VE
Favicon
Proxmox and NAS: Implementing VLANs for Network Isolation
Favicon
Proxmox: Network Storage
Favicon
Create an Ubuntu Cloud-Init Template on Proxmox: The Command Line Guide
Favicon
Proxmox Security Series:Securing Root Access
Favicon
How to Backup Proxmox VM in Vinchin Backup & Recovery?
Favicon
Using Packer and Proxmox to Build Templates
Favicon
Proxmox Security Series: Strengthening Your Virtual Environment
Favicon
Saying Hello to the Room
Favicon
Setting Up The Home Lab: Setting up Kubernetes Using Ansible
Favicon
Setting Up The Home Lab: Terraform and Cloud-Init
Favicon
Effortless VM Data Security with NAKIVO and Proxmox Backup

Featured ones: