我正在ubuntu 14.04上试验lxc。为了管理几个lxc实例,我使用python3-lxc。使用pyhthon3-lxc,我确实无法克隆现有的容器:
>>> import lxc
>>> c = lxc.Container('vanilla')
>>> c.defined
True
>>> c2 = c.clone('vanilla_clone')
>>> c2.defined
False相应地,/var/lib/lxc中没有用于vanilla_clone的根文件系统。使用
$ lxc-clone vanilla vanilla_clone工作正常。(python3和lxc-clone都以sudo开头。)这是python3_lxc中的错误或限制,还是我遗漏了什么?
事后再想一想:使用lxc.Container.create需要一个模板,而从现有对象克隆时不需要这个模板。
发布于 2015-04-26 22:36:53
我也有同样的问题,我发现当一个同名的容器已经存在或者它认为它已经存在时,它就会发生!所以你需要做的就是在开始克隆之前检查它。我是这样做的:
>>> 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。看看这部分:
# 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()https://stackoverflow.com/questions/23864623
复制相似问题