简体   繁体   中英

cloning lxc container using python3-lxc

I am experimenting with lxc on ubuntu 14.04. In order to manage a couple of lxc instances I am using python3-lxc. Using pyhthon3-lxc, I do fail to clone an existing container:

>>> import lxc
>>> c = lxc.Container('vanilla')
>>> c.defined
True
>>> c2 = c.clone('vanilla_clone')
>>> c2.defined
False

Correspondingly there is no rootfs for vanilla_clone in /var/lib/lxc. Using

$ lxc-clone vanilla vanilla_clone

works fine. (python3 and lxc-clone where both started with sudo.) Is this a bug or a limitation in python3_lxc or am I missing something ?

Afterthought: Using lxc.Container.create is requiring a template which I should not need when cloning from an existing object.

I had same problem, I found that it occurred when a container with a same name is already existed or it think that it's existed! so all you need is to check it before start cloning. I did it like this:

>>> 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

I really don't know why, but even Stéphane Graber do the same thing here . look at this part:

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM