Advertisement
Guest User

Untitled

a guest
Sep 9th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.18 KB | None | 0 0
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2.  
  3. # Copyright (c) 2010 Citrix Systems, Inc.
  4. # Copyright 2011 Piston Cloud Computing, Inc.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License. You may obtain
  8. # a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. # License for the specific language governing permissions and limitations
  16. # under the License.
  17.  
  18. """
  19. Helper methods for operations related to the management of VM records and
  20. their attributes like VDIs, VIFs, as well as their lookup functions.
  21. """
  22.  
  23. import json
  24. import os
  25. import pickle
  26. import re
  27. import sys
  28. import tempfile
  29. import time
  30. import urllib
  31. import uuid
  32. from xml.dom import minidom
  33.  
  34. import glance.client
  35. from nova import db
  36. from nova import exception
  37. from nova import flags
  38. import nova.image
  39. from nova.image import glance as glance_image_service
  40. from nova import log as logging
  41. from nova import utils
  42. from nova.compute import instance_types
  43. from nova.compute import power_state
  44. from nova.virt import disk
  45. from nova.virt import images
  46. from nova.virt.xenapi import HelperBase
  47. from nova.virt.xenapi.volume_utils import StorageError
  48.  
  49.  
  50. LOG = logging.getLogger("nova.virt.xenapi.vm_utils")
  51.  
  52. FLAGS = flags.FLAGS
  53. flags.DEFINE_string('default_os_type', 'linux', 'Default OS type')
  54. flags.DEFINE_integer('block_device_creation_timeout', 10,
  55. 'time to wait for a block device to be created')
  56. flags.DEFINE_integer('max_kernel_ramdisk_size', 16 * 1024 * 1024,
  57. 'maximum size in bytes of kernel or ramdisk images')
  58.  
  59. XENAPI_POWER_STATE = {
  60. 'Halted': power_state.SHUTDOWN,
  61. 'Running': power_state.RUNNING,
  62. 'Paused': power_state.PAUSED,
  63. 'Suspended': power_state.SUSPENDED,
  64. 'Crashed': power_state.CRASHED}
  65.  
  66.  
  67. SECTOR_SIZE = 512
  68. MBR_SIZE_SECTORS = 63
  69. MBR_SIZE_BYTES = MBR_SIZE_SECTORS * SECTOR_SIZE
  70. KERNEL_DIR = '/boot/guest'
  71.  
  72.  
  73. class ImageType:
  74. """
  75. Enumeration class for distinguishing different image types
  76. 0 - kernel image (goes on dom0's filesystem)
  77. 1 - ramdisk image (goes on dom0's filesystem)
  78. 2 - disk image (local SR, partitioned by objectstore plugin)
  79. 3 - raw disk image (local SR, NOT partitioned by plugin)
  80. 4 - vhd disk image (local SR, NOT inspected by XS, PV assumed for
  81. linux, HVM assumed for Windows)
  82. 5 - ISO disk image (local SR, NOT partitioned by plugin)
  83. """
  84.  
  85. KERNEL = 0
  86. RAMDISK = 1
  87. DISK = 2
  88. DISK_RAW = 3
  89. DISK_VHD = 4
  90. DISK_ISO = 5
  91. _ids = (KERNEL, RAMDISK, DISK, DISK_RAW, DISK_VHD, DISK_ISO)
  92.  
  93. KERNEL_STR = "kernel"
  94. RAMDISK_STR = "ramdisk"
  95. DISK_STR = "os"
  96. DISK_RAW_STR = "os_raw"
  97. DISK_VHD_STR = "vhd"
  98. DISK_ISO_STR = "iso"
  99. _strs = (KERNEL_STR, RAMDISK_STR, DISK_STR, DISK_RAW_STR, DISK_VHD_STR,
  100. DISK_ISO_STR)
  101.  
  102. @classmethod
  103. def to_string(cls, image_type):
  104. return dict(zip(ImageType._ids, ImageType._strs)).get(image_type)
  105.  
  106. @classmethod
  107. def from_string(cls, image_type_str):
  108. return dict(zip(ImageType._strs, ImageType._ids)).get(image_type_str)
  109.  
  110.  
  111. class VMHelper(HelperBase):
  112. """
  113. The class that wraps the helper methods together.
  114. """
  115.  
  116. @classmethod
  117. def create_vm(cls, session, instance, kernel, ramdisk,
  118. use_pv_kernel=False):
  119. """Create a VM record. Returns a Deferred that gives the new
  120. VM reference.
  121. the use_pv_kernel flag indicates whether the guest is HVM or PV
  122.  
  123. There are 3 scenarios:
  124.  
  125. 1. Using paravirtualization, kernel passed in
  126.  
  127. 2. Using paravirtualization, kernel within the image
  128.  
  129. 3. Using hardware virtualization
  130. """
  131.  
  132. inst_type_id = instance.instance_type_id
  133. instance_type = instance_types.get_instance_type(inst_type_id)
  134. mem = str(long(instance_type['memory_mb']) * 1024 * 1024)
  135. vcpus = str(instance_type['vcpus'])
  136. rec = {
  137. 'actions_after_crash': 'destroy',
  138. 'actions_after_reboot': 'restart',
  139. 'actions_after_shutdown': 'destroy',
  140. 'affinity': '',
  141. 'blocked_operations': {},
  142. 'ha_always_run': False,
  143. 'ha_restart_priority': '',
  144. 'HVM_boot_params': {},
  145. 'HVM_boot_policy': '',
  146. 'is_a_template': False,
  147. 'memory_dynamic_min': mem,
  148. 'memory_dynamic_max': mem,
  149. 'memory_static_min': '0',
  150. 'memory_static_max': mem,
  151. 'memory_target': mem,
  152. 'name_description': '',
  153. 'name_label': instance.name,
  154. 'other_config': {'allowvssprovider': False},
  155. 'other_config': {},
  156. 'PCI_bus': '',
  157. 'platform': {'acpi': 'true', 'apic': 'true', 'pae': 'true',
  158. 'viridian': 'true', 'timeoffset': '0'},
  159. 'PV_args': '',
  160. 'PV_bootloader': '',
  161. 'PV_bootloader_args': '',
  162. 'PV_kernel': '',
  163. 'PV_legacy_args': '',
  164. 'PV_ramdisk': '',
  165. 'recommendations': '',
  166. 'tags': [],
  167. 'user_version': '0',
  168. 'VCPUs_at_startup': vcpus,
  169. 'VCPUs_max': vcpus,
  170. 'VCPUs_params': {},
  171. 'xenstore_data': {}}
  172. # Complete VM configuration record according to the image type
  173. # non-raw/raw with PV kernel/raw in HVM mode
  174. if use_pv_kernel:
  175. rec['platform']['nx'] = 'false'
  176. if instance.kernel_id:
  177. # 1. Kernel explicitly passed in, use that
  178. rec['PV_args'] = 'root=/dev/xvda1'
  179. rec['PV_kernel'] = kernel
  180. rec['PV_ramdisk'] = ramdisk
  181. else:
  182. # 2. Use kernel within the image
  183. rec['PV_bootloader'] = 'pygrub'
  184. else:
  185. # 3. Using hardware virtualization
  186. rec['platform']['nx'] = 'true'
  187. rec['HVM_boot_params'] = {'order': 'dc'}
  188. rec['HVM_boot_policy'] = 'BIOS order'
  189.  
  190. LOG.debug(_('Created VM %s...'), instance.name)
  191. vm_ref = session.call_xenapi('VM.create', rec)
  192. instance_name = instance.name
  193. LOG.debug(_('Created VM %(instance_name)s as %(vm_ref)s.') % locals())
  194. return vm_ref
  195.  
  196. @classmethod
  197. def ensure_free_mem(cls, session, instance):
  198. inst_type_id = instance.instance_type_id
  199. instance_type = instance_types.get_instance_type(inst_type_id)
  200. mem = long(instance_type['memory_mb']) * 1024 * 1024
  201. #get free memory from host
  202. host = session.get_xenapi_host()
  203. host_free_mem = long(session.get_xenapi().host.
  204. compute_free_memory(host))
  205. return host_free_mem >= mem
  206.  
  207. @classmethod
  208. def create_vbd(cls, session, vm_ref, vdi_ref, userdevice, bootable):
  209. """Create a VBD record. Returns a Deferred that gives the new
  210. VBD reference."""
  211. vbd_rec = {}
  212. vbd_rec['VM'] = vm_ref
  213. vbd_rec['VDI'] = vdi_ref
  214. vbd_rec['userdevice'] = str(userdevice)
  215. vbd_rec['bootable'] = bootable
  216. vbd_rec['mode'] = 'RW'
  217. vbd_rec['type'] = 'disk'
  218. vbd_rec['unpluggable'] = True
  219. vbd_rec['empty'] = False
  220. vbd_rec['other_config'] = {}
  221. vbd_rec['qos_algorithm_type'] = ''
  222. vbd_rec['qos_algorithm_params'] = {}
  223. vbd_rec['qos_supported_algorithms'] = []
  224. LOG.debug(_('Creating VBD for VM %(vm_ref)s,'
  225. ' VDI %(vdi_ref)s ... ') % locals())
  226. vbd_ref = session.call_xenapi('VBD.create', vbd_rec)
  227. LOG.debug(_('Created VBD %(vbd_ref)s for VM %(vm_ref)s,'
  228. ' VDI %(vdi_ref)s.') % locals())
  229. return vbd_ref
  230.  
  231. @classmethod
  232. def create_cd_vbd(cls, session, vm_ref, vdi_ref, userdevice, bootable):
  233. """Create a VBD record. Returns a Deferred that gives the new
  234. VBD reference specific to CDRom devices."""
  235. vbd_rec = {}
  236. vbd_rec['VM'] = vm_ref
  237. vbd_rec['VDI'] = vdi_ref
  238. vbd_rec['userdevice'] = str(userdevice)
  239. vbd_rec['bootable'] = bootable
  240. vbd_rec['mode'] = 'RO'
  241. vbd_rec['type'] = 'CD'
  242. vbd_rec['unpluggable'] = True
  243. vbd_rec['empty'] = False
  244. vbd_rec['other_config'] = {}
  245. vbd_rec['qos_algorithm_type'] = ''
  246. vbd_rec['qos_algorithm_params'] = {}
  247. vbd_rec['qos_supported_algorithms'] = []
  248. LOG.debug(_('Creating a CDROM-specific VBD for VM %(vm_ref)s,'
  249. ' VDI %(vdi_ref)s ... ') % locals())
  250. vbd_ref = session.call_xenapi('VBD.create', vbd_rec)
  251. LOG.debug(_('Created a CDROM-specific VBD %(vbd_ref)s '
  252. ' for VM %(vm_ref)s, VDI %(vdi_ref)s.') % locals())
  253. return vbd_ref
  254.  
  255. @classmethod
  256. def find_vbd_by_number(cls, session, vm_ref, number):
  257. """Get the VBD reference from the device number"""
  258. vbd_refs = session.get_xenapi().VM.get_VBDs(vm_ref)
  259. if vbd_refs:
  260. for vbd_ref in vbd_refs:
  261. try:
  262. vbd_rec = session.get_xenapi().VBD.get_record(vbd_ref)
  263. if vbd_rec['userdevice'] == str(number):
  264. return vbd_ref
  265. except cls.XenAPI.Failure, exc:
  266. LOG.exception(exc)
  267. raise StorageError(_('VBD not found in instance %s') % vm_ref)
  268.  
  269. @classmethod
  270. def unplug_vbd(cls, session, vbd_ref):
  271. """Unplug VBD from VM"""
  272. try:
  273. vbd_ref = session.call_xenapi('VBD.unplug', vbd_ref)
  274. except cls.XenAPI.Failure, exc:
  275. LOG.exception(exc)
  276. if exc.details[0] != 'DEVICE_ALREADY_DETACHED':
  277. raise StorageError(_('Unable to unplug VBD %s') % vbd_ref)
  278.  
  279. @classmethod
  280. def destroy_vbd(cls, session, vbd_ref):
  281. """Destroy VBD from host database"""
  282. try:
  283. task = session.call_xenapi('Async.VBD.destroy', vbd_ref)
  284. session.wait_for_task(task)
  285. except cls.XenAPI.Failure, exc:
  286. LOG.exception(exc)
  287. raise StorageError(_('Unable to destroy VBD %s') % vbd_ref)
  288.  
  289. @classmethod
  290. def destroy_vdi(cls, session, vdi_ref):
  291. try:
  292. task = session.call_xenapi('Async.VDI.destroy', vdi_ref)
  293. session.wait_for_task(task)
  294. except cls.XenAPI.Failure, exc:
  295. LOG.exception(exc)
  296. raise StorageError(_('Unable to destroy VDI %s') % vdi_ref)
  297.  
  298. @classmethod
  299. def create_vdi(cls, session, sr_ref, name_label, virtual_size, read_only):
  300. """Create a VDI record and returns its reference."""
  301. vdi_ref = session.get_xenapi().VDI.create(
  302. {'name_label': name_label,
  303. 'name_description': '',
  304. 'SR': sr_ref,
  305. 'virtual_size': str(virtual_size),
  306. 'type': 'User',
  307. 'sharable': False,
  308. 'read_only': read_only,
  309. 'xenstore_data': {},
  310. 'other_config': {},
  311. 'sm_config': {},
  312. 'tags': []})
  313. LOG.debug(_('Created VDI %(vdi_ref)s (%(name_label)s,'
  314. ' %(virtual_size)s, %(read_only)s) on %(sr_ref)s.')
  315. % locals())
  316. return vdi_ref
  317.  
  318. @classmethod
  319. def get_vdi_for_vm_safely(cls, session, vm_ref):
  320. """Retrieves the primary VDI for a VM"""
  321. vbd_refs = session.get_xenapi().VM.get_VBDs(vm_ref)
  322. for vbd in vbd_refs:
  323. vbd_rec = session.get_xenapi().VBD.get_record(vbd)
  324. # Convention dictates the primary VDI will be userdevice 0
  325. if vbd_rec['userdevice'] == '0':
  326. vdi_rec = session.get_xenapi().VDI.get_record(vbd_rec['VDI'])
  327. return vbd_rec['VDI'], vdi_rec
  328. raise exception.Error(_("No primary VDI found for"
  329. "%(vm_ref)s") % locals())
  330.  
  331. @classmethod
  332. def create_snapshot(cls, session, instance_id, vm_ref, label):
  333. """Creates Snapshot (Template) VM, Snapshot VBD, Snapshot VDI,
  334. Snapshot VHD"""
  335. #TODO(sirp): Add quiesce and VSS locking support when Windows support
  336. # is added
  337. LOG.debug(_("Snapshotting VM %(vm_ref)s with label '%(label)s'...")
  338. % locals())
  339.  
  340. vm_vdi_ref, vm_vdi_rec = cls.get_vdi_for_vm_safely(session, vm_ref)
  341. sr_ref = vm_vdi_rec["SR"]
  342.  
  343. original_parent_uuid = get_vhd_parent_uuid(session, vm_vdi_ref)
  344.  
  345. task = session.call_xenapi('Async.VM.snapshot', vm_ref, label)
  346. template_vm_ref = session.wait_for_task(task, instance_id)
  347. template_vdi_rec = cls.get_vdi_for_vm_safely(session,
  348. template_vm_ref)[1]
  349. template_vdi_uuid = template_vdi_rec["uuid"]
  350.  
  351. LOG.debug(_('Created snapshot %(template_vm_ref)s from'
  352. ' VM %(vm_ref)s.') % locals())
  353.  
  354. parent_uuid = wait_for_vhd_coalesce(
  355. session, instance_id, sr_ref, vm_vdi_ref, original_parent_uuid)
  356.  
  357. #TODO(sirp): we need to assert only one parent, not parents two deep
  358. template_vdi_uuids = {'image': parent_uuid,
  359. 'snap': template_vdi_uuid}
  360. return template_vm_ref, template_vdi_uuids
  361.  
  362. @classmethod
  363. def get_sr_path(cls, session):
  364. """Return the path to our storage repository
  365.  
  366. This is used when we're dealing with VHDs directly, either by taking
  367. snapshots or by restoring an image in the DISK_VHD format.
  368. """
  369. sr_ref = safe_find_sr(session)
  370. sr_rec = session.get_xenapi().SR.get_record(sr_ref)
  371. sr_uuid = sr_rec["uuid"]
  372. return os.path.join(FLAGS.xenapi_sr_base_path, sr_uuid)
  373.  
  374. @classmethod
  375. def upload_image(cls, context, session, instance, vdi_uuids, image_id):
  376. """ Requests that the Glance plugin bundle the specified VDIs and
  377. push them into Glance using the specified human-friendly name.
  378. """
  379. # NOTE(sirp): Currently we only support uploading images as VHD, there
  380. # is no RAW equivalent (yet)
  381. logging.debug(_("Asking xapi to upload %(vdi_uuids)s as"
  382. " ID %(image_id)s") % locals())
  383.  
  384. os_type = instance.os_type or FLAGS.default_os_type
  385.  
  386. glance_host, glance_port = \
  387. glance_image_service.pick_glance_api_server()
  388. params = {'vdi_uuids': vdi_uuids,
  389. 'image_id': image_id,
  390. 'glance_host': glance_host,
  391. 'glance_port': glance_port,
  392. 'sr_path': cls.get_sr_path(session),
  393. 'os_type': os_type,
  394. 'auth_token': getattr(context, 'auth_token', None)}
  395.  
  396. kwargs = {'params': pickle.dumps(params)}
  397. task = session.async_call_plugin('glance', 'upload_vhd', kwargs)
  398. session.wait_for_task(task, instance.id)
  399.  
  400. @classmethod
  401. def fetch_blank_disk(cls, session, instance_type_id):
  402. # Size the blank harddrive to suit the machine type:
  403. one_gig = 1024 * 1024 * 1024
  404. req_type = instance_types.get_instance_type(instance_type_id)
  405. req_size = req_type['local_gb']
  406.  
  407. LOG.debug("Creating blank HD of size %(req_size)d gigs"
  408. % locals())
  409. vdi_size = one_gig * req_size
  410.  
  411. LOG.debug("ISO vm create: Looking for the SR")
  412. sr_ref = safe_find_sr(session)
  413.  
  414. vdi_ref = cls.create_vdi(session, sr_ref, 'blank HD', vdi_size, False)
  415. return vdi_ref
  416.  
  417. @classmethod
  418. def fetch_image(cls, context, session, instance, image, user_id,
  419. project_id, image_type):
  420. """Fetch image from glance based on image type.
  421.  
  422. Returns: A single filename if image_type is KERNEL or RAMDISK
  423. A list of dictionaries that describe VDIs, otherwise
  424. """
  425. if image_type == ImageType.DISK_VHD:
  426. return cls._fetch_image_glance_vhd(context,
  427. session, instance, image, image_type)
  428. else:
  429. return cls._fetch_image_glance_disk(context,
  430. session, instance, image, image_type)
  431.  
  432. @classmethod
  433. def _fetch_image_glance_vhd(cls, context, session, instance, image,
  434. image_type):
  435. """Tell glance to download an image and put the VHDs into the SR
  436.  
  437. Returns: A list of dictionaries that describe VDIs
  438. """
  439. instance_id = instance.id
  440. LOG.debug(_("Asking xapi to fetch vhd image %(image)s")
  441. % locals())
  442. sr_ref = safe_find_sr(session)
  443.  
  444. # NOTE(sirp): The Glance plugin runs under Python 2.4
  445. # which does not have the `uuid` module. To work around this,
  446. # we generate the uuids here (under Python 2.6+) and
  447. # pass them as arguments
  448. uuid_stack = [str(uuid.uuid4()) for i in xrange(2)]
  449.  
  450. glance_host, glance_port = \
  451. glance_image_service.pick_glance_api_server()
  452. params = {'image_id': image,
  453. 'glance_host': glance_host,
  454. 'glance_port': glance_port,
  455. 'uuid_stack': uuid_stack,
  456. 'sr_path': cls.get_sr_path(session),
  457. 'auth_token': getattr(context, 'auth_token', None)}
  458.  
  459. kwargs = {'params': pickle.dumps(params)}
  460. task = session.async_call_plugin('glance', 'download_vhd', kwargs)
  461. result = session.wait_for_task(task, instance_id)
  462. # 'download_vhd' will return a json encoded string containing
  463. # a list of dictionaries describing VDIs. The dictionary will
  464. # contain 'vdi_type' and 'vdi_uuid' keys. 'vdi_type' can be
  465. # 'os' or 'swap' right now.
  466. vdis = json.loads(result)
  467. for vdi in vdis:
  468. LOG.debug(_("xapi 'download_vhd' returned VDI of "
  469. "type '%(vdi_type)s' with UUID '%(vdi_uuid)s'" % vdi))
  470.  
  471. cls.scan_sr(session, instance_id, sr_ref)
  472.  
  473. # Pull out the UUID of the first VDI (which is the os VDI)
  474. os_vdi_uuid = vdis[0]['vdi_uuid']
  475.  
  476. # Set the name-label to ease debugging
  477. vdi_ref = session.get_xenapi().VDI.get_by_uuid(os_vdi_uuid)
  478. primary_name_label = get_name_label_for_image(image)
  479. session.get_xenapi().VDI.set_name_label(vdi_ref, primary_name_label)
  480.  
  481. cls._check_vdi_size(context, session, instance, os_vdi_uuid)
  482. return vdis
  483.  
  484. @classmethod
  485. def _get_vdi_chain_size(cls, context, session, vdi_uuid):
  486. """Compute the total size of a VDI chain, starting with the specified
  487. VDI UUID.
  488.  
  489. This will walk the VDI chain to the root, add the size of each VDI into
  490. the total.
  491. """
  492. size_bytes = 0
  493. for vdi_rec in walk_vdi_chain(session, vdi_uuid):
  494. cur_vdi_uuid = vdi_rec['uuid']
  495. vdi_size_bytes = int(vdi_rec['physical_utilisation'])
  496. LOG.debug(_('vdi_uuid=%(cur_vdi_uuid)s vdi_size_bytes='
  497. '%(vdi_size_bytes)d' % locals()))
  498. size_bytes += vdi_size_bytes
  499. return size_bytes
  500.  
  501. @classmethod
  502. def _check_vdi_size(cls, context, session, instance, vdi_uuid):
  503. size_bytes = cls._get_vdi_chain_size(context, session, vdi_uuid)
  504.  
  505. # FIXME(jk0): this was copied directly from compute.manager.py, let's
  506. # refactor this to a common area
  507. instance_type_id = instance['instance_type_id']
  508. instance_type = db.instance_type_get(context,
  509. instance_type_id)
  510. allowed_size_gb = instance_type['local_gb']
  511. allowed_size_bytes = allowed_size_gb * 1024 * 1024 * 1024
  512.  
  513. LOG.debug(_("image_size_bytes=%(size_bytes)d, allowed_size_bytes="
  514. "%(allowed_size_bytes)d") % locals())
  515.  
  516. if size_bytes > allowed_size_bytes:
  517. LOG.info(_("Image size %(size_bytes)d exceeded"
  518. " instance_type allowed size "
  519. "%(allowed_size_bytes)d")
  520. % locals())
  521. raise exception.ImageTooLarge()
  522.  
  523. @classmethod
  524. def _fetch_image_glance_disk(cls, context, session, instance, image,
  525. image_type):
  526. """Fetch the image from Glance
  527.  
  528. NOTE:
  529. Unlike _fetch_image_glance_vhd, this method does not use the Glance
  530. plugin; instead, it streams the disks through domU to the VDI
  531. directly.
  532.  
  533. Returns: A single filename if image_type is KERNEL_RAMDISK
  534. A list of dictionaries that describe VDIs, otherwise
  535. """
  536. instance_id = instance.id
  537. # FIXME(sirp): Since the Glance plugin seems to be required for the
  538. # VHD disk, it may be worth using the plugin for both VHD and RAW and
  539. # DISK restores
  540. LOG.debug(_("Fetching image %(image)s") % locals())
  541. LOG.debug(_("Image Type: %s"), ImageType.to_string(image_type))
  542.  
  543. if image_type == ImageType.DISK_ISO:
  544. sr_ref = safe_find_iso_sr(session)
  545. LOG.debug(_("ISO: Found sr possibly containing the ISO image"))
  546. else:
  547. sr_ref = safe_find_sr(session)
  548.  
  549. glance_client, image_id = nova.image.get_glance_client(image)
  550. glance_client.set_auth_token(getattr(context, 'auth_token', None))
  551. meta, image_file = glance_client.get_image(image_id)
  552. virtual_size = int(meta['size'])
  553. vdi_size = virtual_size
  554. LOG.debug(_("Size for image %(image)s:" +
  555. "%(virtual_size)d") % locals())
  556. if image_type == ImageType.DISK:
  557. # Make room for MBR.
  558. vdi_size += MBR_SIZE_BYTES
  559. elif image_type in (ImageType.KERNEL, ImageType.RAMDISK) and \
  560. vdi_size > FLAGS.max_kernel_ramdisk_size:
  561. max_size = FLAGS.max_kernel_ramdisk_size
  562. raise exception.Error(
  563. _("Kernel/Ramdisk image is too large: %(vdi_size)d bytes, "
  564. "max %(max_size)d bytes") % locals())
  565.  
  566. name_label = get_name_label_for_image(image)
  567. vdi_ref = cls.create_vdi(session, sr_ref, name_label, vdi_size, False)
  568. # From this point we have a VDI on Xen host;
  569. # If anything goes wrong, we need to remember its uuid.
  570. try:
  571. filename = None
  572. vdi_uuid = session.get_xenapi().VDI.get_uuid(vdi_ref)
  573. with_vdi_attached_here(session, vdi_ref, False,
  574. lambda dev:
  575. _stream_disk(dev, image_type,
  576. virtual_size, image_file))
  577. if image_type in (ImageType.KERNEL, ImageType.RAMDISK):
  578. # We need to invoke a plugin for copying the
  579. # content of the VDI into the proper path.
  580. LOG.debug(_("Copying VDI %s to /boot/guest on dom0"), vdi_ref)
  581. fn = "copy_kernel_vdi"
  582. args = {}
  583. args['vdi-ref'] = vdi_ref
  584. # Let the plugin copy the correct number of bytes.
  585. args['image-size'] = str(vdi_size)
  586. task = session.async_call_plugin('glance', fn, args)
  587. filename = session.wait_for_task(task, instance_id)
  588. # Remove the VDI as it is not needed anymore.
  589. session.get_xenapi().VDI.destroy(vdi_ref)
  590. LOG.debug(_("Kernel/Ramdisk VDI %s destroyed"), vdi_ref)
  591. return [dict(vdi_type=ImageType.to_string(image_type),
  592. vdi_uuid=None,
  593. file=filename)]
  594. else:
  595. return [dict(vdi_type=ImageType.to_string(image_type),
  596. vdi_uuid=vdi_uuid,
  597. file=None)]
  598. except (cls.XenAPI.Failure, IOError, OSError) as e:
  599. # We look for XenAPI and OS failures.
  600. LOG.exception(_("instance %s: Failed to fetch glance image"),
  601. instance_id, exc_info=sys.exc_info())
  602. e.args = e.args + ([dict(vdi_type=ImageType.
  603. to_string(image_type),
  604. vdi_uuid=vdi_uuid,
  605. file=filename)],)
  606. raise e
  607.  
  608. @classmethod
  609. def determine_disk_image_type(cls, instance):
  610. """Disk Image Types are used to determine where the kernel will reside
  611. within an image. To figure out which type we're dealing with, we use
  612. the following rules:
  613.  
  614. 1. If we're using Glance, we can use the image_type field to
  615. determine the image_type
  616.  
  617. 2. If we're not using Glance, then we need to deduce this based on
  618. whether a kernel_id is specified.
  619. """
  620. def log_disk_format(image_type):
  621. pretty_format = {ImageType.KERNEL: 'KERNEL',
  622. ImageType.RAMDISK: 'RAMDISK',
  623. ImageType.DISK: 'DISK',
  624. ImageType.DISK_RAW: 'DISK_RAW',
  625. ImageType.DISK_VHD: 'DISK_VHD',
  626. ImageType.DISK_ISO: 'DISK_ISO'}
  627. disk_format = pretty_format[image_type]
  628. image_ref = instance.image_ref
  629. instance_id = instance.id
  630. LOG.debug(_("Detected %(disk_format)s format for image "
  631. "%(image_ref)s, instance %(instance_id)s") % locals())
  632.  
  633. def determine_from_glance():
  634. glance_disk_format2nova_type = {
  635. 'ami': ImageType.DISK,
  636. 'aki': ImageType.KERNEL,
  637. 'ari': ImageType.RAMDISK,
  638. 'raw': ImageType.DISK_RAW,
  639. 'vhd': ImageType.DISK_VHD,
  640. 'iso': ImageType.DISK_ISO}
  641. image_ref = instance.image_ref
  642. glance_client, image_id = nova.image.get_glance_client(image_ref)
  643. meta = glance_client.get_image_meta(image_id)
  644. disk_format = meta['disk_format']
  645. try:
  646. return glance_disk_format2nova_type[disk_format]
  647. except KeyError:
  648. raise exception.InvalidDiskFormat(disk_format=disk_format)
  649.  
  650. def determine_from_instance():
  651. if instance.kernel_id:
  652. return ImageType.DISK
  653. else:
  654. return ImageType.DISK_RAW
  655.  
  656. image_type = determine_from_glance()
  657.  
  658. log_disk_format(image_type)
  659. return image_type
  660.  
  661. @classmethod
  662. def determine_is_pv(cls, session, instance_id, vdi_ref, disk_image_type,
  663. os_type):
  664. """
  665. Determine whether the VM will use a paravirtualized kernel or if it
  666. will use hardware virtualization.
  667.  
  668. 1. Glance (VHD): then we use `os_type`, raise if not set
  669.  
  670. 2. Glance (DISK_RAW): use Pygrub to figure out if pv kernel is
  671. available
  672.  
  673. 3. Glance (DISK): pv is assumed
  674.  
  675. 4. Glance (DISK_ISO): no pv is assumed
  676. """
  677.  
  678. LOG.debug(_("Looking up vdi %s for PV kernel"), vdi_ref)
  679. if disk_image_type == ImageType.DISK_VHD:
  680. # 1. VHD
  681. if os_type == 'windows':
  682. is_pv = False
  683. else:
  684. is_pv = True
  685. elif disk_image_type == ImageType.DISK_RAW:
  686. # 2. RAW
  687. is_pv = with_vdi_attached_here(session, vdi_ref, True, _is_vdi_pv)
  688. elif disk_image_type == ImageType.DISK:
  689. # 3. Disk
  690. is_pv = True
  691. elif disk_image_type == ImageType.DISK_ISO:
  692. # 4. ISO
  693. is_pv = False
  694. else:
  695. raise exception.Error(_("Unknown image format %(disk_image_type)s")
  696. % locals())
  697.  
  698. return is_pv
  699.  
  700. @classmethod
  701. def lookup(cls, session, name_label):
  702. """Look the instance i up, and returns it if available"""
  703. vm_refs = session.get_xenapi().VM.get_by_name_label(name_label)
  704. n = len(vm_refs)
  705. if n == 0:
  706. return None
  707. elif n > 1:
  708. raise exception.InstanceExists(name=name_label)
  709. else:
  710. return vm_refs[0]
  711.  
  712. @classmethod
  713. def lookup_vm_vdis(cls, session, vm_ref):
  714. """Look for the VDIs that are attached to the VM"""
  715. # Firstly we get the VBDs, then the VDIs.
  716. # TODO(Armando): do we leave the read-only devices?
  717. vbd_refs = session.get_xenapi().VM.get_VBDs(vm_ref)
  718. vdi_refs = []
  719. if vbd_refs:
  720. for vbd_ref in vbd_refs:
  721. try:
  722. vdi_ref = session.get_xenapi().VBD.get_VDI(vbd_ref)
  723. # Test valid VDI
  724. record = session.get_xenapi().VDI.get_record(vdi_ref)
  725. LOG.debug(_('VDI %s is still available'), record['uuid'])
  726. except cls.XenAPI.Failure, exc:
  727. LOG.exception(exc)
  728. else:
  729. vdi_refs.append(vdi_ref)
  730. if len(vdi_refs) > 0:
  731. return vdi_refs
  732. else:
  733. return None
  734.  
  735. @classmethod
  736. def preconfigure_instance(cls, session, instance, vdi_ref, network_info):
  737. """Makes alterations to the image before launching as part of spawn.
  738. """
  739.  
  740. # As mounting the image VDI is expensive, we only want do do it once,
  741. # if at all, so determine whether it's required first, and then do
  742. # everything
  743. mount_required = False
  744. key, net, metadata = _prepare_injectables(instance, network_info)
  745. mount_required = key or net or metadata
  746. if not mount_required:
  747. return
  748.  
  749. with_vdi_attached_here(session, vdi_ref, False,
  750. lambda dev: _mounted_processing(dev, key, net,
  751. metadata))
  752.  
  753. @classmethod
  754. def lookup_kernel_ramdisk(cls, session, vm):
  755. vm_rec = session.get_xenapi().VM.get_record(vm)
  756. if 'PV_kernel' in vm_rec and 'PV_ramdisk' in vm_rec:
  757. return (vm_rec['PV_kernel'], vm_rec['PV_ramdisk'])
  758. else:
  759. return (None, None)
  760.  
  761. @classmethod
  762. def compile_info(cls, record):
  763. """Fill record with VM status information"""
  764. LOG.info(_("(VM_UTILS) xenserver vm state -> |%s|"),
  765. record['power_state'])
  766. LOG.info(_("(VM_UTILS) xenapi power_state -> |%s|"),
  767. XENAPI_POWER_STATE[record['power_state']])
  768. return {'state': XENAPI_POWER_STATE[record['power_state']],
  769. 'max_mem': long(record['memory_static_max']) >> 10,
  770. 'mem': long(record['memory_dynamic_max']) >> 10,
  771. 'num_cpu': record['VCPUs_max'],
  772. 'cpu_time': 0}
  773.  
  774. @classmethod
  775. def compile_diagnostics(cls, session, record):
  776. """Compile VM diagnostics data"""
  777. try:
  778. host = session.get_xenapi_host()
  779. host_ip = session.get_xenapi().host.get_record(host)["address"]
  780. except (cls.XenAPI.Failure, KeyError) as e:
  781. return {"Unable to retrieve diagnostics": e}
  782.  
  783. try:
  784. diags = {}
  785. xml = get_rrd(host_ip, record["uuid"])
  786. if xml:
  787. rrd = minidom.parseString(xml)
  788. for i, node in enumerate(rrd.firstChild.childNodes):
  789. # We don't want all of the extra garbage
  790. if i >= 3 and i <= 11:
  791. ref = node.childNodes
  792. # Name and Value
  793. if len(ref) > 6:
  794. diags[ref[0].firstChild.data] = \
  795. ref[6].firstChild.data
  796. return diags
  797. except cls.XenAPI.Failure as e:
  798. return {"Unable to retrieve diagnostics": e}
  799.  
  800. @classmethod
  801. def scan_sr(cls, session, instance_id=None, sr_ref=None):
  802. """Scans the SR specified by sr_ref"""
  803. if sr_ref:
  804. LOG.debug(_("Re-scanning SR %s"), sr_ref)
  805. task = session.call_xenapi('Async.SR.scan', sr_ref)
  806. session.wait_for_task(task, instance_id)
  807.  
  808. @classmethod
  809. def scan_default_sr(cls, session):
  810. """Looks for the system default SR and triggers a re-scan"""
  811. sr_ref = find_sr(session)
  812. session.call_xenapi('SR.scan', sr_ref)
  813.  
  814.  
  815. def get_rrd(host, vm_uuid):
  816. """Return the VM RRD XML as a string"""
  817. try:
  818. xml = urllib.urlopen("http://%s:%s@%s/vm_rrd?uuid=%s" % (
  819. FLAGS.xenapi_connection_username,
  820. FLAGS.xenapi_connection_password,
  821. host,
  822. vm_uuid))
  823. return xml.read()
  824. except IOError:
  825. return None
  826.  
  827.  
  828. #TODO(sirp): This code comes from XS5.6 pluginlib.py, we should refactor to
  829. # use that implmenetation
  830. def get_vhd_parent(session, vdi_rec):
  831. """
  832. Returns the VHD parent of the given VDI record, as a (ref, rec) pair.
  833. Returns None if we're at the root of the tree.
  834. """
  835. if 'vhd-parent' in vdi_rec['sm_config']:
  836. parent_uuid = vdi_rec['sm_config']['vhd-parent']
  837. parent_ref = session.get_xenapi().VDI.get_by_uuid(parent_uuid)
  838. parent_rec = session.get_xenapi().VDI.get_record(parent_ref)
  839. vdi_uuid = vdi_rec['uuid']
  840. LOG.debug(_("VHD %(vdi_uuid)s has parent %(parent_ref)s") % locals())
  841. return parent_ref, parent_rec
  842. else:
  843. return None
  844.  
  845.  
  846. def get_vhd_parent_uuid(session, vdi_ref):
  847. vdi_rec = session.get_xenapi().VDI.get_record(vdi_ref)
  848. ret = get_vhd_parent(session, vdi_rec)
  849. if ret:
  850. parent_ref, parent_rec = ret
  851. return parent_rec["uuid"]
  852. else:
  853. return None
  854.  
  855.  
  856. def walk_vdi_chain(session, vdi_uuid):
  857. """Yield vdi_recs for each element in a VDI chain"""
  858. # TODO(jk0): perhaps make get_vhd_parent use this
  859. while True:
  860. vdi_ref = session.get_xenapi().VDI.get_by_uuid(vdi_uuid)
  861. vdi_rec = session.get_xenapi().VDI.get_record(vdi_ref)
  862. yield vdi_rec
  863.  
  864. parent_uuid = vdi_rec['sm_config'].get('vhd-parent')
  865. if parent_uuid:
  866. vdi_uuid = parent_uuid
  867. else:
  868. break
  869.  
  870.  
  871. def wait_for_vhd_coalesce(session, instance_id, sr_ref, vdi_ref,
  872. original_parent_uuid):
  873. """ Spin until the parent VHD is coalesced into its parent VHD
  874.  
  875. Before coalesce:
  876. * original_parent_vhd
  877. * parent_vhd
  878. snapshot
  879.  
  880. Atter coalesce:
  881. * parent_vhd
  882. snapshot
  883. """
  884. max_attempts = FLAGS.xenapi_vhd_coalesce_max_attempts
  885. attempts = {'counter': 0}
  886.  
  887. def _poll_vhds():
  888. attempts['counter'] += 1
  889. if attempts['counter'] > max_attempts:
  890. counter = attempts['counter']
  891. msg = (_("VHD coalesce attempts exceeded (%(counter)d >"
  892. " %(max_attempts)d), giving up...") % locals())
  893. raise exception.Error(msg)
  894.  
  895. VMHelper.scan_sr(session, instance_id, sr_ref)
  896. parent_uuid = get_vhd_parent_uuid(session, vdi_ref)
  897. if original_parent_uuid and (parent_uuid != original_parent_uuid):
  898. LOG.debug(_("Parent %(parent_uuid)s doesn't match original parent"
  899. " %(original_parent_uuid)s, waiting for coalesce...")
  900. % locals())
  901. else:
  902. # Breakout of the loop (normally) and return the parent_uuid
  903. raise utils.LoopingCallDone(parent_uuid)
  904.  
  905. loop = utils.LoopingCall(_poll_vhds)
  906. loop.start(FLAGS.xenapi_vhd_coalesce_poll_interval, now=True)
  907. parent_uuid = loop.wait()
  908. return parent_uuid
  909.  
  910.  
  911. def get_vdi_for_vm_safely(session, vm_ref):
  912. vdi_refs = VMHelper.lookup_vm_vdis(session, vm_ref)
  913. if vdi_refs is None:
  914. raise Exception(_("No VDIs found for VM %s") % vm_ref)
  915. else:
  916. num_vdis = len(vdi_refs)
  917. if num_vdis != 1:
  918. raise exception.Error(_("Unexpected number of VDIs"
  919. "(%(num_vdis)s) found"
  920. " for VM %(vm_ref)s") % locals())
  921.  
  922. vdi_ref = vdi_refs[0]
  923. vdi_rec = session.get_xenapi().VDI.get_record(vdi_ref)
  924. return vdi_ref, vdi_rec
  925.  
  926.  
  927. def safe_find_sr(session):
  928. """Same as find_sr except raises a NotFound exception if SR cannot be
  929. determined
  930. """
  931. sr_ref = find_sr(session)
  932. if sr_ref is None:
  933. raise exception.StorageRepositoryNotFound()
  934. return sr_ref
  935.  
  936.  
  937. def find_sr(session):
  938. """Return the storage repository to hold VM images"""
  939. host = session.get_xenapi_host()
  940. sr_refs = session.get_xenapi().SR.get_all()
  941. for sr_ref in sr_refs:
  942. sr_rec = session.get_xenapi().SR.get_record(sr_ref)
  943. if not ('i18n-key' in sr_rec['other_config'] and
  944. sr_rec['other_config']['i18n-key'] == 'local-storage'):
  945. continue
  946. for pbd_ref in sr_rec['PBDs']:
  947. pbd_rec = session.get_xenapi().PBD.get_record(pbd_ref)
  948. if pbd_rec['host'] == host:
  949. return sr_ref
  950. return None
  951.  
  952.  
  953. def safe_find_iso_sr(session):
  954. """Same as find_iso_sr except raises a NotFound exception if SR cannot be
  955. determined
  956. """
  957. sr_ref = find_iso_sr(session)
  958. if sr_ref is None:
  959. raise exception.NotFound(_('Cannot find SR of content-type ISO'))
  960. return sr_ref
  961.  
  962.  
  963. def find_iso_sr(session):
  964. """Return the storage repository to hold ISO images"""
  965. host = session.get_xenapi_host()
  966. sr_refs = session.get_xenapi().SR.get_all()
  967. for sr_ref in sr_refs:
  968. sr_rec = session.get_xenapi().SR.get_record(sr_ref)
  969.  
  970. LOG.debug(_("ISO: looking at SR %(sr_rec)s") % locals())
  971. if not sr_rec['content_type'] == 'iso':
  972. LOG.debug(_("ISO: not iso content"))
  973. continue
  974. if not 'i18n-key' in sr_rec['other_config']:
  975. LOG.debug(_("ISO: iso content_type, no 'i18n-key' key"))
  976. continue
  977. if not sr_rec['other_config']['i18n-key'] == 'local-storage-iso':
  978. LOG.debug(_("ISO: iso content_type, i18n-key value not "
  979. "'local-storage-iso'"))
  980. continue
  981.  
  982. LOG.debug(_("ISO: SR MATCHing our criteria"))
  983. for pbd_ref in sr_rec['PBDs']:
  984. LOG.debug(_("ISO: ISO, looking to see if it is host local"))
  985. pbd_rec = session.get_xenapi().PBD.get_record(pbd_ref)
  986. pbd_rec_host = pbd_rec['host']
  987. LOG.debug(_("ISO: PBD matching, want %(pbd_rec)s, have %(host)s") %
  988. locals())
  989. if pbd_rec_host == host:
  990. LOG.debug(_("ISO: SR with local PBD"))
  991. return sr_ref
  992. return None
  993.  
  994.  
  995. def remap_vbd_dev(dev):
  996. """Return the appropriate location for a plugged-in VBD device
  997.  
  998. Ubuntu Maverick moved xvd? -> sd?. This is considered a bug and will be
  999. fixed in future versions:
  1000. https://bugs.launchpad.net/ubuntu/+source/linux/+bug/684875
  1001.  
  1002. For now, we work around it by just doing a string replace.
  1003. """
  1004. # NOTE(sirp): This hack can go away when we pull support for Maverick
  1005. should_remap = FLAGS.xenapi_remap_vbd_dev
  1006. if not should_remap:
  1007. return dev
  1008.  
  1009. old_prefix = 'xvd'
  1010. new_prefix = FLAGS.xenapi_remap_vbd_dev_prefix
  1011. remapped_dev = dev.replace(old_prefix, new_prefix)
  1012.  
  1013. return remapped_dev
  1014.  
  1015.  
  1016. def _wait_for_device(dev):
  1017. """Wait for device node to appear"""
  1018. for i in xrange(0, FLAGS.block_device_creation_timeout):
  1019. if os.path.exists('/dev/%s' % dev):
  1020. return
  1021. time.sleep(1)
  1022.  
  1023. raise StorageError(_('Timeout waiting for device %s to be created') % dev)
  1024.  
  1025.  
  1026. def with_vdi_attached_here(session, vdi_ref, read_only, f):
  1027. this_vm_ref = get_this_vm_ref(session)
  1028. vbd_rec = {}
  1029. vbd_rec['VM'] = this_vm_ref
  1030. vbd_rec['VDI'] = vdi_ref
  1031. vbd_rec['userdevice'] = 'autodetect'
  1032. vbd_rec['bootable'] = False
  1033. vbd_rec['mode'] = read_only and 'RO' or 'RW'
  1034. vbd_rec['type'] = 'disk'
  1035. vbd_rec['unpluggable'] = True
  1036. vbd_rec['empty'] = False
  1037. vbd_rec['other_config'] = {}
  1038. vbd_rec['qos_algorithm_type'] = ''
  1039. vbd_rec['qos_algorithm_params'] = {}
  1040. vbd_rec['qos_supported_algorithms'] = []
  1041. LOG.debug(_('Creating VBD for VDI %s ... '), vdi_ref)
  1042. vbd_ref = session.get_xenapi().VBD.create(vbd_rec)
  1043. LOG.debug(_('Creating VBD for VDI %s done.'), vdi_ref)
  1044. try:
  1045. LOG.debug(_('Plugging VBD %s ... '), vbd_ref)
  1046. session.get_xenapi().VBD.plug(vbd_ref)
  1047. LOG.debug(_('Plugging VBD %s done.'), vbd_ref)
  1048. orig_dev = session.get_xenapi().VBD.get_device(vbd_ref)
  1049. LOG.debug(_('VBD %(vbd_ref)s plugged as %(orig_dev)s') % locals())
  1050. dev = remap_vbd_dev(orig_dev)
  1051. if dev != orig_dev:
  1052. LOG.debug(_('VBD %(vbd_ref)s plugged into wrong dev, '
  1053. 'remapping to %(dev)s') % locals())
  1054. if dev != 'autodetect':
  1055. # NOTE(johannes): Unit tests will end up with a device called
  1056. # 'autodetect' which obviously won't exist. It's not ideal,
  1057. # but the alternatives were much messier
  1058. _wait_for_device(dev)
  1059. return f(dev)
  1060. finally:
  1061. LOG.debug(_('Destroying VBD for VDI %s ... '), vdi_ref)
  1062. vbd_unplug_with_retry(session, vbd_ref)
  1063. ignore_failure(session.get_xenapi().VBD.destroy, vbd_ref)
  1064. LOG.debug(_('Destroying VBD for VDI %s done.'), vdi_ref)
  1065.  
  1066.  
  1067. def vbd_unplug_with_retry(session, vbd_ref):
  1068. """Call VBD.unplug on the given VBD, with a retry if we get
  1069. DEVICE_DETACH_REJECTED. For reasons which I don't understand, we're
  1070. seeing the device still in use, even when all processes using the device
  1071. should be dead."""
  1072. # FIXME(sirp): We can use LoopingCall here w/o blocking sleep()
  1073. while True:
  1074. try:
  1075. session.get_xenapi().VBD.unplug(vbd_ref)
  1076. LOG.debug(_('VBD.unplug successful first time.'))
  1077. return
  1078. except VMHelper.XenAPI.Failure, e:
  1079. if (len(e.details) > 0 and
  1080. e.details[0] == 'DEVICE_DETACH_REJECTED'):
  1081. LOG.debug(_('VBD.unplug rejected: retrying...'))
  1082. time.sleep(1)
  1083. LOG.debug(_('Not sleeping anymore!'))
  1084. elif (len(e.details) > 0 and
  1085. e.details[0] == 'DEVICE_ALREADY_DETACHED'):
  1086. LOG.debug(_('VBD.unplug successful eventually.'))
  1087. return
  1088. else:
  1089. LOG.error(_('Ignoring XenAPI.Failure in VBD.unplug: %s'),
  1090. e)
  1091. return
  1092.  
  1093.  
  1094. def ignore_failure(func, *args, **kwargs):
  1095. try:
  1096. return func(*args, **kwargs)
  1097. except VMHelper.XenAPI.Failure, e:
  1098. LOG.error(_('Ignoring XenAPI.Failure %s'), e)
  1099. return None
  1100.  
  1101.  
  1102. def get_this_vm_uuid():
  1103. with file('/sys/hypervisor/uuid') as f:
  1104. return f.readline().strip()
  1105.  
  1106.  
  1107. def get_this_vm_ref(session):
  1108. return session.get_xenapi().VM.get_by_uuid(get_this_vm_uuid())
  1109.  
  1110.  
  1111. def _is_vdi_pv(dev):
  1112. LOG.debug(_("Running pygrub against %s"), dev)
  1113. output = os.popen('pygrub -qn /dev/%s' % dev)
  1114. for line in output.readlines():
  1115. #try to find kernel string
  1116. m = re.search('(?<=kernel:)/.*(?:>)', line)
  1117. if m and m.group(0).find('xen') != -1:
  1118. LOG.debug(_("Found Xen kernel %s") % m.group(0))
  1119. return True
  1120. LOG.debug(_("No Xen kernel found. Booting HVM."))
  1121. return False
  1122.  
  1123.  
  1124. def _stream_disk(dev, image_type, virtual_size, image_file):
  1125. offset = 0
  1126. if image_type == ImageType.DISK:
  1127. offset = MBR_SIZE_BYTES
  1128. _write_partition(virtual_size, dev)
  1129.  
  1130. utils.execute('chown', os.getuid(), '/dev/%s' % dev, run_as_root=True)
  1131.  
  1132. with open('/dev/%s' % dev, 'wb') as f:
  1133. f.seek(offset)
  1134. for chunk in image_file:
  1135. f.write(chunk)
  1136.  
  1137.  
  1138. def _write_partition(virtual_size, dev):
  1139. dest = '/dev/%s' % dev
  1140. primary_first = MBR_SIZE_SECTORS
  1141. primary_last = MBR_SIZE_SECTORS + (virtual_size / SECTOR_SIZE) - 1
  1142.  
  1143. LOG.debug(_('Writing partition table %(primary_first)d %(primary_last)d'
  1144. ' to %(dest)s...') % locals())
  1145.  
  1146. def execute(*cmd, **kwargs):
  1147. return utils.execute(*cmd, **kwargs)
  1148.  
  1149. execute('parted', '--script', dest, 'mklabel', 'msdos', run_as_root=True)
  1150. execute('parted', '--script', dest, 'mkpart', 'primary',
  1151. '%ds' % primary_first,
  1152. '%ds' % primary_last,
  1153. run_as_root=True)
  1154.  
  1155. LOG.debug(_('Writing partition table %s done.'), dest)
  1156.  
  1157.  
  1158. def get_name_label_for_image(image):
  1159. # TODO(sirp): This should eventually be the URI for the Glance image
  1160. return _('Glance image %s') % image
  1161.  
  1162.  
  1163. def _mount_filesystem(dev_path, dir):
  1164. """mounts the device specified by dev_path in dir"""
  1165. try:
  1166. out, err = utils.execute('mount',
  1167. '-t', 'ext2,ext3',
  1168. dev_path, dir, run_as_root=True)
  1169. except exception.ProcessExecutionError as e:
  1170. err = str(e)
  1171. return err
  1172.  
  1173.  
  1174. def _find_guest_agent(base_dir, agent_rel_path):
  1175. """
  1176. tries to locate a guest agent at the path
  1177. specificed by agent_rel_path
  1178. """
  1179. agent_path = os.path.join(base_dir, agent_rel_path)
  1180. if os.path.isfile(agent_path):
  1181. # The presence of the guest agent
  1182. # file indicates that this instance can
  1183. # reconfigure the network from xenstore data,
  1184. # so manipulation of files in /etc is not
  1185. # required
  1186. LOG.info(_('XenServer tools installed in this '
  1187. 'image are capable of network injection. '
  1188. 'Networking files will not be'
  1189. 'manipulated'))
  1190. return True
  1191. xe_daemon_filename = os.path.join(base_dir,
  1192. 'usr', 'sbin', 'xe-daemon')
  1193. if os.path.isfile(xe_daemon_filename):
  1194. LOG.info(_('XenServer tools are present '
  1195. 'in this image but are not capable '
  1196. 'of network injection'))
  1197. else:
  1198. LOG.info(_('XenServer tools are not '
  1199. 'installed in this image'))
  1200. return False
  1201.  
  1202.  
  1203. def _mounted_processing(device, key, net, metadata):
  1204. """Callback which runs with the image VDI attached"""
  1205.  
  1206. dev_path = '/dev/' + device + '1' # NB: Partition 1 hardcoded
  1207. tmpdir = tempfile.mkdtemp()
  1208. try:
  1209. # Mount only Linux filesystems, to avoid disturbing NTFS images
  1210. err = _mount_filesystem(dev_path, tmpdir)
  1211. if not err:
  1212. try:
  1213. # This try block ensures that the umount occurs
  1214. if not _find_guest_agent(tmpdir, FLAGS.xenapi_agent_path):
  1215. LOG.info(_('Manipulating interface files '
  1216. 'directly'))
  1217. disk.inject_data_into_fs(tmpdir, key, net, metadata,
  1218. utils.execute)
  1219. finally:
  1220. utils.execute('umount', dev_path, run_as_root=True)
  1221. else:
  1222. LOG.info(_('Failed to mount filesystem (expected for '
  1223. 'non-linux instances): %s') % err)
  1224. finally:
  1225. # remove temporary directory
  1226. os.rmdir(tmpdir)
  1227.  
  1228.  
  1229. def _prepare_injectables(inst, networks_info):
  1230. """
  1231. prepares the ssh key and the network configuration file to be
  1232. injected into the disk image
  1233. """
  1234. #do the import here - Cheetah.Template will be loaded
  1235. #only if injection is performed
  1236. from Cheetah import Template as t
  1237. template = t.Template
  1238. template_data = open(FLAGS.injected_network_template).read()
  1239.  
  1240. metadata = inst['metadata']
  1241. key = str(inst['key_data'])
  1242. net = None
  1243. if networks_info:
  1244. ifc_num = -1
  1245. interfaces_info = []
  1246. have_injected_networks = False
  1247. for (network_ref, info) in networks_info:
  1248. ifc_num += 1
  1249. if not network_ref['injected']:
  1250. continue
  1251.  
  1252. have_injected_networks = True
  1253. ip_v4 = ip_v6 = None
  1254. if 'ips' in info and len(info['ips']) > 0:
  1255. ip_v4 = info['ips'][0]
  1256. if 'ip6s' in info and len(info['ip6s']) > 0:
  1257. ip_v6 = info['ip6s'][0]
  1258. if len(info['dns']) > 0:
  1259. dns = info['dns'][0]
  1260. else:
  1261. dns = ''
  1262. interface_info = {'name': 'eth%d' % ifc_num,
  1263. 'address': ip_v4 and ip_v4['ip'] or '',
  1264. 'netmask': ip_v4 and ip_v4['netmask'] or '',
  1265. 'gateway': info['gateway'],
  1266. 'broadcast': info['broadcast'],
  1267. 'dns': dns,
  1268. 'address_v6': ip_v6 and ip_v6['ip'] or '',
  1269. 'netmask_v6': ip_v6 and ip_v6['netmask'] or '',
  1270. 'gateway_v6': ip_v6 and info['gateway6'] or '',
  1271. 'use_ipv6': FLAGS.use_ipv6}
  1272. interfaces_info.append(interface_info)
  1273.  
  1274. if have_injected_networks:
  1275. net = str(template(template_data,
  1276. searchList=[{'interfaces': interfaces_info,
  1277. 'use_ipv6': FLAGS.use_ipv6}]))
  1278. return key, net, metadata
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement