Container Management - FreshPerf/PVE4J GitHub Wiki
This guide covers LXC container management using PVE4J.
- Listing Containers
- Container Information
- Creating Containers
- Lifecycle Operations
- Container Configuration
- Cloning Containers
- Migration
- Templates
- Snapshots
- Network Interfaces
- Feature Availability
- Working with Multiple Containers
- Common Use Cases
import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcIndex;
import java.util.List;
try {
List<PveLxcIndex> containers = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.getIndex()
.execute();
for (PveLxcIndex container : containers) {
System.out.printf("VMID: %d, Name: %s, Status: %s%n",
container.getVmid(), container.getName(), container.getStatus());
}
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.cluster.resources.PveClusterResources;
import java.util.List;
try {
List<PveClusterResources> resources = proxmox.getCluster()
.getResources()
.execute();
for (PveClusterResources resource : resources) {
if ("lxc".equals(resource.getType())) {
System.out.printf("VMID: %d, Name: %s, Node: %s, Status: %s%n",
resource.getVmid(), resource.getName(),
resource.getNode(), resource.getStatus());
}
}
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcStatus;
try {
PveLxcStatus status = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getStatus()
.execute();
System.out.println("Status: " + status.getStatus());
System.out.println("CPU Usage: " + status.getCpu());
System.out.println("Memory: " + status.getMem() + " / " + status.getMaxmem());
System.out.println("Swap: " + status.getSwap() + " / " + status.getMaxswap());
System.out.println("Disk: " + status.getDisk() + " / " + status.getMaxdisk());
System.out.println("Uptime: " + status.getUptime() + " seconds");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcConfig;
try {
PveLxcConfig config = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getConfig()
.execute();
System.out.println("Hostname: " + config.getHostname());
System.out.println("Memory: " + config.getMemory());
System.out.println("Swap: " + config.getSwap());
System.out.println("Cores: " + config.getCores());
System.out.println("Rootfs: " + config.getRootfs());
System.out.println("OS Type: " + config.getOstype());
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
List<PveLxcPendingChange> pending = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getPending()
.execute();
for (PveLxcPendingChange change : pending) {
System.out.println("Pending: " + change);
}
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcCreateOptions;
try {
PveLxcCreateOptions options = PveLxcCreateOptions.builder()
.ostemplate("local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst")
.hostname("my-container")
.memory(1024)
.swap(512)
.cores(2)
.rootfs("local-lvm:8")
.password("secure-password")
.unprivileged(true)
.onboot(true)
.start(true)
.build();
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.create(200, options)
.waitForCompletion(proxmox)
.execute();
System.out.println("Container created!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveLxcCreateOptions options = PveLxcCreateOptions.builder()
.ostemplate("local:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst")
.hostname("ssh-container")
.memory(512)
.cores(1)
.rootfs("local-lvm:4")
.sshPublicKeys("ssh-rsa AAAA... user@host")
.nameserver("8.8.8.8")
.searchdomain("example.com")
.unprivileged(true)
.build();
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.create(201, options)
.waitForCompletion(proxmox)
.execute();
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.start()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container started!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.stop()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container stopped!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.shutdown()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container shutdown!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}// Reboot
container.reboot().waitForCompletion(proxmox).execute();
// Suspend
container.suspend().waitForCompletion(proxmox).execute();
// Resume
container.resume().waitForCompletion(proxmox).execute();try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.delete()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container deleted!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcConfigUpdateOptions;
try {
PveLxcConfigUpdateOptions options = PveLxcConfigUpdateOptions.builder()
.memory(2048)
.swap(1024)
.cores(4)
.build();
proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.updateConfig(options)
.execute();
System.out.println("Container configuration updated!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.cloneContainer(201)
.waitForCompletion(proxmox)
.execute();
System.out.println("Container cloned!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcCloneOptions;
try {
PveLxcCloneOptions options = PveLxcCloneOptions.builder()
.hostname("cloned-container")
.storage("local-lvm")
.full(true)
.build();
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.cloneContainer(201, options)
.waitForCompletion(proxmox)
.execute();
System.out.println("Container cloned with options!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.migrate("pve-node-02")
.waitForCompletion(proxmox)
.execute();
System.out.println("Container migrated!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.PveLxcMigrateOptions;
try {
PveLxcMigrateOptions options = PveLxcMigrateOptions.builder()
.online(true)
.targetStorage("local-lvm")
.restart(true)
.timeout(120)
.build();
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.migrate("pve-node-02", options)
.waitForCompletion(proxmox)
.execute();
System.out.println("Container migrated with options!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.template()
.execute();
System.out.println("Container converted to template!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
List<PveLxcSnapshot> snapshots = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getSnapshots()
.list()
.execute();
for (PveLxcSnapshot snapshot : snapshots) {
System.out.println("Snapshot: " + snapshot.getName());
}
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import fr.freshperf.pve4j.entities.nodes.node.lxc.snapshot.PveLxcSnapshotCreateOptions;
try {
PveLxcSnapshotCreateOptions options = PveLxcSnapshotCreateOptions.builder()
.description("Before upgrade")
.build();
PveTask task = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getSnapshots()
.create("pre-upgrade", options)
.waitForCompletion(proxmox)
.execute();
System.out.println("Snapshot created!");
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}var snapItem = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getSnapshots()
.get("pre-upgrade");
// Get snapshot configuration
PveLxcSnapshotConfig config = snapItem.getConfig().execute();
// Rollback
snapItem.rollback().waitForCompletion(proxmox).execute();
// Update description
snapItem.update(PveLxcSnapshotUpdateOptions.builder()
.description("Updated description")
.build()).execute();
// Delete
snapItem.delete().waitForCompletion(proxmox).execute();try {
List<PveLxcInterface> interfaces = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getInterfaces()
.execute();
for (PveLxcInterface iface : interfaces) {
System.out.println("Interface: " + iface);
}
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}try {
PveLxcFeatureAvailability feature = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getFeature("snapshot") // "snapshot", "clone", or "copy"
.execute();
System.out.println("Feature available: " + feature);
// Check with a specific snapshot
PveLxcFeatureAvailability cloneFeature = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(200)
.getFeature("clone", "my-snapshot")
.execute();
} catch (ProxmoxAPIError | InterruptedException e) {
e.printStackTrace();
}import java.util.Arrays;
import java.util.List;
List<Integer> containerIds = Arrays.asList(200, 201, 202, 203);
for (int ctid : containerIds) {
try {
proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(ctid)
.start()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container " + ctid + " started");
} catch (ProxmoxAPIError | InterruptedException e) {
System.err.println("Failed to start container " + ctid + ": " + e.getMessage());
}
}public class ContainerManager {
private final Proxmox proxmox;
private final String nodeName;
public ContainerManager(Proxmox proxmox, String nodeName) {
this.proxmox = proxmox;
this.nodeName = nodeName;
}
public void ensureRunning(int ctid) throws ProxmoxAPIError, InterruptedException {
PveLxcStatus status = proxmox.getNodes()
.get(nodeName)
.getLxc()
.get(ctid)
.getStatus()
.execute();
if (!"running".equals(status.getStatus())) {
proxmox.getNodes()
.get(nodeName)
.getLxc()
.get(ctid)
.start()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container " + ctid + " started");
}
}
public void ensureStopped(int ctid) throws ProxmoxAPIError, InterruptedException {
PveLxcStatus status = proxmox.getNodes()
.get(nodeName)
.getLxc()
.get(ctid)
.getStatus()
.execute();
if ("running".equals(status.getStatus())) {
proxmox.getNodes()
.get(nodeName)
.getLxc()
.get(ctid)
.shutdown()
.waitForCompletion(proxmox)
.execute();
System.out.println("Container " + ctid + " shutdown initiated");
}
}
}public boolean isContainerHealthy(int ctid) {
try {
PveLxcStatus status = proxmox.getNodes()
.get("pve-node-01")
.getLxc()
.get(ctid)
.getStatus()
.execute();
boolean isRunning = "running".equals(status.getStatus());
boolean memoryOk = (double) status.getMem() / status.getMaxmem() < 0.9;
boolean diskOk = (double) status.getDisk() / status.getMaxdisk() < 0.9;
return isRunning && memoryOk && diskOk;
} catch (Exception e) {
return false;
}
}- VM Management - QEMU VM operations
- Snapshot Management - Detailed snapshot guide
- Cluster Operations - Cluster-wide management
- Storage Management - Storage operations