<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">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 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 class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com"><lvroyce@linux.vnet.ibm.com></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 class="moz-txt-link-rfc2396E" href="mailto:lvroyce@linux.vnet.ibm.com"><lvroyce@linux.vnet.ibm.com></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 >= 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 >= 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
will raise some exception when they are mounted or the disk is <font
color="#3333ff">encrypted.</font><br>
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. <br>
<br>
def inspect_os (self):<br>
"""<br>
...<br>
This function uses other libguestfs features such as<br>
"g.mount_ro" and "g.umount_all" in order to mount and<br>
unmount filesystems and look at the contents. <font
color="#3333ff">This<br>
should be called with no disks currently mounted</font>.
The<br>
function may also use Augeas, so any existing Augeas<br>
handle will be closed.<br>
<br>
This function <font color="#3333ff">cannot decrypt
encrypted disks.</font> The caller<br>
must do that first (supplying the necessary keys) if the<br>
disk is encrypted.<br>
...<br>
"""<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>
seems it is not needed? <br>
but seems it will calls called implicitly the program ends.<br>
<br>
def close (self):<br>
"""Explicitly close the guestfs handle.<br>
<br>
The handle is closed implicitly when its reference count
goes<br>
to zero (eg. when it goes out of scope or the program ends).<br>
<br>
This call is only needed if you want to force the handle to<br>
close now. After calling this, the program must not call<br>
any method on the handle (except the implicit call to<br>
__del__ which happens when the final reference is cleaned
up).<br>
"""<br>
<br>
def shutdown (self):<br>
"""This is the opposite of "g.launch". It performs an<br>
orderly shutdown of the backend process(es). If the<br>
autosync flag is set (which is the default) then the<br>
disk image is synchronized.<br>
<br>
If the subprocess exits with an error then this function<br>
will return an error, which should *not* be ignored (it<br>
may indicate that the disk image could not be written<br>
out properly).<br>
<br>
It is safe to call this multiple times. Extra calls are<br>
ignored.<br>
<br>
This call does *not* close or free up the handle. You<br>
still need to call "g.close" afterwards.<br>
<br>
"g.close" will call this if you don't do it explicitly,<br>
but note that any errors are ignored in that case.<br>
"""<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(冯少合)<a class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com"><shaohef@linux.vnet.ibm.com></a>
IBM Linux Technology Center</pre>
</body>
</html>