[SOLVED] Proxmox PHP API - Create lxc container.

Sharminte

New Member
Jan 3, 2018
2
0
1
31
Hello, I have a problem with creation of LXC container via PHP.

API is connectig correctly with pve, because i can start / stop existing container, i am able to list nodes, and check what VIM is next. but while creating LXC i receive bool(false) value.

My code look's like that:
PHP:
<?php
require ('pve-1/pve2_api.class.php');

$pve2 = new PVE2_API("SERVER_ADRESS", "USERNAME", "pam", "PASSWORD");

$pve2->login();

if ($pve2->login()) {

    # Get first node name.
    $nodes = $pve2->get_node_list();
    $first_node = $nodes[0];
    unset($nodes);

$nextVMID = $pve2->get_next_vmid();


    $new_container_settings = array();
    $new_container_settings['ostemplate'] = "local:vztmpl/ubuntu-16.04-standard_16.04-1_amd64.tar.gz";
    $new_container_settings['vmid'] = $nextVMID;
    $new_container_settings['cpus'] = "2";
    $new_container_settings['description'] = "Test VM using Proxmox 2.0 API";
    $new_container_settings['disk'] = "8";
    $new_container_settings['hostname'] = "testapi.domain.tld";
    $new_container_settings['memory'] = "1024";
    $new_container_settings['nameserver'] = "10.11.12.252";
    // print_r($new_container_settings);
    print("---------------------------\n");

    var_dump($pve2->post("/nodes/".$first_node."/lxc/", $new_container_settings));
    print("\n\n");

// print_r($pve2->post("/nodes/".$first_node."/lxc/107/status/start", array()));     // start lxc
// print_r($pve2->post("/nodes/".$first_node."/lxc/107/status/stop", array()));     // stop lxc
} else {
    print("Login to Proxmox Host failed.\n");
    exit;
}

Any Idea what could i do wrong ?
 
if you have problems with an api client, i would ask the maintainer/creator of that client (ideally you would get the error message from the api back somehow)

in this case i would guess it is due to the parameter 'disk' which does not exist for containers but it should be 'rootfs' maybe?
 
Thanks :)
So basicly to work correctly I have made these changes:

PHP:
$new_container_settings = array();
    $new_container_settings['ostemplate'] = "local:vztmpl/ubuntu-16.04-standard_16.04-1_amd64.tar.gz";
    $new_container_settings['vmid'] = $nextVMID;
    $new_container_settings['hostname'] = "test";
    $new_container_settings['cores'] = "1";
    $new_container_settings['memory'] = "512";
    $new_container_settings['swap'] = "512";
    $new_container_settings['rootfs'] = "local-zfs:20";                                                        
    $new_container_settings['net0'] = "bridge=vmbr0,name=eth0,ip=dhcp,ip6=dhcp";                              
    $new_container_settings['searchdomain'] = "8.8.8.8";                                                        
    $new_container_settings['nameserver'] = "8.8.4.4";
 
Hy guys, i use the same method to start, stop suspend an ct / lxc but i got a problem i truly dont fiind how to destroy an lxc / ct :(

this is my code, i use it for whmcs to some product we made, we have proxmox 3 and 5 the start/stop/suspend works perfect but terminate/destroy no and i dont figure out how to do it.

require("pve2_api.class.php");

....

$pve2 = new PVE2_API("$serverip", "$serverusername", "pam", "$serverpassword");
$pve2->login();

if ($pve2->login()) {
$nodes = $pve2->get_node_list();
$first_node = $nodes[0];
$versiune = $pve2->get_version();

if ("$versiune" < "5") {
print_r($pve2->post("/nodes/$first_node/openvz/$vmid/delete", array()));
} else {
print_r($pve2->post("/nodes/$first_node/lxc/$vmid/delete", array()));
}


i tryed with delete and destroy and nothink.

Thank you.

Edit: Founded the solution is instead of post put delete :)
 
Last edited:
Founded the problem, or i didnt understood the api ori the api is explained wrong.

$data = array(
'newid' => "$nextVMID",
'hostname' => '$serviceid",
'full' => "1"
);

print_r($pve2->post("/nodes/$first_node/lxc/1000/clone", $data));

if you dont use full you wil get error
 

Attachments

  • lol.png
    lol.png
    15.8 KB · Views: 30
Last edited:
Hello guys, i`m back with a small problem i can't make it, i use this

$info = $pve2->get("/nodes/$serverip/lxc/$IDProxMox/status/current");
It works but it shows me the entire lxc details and i dont know how to exploade to extract only the name of the vm.
On the documentation i didnt fiind any ouput details

Can anyone give me a hand please ?
Thank you
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!