首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python3-lxc克隆lxc容器

使用python3-lxc克隆lxc容器
EN

Stack Overflow用户
提问于 2014-05-26 15:12:26
回答 1查看 944关注 0票数 1

我正在ubuntu 14.04上试验lxc。为了管理几个lxc实例,我使用python3-lxc。使用pyhthon3-lxc,我确实无法克隆现有的容器:

代码语言:javascript
复制
>>> import lxc
>>> c = lxc.Container('vanilla')
>>> c.defined
True
>>> c2 = c.clone('vanilla_clone')
>>> c2.defined
False

相应地,/var/lib/lxc中没有用于vanilla_clone的根文件系统。使用

代码语言:javascript
复制
$ lxc-clone vanilla vanilla_clone

工作正常。(python3和lxc-clone都以sudo开头。)这是python3_lxc中的错误或限制,还是我遗漏了什么?

事后再想一想:使用lxc.Container.create需要一个模板,而从现有对象克隆时不需要这个模板。

EN

回答 1

Stack Overflow用户

发布于 2015-04-26 22:36:53

我也有同样的问题,我发现当一个同名的容器已经存在或者它认为它已经存在时,它就会发生!所以你需要做的就是在开始克隆之前检查它。我是这样做的:

代码语言:javascript
复制
>>> import lxc
>>> c = lxc.Container('vanilla')
>>> c2 = lxc.Container('vanilla_clone')
>>> if not c2.defined:
...     c2 = c.clone('vanilla_clone')
>>> c.defined
True
>>> c2.defined
True

我真的不知道为什么,但即使是Stéphane Graber也会做同样的事情here。看看这部分:

代码语言:javascript
复制
# Create a base container (if missing) using an Ubuntu 14.04 image
base = lxc.Container("base")
if not base.defined:
    base.create("download", lxc.LXC_CREATE_QUIET, {"dist": "ubuntu",
                                                   "release": "precise",
                                                   "arch": "i386"})

    # Customize it a bit
    base.start()
    base.get_ips(timeout=30)
    base.attach_wait(lxc.attach_run_command, ["apt-get", "update"])
    base.attach_wait(lxc.attach_run_command, ["apt-get", "dist-upgrade", "-y"])

    if not base.shutdown(30):
        base.stop()

# Clone it as web (if not already existing)
web = lxc.Container("web")
if not web.defined:
    # Clone base using an overlayfs overlay
    web = base.clone("web", bdevtype="overlayfs",
                     flags=lxc.LXC_CLONE_SNAPSHOT)

    # Install apache
    web.start()
    web.get_ips(timeout=30)
    web.attach_wait(lxc.attach_run_command, ["apt-get", "update"])
    web.attach_wait(lxc.attach_run_command, ["apt-get", "install",
                                             "apache2", "-y"])

    if not web.shutdown(30):
        web.stop()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23864623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档