This documentation outlines the steps taken to create a virtual machine (VM) in Proxmox using a Python script with the Proxmox API. It also includes instructions for generating a user in Proxmo
Table of Contents
- Prerequisites
- User Generation in Proxmox
- Python Script for VM Creation
- Key Components of the Script
- Running the Script
- Troubleshooting
1.Prerequisites
- A running Proxmox server.
- Python 3.x installed on your machine.
- Required Python libraries:
pip install proxmoxer
2.User Generation in Proxmox
To create a user in Proxmox, follow these steps:
1.Log in to the Proxmox Web Interface.
2.Navigate to Datacenter:
- Click on the “Datacenter” in the left sidebar.
3.Create a User:
- Go to the “Permissions” tab.
- Click on “Users”.
- Click on “Add” to create a new user.
- Fill in the required fields:
- User ID:
username@pve
- Password: Set a strong password.
- Realm: Select
PVE
(Proxmox VE).
- User ID:
- Click “Add” to save the user.
4.Assign Permissions:
- Go to the “Permissions” tab.
- Click on “Add” to assign permissions to the user.
- Select the user and assign the appropriate role (e.g.,
PVEAdmin
).
Command-Line User Creation
You can also create a user via the command line:
pveum useradd username@pve --password your_password
Assign permissions:
pveum acl add / -user username@pve -role PVEAdmin
3.Python Script for VM Creation
The following Python script creates a VM in Proxmox using the Proxmox API:
“`python:network_infra/vm_vnc_setup.py
import paramiko
from proxmoxer import ProxmoxAPI
import time
Proxmox connection details
PROXMOX_HOST = “192.168.1.10”
PROXMOX_USER = “username@pve”
PROXMOX_PASSWORD = “your_password”
NODE = “pve1”
VM configuration
VM_NAME = “lubuntu-vm”
MEMORY = 2048 # in MB
CORES = 1
DISK_SIZE = “20G” # Just the size
ISO = “local:iso/ubuntu-desktop.iso” # Corrected ISO path
Connect to Proxmox API
proxmox = ProxmoxAPI(PROXMOX_HOST, user=PROXMOX_USER, password=PROXMOX_PASSWORD, verify_ssl=False)
def create_vm():
# Find the next available VM ID
try:
vmid = max([vm[‘vmid’] for vm in proxmox.nodes(NODE).qemu.get()]) + 1
except ValueError:
vmid = 100 # Default starting VM ID
# Correctly format the disk path
disk_name = "local-lvm"
disk_path = f"{disk_name}:vm-{vmid}-disk-0" # Correct format without illegal characters
# Create the VM
proxmox.nodes(NODE).qemu.create(
vmid=vmid,
name=VM_NAME,
memory=MEMORY,
cores=CORES,
net0='virtio,bridge=vmbr0',
ide2=ISO, # Use the validated ISO path
virtio0=disk_path, # Use the correctly formatted disk specification
boot='order=ide2;virtio0',
ostype='l26', # Linux 2.6/3.x/4.x/5.x
)
print(f"VM {VM_NAME} created with ID {vmid}")
# Wait for the VM to be accessible
time.sleep(10) # Adjust as necessary
if name == "main":
create_vm()
```
4.Key Components of the Script
- Proxmox Connection: Connects to the Proxmox API using the provided credentials.
- VM Configuration: Sets the VM name, memory, CPU cores, disk size, and ISO path.
- VM Creation: Uses the Proxmox API to create the VM with the specified parameters.
5.Running the Script
To run the script, execute the following command in your terminal:
python network_infra/vm_vnc_setup.py
6.Troubleshooting
- Invalid ISO Path: Ensure the ISO path is correctly formatted as
storage_name:iso_path
. - Disk Specification Errors: Ensure the disk path is formatted as
storage_name:vm-id-disk-0
. - User Permissions: Ensure the user has the necessary permissions to create VMs.