Proxmox VE Automation Skill
This skill provides comprehensive automation for Proxmox VE (Virtual Environment) management through its REST API.
Overview
Proxmox VE is an open-source server virtualization management platform that supports:
- - QEMU/KVM Virtual Machines (VMs)
- LXC Containers (Linux Containers)
- Storage management
- Cluster management
- Backup and snapshots
API Basics
- Default port:
8006
- Custom port can be configured in PVE settings
- - Authentication: Ticket-based (cookie) or API Token-based
- Data format: JSON
- API Explorer: INLINECODE2
Custom API Port
PVE API port can be customized from the default 8006:
CODEBLOCK0
Using custom port in code:
CODEBLOCK1
Authentication Methods
Method 1: API Token (Recommended for automation)
API tokens provide stateless access without CSRF tokens:
CODEBLOCK2
Creating an API Token:
- 1. Web UI: Datacenter → Permissions → API Tokens → Add
- Or via API: INLINECODE3
Method 2: Ticket-based Authentication
For interactive sessions or when API tokens aren't available:
CODEBLOCK3
Tickets expire after 2 hours. Renew by passing the old ticket as password.
Common Operations
Node Operations
CODEBLOCK4
VM (QEMU) Operations
CODEBLOCK5
LXC Container Operations
CODEBLOCK6
VM/Container Creation
Create a VM from template:
CODEBLOCK7
Create an LXC container:
CODEBLOCK8
Template Management
CODEBLOCK9
Cloud-Init Configuration
Cloud-init allows automatic VM initialization on first boot:
CODEBLOCK10
Important: Cloud-init requires a cloud-init capable image (Ubuntu Cloud Images, Debian Cloud, etc.) and a configured CD/DVD drive (usually ide2) for the config drive.
Snapshots and Backups
CODEBLOCK11
Storage Operations
CODEBLOCK12
Network Configuration
CODEBLOCK13
Cluster Operations
CODEBLOCK14
Working with Tasks
Most write operations return a task UPID (Unique Process ID). Poll the task status to track completion:
CODEBLOCK15
Best Practices
1. VM ID Management
- - VM IDs must be unique across the cluster
- Common convention: 100-999 for VMs, 100000+ for containers (though not required)
- Check available VM IDs:
GET /cluster/resources and filter by type
2. Resource Allocation
- - Always specify memory and cores when creating VMs
- Leave overhead for the host (don't allocate 100% of resources)
- Use ballooning for dynamic memory:
balloon: 1024 with INLINECODE6
3. Network Configuration
- - Use virtio network drivers for best performance
- Configure bridges properly before creating VMs
- For VLANs: INLINECODE7
4. Storage
- - Use appropriate storage types:
-
local: Directory storage
-
local-lvm: LVM thin provisioning
-
zfs: ZFS storage
-
nfs,
iscsi,
ceph: Network storage
5. Security
- - Use API tokens with limited permissions instead of root
- Enable 2FA for user accounts
- Use firewall rules: INLINECODE14
Common Workflows
Create a VM from Cloud Image
- 1. Download cloud image ISO to storage
- Create VM with cloud-init drive
- Configure cloud-init parameters
- Start VM
Clone a Template VM
CODEBLOCK16
Bulk Operations
For multiple VMs, loop through and call API for each:
- - Always add delays between operations to avoid overwhelming the API
- Check task completion before proceeding
- Handle errors gracefully
API Parameters Reference
VM Creation Parameters (POST /nodes/{node}/qemu)
Required Parameters:
- -
vmid (integer): Unique VM ID (100-999999999), must be unique across cluster - INLINECODE16 (string): VM name (must be valid DNS name)
Resource Parameters:
- -
memory (integer): RAM in MB. Examples: 512, 1024, 2048, 4096, 8192 - INLINECODE18 (integer): Minimum memory for ballooning in MB (optional, enables memory overcommit)
- INLINECODE19 (integer): Number of CPU cores per socket (1-128)
- INLINECODE20 (integer): Number of CPU sockets (1-4)
- INLINECODE21 (string): CPU type. Common values:
host, kvm64, qemu64, INLINECODE25 - INLINECODE26 (integer): Number of hotplugged vCPUs (defaults to cores*sockets)
Storage Parameters:
- -
scsi0, scsi1, virtio0, virtio1, ide0, ide1, sata0, etc.
- Format:
<storage>:<size> or
<storage>:<volume-name>
- Examples:
local-lvm:32,
zfs:vm-disk-001,
local-lvm:64,format=raw
- -
efidisk0 (string): EFI system partition. Example: INLINECODE40
Network Parameters:
- -
net0, net1, etc. (string): Network interface config
- Format:
<model>,<options>
- Models:
virtio (recommended),
e1000,
rtl8139,
vmxnet3
- Examples:
-
virtio,bridge=vmbr0 (basic bridge)
-
virtio,bridge=vmbr0,tag=100 (VLAN 100)
-
virtio,bridge=vmbr0,ip=192.168.1.10/24,gw=192.168.1.1 (static IP via cloud-init)
Boot/Media Parameters:
- -
ostype (string): OS type for optimizations
- Linux:
l24 (2.4),
l26 (2.6+),
other
- Windows:
wxp,
w2k,
w2k3,
w2k8,
wvista,
win7,
win8,
win10,
win11
- -
ide2 (string): CD/DVD drive or cloud-init drive
- ISO:
local:iso/ubuntu-22.04.iso,media=cdrom
- Cloud-init:
local-lvm:cloudinit,media=cdrom
- -
boot (string): Boot order. Example: INLINECODE68 - INLINECODE69 (string):
seabios (default) or ovmf (UEFI)
Cloud-Init Parameters:
- -
ciuser (string): Default username - INLINECODE73 (string): Default password (will be hashed)
- INLINECODE74 (string): Path to custom cloud-init config
- INLINECODE75 (string):
configdrive2 or INLINECODE77 - INLINECODE78 (string): URL-encoded SSH public keys
- INLINECODE79 ,
ipconfig1 (string): Network config
- DHCP:
ip=dhcp
- Static:
ip=192.168.1.10/24,gw=192.168.1.1
- -
nameserver (string): DNS servers (comma-separated) - INLINECODE84 (string): DNS search domain
LXC Container Parameters (POST /nodes/{node}/lxc)
Required Parameters:
- -
vmid (integer): Unique container ID - INLINECODE86 (string): Template path. Example: INLINECODE87
Resource Parameters:
- -
memory (integer): RAM in MB - INLINECODE89 (integer): Swap in MB
- INLINECODE90 (integer): CPU cores
- INLINECODE91 (number): CPU limit (0-128, 0=unlimited)
- INLINECODE92 (integer): CPU weight (0-500000, default: 1024)
Storage Parameters:
- -
rootfs (string): Root filesystem. Format: INLINECODE94
- Example:
local-lvm:8,
local-lvm:16,mountoptions=noatime
- -
mp0, mp1, etc. (string): Mount points
- Example:
local-lvm:32,mp=/data, INLINECODE100
Network Parameters:
- -
net0, net1, etc. (string): Network config
- Format:
name=<ifname>,bridge=<br>[,options]
- Examples:
-
name=eth0,bridge=vmbr0,ip=dhcp
-
name=eth0,bridge=vmbr0,ip=192.168.1.10/24,gw=192.168.1.1
-
name=eth0,bridge=vmbr0,tag=100,ip=dhcp (VLAN)
System Parameters:
- -
hostname (string): Container hostname - INLINECODE108 (string): Root password
- INLINECODE109 (string): SSH public keys (newline-separated)
- INLINECODE110 (boolean): Create unprivileged container (0/1)
- INLINECODE111 (string): Enabled features. Example: INLINECODE112
- INLINECODE113 (boolean): Start on boot (0/1)
- INLINECODE114 (string): Startup order and delay. Example: INLINECODE115
Safety and Resource Protection
1. Check Available Resources Before Creation
Always verify node resources before creating VMs/containers:
CODEBLOCK17
2. Get Cluster-Wide Resource Usage
CODEBLOCK18
3. Safe VM/Container Creation Helper
CODEBLOCK19
4. Dry-Run Mode for Bulk Operations
CODEBLOCK20
5. Safe Defaults Checklist
When generating PVE automation code, ensure these safeguards are included:
- 1. Pre-flight Checks:
- Verify VMID is unique (not already in use)
- Check node has sufficient resources
- Validate storage exists and has space
- 2. Resource Limits:
- Set reasonable defaults (2GB RAM, 2 cores, 32GB disk)
- Warn if allocating >50% of node resources to single VM
- Use ballooning for memory overcommit
- 3. Operation Safety:
- Always use dry-run for bulk operations
- Confirm destructive operations (delete, rollback)
- Check VM is stopped before deletion or disk modification
- 4. Error Handling:
- Handle 409 Conflict (VMID exists)
- Handle 507 Insufficient Storage
- Handle 595 PVE errors with meaningful messages
- 5. Network Safety:
- Validate bridge exists before creating VM
- Warn about IP conflicts (manual check required)
- Use VLAN tags appropriately
Error Handling
Common HTTP status codes:
- -
400: Bad request (missing/invalid parameters) - INLINECODE117 : Unauthorized (invalid credentials)
- INLINECODE118 : Forbidden (insufficient permissions)
- INLINECODE119 : Resource not found
- INLINECODE120 : Conflict (VMID already exists)
- INLINECODE121 : Internal server error
- INLINECODE122 : Insufficient Storage
- INLINECODE123 : PVE specific errors (check response body)
Error response format:
CODEBLOCK21
Common Error Patterns and Solutions
| Error | Cause | Solution |
|---|
| INLINECODE124 | VMID in use | Query /cluster/resources to find next available |
| INLINECODE126 |
Node overloaded | Check
memory.free before creation |
|
unable to create disk: out of space | Storage full | Check storage usage before creation |
|
bridge 'vmbrX' does not exist | Invalid network config | List
/nodes/{node}/network to get valid bridges |
|
OSTYPE not set | Missing OS type | Add
ostype parameter (l26 for Linux, win10 for Windows) |
|
timeout | Task taking too long | Increase timeout for slow operations (template download) |
Python Example
CODEBLOCK22
Advanced Operations
1. High Availability (HA)
HA ensures VMs/containers restart on another node if the current node fails.
HA Resources:
CODEBLOCK23
HA Resources (VMs to protect):
CODEBLOCK24
HA Status and Control:
CODEBLOCK25
2. Live Migration
Migrate running VMs between nodes (requires shared storage or storage migration).
CODEBLOCK26
Migration Requirements:
- - Shared storage (VM disks accessible from both nodes) OR INLINECODE134
- Same network bridges on both nodes (vmbr0, etc.)
- CPUs compatible (or use
cpu: kvm64 for portability) - For live migration: Intel-Intel or AMD-AMD (same vendor)
3. PCI/GPU Passthrough
Pass physical PCI devices (GPUs, NICs) to VMs.
Prerequisites:
- - Enable IOMMU in BIOS and kernel
- Load VFIO modules
- Blacklist GPU drivers on host
Configuration:
CODEBLOCK27
USB Passthrough:
CODEBLOCK28
4. Backup and Restore
Backup (vzdump):
CODEBLOCK29
Restore:
CODEBLOCK30
Backup Management:
CODEBLOCK31
5. Scheduled Jobs
Automate backups and other tasks.
CODEBLOCK32
Cron Expression Examples:
- -
"0 2 * * *": Daily at 2 AM - INLINECODE137 : Weekly on Sunday midnight
- INLINECODE138 : Every 6 hours
- INLINECODE139 : Monthly on 1st at 3 AM
6. Permission Management
Users:
CODEBLOCK33
Groups:
CODEBLOCK34
Roles:
CODEBLOCK35
ACLs (Access Control):
CODEBLOCK36
Pools:
CODEBLOCK37
7. Notifications (Webhooks)
Configure notifications for automation workflows.
CODEBLOCK38
Notification Events:
- - Backup jobs:
vzdump, INLINECODE141 - Replication: INLINECODE142
- HA events: Resource migration, failover
- System: Updates, errors
8. Replication
Replicate VM disks to another node for disaster recovery.
CODEBLOCK39
9. Firewall
Host and VM-level firewall configuration.
Host Firewall:
CODEBLOCK40
VM Firewall:
CODEBLOCK41
Security Groups:
CODEBLOCK42
10. Storage Configuration
Storage Types:
| Type | Description | Common Options |
|---|
| INLINECODE143 | Directory on local filesystem | path, prune-backups |
| INLINECODE144 |
LVM logical volumes | vgname, base, safe-remove |
|
lvmthin | LVM thin provisioning | vgname, thinpool |
|
zfspool | ZFS pool | pool, sparse, blocksize |
|
nfs | NFS share | server, export, options |
|
cifs | SMB/CIFS share | server, share, username, password |
|
iscsi | iSCSI target | portal, target |
|
cephfs | CephFS | monhost, fs-name, userid |
|
rbd | Ceph RBD | monhost, pool, userid |
|
glusterfs | GlusterFS | server, volume, transport |
Storage Operations:
CODEBLOCK43
11. Certificate Management
CODEBLOCK44
12. Node Maintenance
CODEBLOCK45
References
Proxmox VE 自动化技能
本技能通过Proxmox VE的REST API提供全面的自动化管理功能。
概述
Proxmox VE是一个开源服务器虚拟化管理平台,支持:
- - QEMU/KVM虚拟机(VM)
- LXC容器(Linux容器)
- 存储管理
- 集群管理
- 备份与快照
API基础
- - 基础URL:https://:/api2/json/
- 默认端口:8006
- 可在PVE设置中配置自定义端口
- - 认证方式:基于票据(Cookie)或API令牌
- 数据格式:JSON
- API浏览器:https://:/pve-docs/api-viewer/
自定义API端口
PVE API端口可从默认的8006进行自定义:
bash
检查当前API端口配置
grep port /etc/default/pveproxy
或通过API:GET /nodes/{node}/services
在代码中使用自定义端口:
python
选项1:在主机中包含端口
client = PVEClient(host=pve.example.com:8443, ...)
选项2:显式端口参数
class PVEClient:
def
init(self, host, port=8006, ...):
self.base_url = fhttps://{host}:{port}/api2/json
选项3:环境变量
port = os.environ.get(PVE_PORT, 8006)
认证方法
方法1:API令牌(推荐用于自动化)
API令牌提供无状态访问,无需CSRF令牌:
bash
请求头格式
Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID
示例
curl -H Authorization: PVEAPIToken=root@pam!automation=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee \
https://pve.example.com:8006/api2/json/nodes
创建API令牌:
- 1. Web界面:数据中心 → 权限 → API令牌 → 添加
- 或通过API:POST /access/users/{userid}/token/{tokenid}
方法2:基于票据的认证
适用于交互式会话或无法使用API令牌时:
bash
1. 获取票据和CSRF令牌
curl -k -d username=root@pam --data-urlencode password=secret \
https://pve.example.com:8006/api2/json/access/ticket
响应:{ data: { ticket: ..., CSRFPreventionToken: ... } }
2. 在后续请求中使用票据
curl -k -b PVEAuthCookie=
\
-H CSRFPreventionToken: \
https://pve.example.com:8006/api2/json/nodes
票据有效期2小时。通过将旧票据作为密码传递来续期。
常见操作
节点操作
bash
列出所有节点
GET /nodes
获取节点状态
GET /nodes/{node}/status
获取节点资源(CPU、内存、磁盘使用情况)
GET /nodes/{node}/resources
虚拟机(QEMU)操作
bash
列出节点上的虚拟机
GET /nodes/{node}/qemu
获取虚拟机状态
GET /nodes/{node}/qemu/{vmid}/status/current
启动虚拟机
POST /nodes/{node}/qemu/{vmid}/status/start
停止虚拟机(优雅关机)
POST /nodes/{node}/qemu/{vmid}/status/shutdown
停止虚拟机(强制/断电)
POST /nodes/{node}/qemu/{vmid}/status/stop
重置虚拟机
POST /nodes/{node}/qemu/{vmid}/status/reset
删除虚拟机
DELETE /nodes/{node}/qemu/{vmid}
LXC容器操作
bash
列出容器
GET /nodes/{node}/lxc
获取容器状态
GET /nodes/{node}/lxc/{vmid}/status/current
启动容器
POST /nodes/{node}/lxc/{vmid}/status/start
停止容器
POST /nodes/{node}/lxc/{vmid}/status/stop
删除容器
DELETE /nodes/{node}/lxc/{vmid}
虚拟机/容器创建
从模板创建虚拟机:
bash
POST /nodes/{node}/qemu
参数:
- - vmid:唯一虚拟机ID(100-999999999)
- name:虚拟机名称
- memory:内存(MB)
- cores:CPU核心数
- sockets:CPU插槽数
- cpu:CPU类型(host、kvm64等)
- net0:网络配置(例如virtio,bridge=vmbr0)
- ide2:CD/DVD驱动器或cloud-init(例如local:iso/ubuntu-22.04.iso,media=cdrom)
- scsi0:磁盘(例如local-lvm:32,format=raw)
- ostype:操作系统类型(l26适用于Linux 2.6+,win11适用于Windows 11)
创建LXC容器:
bash
POST /nodes/{node}/lxc
参数:
- - vmid:唯一容器ID
- hostname:容器主机名
- memory:内存(MB)
- swap:交换空间(MB)
- cores:CPU核心数
- ostemplate:模板路径(例如local:vztmpl/debian-12-standard12.0-1amd64.tar.zst)
- storage:rootfs的存储
- rootfs:根文件系统大小(例如local-lvm:8)
- net0:网络配置(例如name=eth0,bridge=vmbr0,ip=dhcp)
- password:root密码(或使用ssh-public-keys)
模板管理
bash
列出存储上的可用模板
GET /nodes/{node}/storage/{storage}/content?content=vztmpl
从Proxmox VE仓库下载模板
POST /nodes/{node}/storage/{storage}/download-url
参数:
- - url:模板URL
- filename:目标文件名
- content:容器模板为vztmpl,ISO镜像为iso
列出可供下载的容器模板
GET /nodes/{node}/aplinfo
从aplinfo下载(官方模板)
POST /nodes/{node}/storage/{storage}/aplinfo
Cloud-Init配置
Cloud-init允许虚拟机在首次启动时自动初始化:
bash
设置cloud-init参数
POST /nodes/{node}/qemu/{vmid}/config
参数:
- - cicustom:自定义cloud-init配置(可选)
- ciuser:默认用户名
- cipassword:默认用户密码
- citype:配置驱动器类型(configdrive2、nocloud)
- ciupgrade:启动时运行软件包升级(0/1)
- sshkeys:公共SSH密钥(URL编码)
- ipconfig0:IP配置(例如ip=192.168.1.100/24,gw=192.168.1.1)
- ipconfig1:额外网络配置
- nameserver:DNS服务器
- searchdomain:DNS搜索域
重要提示:Cloud-init需要支持cloud-init的镜像(Ubuntu Cloud Images、Debian Cloud等)以及已配置的CD/DVD驱动器(通常为ide2)用于配置驱动器。
快照与备份
bash
列出快照
GET /nodes/{node}/qemu/{vmid}/snapshot
创建快照
POST /nodes/{node}/qemu/{vmid}/snapshot
参数:
- - snapname:快照名称
- description:可选描述
- vmstate:包含RAM状态(1/0)
回滚到快照
POST /nodes/{node}/qemu/{vmid}/snapshot/{snapname}/rollback
删除快照
DELETE /nodes/{node}/qemu/{vmid}/snapshot/{snapname}
创建备份
POST /nodes/{node}/vzdump
参数:
- - vmid:要备份的虚拟机ID
- storage:目标存储
- mode:快照/挂起/停止
- compress:压缩级别(0-9)
存储操作
bash
列出存储
GET /storage
获取存储状态
GET /nodes/{node}/storage/{storage}/status
列出存储内容
GET /nodes/{node}/storage/{storage}/content
上传文件到存储(ISO、模板)
POST /nodes/{node}/storage/{storage}/upload
Content-Type: multipart/form-data
网络配置
bash
列出节点上的网络接口
GET /nodes/{node}/network
创建网络桥接
POST /nodes/{