[libvirt] [sandbox PATCH v4 13/21] Image: Add get_disk function to Source

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


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 | 16 ++++++++++++++++
 virt-sandbox-image/sources/Source.py       |  4 ++++
 virt-sandbox-image/virt-sandbox-image.py   |  9 +++++++++
 3 files changed, 29 insertions(+)

diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
index 3e0362b..87fbcf3 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -372,6 +372,22 @@ class DockerSource(Source):
                     parent = None
             imagetagid = parent
 
+    def get_disk(self,**args):
+        name = args['name']
+        destdir = args['templatedir']
+        sandboxid = args['id']
+        imageList = self._get_image_list(name,destdir)
+        toplayer = imageList[0]
+        diskfile = destdir + "/" + toplayer + "/template.qcow2"
+        configfile = destdir + "/" + toplayer + "/template.json"
+        tempfile = destdir + "/" + toplayer + "/" + sandboxid + ".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
diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
index c46abd4..a73619c 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -31,6 +31,8 @@ import shutil
 import sys
 import urllib2
 import subprocess
+import random
+import string
 
 template_dir = None
 storage_dir = None
@@ -128,6 +130,12 @@ def check_connect(connectstr):
             raise ValueError("%s is not supported by Virt-sandbox" %connectstr)
         return True
 
+def requires_id(parser):
+    randomid = ''.join(random.choice(string.lowercase) for i in range(10))
+    parser.add_argument("-d","--id",
+                        default=randomid,
+                        help=_("id of the running sandbox"))
+
 def requires_name(parser):
     parser.add_argument("name",
                         help=_("name of the template"))
@@ -187,6 +195,7 @@ def gen_create_args(subparser):
 def gen_run_args(subparser):
     parser = subparser.add_parser("run",
                                   help=_("Run a already built image"))
+    requires_id(parser)
     requires_name(parser)
     requires_source(parser)
     requires_connect(parser)
-- 
2.1.0




More information about the libvir-list mailing list