[libvirt] [sandbox PATCH v3 13/22] Image: Add get_disk function to Source

Cedric Bosdonnat cbosdonnat at suse.com
Tue Aug 18 09:24:13 UTC 2015


On Tue, 2015-08-18 at 06:53 +0000, Eren Yagdiran wrote:
> Provide a way to know which disk image to use for the sandbox depending on the used source
> DockerSource will need to locate the topmost disk image among all the layers images
> ---
>  virt-sandbox-image/sources/DockerSource.py | 18 ++++++++++++++++++
>  virt-sandbox-image/sources/Source.py       |  4 ++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
> index 3e0362b..1e7f633 100644
> --- a/virt-sandbox-image/sources/DockerSource.py
> +++ b/virt-sandbox-image/sources/DockerSource.py
> @@ -28,6 +28,8 @@ import traceback
>  import os
>  import subprocess
>  import shutil
> +import random
> +import string
>  
>  class DockerConfParser():
>  
> @@ -372,6 +374,22 @@ class DockerSource(Source):
>                      parent = None
>              imagetagid = parent
>  
> +    def get_disk(self,**args):
> +        name = args['name']
> +        destdir = args['templatedir']
> +        imageList = self._get_image_list(name,destdir)
> +        toplayer = imageList[0]
> +        diskfile = destdir + "/" + toplayer + "/template.qcow2"
> +        configfile = destdir + "/" + toplayer + "/template.json"
> +        tempfile = ''.join(random.choice(string.lowercase) for i in range(10))

This isn't really where the temporary disk should be created. I would
let the get_disk function return the top-most image, and have the
temporary disk created in the run function of virt-sandbox-image.py.

More over the temporary disk image shouldn't be created next to the
templates, but in the sandbox state directory. I wouldn't have it named
randomly, but rather <sandbox-name>.qcow, where the <sandbox-name> would
be computed based on the template name and an incremented counter.

--
Cedric

> +        tempfile = destdir + "/" + toplayer + "/" + tempfile + ".qcow2"
> +        cmd = ["qemu-img","create","-q","-f","qcow2"]
> +        cmd.append("-o")
> +        cmd.append("backing_fmt=qcow2,backing_file=%s" % diskfile)
> +        cmd.append(tempfile)
> +        subprocess.call(cmd)
> +        return (tempfile,configfile)
> +
>      def get_command(self,configfile):
>          configParser = DockerConfParser(configfile)
>          commandToRun = configParser.getRunCommand()
> diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
> index 9daf62d..9a3da59 100644
> --- a/virt-sandbox-image/sources/Source.py
> +++ b/virt-sandbox-image/sources/Source.py
> @@ -41,3 +41,7 @@ class Source():
>      @abstractmethod
>      def get_command(self,**args):
>        pass
> +
> +    @abstractmethod
> +    def get_disk(self,**args):
> +      pass





More information about the libvir-list mailing list