[Ovirt-devel] [PATCH server] Added support for booting a VM from an ISO image.

Darryl L. Pierce dpierce at redhat.com
Wed Sep 24 21:09:04 UTC 2008


Also added a few helper methods to Vm to contain the knowledge
of how Cobbler integration is containined.

This patch is not complete. I'm going to be away for a few days and am 
putting this out there if someone else has time to pick it up. Otherwise,
I'll continue with it on Monday.

Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>
---
 src/app/controllers/vm_controller.rb |   10 +++++-
 src/app/models/vm.rb                 |   31 +++++++++++++++++-
 src/task-omatic/task_vm.rb           |   22 ++++++++++---
 src/test/unit/vm_test.rb             |   57 ++++++++++++++++++++++++++++++++--
 4 files changed, 108 insertions(+), 12 deletions(-)

diff --git a/src/app/controllers/vm_controller.rb b/src/app/controllers/vm_controller.rb
index b6192e2..bf50980 100644
--- a/src/app/controllers/vm_controller.rb
+++ b/src/app/controllers/vm_controller.rb
@@ -224,10 +224,16 @@ class VmController < ApplicationController
   def _setup_provisioning_options
     @provisioning_options = [[Vm::PXE_OPTION_LABEL, Vm::PXE_OPTION_VALUE],
                              [Vm::HD_OPTION_LABEL, Vm::HD_OPTION_VALUE]]
-    # FIXME add cobbler images too
+
     begin
+      @provisioning_options += Cobbler::Image.find.collect do |image|
+        [image.name + Vm::COBBLER_IMAGE_SUFFIX, 
+          "#{Vm::IMAGE_PREFIX}@#{Vm::COBBLER_PREFIX}#{Vm::PROVISIONING_DELIMITER}#{image.name}"]
+      end
+
       @provisioning_options += Cobbler::Profile.find.collect do |profile|
-        [profile.name + Vm::COBBLER_PROFILE_SUFFIX, profile.name]
+        [profile.name + Vm::COBBLER_PROFILE_SUFFIX, 
+          "#{Vm::PROFILE_PREFIX}@#{Vm::COBBLER_PREFIX}:#{profile.name}"]
 
     end
     rescue
diff --git a/src/app/models/vm.rb b/src/app/models/vm.rb
index ace6fb1..a5def2b 100644
--- a/src/app/models/vm.rb
+++ b/src/app/models/vm.rb
@@ -49,7 +49,7 @@ class Vm < ActiveRecord::Base
   PROFILE_PREFIX         = "profile"
   IMAGE_PREFIX           = "image"
   COBBLER_PROFILE_SUFFIX = " (Cobbler Profile)"
-  COBBLER_IMAGE_SUFFIX   = " (Cobbler Profile)"
+  COBBLER_IMAGE_SUFFIX   = " (Cobbler Image)"
 
   PXE_OPTION_LABEL       = "PXE Boot"
   PXE_OPTION_VALUE       = "pxe"
@@ -139,7 +139,12 @@ class Vm < ActiveRecord::Base
   end
 
   def provisioning_and_boot_settings=(settings)
-    if settings==PXE_OPTION_VALUE
+    # if the settings have a prefix that matches cobber settings, then process 
+    # those details
+    if settings =~ /\@#{COBBLER_PREFIX}/
+      self[:boot_device] = BOOT_DEV_NETWORK
+      self[:provisioning] = settings
+    elsif settings==PXE_OPTION_VALUE
       self[:boot_device]= BOOT_DEV_NETWORK
       self[:provisioning]= nil
     elsif settings==HD_OPTION_VALUE
@@ -241,6 +246,28 @@ class Vm < ActiveRecord::Base
   def search_users
     vm_resource_pool.search_users
   end
+  
+  # Reports whether the VM is uses Cobbler for booting.
+  #
+  def uses_cobbler?
+    self.provisioning.include? COBBLER_PREFIX
+  end
+  
+  # Returns the cobbler type.
+  #
+  def cobbler_type
+    if self.uses_cobbler?
+      self.provisioning[/^(.*)@/,1]
+    end
+  end
+
+  # Returns the cobbler provisioning name.
+  #
+  def cobbler_name
+    if self.uses_cobbler?
+      self.provisioning[/^.*@.*:(.*)/,1]
+    end
+  end
 
   protected
   def validate
diff --git a/src/task-omatic/task_vm.rb b/src/task-omatic/task_vm.rb
index 3588224..11b7b8a 100644
--- a/src/task-omatic/task_vm.rb
+++ b/src/task-omatic/task_vm.rb
@@ -154,15 +154,12 @@ def create_vm(task)
   # create cobbler system profile
   begin
     if vm.provisioning and !vm.provisioning.empty?
