[libvirt] [sandbox PATCH v4 16/21] Image: Add Volume Support

Eren Yagdiran erenyagdiran at gmail.com
Fri Aug 28 13:47:44 UTC 2015


Volumes let user to map host-paths into sandbox. Docker containers
need volumes for data persistence.
---
 virt-sandbox-image/sources/DockerSource.py | 12 ++++++++++++
 virt-sandbox-image/sources/Source.py       |  4 ++++
 virt-sandbox-image/virt-sandbox-image.py   | 22 ++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
index 87fbcf3..1022107 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -28,6 +28,7 @@ import traceback
 import os
 import subprocess
 import shutil
+import collections
 
 class DockerConfParser():
 
@@ -37,6 +38,13 @@ class DockerConfParser():
     def getRunCommand(self):
         cmd = self.json_data['container_config']['Cmd'][2]
         return cmd[cmd.index('"') + 1:cmd.rindex('"')]
+    def getVolumes(self):
+        volumes = self.json_data['container_config']['Volumes']
+        volumelist = []
+        if isinstance(volumes,collections.Iterable):
+          for key,value in volumes.iteritems():
+            volumelist.append(key)
+        return volumelist
 
 class DockerSource(Source):
 
@@ -393,5 +401,9 @@ class DockerSource(Source):
         commandToRun = configParser.getRunCommand()
         return commandToRun
 
+    def get_volume(self,configfile):
+        configParser = DockerConfParser(configfile)
+        return configParser.getVolumes()
+
 def debug(msg):
     sys.stderr.write(msg)
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
index 9a3da59..8cc508e 100644
--- a/virt-sandbox-image/sources/Source.py
+++ b/virt-sandbox-image/sources/Source.py
@@ -45,3 +45,7 @@ class Source():
     @abstractmethod
     def get_disk(self,**args):
       pass
+
+    @abstractmethod
+    def get_volume(self,**args):
+      pass
diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
index 058738a..79f8d8c 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -132,6 +132,7 @@ def check_connect(connectstr):
 
 def run(args):
     try:
+        global storage_dir
         if args.connect is not None:
             check_connect(args.connect)
         source = dynamic_source_loader(args.source)
@@ -150,6 +151,25 @@ def run(args):
         if networkArgs is not None:
             params.append('-N')
             params.append(networkArgs)
+        allVolumes = source.get_volume(configfile)
+        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 = storage_dir + guestPath
+                if not os.path.exists(hostPath):
+                    os.makedirs(hostPath)
+            else:
+                pass
+            params.append("--mount")
+            params.append("host-bind:%s=%s" %(guestPath,hostPath))
         params.append('--')
         params.append(commandToRun)
         cmd = cmd + params
@@ -234,6 +254,8 @@ def gen_run_args(subparser):
                         help=_("Igniter command for image"))
     parser.add_argument("-n","--network",
                         help=_("Network params for running template"))
+    parser.add_argument("-v","--volume",action="append",
+                        help=_("Volume params for running template"))
     parser.set_defaults(func=run)
 
 if __name__ == '__main__':
-- 
2.1.0




More information about the libvir-list mailing list