[libvirt] [PATCH sandbox v5 20/20] Image: Add Volume Support

Cedric Bosdonnat cbosdonnat at suse.com
Wed Sep 9 12:08:08 UTC 2015


On Tue, 2015-09-08 at 17:29 +0100, Daniel P. Berrange wrote:
> From: Eren Yagdiran <erenyagdiran at gmail.com>
> 
> Volumes let user to map host-paths into sandbox. Docker containers
> need volumes for data persistence.
> 
> Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
> ---
>  libvirt-sandbox/image/cli.py                  | 24 +++++++++++++++++++++++-
>  libvirt-sandbox/image/sources/DockerSource.py | 13 +++++++++++++
>  libvirt-sandbox/image/sources/Source.py       | 10 ++++++++++
>  3 files changed, 46 insertions(+), 1 deletion(-)
> 
> diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
> index c17b577..3f1ab0d 100755
> --- a/libvirt-sandbox/image/cli.py
> +++ b/libvirt-sandbox/image/cli.py
> @@ -101,6 +101,7 @@ def create(args):
>  
>  def run(args):
>      try:
> +        global image_dir
>          if args.connect is not None:
>              check_connect(args.connect)
>          source = dynamic_source_loader(args.source)
> @@ -142,6 +143,26 @@ def run(args):
>              else:
>                  pass
>  
> +        allVolumes = source.get_volumes(args.template, args.template_dir)
> +        volumeArgs = args.volume
> +        if volumeArgs is not None:
> +            allVolumes = allVolumes + volumeArgs
> +        for volume in allVolumes:
> +            volumeSplit = volume.split(":")
> +            volumelen = len(volumeSplit)
> +            if volumelen == 2:
> +                hostPath = volumeSplit[0]
> +                guestPath = volumeSplit[1]
> +            elif volumelen == 1:
> +                guestPath = volumeSplit[0]
> +                hostPath = image_dir + guestPath
> +                if not os.path.exists(hostPath):
> +                    os.makedirs(hostPath)
> +            else:
> +                pass
> +            params.append("--mount")
> +            params.append("host-bind:%s=%s" %(guestPath,hostPath))
> +
>          cmd = cmd + params + ['--'] + commandToRun
>          subprocess.call(cmd)
>          os.unlink(diskfile)

On top of the questions mentioned in the other thread regarding volumes,
we also need to decide what to do for the data volumes cleanup. We may
want to keep them, but we surely also want to have a way to prune them.

--
Cedric

> @@ -237,7 +258,8 @@ def gen_run_args(subparser):
>                          help=_("Network params for running template"))
>      parser.add_argument("-e","--env",action="append",
>                          help=_("Environment params for running template"))
> -
> +    parser.add_argument("--volume",action="append",
> +                        help=_("Volume params for running template"))
>      parser.set_defaults(func=run)
>  
>  def main():
> diff --git a/libvirt-sandbox/image/sources/DockerSource.py b/libvirt-sandbox/image/sources/DockerSource.py
> index 4455198..2c358fe 100644
> --- a/libvirt-sandbox/image/sources/DockerSource.py
> +++ b/libvirt-sandbox/image/sources/DockerSource.py
> @@ -28,6 +28,7 @@ import traceback
>  import os
>  import subprocess
>  import shutil
> +import collections
>  
>  class DockerConfParser():
>  
> @@ -44,6 +45,13 @@ class DockerConfParser():
>            return lst
>          else:
>            return []
> +    def getVolumes(self):
> +        volumes = self.json_data['config']['Volumes']
> +        volumelist = []
> +        if isinstance(volumes,collections.Iterable):
> +            for key,value in volumes.iteritems():
> +                volumelist.append(key)
> +        return volumelist
>  
>  class DockerSource(Source):
>  
> @@ -399,5 +407,10 @@ class DockerSource(Source):
>          configParser = DockerConfParser(configfile)
>          return configParser.getEnvs()
>  
> +    def get_volumes(self, templatename, templatedir):
> +        configfile, diskfile = self._get_template_data(templatename, templatedir)
> +        configParser = DockerConfParser(configfile)
> +        return configParser.getVolumes()
> +
>  def debug(msg):
>      sys.stderr.write(msg)
> diff --git a/libvirt-sandbox/image/sources/Source.py b/libvirt-sandbox/image/sources/Source.py
> index 8a21f90..f1dd3e7 100644
> --- a/libvirt-sandbox/image/sources/Source.py
> +++ b/libvirt-sandbox/image/sources/Source.py
> @@ -105,3 +105,13 @@ class Source():
>          Get the dict of environment variables to set
>          """
>          pass
> +
> +    @abstractmethod
> +    def get_volumes(self,templatename, templatedir):
> +        """
> +        :param templatename: name of the template image to download
> +        :param templatedir: local directory path in which to find template
> +
> +        Get the list of volumes associated with the template
> +        """
> +        pass





More information about the libvir-list mailing list