[Kimchi-devel] [PATCH] issue #417: Validate image file path when creating a new template

Aline Manera alinefm at linux.vnet.ibm.com
Wed Sep 24 00:19:55 UTC 2014


To create a template using an image file, the user must specify an existing
file on system.
Add this verification to avoid error while dealing with guestfs.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/i18n.py      |  1 +
 src/kimchi/imageinfo.py | 14 ++++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 552be25..8315d1d 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -65,6 +65,7 @@ messages = {
     "KCHIMG0001E": _("An error occurred when probing image OS information."),
     "KCHIMG0002E": _("No OS information found in given image."),
     "KCHIMG0003E": _("Unable to read image file %(filename)s"),
+    "KCHIMG0004E": _("Image file must be an existing file on system. %(filename)s is not a valid input."),
 
     "KCHVM0001E": _("Virtual machine %(name)s already exists"),
     "KCHVM0002E": _("Virtual machine %(name)s does not exist"),
diff --git a/src/kimchi/imageinfo.py b/src/kimchi/imageinfo.py
index f4c6356..89d1e0a 100644
--- a/src/kimchi/imageinfo.py
+++ b/src/kimchi/imageinfo.py
@@ -1,7 +1,7 @@
 #
 # Kimchi
 #
-# Copyright IBM Corp, 2014
+# 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
@@ -22,7 +22,7 @@ import os
 import sys
 import guestfs
 
-from kimchi.exception import ImageFormatError, TimeoutExpired
+from kimchi.exception import ImageFormatError, InvalidParameter, TimeoutExpired
 from kimchi.utils import run_command, kimchi_log
 
 
@@ -42,15 +42,21 @@ def probe_img_info(path):
 
 
 def probe_image(image_path):
+    if not os.path.isfile(image_path):
+        raise InvalidParameter("KCHIMG0004E", {'filename': image_path})
+
+    if not os.access(image_path, os.R_OK):
+        raise ImageFormatError("KCHIMG0003E", {'filename': 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")
 
-- 
1.9.3




More information about the Kimchi-devel mailing list