[Kimchi-devel] [PATCHv2] Add image probe function

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Mon Jul 7 10:27:50 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

v1>v2, Add handling exception.

Image file probe will be used in identify image file os info and
generate reasonable configuration for it.
This will be useful when import image and create a vm from it.

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 docs/README.md          |  9 ++++++---
 src/kimchi/exception.py |  4 ++++
 src/kimchi/i18n.py      |  4 ++++
 src/kimchi/imageinfo.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+), 3 deletions(-)
 create mode 100644 src/kimchi/imageinfo.py

diff --git a/docs/README.md b/docs/README.md
index ab03918..24537e1 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -54,7 +54,8 @@ Install Dependencies
                         qemu-kvm python-psutil python-ethtool sos \
                         python-ipaddr python-lxml nfs-utils \
                         iscsi-initiator-utils libxslt pyparted nginx \
-                        policycoreutils-python
+                        policycoreutils-python python-libguestfs \
+                        libguestfs-tools
      # If using RHEL6, install the following additional packages:
      $ sudo yum install python-unittest2 python-ordereddict
      # Restart libvirt to allow configuration changes to take effect
@@ -76,7 +77,8 @@ for more information on how to configure your system to access this repository.
                            python-pam python-m2crypto python-jsonschema \
                            qemu-kvm libtool python-psutil python-ethtool \
                            sosreport python-ipaddr python-lxml nfs-common \
-                           open-iscsi lvm2 xsltproc python-parted nginx firewalld
+                           open-iscsi lvm2 xsltproc python-parted nginx \
+                           firewalld python-guestfs libguestfs-tools
 
     Packages version requirement:
         python-jsonschema >= 1.3.0
@@ -90,7 +92,8 @@ for more information on how to configure your system to access this repository.
                           python-pam python-M2Crypto python-jsonschema \
                           rpm-build kvm python-psutil python-ethtool \
                           python-ipaddr python-lxml nfs-client open-iscsi \
-                          libxslt-tools python-xml python-parted
+                          libxslt-tools python-xml python-parted \
+                          python-libguestfs guestfs-tools
 
     Packages version requirement:
         python-psutil >= 0.6.0
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py
index fcf60cc..6b4c913 100644
--- a/src/kimchi/exception.py
+++ b/src/kimchi/exception.py
@@ -89,5 +89,9 @@ class IsoFormatError(KimchiException):
     pass
 
 
+class ImageFormatError(KimchiException):
+    pass
+
+
 class TimeoutExpired(KimchiException):
     pass
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 91684b6..daeeed8 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -61,6 +61,10 @@ messages = {
                      "'%(user)s' to the ISO path group, or (not recommended) 'chmod -R o+x 'path_to_iso'."
                      "Details: %(err)s" ),
 
+    "KCHIMG0001E": _("Error occurs when probing image os information."),
+    "KCHIMG0002E": _("No os information found in given image."),
+    "KCHIMG0003E": _("Unable to find/read image file %(filename)s"),
+
     "KCHVM0001E": _("Virtual machine %(name)s already exists"),
     "KCHVM0002E": _("Virtual machine %(name)s does not exist"),
     "KCHVM0003E": _("Unable to rename virtual machine %(name)s. The name %(new_name)s already exists or it is not powered off."),
diff --git a/src/kimchi/imageinfo.py b/src/kimchi/imageinfo.py
new file mode 100644
index 0000000..26a9480
--- /dev/null
+++ b/src/kimchi/imageinfo.py
@@ -0,0 +1,48 @@
+#
+# Kimchi
+#
+# Copyright IBM Corp, 2013
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import os
+import sys
+import guestfs
+
+from kimchi.exception import ImageFormatError
+
+def probe_image(image_path):
+    g = guestfs.GuestFS(python_return_dict=True)
+    g.add_drive_opts(image_path, readonly=1)
+    g.launch()
+    if not os.access(image_path, os.R_OK):
+        raise ImageFormatError("KCHIMG0003E", {'filename': image_path})
+    try:
+        roots = g.inspect_os()
+    except:
+        raise ImageFormatError("KCHIMG0001E")
+    if len(roots) == 0:
+        raise ImageFormatError("KCHIMG0002E")
+
+    for root in roots:
+        version = "%d.%d" % (g.inspect_get_major_version(root),
+                             g.inspect_get_minor_version(root))
+        distro = "%s" % (g.inspect_get_distro(root))
+
+    return (distro, version)
+
+
+if __name__ == '__main__':
+    print probe_image(sys.argv[1])
-- 
1.8.3.2




More information about the Kimchi-devel mailing list