Alat baris perintah

Untuk alat baris perintah, tolong merujuk ke man pages.

API

LXC dikirimkan dengan C API yang stabil dan banyak bindings. API ini stabil dan diberikan versi yang baik. Kami mungkin membuat penambahan kedalam liblxcl API didalam rilis LXC namun tidak menghapus atau merubah symbol yang sudah ada tanpa memanggil ini liblxc2.

Versi pertama LXC untuk dikirim dengan stabil API adalah LXC 1.0.0. Hanya simbol yang terdaftar didalam lxccontainer.h adalah bagian dari API, selainnya adalah internal ke LXC dan dapat berubah kapan saja.

C

Seperti yang disebutkan diatas, lxccontainer.h adalah C publik API kami.

Beberapa contoh terbaik penggunaan API adalah binding dan alat LXC itu sendiri.

Kami juga memiliki dokumentasi API terbaru untuk git master saat ini disini.

Dan sekarang sebuah contoh sederhana terkait bagaimana untuk menggunakan API untuk create, start, stop dan destroy sebuah container.

#include <stdio.h>

#include <lxc/lxccontainer.h>

int main() {
    struct lxc_container *c;
    int ret = 1;

    /* Setup container struct */
    c = lxc_container_new("apicontainer", NULL);
    if (!c) {
        fprintf(stderr, "Failed to setup lxc_container struct\n");
        goto out;
    }

    if (c->is_defined(c)) {
        fprintf(stderr, "Container already exists\n");
        goto out;
    }

    /* Create the container */
    if (!c->createl(c, "download", NULL, NULL, LXC_CREATE_QUIET,
                    "-d", "ubuntu", "-r", "trusty", "-a", "i386", NULL)) {
        fprintf(stderr, "Failed to create container rootfs\n");
        goto out;
    }

    /* Start the container */
    if (!c->start(c, 0, NULL)) {
        fprintf(stderr, "Failed to start the container\n");
        goto out;
    }

    /* Query some information */
    printf("Container state: %s\n", c->state(c));
    printf("Container PID: %d\n", c->init_pid(c));

    /* Stop the container */
    if (!c->shutdown(c, 30)) {
        printf("Failed to cleanly shutdown the container, forcing.\n");
        if (!c->stop(c)) {
            fprintf(stderr, "Failed to kill the container.\n");
            goto out;
        }
    }

    /* Destroy the container */
    if (!c->destroy(c)) {
        fprintf(stderr, "Failed to destroy the container.\n");
        goto out;
    }

    ret = 0;
out:
    lxc_container_put(c);
    return ret;
}

Python

Python binding biasanya sangat dekat dengan C API kecuali dalam bagian export properti object alih-alih structs.

Binding dibuat dalam 2 bagian, raw "_lxc" ekstensi C dan "lxc" pyhton overlay yang menyediakan pengalaman pengguna yang lebih ditingkatkan.

Membuat sebuah container bernama "test" dapat dilakukkan dengan:

import lxc
container = lxc.Container("test")

Untuk convenience, network dapat diakses sebagai list (dan dimodifikasi seperti itu juga):

container.network[0].ipv4 = "10.0.3.50"
container.network[0].ipv4_gateway = "10.0.3.1"

Konfigurasi input Multi-value direpresentasikan sebagai list:

container.get_config_item("lxc.cap.drop")
    ['mac_admin', 'mac_override', 'sys_time', 'sys_module']

container.append_config_item("lxc.cap.drop", "net_admin")
    True

container.get_config_item("lxc.cap.drop")
    ['mac_admin', 'mac_override', 'sys_time', 'sys_module', 'net_admin']

container.set_config_item("lxc.cap.drop", ["mac_admin", "mac_override"])
    True

container.get_config_item("lxc.cap.drop")
    ['mac_admin', 'mac_override'])

Dan sekarang untuk contoh end-to-end yang sudah selesai di C:

#!/usr/bin/python3
import lxc
import sys

# Setup the container object
c = lxc.Container("apicontainer")
if c.defined:
    print("Container already exists", file=sys.stderr)
    sys.exit(1)

# Create the container rootfs
if not c.create("download", lxc.LXC_CREATE_QUIET, {"dist": "ubuntu",
                                                   "release": "trusty",
                                                   "arch": "i386"}):
    print("Failed to create the container rootfs", file=sys.stderr)
    sys.exit(1)

# Start the container
if not c.start():
    print("Failed to start the container", file=sys.stderr)
    sys.exit(1)

# Query some information
print("Container state: %s" % c.state)
print("Container PID: %s" % c.init_pid)

# Stop the container
if not c.shutdown(30):
    print("Failed to cleanly shutdown the container, forcing.")
    if not c.stop():
        print("Failed to kill the container", file=sys.stderr)
        sys.exit(1)

# Destroy the container
if not c.destroy():
    print("Failed to destroy the container.", file=sys.stderr)
    sys.exit(1)

Sebuah fitur yang bagus di python binding adalah kemampuan untuk menjalankan fungsi didalam container's context seperti pada contoh dibawah adalah script mengupdate semua container Anda:

#!/usr/bin/python3
import lxc
import sys

for container in lxc.list_containers(as_object=True):
    # Start the container (if not started)
    started = False
    if not container.running:
        if not container.start():
            continue
        started = True

    if not container.state == "RUNNING":
        continue

    # Wait for connectivity
    if not container.get_ips(timeout=30):
        continue

    # Run the updates
    container.attach_wait(lxc.attach_run_command,
                          ["apt-get", "update"])
    container.attach_wait(lxc.attach_run_command,
                          ["apt-get", "dist-upgrade", "-y"])

    # Shutdown the container
    if started:
        if not container.shutdown(30):
            container.stop()