[libvirt] [sandbox PATCH v3 16/22] Image: Add Volume Support

Eren Yagdiran erenyagdiran at gmail.com
Tue Aug 18 06:53:37 UTC 2015


Volumes let user to map host-paths into guest. Docker containers need volumes because its
filesystem read-only by default.
---
 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 1e7f633..67bcf6b 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -30,6 +30,7 @@ import subprocess
 import shutil
 import random
 import string
+import collections
 
 class DockerConfParser():
 
@@ -39,6 +40,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):
 
@@ -395,5 +403,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 3ce3a8b..e554d8a 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -130,6 +130,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)
@@ -148,6 +149,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
@@ -225,6 +245,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