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

Cedric Bosdonnat cbosdonnat at suse.com
Mon Oct 5 18:15:42 UTC 2015


On Mon, 2015-10-05 at 16:49 +0100, Daniel P. Berrange wrote:
> On Mon, Oct 05, 2015 at 04:37:59PM +0200, Cédric Bosdonnat wrote:
> > 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
> 
> > +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)
> 
> We should probably wrap the method in a try/finally block, and put the
> two os.unlink() calls in the finally block so we can guarantee we
> clean up the intermediate files if an exception is raised.
> 
> Aside from that it looks fine. It is a shame virt-builder cannot produce
> a partitionless image straight away for us :-(

This and the others have been pushed with the changes.

--
Cedric

> 
> Regards,
> Daniel





More information about the libvir-list mailing list