命令行工具¶
有关命令行工具,请参阅手册页。
API¶
LXC 附带一个稳定的 C API 和一堆绑定。该 API 是稳定的并且正确地进行了版本控制。我们可能会在 LXC 版本中向 liblxc1 API 添加内容,但不会在不将其称为 liblxc2 的情况下删除或更改现有符号。
第一个附带稳定 API 的 LXC 版本是 LXC 1.0.0。只有 lxccontainer.h 中列出的符号属于 API,其他所有内容都是 LXC 的内部内容,并且可以在任何时候更改。
C¶
如上所述,lxccontainer.h 是我们的公共 C API。
API 使用的一些最佳示例是绑定和 LXC 工具本身。
我们还提供了当前 git master 的最新 API 文档此处。
现在,这是一个关于如何使用 API 创建、启动、停止和销毁容器的简单示例
#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 绑定通常非常接近 C API,除了它导出适当的对象而不是结构的部分。
绑定分为两个部分,原始的“_lxc” C 扩展和提供改进的用户体验的“lxc” python 覆盖层。
加载名为“test”的容器可以使用
import lxc container = lxc.Container("test")
为方便起见,网络可以作为列表访问(并以这种方式修改)
container.network[0].ipv4 = "10.0.3.50" container.network[0].ipv4_gateway = "10.0.3.1"
多值配置条目表示为列表
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'])
现在,与在 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)
python 绑定的一个很棒的功能是在容器的上下文中运行函数的能力,如下面的更新所有容器的脚本示例所示
#!/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()