
Reviewed-by: Aline Manera <alinefm@linux.vnet.ibm.com> On 07/20/2014 12:07 PM, lvroyce0210@gmail.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
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@linux.vnet.ibm.com> --- Makefile.am | 1 + contrib/DEBIAN/control.in | 4 +++- contrib/kimchi.spec.fedora.in | 2 ++ contrib/kimchi.spec.suse.in | 2 ++ docs/README.md | 9 +++++--- src/kimchi/exception.py | 4 ++++ src/kimchi/i18n.py | 4 ++++ src/kimchi/imageinfo.py | 49 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/kimchi/imageinfo.py
diff --git a/Makefile.am b/Makefile.am index 44c2515..3293d9e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ PEP8_WHITELIST = \ src/kimchi/distroloader.py \ src/kimchi/exception.py \ src/kimchi/featuretests.py \ + src/kimchi/imageinfo.py \ src/kimchi/iscsi.py \ src/kimchi/isoinfo.py \ src/kimchi/kvmusertests.py \ diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in index aac1a24..3754fa2 100644 --- a/contrib/DEBIAN/control.in +++ b/contrib/DEBIAN/control.in @@ -23,7 +23,9 @@ Depends: python-cherrypy3 (>= 3.2.0), python-lxml, open-iscsi, firewalld, - nginx + nginx, + python-guestfs, + libguestfs-tools Build-Depends: libxslt, python-libxml2 Maintainer: Aline Manera <alinefm@br.ibm.com> diff --git a/contrib/kimchi.spec.fedora.in b/contrib/kimchi.spec.fedora.in index ca3f7e1..5fa93f7 100644 --- a/contrib/kimchi.spec.fedora.in +++ b/contrib/kimchi.spec.fedora.in @@ -27,6 +27,8 @@ Requires: nfs-utils Requires: nginx Requires: iscsi-initiator-utils Requires: policycoreutils-python +Requires: python-libguestfs +Requires: libguestfs-tools BuildRequires: libxslt BuildRequires: libxml2-python
diff --git a/contrib/kimchi.spec.suse.in b/contrib/kimchi.spec.suse.in index 73304c2..9e87ada 100644 --- a/contrib/kimchi.spec.suse.in +++ b/contrib/kimchi.spec.suse.in @@ -26,6 +26,8 @@ Requires: python-xml Requires: nfs-client Requires: nginx Requires: open-iscsi +Requires: python-libguestfs +Requires: guestfs-tools BuildRequires: libxslt-tools BuildRequires: python-libxml2
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 d84ddb9..f8f5fb3 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -90,5 +90,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 0c76145..14b057f 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..f874ece --- /dev/null +++ b/src/kimchi/imageinfo.py @@ -0,0 +1,49 @@ +# +# Kimchi +# +# Copyright IBM Corp, 2014 +# +# 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])