Package virtualization :: Module start_domain
[hide private]
[frames] | no frames]

Source Code for Module virtualization.start_domain

  1  # 
  2  # Copyright (c) 2008--2013 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  try: 
 17      # python 2 
 18      import commands 
 19  except ImportError: 
 20      import subprocess as commands 
 21   
 22  import libvirt 
 23  import os 
 24  import os.path 
 25  import re 
 26  import sys 
 27   
 28  from virtualization.domain_directory import DomainDirectory 
 29  from virtualization.domain_config    import DomainConfig, DomainConfigError 
 30  from virtualization.errors           import VirtualizationException 
 31  from spacewalk.common.usix import raise_with_tb 
 32   
 33  ############################################################################### 
 34  # Constants 
 35  ############################################################################### 
 36   
 37  PYGRUB = "/usr/bin/pygrub" 
 38   
 39  ############################################################################### 
 40  # Public Interface 
 41  ############################################################################### 
 42   
43 -def start_domain(uuid):
44 """ 45 Boots the domain for the first time after installation is complete. 46 """ 47 # Load the configuration file for this UUID. 48 domain = DomainDirectory() 49 config = domain.load_config(uuid) 50 51 # Connect to the hypervisor. 52 connection = libvirt.open(None) 53 54 # We will attempt to determine if the domain is configured to use a 55 # bootloader. If not, we'll have to explicitly use the kernel and initrd 56 # data provided in the config to start the domain. 57 try: 58 config.getConfigItem(DomainConfig.BOOTLOADER) 59 except DomainConfigError: 60 dce = sys.exc_info()[1] 61 # No bootloader tag present. Use pygrub to extract the kernel from 62 # the disk image if its Xen. For fully virt we dont have pygrub, it 63 # directly emulates the BIOS loading the first sector of the boot disk. 64 if connection.getType() == 'Xen': 65 # This uses pygrub which comes only with xen 66 _prepare_guest_kernel_and_ramdisk(config) 67 68 # Now, we'll restart the instance, this time using the re-create XML. 69 try: 70 domain = connection.createLinux(config.toXML(), 0) 71 except Exception: 72 e = sys.exc_info()[1] 73 raise_with_tb(VirtualizationException("Error occurred while attempting to recreate domain %s: %s" % 74 (uuid, str(e))), sys.exc_info()[2])
75 76 ############################################################################### 77 # Helper Methods 78 ############################################################################### 79
80 -def _prepare_guest_kernel_and_ramdisk(config):
81 """ 82 Use PyGrub to extract the kernel and ramdisk from the given disk image. 83 """ 84 85 disk_image = config.getConfigItem(DomainConfig.DISK_IMAGE_PATH) 86 87 # Use pygrub to extract the initrd and the kernel from the disk image. 88 (status, output) = \ 89 commands.getstatusoutput("%s -q %s" % (PYGRUB, disk_image)) 90 if status != 0: 91 raise VirtualizationException("Error occurred while executing '%s' (status=%d). Output=%s" % 92 (PYGRUB, status, output)) 93 94 # Now analyze the output and extract the names of the new kernel and initrd 95 # images from it. 96 (pygrub_kernel_path, pygrub_initrd_path) = \ 97 _extract_image_paths_from_pygrub_output(output) 98 99 # Rename the extracted images to the names we are pointing to in the 100 # configuration file. 101 runtime_kernel_path = config.getConfigItem(DomainConfig.KERNEL_PATH) 102 runtime_initrd_path = config.getConfigItem(DomainConfig.RAMDISK_PATH) 103 104 try: 105 os.rename(pygrub_kernel_path, runtime_kernel_path) 106 os.rename(pygrub_initrd_path, runtime_initrd_path) 107 except OSError: 108 oe = sys.exc_info()[1] 109 raise_with_tb(VirtualizationException("Error occurred while renaming runtime image paths: %s" % str(oe)), 110 sys.exc_info()[2])
111 112
113 -def _extract_image_paths_from_pygrub_output(output):
114 """ 115 Searches for the paths of the kernel and initrd files in the output of 116 pygrub. If not found, a VirtualizationException is raised. Otherwise, 117 the (kernel_path, initrd_path) tuple is returned. 118 """ 119 match = re.search("^linux \(kernel (\S+)\)\(ramdisk (\S+)\)", 120 output, 121 re.MULTILINE) 122 if match is None or len(match.groups()) != 2: 123 raise VirtualizationException("Could not locate kernel and initrd in pygrub output: %s" % output) 124 125 kernel_path = match.group(1) 126 initrd_path = match.group(2) 127 128 return (kernel_path, initrd_path)
129 130 if __name__ == "__main__": 131 print("result=", start_domain(sys.argv[1])) 132