-      provisioning_arr = vm.provisioning.split(Vm::PROVISIONING_DELIMITER)
-      if provisioning_arr[0]==Vm::COBBLER_PREFIX
-        if provisioning_arr[1]==Vm::PROFILE_PREFIX
+      if vm.uses_cobbler?
+        if vm.cobbler_type == Vm::PROFILE_PREFIX: 
           system = Cobbler::System.new('name' => vm.uuid,
                                        'profile' => provisioning_arr[2])
           system.interfaces=[Cobbler::NetworkInterface.new({'mac_address' => vm.vnic_mac_addr})]
           system.save
-        elsif provisioning_arr[1]==Vm::IMAGE_PREFIX
-          #FIXME handle cobbler images
         end
       end
     end
@@ -267,6 +264,21 @@ def start_vm(task)
     host = findHostSLA(vm)
 
     conn = Libvirt::open("qemu+tcp://" + host.hostname + "/system")
+    
+    # if the VM is an image, then prepare things
+    if vm.uses_cobbler? && (vm.cobbler_type == Vm.IMAGE_PREFIX)
+        details = Cobbler::Image.find_one(vm.cobbler_name)
+        
+        raise Exception.new("Image #{vm.cobbler_name} not found in Cobbler server") unless details
+  
+        # if the file's an .ISO then we'll have to mount as a CDrom, otherwise
+        # we'll mount as a hard drive
+        if details.file[/.iso/]
+          # TODO mount details.file as a CDROM
+        else 
+          # TODO mount details.file as a hard disk
+        end
+    end
 
     storagedevs = connect_storage_pools(conn, vm)
 
diff --git a/src/test/unit/vm_test.rb b/src/test/unit/vm_test.rb
index 4a5e353..2fbdf59 100644
--- a/src/test/unit/vm_test.rb
+++ b/src/test/unit/vm_test.rb
@@ -22,8 +22,59 @@ require File.dirname(__FILE__) + '/../test_helper'
 class VmTest < Test::Unit::TestCase
   fixtures :vms
 
-  # Replace this with your real tests.
-  def test_truth
-    assert true
+  def setup
+    @vm_name = "Test"
+    @no_cobbler_provisioning = "#{@vm_name}"
+    @cobbler_image_provisioning = 
+      "#{Vm::IMAGE_PREFIX}@#{Vm::COBBLER_PREFIX}#{Vm::PROVISIONING_DELIMITER}#{@vm_name}"
+    @cobbler_profile_provisioning = 
+      "#{Vm::PROFILE_PREFIX}@#{Vm::COBBLER_PREFIX}#{Vm::PROVISIONING_DELIMITER}#{@vm_name}"
+    
+  end
+  
+  # Ensures that, if the VM does not contain the Cobbler prefix, that it
+  # does not claim to be a Cobbler VM.
+  #
+  def test_uses_cobbler_without_cobbler_prefix
+    vm = Vm.new
+    
+    vm.provisioning_and_boot_settings=@no_cobbler_provisioning
+    
+    flunk "VM is not a Cobbler provisioned one." if vm.uses_cobbler?
+    assert_equal @vm_name, vm.provisioning, "Wrong name reported."
+  end
+  
+  # Ensures that the VM reports that it uses Cobbler if the provisioning
+  # is for a Cobbler profile.
+  #
+  def test_uses_cobbler_with_cobbler_profile
+    vm = Vm.new    
+    
+    vm.provisioning_and_boot_settings = @cobbler_profile_provisioning
+    
+    flunk "VM did not report that it's Cobbler provisioned." unless vm.uses_cobbler?
+    assert_equal Vm::PROFILE_PREFIX, 
+      vm.cobbler_type, 
+      "Wrong cobbler type reported."
+    assert_equal @vm_name, 
+      vm.cobbler_name, 
+      "Wrong name reported."
+  end
+  
+  # Ensures that the VM reports that it uses Cobbler if the provisioning 
+  # is for a Cobbler image.
+  #
+  def test_uses_cobbler_With_cobbler_image
+    vm = Vm.new
+    
+    vm.provisioning_and_boot_settings = @cobbler_image_provisioning
+    
+    flunk "VM did not report that it's Cobbler provisioned." unless vm.uses_cobbler?
+    assert_equal Vm::IMAGE_PREFIX, 
+      vm.cobbler_type, 
+      "Wrong cobbler type reported."
+    assert_equal @vm_name,
+      vm.cobbler_name,
+      "Wrong name reported."
   end
 end
-- 
1.5.5.1




More information about the ovirt-devel mailing list