<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 2014&#24180;06&#26376;18&#26085; 22:56, Sheldon wrote:<br>
    </div>
    <blockquote cite="mid:53A1A8AD.8010205@linux.vnet.ibm.com"
      type="cite">
      <meta content="text/html; charset=ISO-8859-1"
        http-equiv="Content-Type">
      <div class="moz-cite-prefix">good. seems python-libguestfs is easy
        to use than shell. <span class="moz-smiley-s1"><span> :-) </span></span><br>
        <br>
        See inline comments below.<br>
        <br>
        On 06/18/2014 05:35 PM, <a moz-do-not-send="true"
          class="moz-txt-link-abbreviated"
          href="mailto:lvroyce@linux.vnet.ibm.com">lvroyce@linux.vnet.ibm.com</a>
        wrote:<br>
      </div>
      <blockquote
        cite="mid:1403084103-31850-1-git-send-email-lvroyce@linux.vnet.ibm.com"
        type="cite">
        <pre wrap="">From: Royce Lv <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com">&lt;lvroyce@linux.vnet.ibm.com&gt;</a>

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 <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com">&lt;lvroyce@linux.vnet.ibm.com&gt;</a>
---
 docs/README.md          |  9 ++++++---
 src/kimchi/exception.py |  2 ++
 src/kimchi/imageinfo.py | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 src/kimchi/imageinfo.py

diff --git a/docs/README.md b/docs/README.md
index c658637..c341a5d 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -53,7 +53,8 @@ Install Dependencies
                         PyPAM m2crypto python-jsonschema rpm-build \
                         qemu-kvm python-psutil python-ethtool sos \
                         python-ipaddr python-lxml nfs-utils \
-                        iscsi-initiator-utils libxslt pyparted nginx
+                        iscsi-initiator-utils libxslt pyparted nginx \
+                        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
@@ -75,7 +76,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
+                           open-iscsi lvm2 xsltproc python-parted nginx \
+                           python-guestfs libguestfs-tools

     Packages version requirement:
         python-jsonschema &gt;= 1.3.0
@@ -89,7 +91,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 &gt;= 0.6.0
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py
index fcf60cc..a983d46 100644
--- a/src/kimchi/exception.py
+++ b/src/kimchi/exception.py
@@ -88,6 +88,8 @@ class InvalidOperation(KimchiException):
 class IsoFormatError(KimchiException):
     pass

+class ImageFormatError(KimchiException):
+    pass

 class TimeoutExpired(KimchiException):
     pass
diff --git a/src/kimchi/imageinfo.py b/src/kimchi/imageinfo.py
new file mode 100644
index 0000000..d57ecac
--- /dev/null
+++ b/src/kimchi/imageinfo.py
@@ -0,0 +1,42 @@
+#
+# 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 sys
+import guestfs
+
+
+def probe_image(image_path):
+    g = guestfs.GuestFS(python_return_dict=True)
+    g.add_drive_opts(image_path, readonly=1)
+    g.launch()
+
+    roots = g.inspect_os()</pre>
      </blockquote>
      <br>
      Not sure inspect_os&nbsp; will raise some exception when they are
      mounted or the disk is <font color="#3333ff">encrypted.</font><br>
    </blockquote>
    If it is encrypted, there is no way that libguestfs can decrypted
    according to me.<br>
    For mounted or encrypted disks, I will add exception handling for
    them.<br>
    <blockquote cite="mid:53A1A8AD.8010205@linux.vnet.ibm.com"
      type="cite"> I have try opensuse. it failed to get the os info.<br>
      <br>
      Also try ubuntu 12.10 and rhel 6.4, F17.<br>
      Not try windows.&nbsp; <br>
      <br>
      &nbsp;&nbsp;&nbsp; def inspect_os (self):<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This function uses other libguestfs features such as<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "g.mount_ro" and "g.umount_all" in order to mount and<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unmount filesystems and look at the contents. <font
        color="#3333ff">This<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; should be called with no disks currently mounted</font>.
      The<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function may also use Augeas, so any existing Augeas<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; handle will be closed.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This function <font color="#3333ff">cannot decrypt
        encrypted disks.</font> The caller<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; must do that first (supplying the necessary keys) if the<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; disk is encrypted.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """<br>
      <blockquote
        cite="mid:1403084103-31850-1-git-send-email-lvroyce@linux.vnet.ibm.com"
        type="cite">
        <pre wrap="">+    if len(roots) == 0:
+        raise ImageFormatError("No os found in given image.")
+
+    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))
+</pre>
      </blockquote>
      g.close()?<br>
    </blockquote>
    ACK.<br>
    <blockquote cite="mid:53A1A8AD.8010205@linux.vnet.ibm.com"
      type="cite"> seems it is not needed? <br>
      but seems it will calls called implicitly the program ends.<br>
      <br>
      &nbsp;&nbsp; def close (self):<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """Explicitly close the guestfs handle.<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The handle is closed implicitly when its reference count
      goes<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to zero (eg. when it goes out of scope or the program
      ends).<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This call is only needed if you want to force the handle
      to<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close now.&nbsp; After calling this, the program must not call<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; any method on the handle (except the implicit call to<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __del__ which happens when the final reference is cleaned
      up).<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """<br>
      <br>
      &nbsp;&nbsp;&nbsp; def shutdown (self):<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """This is the opposite of "g.launch". It performs an<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; orderly shutdown of the backend process(es). If the<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; autosync flag is set (which is the default) then the<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; disk image is synchronized.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If the subprocess exits with an error then this function<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; will return an error, which should *not* be ignored (it<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; may indicate that the disk image could not be written<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out properly).<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; It is safe to call this multiple times. Extra calls are<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ignored.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This call does *not* close or free up the handle. You<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; still need to call "g.close" afterwards.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "g.close" will call this if you don't do it explicitly,<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; but note that any errors are ignored in that case.<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """<br>
      <br>
      <blockquote
        cite="mid:1403084103-31850-1-git-send-email-lvroyce@linux.vnet.ibm.com"
        type="cite">
        <pre wrap="">+    return (distro, version)
+
+
+if __name__ == '__main__':
+    print probe_image(sys.argv[1])
</pre>
      </blockquote>
      <blockquote
        cite="mid:1403084103-31850-1-git-send-email-lvroyce@linux.vnet.ibm.com"
        type="cite"> </blockquote>
      <br>
      <br>
      <pre class="moz-signature" cols="72">-- 
Thanks and best regards!

Sheldon Feng(&#20911;&#23569;&#21512;)<a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com">&lt;shaohef@linux.vnet.ibm.com&gt;</a>
IBM Linux Technology Center</pre>
    </blockquote>
    <br>
  </body>
</html>