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

Sheldon shaohef at linux.vnet.ibm.com
Wed Jun 18 14:56:45 UTC 2014


good. seems python-libguestfs is easy to use than shell. :-)

See inline comments below.

On 06/18/2014 05:35 PM, lvroyce at linux.vnet.ibm.com wrote:
> From: Royce Lv <lvroyce at 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 at linux.vnet.ibm.com>
> ---
>   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()

Not sure inspect_os will raise some exception when they are mounted or 
the disk is encrypted.
I have try opensuse. it failed to get the os info.

Also try ubuntu 12.10 and rhel 6.4, F17.
Not try windows.

     def inspect_os (self):
         """
         ...
         This function uses other libguestfs features such as
         "g.mount_ro" and "g.umount_all" in order to mount and
         unmount filesystems and look at the contents. This
         should be called with no disks currently mounted. The
         function may also use Augeas, so any existing Augeas
         handle will be closed.

         This function cannot decrypt encrypted disks. The caller
         must do that first (supplying the necessary keys) if the
         disk is encrypted.
         ...
         """
> +    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))
> +
g.close()?
seems it is not needed?
but seems it will calls called implicitly the program ends.

    def close (self):
         """Explicitly close the guestfs handle.

         The handle is closed implicitly when its reference count goes
         to zero (eg. when it goes out of scope or the program ends).

         This call is only needed if you want to force the handle to
         close now.  After calling this, the program must not call
         any method on the handle (except the implicit call to
         __del__ which happens when the final reference is cleaned up).
         """

     def shutdown (self):
         """This is the opposite of "g.launch". It performs an
         orderly shutdown of the backend process(es). If the
         autosync flag is set (which is the default) then the
         disk image is synchronized.

         If the subprocess exits with an error then this function
         will return an error, which should *not* be ignored (it
         may indicate that the disk image could not be written
         out properly).

         It is safe to call this multiple times. Extra calls are
         ignored.

         This call does *not* close or free up the handle. You
         still need to call "g.close" afterwards.

         "g.close" will call this if you don't do it explicitly,
         but note that any errors are ignored in that case.
         """

> +    return (distro, version)
> +
> +
> +if __name__ == '__main__':
> +    print probe_image(sys.argv[1])


-- 
Thanks and best regards!

Sheldon Feng(???)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/kimchi-devel/attachments/20140618/083ccf0a/attachment.html>


More information about the Kimchi-devel mailing list