From df5ab9f0d7b462f9c3c513f45599338c9affe20b Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Thu, 29 Jan 2009 15:50:38 +0000
Subject: [PATCH] KVM: make the kernel and initrd arguments optional

Under KVM we don't strictly need a kernel and initrd. If some are passed
we'll use them, otherwise the guest OS will need to behave as fully
native, and have its own boot loader and kernel.
The root_path hypervisor parameter becomes mandatory only if a kernel is
specified.

Reviewed-by: iustinp
---
 lib/hypervisor/hv_kvm.py | 41 ++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index 48a8850c9..25fbb9d9e 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -239,17 +239,17 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       drive_val = 'file=%s,format=raw%s%s' % (dev_path, if_val, boot_val)
       kvm_cmd.extend(['-drive', drive_val])
 
-    kvm_cmd.extend(['-kernel', instance.hvparams[constants.HV_KERNEL_PATH]])
-
-    initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
-    if initrd_path:
-      kvm_cmd.extend(['-initrd', initrd_path])
-
-    root_append = 'root=%s ro' % instance.hvparams[constants.HV_ROOT_PATH]
-    if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
-      kvm_cmd.extend(['-append', 'console=ttyS0,38400 %s' % root_append])
-    else:
-      kvm_cmd.extend(['-append', root_append])
+    kernel_path = instance.hvparams[constants.HV_KERNEL_PATH]
+    if kernel_path:
+      kvm_cmd.extend(['-kernel', kernel_path])
+      initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
+      if initrd_path:
+        kvm_cmd.extend(['-initrd', initrd_path])
+      root_append = 'root=%s ro' % instance.hvparams[constants.HV_ROOT_PATH]
+      if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
+        kvm_cmd.extend(['-append', 'console=ttyS0,38400 %s' % root_append])
+      else:
+        kvm_cmd.extend(['-append', root_append])
 
     #"hvm_boot_order",
     #"hvm_cdrom_image_path",
@@ -634,18 +634,19 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     """
     super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
 
-    if not hvparams[constants.HV_KERNEL_PATH]:
-      raise errors.HypervisorError("Need a kernel for the instance")
-
-    if not os.path.isabs(hvparams[constants.HV_KERNEL_PATH]):
-      raise errors.HypervisorError("The kernel path must be an absolute path")
+    kernel_path = hvparams[constants.HV_KERNEL_PATH]
+    if kernel_path:
+      if not os.path.isabs(hvparams[constants.HV_KERNEL_PATH]):
+        raise errors.HypervisorError("The kernel path must be an absolute path"
+                                     ", if defined")
 
-    if not hvparams[constants.HV_ROOT_PATH]:
-      raise errors.HypervisorError("Need a root partition for the instance")
+      if not hvparams[constants.HV_ROOT_PATH]:
+        raise errors.HypervisorError("Need a root partition for the instance"
+                                     ", if a kernel is defined")
 
     if hvparams[constants.HV_INITRD_PATH]:
       if not os.path.isabs(hvparams[constants.HV_INITRD_PATH]):
-        raise errors.HypervisorError("The initrd path must be an absolute path"
+        raise errors.HypervisorError("The initrd path must an absolute path"
                                      ", if defined")
 
   def ValidateParameters(self, hvparams):
@@ -658,7 +659,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     super(KVMHypervisor, self).ValidateParameters(hvparams)
 
     kernel_path = hvparams[constants.HV_KERNEL_PATH]
-    if not os.path.isfile(kernel_path):
+    if kernel_path and not os.path.isfile(kernel_path):
       raise errors.HypervisorError("Instance kernel '%s' not found or"
                                    " not a file" % kernel_path)
     initrd_path = hvparams[constants.HV_INITRD_PATH]
-- 
GitLab