[sandbox v2 5/5] virt-sandbox-image: add a virt-builder source

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Allow virt-sandbox-image to pull templates from virt-builder and run
sandboxes on top of them.
---
 libvirt-sandbox.spec.in                            |  1 +
 libvirt-sandbox/image/cli.py                       |  1 +
 libvirt-sandbox/image/sources/Makefile.am          |  1 +
 libvirt-sandbox/image/sources/VirtBuilderSource.py | 84 ++++++++++++++++++++++
 libvirt-sandbox/image/template.py                  |  2 +
 5 files changed, 89 insertions(+)
 create mode 100644 libvirt-sandbox/image/sources/VirtBuilderSource.py

diff --git a/libvirt-sandbox.spec.in b/libvirt-sandbox.spec.in
index 54fde55..f84eabc 100644
--- a/libvirt-sandbox.spec.in
+++ b/libvirt-sandbox.spec.in
@@ -36,6 +36,7 @@ Requires: systemd >= 198
 Requires: pygobject3-base
 Requires: libselinux-python
 Requires: %{name}-libs = %{version}-%{release}
+Requires: %{_bindir}/virt-builder
 
 %package libs
 Group: Development/Libraries
diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index 05d593a..f7fa8ee 100755
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -172,6 +172,7 @@ Example supported URI formats:
   docker:///ubuntu?tag=15.04
   docker://username:password@xxxxxxxxxxxxxxx/private/image
   docker://registry.access.redhat.com/rhel6
+  virt-builder:///fedora-20
 """)
     return parser
 
diff --git a/libvirt-sandbox/image/sources/Makefile.am b/libvirt-sandbox/image/sources/Makefile.am
index 069557d..52e9a7e 100644
--- a/libvirt-sandbox/image/sources/Makefile.am
+++ b/libvirt-sandbox/image/sources/Makefile.am
@@ -4,6 +4,7 @@ pythonimage_DATA = \
 	__init__.py \
 	Source.py \
 	DockerSource.py \
+	VirtBuilderSource.py \
 	$(NULL)
 
 EXTRA_DIST = $(pythonimage_DATA)
diff --git a/libvirt-sandbox/image/sources/VirtBuilderSource.py b/libvirt-sandbox/image/sources/VirtBuilderSource.py
new file mode 100644
index 0000000..3ce1867
--- /dev/null
+++ b/libvirt-sandbox/image/sources/VirtBuilderSource.py
@@ -0,0 +1,84 @@
+#!/usr/bin/python
+#
+# Copyright (C) 2015 SUSE LLC
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Author: Cedric Bosdonnat <cbosdonnat@xxxxxxxx>
+#
+
+from Source import Source
+import os
+import os.path
+import subprocess
+
+class VirtBuilderSource(Source):
+
+    def _get_template_name(self, template):
+        # We shouldn't have '/' in the names, but let's make sure
+        # nobody can try to alter the folders structure later.
+        return template.path[1:].replace('/', '_')
+
+    def has_template(self, template, templatedir):
+        imagepath = "%s/%s.qcow2" % (templatedir, template.path)
+        return os.path.exists(imagepath)
+
+    def create_template(self, template, templatedir, connect=None):
+        if not os.path.exists(templatedir):
+            os.makedirs(templatedir)
+
+        # Get the image using virt-builder
+        templatename = self._get_template_name(template)
+        imagepath_original = "%s/%s-original.qcow2" % (templatedir, templatename)
+        imagepath = "%s/%s.qcow2" % (templatedir, templatename)
+        cmd = ["virt-builder", templatename,
+               "-o", imagepath_original, "--format", "qcow2"]
+        subprocess.call(cmd)
+
+        # We need to convert this image into a single partition one.
+        tarfile = "%s/%s.tar" % (templatedir, templatename)
+        cmd = ["virt-tar-out", "-a", imagepath_original, "/", tarfile]
+        subprocess.call(cmd)
+
+        os.unlink(imagepath_original)
+
+        cmd = ["qemu-img", "create", "-q", "-f", "qcow2", imagepath, "10G"]
+        subprocess.call(cmd)
+
+        self.format_disk(imagepath, "qcow2", connect)
+        self.extract_tarball(imagepath, "qcow2", tarfile, connect)
+        os.unlink(tarfile)
+
+
+    def delete_template(self, template, templatedir):
+        os.unlink("%s/%s.qcow2" % (templatedir, self._get_template_name(template)))
+
+    def get_command(self, template, templatedir, userargs):
+        return userargs
+
+    def get_disk(self,template, templatedir, imagedir, sandboxname):
+        diskfile = "%s/%s.qcow2" % (templatedir, self._get_template_name(template))
+        tempfile = imagedir + "/" + sandboxname + ".qcow2"
+        if not os.path.exists(imagedir):
+            os.makedirs(imagedir)
+        cmd = ["qemu-img", "create", "-q",
+               "-f", "qcow2",
+               "-o", "backing_fmt=qcow2,backing_file=%s" % diskfile,
+               tempfile]
+        subprocess.call(cmd)
+        return tempfile
+
+    def get_env(self,template, templatedir):
+        return []
diff --git a/libvirt-sandbox/image/template.py b/libvirt-sandbox/image/template.py
index 52d366c..58904a2 100644
--- a/libvirt-sandbox/image/template.py
+++ b/libvirt-sandbox/image/template.py
@@ -43,6 +43,8 @@ class Template(object):
         docker:///ubuntu
 
         docker+https://index.docker.io/ubuntu?tag=latest
+
+        virt-builder:///fedora-20
         """
 
         self.source = source
-- 
2.1.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]