PageRenderTime 845ms CodeModel.GetById 37ms RepoModel.GetById 17ms app.codeStats 1ms

/components/openstack/nova/files/solariszones/driver.py

https://bitbucket.org/dilos/userland-gate
Python | 4830 lines | 4402 code | 149 blank | 279 comment | 216 complexity | 25104b04cf6add9d01b73ed8ea618321 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-3.0, IPL-1.0, Apache-2.0, MPL-2.0-no-copyleft-exception, Unlicense, CC-BY-SA-4.0, AGPL-1.0, GPL-3.0, LGPL-2.0, LGPL-2.1, CC-BY-3.0, MIT, ISC, EPL-1.0, GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause, LGPL-3.0, BSD-2-Clause, 0BSD, JSON
  1. # Copyright 2011 Justin Santa Barbara
  2. # All Rights Reserved.
  3. #
  4. # Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
  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. Driver for Solaris Zones (nee Containers):
  19. """
  20. import base64
  21. import glob
  22. import os
  23. import platform
  24. import shutil
  25. import tempfile
  26. import uuid
  27. from collections import defaultdict
  28. from openstack_common import get_ovsdb_info
  29. import rad.bindings.com.oracle.solaris.rad.archivemgr_1 as archivemgr
  30. import rad.bindings.com.oracle.solaris.rad.kstat_2 as kstat
  31. import rad.bindings.com.oracle.solaris.rad.zonemgr_1 as zonemgr
  32. import rad.client
  33. import rad.connect
  34. from solaris_install.target.size import Size
  35. from cinderclient import exceptions as cinder_exception
  36. from cinderclient.v1 import client as v1_client
  37. from eventlet import greenthread
  38. from keystoneclient import exceptions as keystone_exception
  39. from lxml import etree
  40. from oslo_concurrency import lockutils, processutils
  41. from oslo_config import cfg
  42. from oslo_log import log as logging
  43. from oslo_serialization import jsonutils
  44. from oslo_utils import excutils
  45. from oslo_utils import fileutils
  46. from oslo_utils import strutils
  47. from oslo_utils import versionutils
  48. from passlib.hash import sha256_crypt
  49. from nova.api.metadata import password
  50. from nova.compute import arch
  51. from nova.compute import hv_type
  52. from nova.compute import power_state
  53. from nova.compute import task_states
  54. from nova.compute import vm_mode
  55. from nova.compute import vm_states
  56. from nova import conductor
  57. import nova.conf
  58. from nova.console import type as ctype
  59. from nova import context as nova_context
  60. from nova import crypto
  61. from nova import exception
  62. from nova.i18n import _, _LE, _LI
  63. from nova.image import API as glance_api
  64. from nova.image import glance
  65. from nova.network.neutronv2 import api as neutronv2_api
  66. from nova import objects
  67. from nova.objects import flavor as flavor_obj
  68. from nova.objects import migrate_data as migrate_data_obj
  69. from nova import utils
  70. from nova.virt import driver
  71. from nova.virt import event as virtevent
  72. from nova.virt import hardware
  73. from nova.virt import images
  74. from nova.virt.solariszones import sysconfig
  75. from nova.volume.cinder import API
  76. from nova.volume.cinder import cinderclient
  77. from nova.volume.cinder import translate_volume_exception
  78. from nova.volume.cinder import _untranslate_volume_summary_view
  79. solariszones_opts = [
  80. cfg.StrOpt('boot_volume_type',
  81. default=None,
  82. help='Cinder volume type to use for boot volumes'),
  83. cfg.StrOpt('boot_volume_az',
  84. default=None,
  85. help='Cinder availability zone to use for boot volumes'),
  86. cfg.StrOpt('glancecache_dirname',
  87. default='/var/share/nova/images',
  88. help='Default path to Glance cache for Solaris Zones.'),
  89. cfg.StrOpt('live_migration_cipher',
  90. help='Cipher to use for encryption of memory traffic during '
  91. 'live migration. If not specified, a common encryption '
  92. 'algorithm will be negotiated. Options include: none or '
  93. 'the name of a supported OpenSSL cipher algorithm.'),
  94. cfg.StrOpt('solariszones_snapshots_directory',
  95. default='$instances_path/snapshots',
  96. help='Location to store snapshots before uploading them to the '
  97. 'Glance image service.'),
  98. cfg.StrOpt('zones_suspend_path',
  99. default='/var/share/zones/SYSsuspend',
  100. help='Default path for suspend images for Solaris Zones.'),
  101. cfg.BoolOpt('solariszones_boot_options',
  102. default=True,
  103. help='Allow kernel boot options to be set in instance '
  104. 'metadata.'),
  105. ]
  106. CONF = nova.conf.CONF
  107. CONF.register_opts(solariszones_opts, 'solariszones')
  108. LOG = logging.getLogger(__name__)
  109. # These should match the strings returned by the zone_state_str()
  110. # function in the (private) libzonecfg library. These values are in turn
  111. # returned in the 'state' string of the Solaris Zones' RAD interface by
  112. # the zonemgr(3RAD) provider.
  113. ZONE_STATE_CONFIGURED = 'configured'
  114. ZONE_STATE_INCOMPLETE = 'incomplete'
  115. ZONE_STATE_UNAVAILABLE = 'unavailable'
  116. ZONE_STATE_INSTALLED = 'installed'
  117. ZONE_STATE_READY = 'ready'
  118. ZONE_STATE_RUNNING = 'running'
  119. ZONE_STATE_SHUTTING_DOWN = 'shutting_down'
  120. ZONE_STATE_DOWN = 'down'
  121. ZONE_STATE_MOUNTED = 'mounted'
  122. # Mapping between zone state and Nova power_state.
  123. SOLARISZONES_POWER_STATE = {
  124. ZONE_STATE_CONFIGURED: power_state.NOSTATE,
  125. ZONE_STATE_INCOMPLETE: power_state.NOSTATE,
  126. ZONE_STATE_UNAVAILABLE: power_state.NOSTATE,
  127. ZONE_STATE_INSTALLED: power_state.SHUTDOWN,
  128. ZONE_STATE_READY: power_state.RUNNING,
  129. ZONE_STATE_RUNNING: power_state.RUNNING,
  130. ZONE_STATE_SHUTTING_DOWN: power_state.RUNNING,
  131. ZONE_STATE_DOWN: power_state.RUNNING,
  132. ZONE_STATE_MOUNTED: power_state.NOSTATE
  133. }
  134. # Solaris Zones brands as defined in brands(5).
  135. ZONE_BRAND_LABELED = 'labeled'
  136. ZONE_BRAND_SOLARIS = 'solaris'
  137. ZONE_BRAND_SOLARIS_KZ = 'solaris-kz'
  138. ZONE_BRAND_SOLARIS10 = 'solaris10'
  139. # Mapping between supported zone brands and the name of the corresponding
  140. # brand template.
  141. ZONE_BRAND_TEMPLATE = {
  142. ZONE_BRAND_SOLARIS: 'SYSdefault',
  143. ZONE_BRAND_SOLARIS_KZ: 'SYSsolaris-kz',
  144. }
  145. MAX_CONSOLE_BYTES = 102400
  146. VNC_CONSOLE_BASE_FMRI = 'svc:/application/openstack/nova/zone-vnc-console'
  147. # Required in order to create a zone VNC console SMF service instance
  148. VNC_SERVER_PATH = '/usr/bin/vncserver'
  149. XTERM_PATH = '/usr/bin/xterm'
  150. ROOTZPOOL_RESOURCE = 'rootzpool'
  151. # The underlying Solaris Zones framework does not expose a specific
  152. # version number, instead relying on feature tests to identify what is
  153. # and what is not supported. A HYPERVISOR_VERSION is defined here for
  154. # Nova's use but it generally should not be changed unless there is a
  155. # incompatible change such as concerning kernel zone live migration.
  156. HYPERVISOR_VERSION = '5.11'
  157. shared_storage = ['iscsi', 'fibre_channel']
  158. KSTAT_TYPE = {
  159. 'NVVT_STR': 'string',
  160. 'NVVT_STRS': 'strings',
  161. 'NVVT_INT': 'integer',
  162. 'NVVT_INTS': 'integers',
  163. 'NVVT_KSTAT': 'kstat',
  164. }
  165. def lookup_resource(zone, resource):
  166. """Lookup specified resource from specified Solaris Zone."""
  167. try:
  168. val = zone.getResources(zonemgr.Resource(resource))
  169. except rad.client.ObjectError:
  170. return None
  171. except Exception:
  172. raise
  173. return val[0] if val else None
  174. def lookup_resource_property(zone, resource, prop, filter=None):
  175. """Lookup specified property from specified Solaris Zone resource."""
  176. try:
  177. val = zone.getResourceProperties(zonemgr.Resource(resource, filter),
  178. [prop])
  179. except rad.client.ObjectError:
  180. return None
  181. except Exception:
  182. raise
  183. return val[0].value if val else None
  184. def lookup_resource_property_value(zone, resource, prop, value):
  185. """Lookup specified property with value from specified Solaris Zone
  186. resource. Returns resource object if matching value is found, else None
  187. """
  188. try:
  189. resources = zone.getResources(zonemgr.Resource(resource))
  190. for resource in resources:
  191. for propertee in resource.properties:
  192. if propertee.name == prop and propertee.value == value:
  193. return resource
  194. else:
  195. return None
  196. except rad.client.ObjectError:
  197. return None
  198. except Exception:
  199. raise
  200. def zonemgr_strerror(ex):
  201. """Format the payload from a zonemgr(3RAD) rad.client.ObjectError
  202. exception into a sensible error string that can be logged. Newlines
  203. are converted to a colon-space string to create a single line.
  204. If the exception was something other than rad.client.ObjectError,
  205. just return it as a string.
  206. """
  207. if not isinstance(ex, rad.client.ObjectError):
  208. return str(ex)
  209. payload = ex.get_payload()
  210. if payload.code == zonemgr.ErrorCode.NONE:
  211. return str(ex)
  212. error = [str(payload.code)]
  213. if payload.str is not None and payload.str != '':
  214. error.append(payload.str)
  215. if payload.stderr is not None and payload.stderr != '':
  216. stderr = payload.stderr.rstrip()
  217. error.append(stderr.replace('\n', ': '))
  218. result = ': '.join(error)
  219. return result
  220. class MemoryAlignmentIncorrect(exception.FlavorMemoryTooSmall):
  221. msg_fmt = _("Requested flavor, %(flavor)s, memory size %(memsize)s does "
  222. "not align on %(align)s boundary.")
  223. class SolarisVolumeAPI(API):
  224. """ Extending the volume api to support additional cinder sub-commands
  225. """
  226. @translate_volume_exception
  227. def create(self, context, size, name, description, snapshot=None,
  228. image_id=None, volume_type=None, metadata=None,
  229. availability_zone=None, source_volume=None):
  230. """Clone the source volume by calling the cinderclient version of
  231. create with a source_volid argument
  232. :param context: the context for the clone
  233. :param size: size of the new volume, must be the same as the source
  234. volume
  235. :param name: display_name of the new volume
  236. :param description: display_description of the new volume
  237. :param snapshot: Snapshot object
  238. :param image_id: image_id to create the volume from
  239. :param volume_type: type of volume
  240. :param metadata: Additional metadata for the volume
  241. :param availability_zone: zone:host where the volume is to be created
  242. :param source_volume: Volume object
  243. Returns a volume object
  244. """
  245. client = cinderclient(context)
  246. if snapshot is not None:
  247. snapshot_id = snapshot['id']
  248. else:
  249. snapshot_id = None
  250. if source_volume is not None:
  251. source_volid = source_volume['id']
  252. else:
  253. source_volid = None
  254. kwargs = dict(snapshot_id=snapshot_id,
  255. volume_type=volume_type,
  256. user_id=context.user_id,
  257. project_id=context.project_id,
  258. availability_zone=availability_zone,
  259. metadata=metadata,
  260. imageRef=image_id,
  261. source_volid=source_volid)
  262. if isinstance(client, v1_client.Client):
  263. kwargs['display_name'] = name
  264. kwargs['display_description'] = description
  265. else:
  266. kwargs['name'] = name
  267. kwargs['description'] = description
  268. try:
  269. item = cinderclient(context).volumes.create(size, **kwargs)
  270. return _untranslate_volume_summary_view(context, item)
  271. except cinder_exception.OverLimit:
  272. raise exception.OverQuota(overs='volumes')
  273. except (cinder_exception.BadRequest,
  274. keystone_exception.BadRequest) as reason:
  275. raise exception.InvalidInput(reason=reason)
  276. @translate_volume_exception
  277. def update(self, context, volume_id, fields):
  278. """Update the fields of a volume for example used to rename a volume
  279. via a call to cinderclient
  280. :param context: the context for the update
  281. :param volume_id: the id of the volume to update
  282. :param fields: a dictionary of of the name/value pairs to update
  283. """
  284. cinderclient(context).volumes.update(volume_id, **fields)
  285. @translate_volume_exception
  286. def extend(self, context, volume, newsize):
  287. """Extend the size of a cinder volume by calling the cinderclient
  288. :param context: the context for the extend
  289. :param volume: the volume object to extend
  290. :param newsize: the new size of the volume in GB
  291. """
  292. cinderclient(context).volumes.extend(volume, newsize)
  293. class ZoneConfig(object):
  294. """ZoneConfig - context manager for access zone configurations.
  295. Automatically opens the configuration for a zone and commits any changes
  296. before exiting
  297. """
  298. def __init__(self, zone):
  299. """zone is a zonemgr object representing either a kernel zone or
  300. non-global zone.
  301. """
  302. self.zone = zone
  303. self.editing = False
  304. def __enter__(self):
  305. """enables the editing of the zone."""
  306. try:
  307. self.zone.editConfig()
  308. self.editing = True
  309. return self
  310. except Exception as ex:
  311. reason = zonemgr_strerror(ex)
  312. LOG.exception(_("Unable to initialize editing of instance '%s' "
  313. "via zonemgr(3RAD): %s")
  314. % (self.zone.name, reason))
  315. raise
  316. def __exit__(self, exc_type, exc_val, exc_tb):
  317. """looks for any kind of exception before exiting. If one is found,
  318. cancel any configuration changes and reraise the exception. If not,
  319. commit the new configuration.
  320. """
  321. if exc_type is not None and self.editing:
  322. # We received some kind of exception. Cancel the config and raise.
  323. self.zone.cancelConfig()
  324. raise
  325. else:
  326. # commit the config
  327. try:
  328. self.zone.commitConfig()
  329. except Exception as ex:
  330. reason = zonemgr_strerror(ex)
  331. LOG.exception(_("Unable to commit the new configuration for "
  332. "instance '%s' via zonemgr(3RAD): %s")
  333. % (self.zone.name, reason))
  334. # Last ditch effort to cleanup.
  335. self.zone.cancelConfig()
  336. raise
  337. def setprop(self, resource, prop, value):
  338. """sets a property for an existing resource OR creates a new resource
  339. with the given property(s).
  340. """
  341. current = lookup_resource_property(self.zone, resource, prop)
  342. if current is not None and current == value:
  343. # the value is already set
  344. return
  345. try:
  346. if current is None:
  347. self.zone.addResource(zonemgr.Resource(
  348. resource, [zonemgr.Property(prop, value)]))
  349. else:
  350. self.zone.setResourceProperties(
  351. zonemgr.Resource(resource),
  352. [zonemgr.Property(prop, value)])
  353. except Exception as ex:
  354. reason = zonemgr_strerror(ex)
  355. LOG.exception(_("Unable to set '%s' property on '%s' resource for "
  356. "instance '%s' via zonemgr(3RAD): %s")
  357. % (prop, resource, self.zone.name, reason))
  358. raise
  359. def addresource(self, resource, props=None, ignore_exists=False):
  360. """creates a new resource with an optional property list, or set the
  361. property if the resource exists and ignore_exists is true.
  362. :param ignore_exists: If the resource exists, set the property for the
  363. resource.
  364. """
  365. if props is None:
  366. props = []
  367. try:
  368. self.zone.addResource(zonemgr.Resource(resource, props))
  369. except Exception as ex:
  370. if isinstance(ex, rad.client.ObjectError):
  371. code = ex.get_payload().code
  372. if (ignore_exists and
  373. code == zonemgr.ErrorCode.RESOURCE_ALREADY_EXISTS):
  374. self.zone.setResourceProperties(
  375. zonemgr.Resource(resource, None), props)
  376. return
  377. reason = zonemgr_strerror(ex)
  378. LOG.exception(_("Unable to create new resource '%s' for instance "
  379. "'%s' via zonemgr(3RAD): %s")
  380. % (resource, self.zone.name, reason))
  381. raise
  382. def removeresources(self, resource, props=None):
  383. """removes resources whose properties include the optional property
  384. list specified in props.
  385. """
  386. if props is None:
  387. props = []
  388. try:
  389. self.zone.removeResources(zonemgr.Resource(resource, props))
  390. except Exception as ex:
  391. reason = zonemgr_strerror(ex)
  392. LOG.exception(_("Unable to remove resource '%s' for instance '%s' "
  393. "via zonemgr(3RAD): %s")
  394. % (resource, self.zone.name, reason))
  395. raise
  396. def clear_resource_props(self, resource, props):
  397. """Clear property values of a given resource
  398. """
  399. try:
  400. self.zone.clearResourceProperties(zonemgr.Resource(resource, None),
  401. props)
  402. except rad.client.ObjectError as ex:
  403. reason = zonemgr_strerror(ex)
  404. LOG.exception(_("Unable to clear '%s' property on '%s' resource "
  405. "for instance '%s' via zonemgr(3RAD): %s")
  406. % (props, resource, self.zone.name, reason))
  407. raise
  408. class SolarisZonesDriver(driver.ComputeDriver):
  409. """Solaris Zones Driver using the zonemgr(3RAD) and kstat(3RAD) providers.
  410. The interface to this class talks in terms of 'instances' (Amazon EC2 and
  411. internal Nova terminology), by which we mean 'running virtual machine'
  412. (XenAPI terminology) or domain (Xen or libvirt terminology).
  413. An instance has an ID, which is the identifier chosen by Nova to represent
  414. the instance further up the stack. This is unfortunately also called a
  415. 'name' elsewhere. As far as this layer is concerned, 'instance ID' and
  416. 'instance name' are synonyms.
  417. Note that the instance ID or name is not human-readable or
  418. customer-controlled -- it's an internal ID chosen by Nova. At the
  419. nova.virt layer, instances do not have human-readable names at all -- such
  420. things are only known higher up the stack.
  421. Most virtualization platforms will also have their own identity schemes,
  422. to uniquely identify a VM or domain. These IDs must stay internal to the
  423. platform-specific layer, and never escape the connection interface. The
  424. platform-specific layer is responsible for keeping track of which instance
  425. ID maps to which platform-specific ID, and vice versa.
  426. Some methods here take an instance of nova.compute.service.Instance. This
  427. is the data structure used by nova.compute to store details regarding an
  428. instance, and pass them into this layer. This layer is responsible for
  429. translating that generic data structure into terms that are specific to the
  430. virtualization platform.
  431. """
  432. capabilities = {
  433. "has_imagecache": False,
  434. "supports_recreate": True,
  435. "supports_migrate_to_same_host": False
  436. }
  437. def __init__(self, virtapi):
  438. self.virtapi = virtapi
  439. self._archive_manager = None
  440. self._compute_event_callback = None
  441. self._conductor_api = conductor.API()
  442. self._fc_hbas = None
  443. self._fc_wwnns = None
  444. self._fc_wwpns = None
  445. self._host_stats = {}
  446. self._initiator = None
  447. self._install_engine = None
  448. self._kstat_control = None
  449. self._pagesize = os.sysconf('SC_PAGESIZE')
  450. self._rad_connection = None
  451. self._rootzpool_suffix = ROOTZPOOL_RESOURCE
  452. self._uname = os.uname()
  453. self._validated_archives = list()
  454. self._volume_api = SolarisVolumeAPI()
  455. self._zone_manager = None
  456. @property
  457. def rad_connection(self):
  458. if self._rad_connection is None:
  459. self._rad_connection = rad.connect.connect_unix()
  460. else:
  461. # taken from rad.connect.RadConnection.__repr__ to look for a
  462. # closed connection
  463. if self._rad_connection._closed is not None:
  464. # the RAD connection has been lost. Reconnect to RAD
  465. self._rad_connection = rad.connect.connect_unix()
  466. return self._rad_connection
  467. @property
  468. def zone_manager(self):
  469. try:
  470. if (self._zone_manager is None or
  471. self._zone_manager._conn._closed is not None):
  472. self._zone_manager = self.rad_connection.get_object(
  473. zonemgr.ZoneManager())
  474. except Exception as ex:
  475. reason = _("Unable to obtain RAD object: %s") % ex
  476. raise exception.NovaException(reason)
  477. return self._zone_manager
  478. @property
  479. def kstat_control(self):
  480. try:
  481. if (self._kstat_control is None or
  482. self._kstat_control._conn._closed is not None):
  483. self._kstat_control = self.rad_connection.get_object(
  484. kstat.Control())
  485. except Exception as ex:
  486. reason = _("Unable to obtain RAD object: %s") % ex
  487. raise exception.NovaException(reason)
  488. return self._kstat_control
  489. @property
  490. def archive_manager(self):
  491. try:
  492. if (self._archive_manager is None or
  493. self._archive_manager._conn._closed is not None):
  494. self._archive_manager = self.rad_connection.get_object(
  495. archivemgr.ArchiveManager())
  496. except Exception as ex:
  497. reason = _("Unable to obtain RAD object: %s") % ex
  498. raise exception.NovaException(reason)
  499. return self._archive_manager
  500. def init_host(self, host):
  501. """Initialize anything that is necessary for the driver to function,
  502. including catching up with currently running VM's on the given host.
  503. """
  504. # TODO(Vek): Need to pass context in for access to auth_token
  505. pass
  506. def cleanup_host(self, host):
  507. """Clean up anything that is necessary for the driver gracefully stop,
  508. including ending remote sessions. This is optional.
  509. """
  510. pass
  511. def _get_fc_hbas(self):
  512. """Get Fibre Channel HBA information."""
  513. if self._fc_hbas:
  514. return self._fc_hbas
  515. out = None
  516. try:
  517. out, err = utils.execute('/usr/sbin/fcinfo', 'hba-port')
  518. except processutils.ProcessExecutionError:
  519. return []
  520. if out is None:
  521. raise RuntimeError(_("Cannot find any Fibre Channel HBAs"))
  522. hbas = []
  523. hba = {}
  524. for line in out.splitlines():
  525. line = line.strip()
  526. # Collect the following hba-port data:
  527. # 1: Port WWN
  528. # 2: State (online|offline)
  529. # 3: Node WWN
  530. if line.startswith("HBA Port WWN:"):
  531. # New HBA port entry
  532. hba = {}
  533. wwpn = line.split()[-1]
  534. hba['port_name'] = wwpn
  535. continue
  536. elif line.startswith("Port Mode:"):
  537. mode = line.split()[-1]
  538. # Skip Target mode ports
  539. if mode != 'Initiator':
  540. break
  541. elif line.startswith("State:"):
  542. state = line.split()[-1]
  543. hba['port_state'] = state
  544. continue
  545. elif line.startswith("Node WWN:"):
  546. wwnn = line.split()[-1]
  547. hba['node_name'] = wwnn
  548. continue
  549. if len(hba) == 3:
  550. hbas.append(hba)
  551. hba = {}
  552. self._fc_hbas = hbas
  553. return self._fc_hbas
  554. def _get_fc_wwnns(self):
  555. """Get Fibre Channel WWNNs from the system, if any."""
  556. hbas = self._get_fc_hbas()
  557. wwnns = []
  558. for hba in hbas:
  559. if hba['port_state'] == 'online':
  560. wwnn = hba['node_name']
  561. wwnns.append(wwnn)
  562. return wwnns
  563. def _get_fc_wwpns(self):
  564. """Get Fibre Channel WWPNs from the system, if any."""
  565. hbas = self._get_fc_hbas()
  566. wwpns = []
  567. for hba in hbas:
  568. if hba['port_state'] == 'online':
  569. wwpn = hba['port_name']
  570. wwpns.append(wwpn)
  571. return wwpns
  572. def _get_iscsi_initiator(self):
  573. """ Return the iSCSI initiator node name IQN for this host """
  574. try:
  575. out, err = utils.execute('/usr/sbin/iscsiadm', 'list',
  576. 'initiator-node')
  577. # Sample first line of command output:
  578. # Initiator node name: iqn.1986-03.com.sun:01:e00000000000.4f757217
  579. initiator_name_line = out.splitlines()[0]
  580. initiator_iqn = initiator_name_line.rsplit(' ', 1)[1]
  581. return initiator_iqn
  582. except processutils.ProcessExecutionError as ex:
  583. LOG.info(_("Failed to get the initiator-node info: %s") % (ex))
  584. return None
  585. def _get_zone_by_name(self, name):
  586. """Return a Solaris Zones object via RAD by name."""
  587. try:
  588. zone = self.rad_connection.get_object(
  589. zonemgr.Zone(), rad.client.ADRGlobPattern({'name': name}))
  590. except rad.client.NotFoundError:
  591. return None
  592. except Exception:
  593. raise
  594. return zone
  595. def _get_state(self, zone):
  596. """Return the running state, one of the power_state codes."""
  597. return SOLARISZONES_POWER_STATE[zone.state]
  598. def _pages_to_kb(self, pages):
  599. """Convert a number of pages of memory into a total size in KBytes."""
  600. return (pages * self._pagesize) / 1024
  601. def _get_max_mem(self, zone):
  602. """Return the maximum memory in KBytes allowed."""
  603. if zone.brand == ZONE_BRAND_SOLARIS:
  604. mem_resource = 'swap'
  605. else:
  606. mem_resource = 'physical'
  607. max_mem = lookup_resource_property(zone, 'capped-memory', mem_resource)
  608. if max_mem is not None:
  609. return strutils.string_to_bytes("%sB" % max_mem) / 1024
  610. # If physical property in capped-memory doesn't exist, this may
  611. # represent a non-global zone so just return the system's total
  612. # memory.
  613. return self._pages_to_kb(os.sysconf('SC_PHYS_PAGES'))
  614. def _get_mem(self, zone):
  615. """Return the memory in KBytes used by the domain."""
  616. # There isn't any way of determining this from the hypervisor
  617. # perspective in Solaris, so just return the _get_max_mem() value
  618. # for now.
  619. return self._get_max_mem(zone)
  620. def _get_num_cpu(self, zone):
  621. """Return the number of virtual CPUs for the domain.
  622. In the case of kernel zones, the number of virtual CPUs a zone
  623. ends up with depends on whether or not there were 'virtual-cpu'
  624. or 'dedicated-cpu' resources in the configuration or whether
  625. there was an assigned pool in the configuration. This algorithm
  626. attempts to emulate what the virtual platform code does to
  627. determine a number of virtual CPUs to use.
  628. """
  629. # If a 'virtual-cpu' resource exists, use the minimum number of
  630. # CPUs defined there.
  631. ncpus = lookup_resource_property(zone, 'virtual-cpu', 'ncpus')
  632. if ncpus is not None:
  633. min = ncpus.split('-', 1)[0]
  634. if min.isdigit():
  635. return int(min)
  636. # Otherwise if a 'dedicated-cpu' resource exists, use the maximum
  637. # number of CPUs defined there.
  638. ncpus = lookup_resource_property(zone, 'dedicated-cpu', 'ncpus')
  639. if ncpus is not None:
  640. max = ncpus.split('-', 1)[-1]
  641. if max.isdigit():
  642. return int(max)
  643. # Finally if neither resource exists but the zone was assigned a
  644. # pool in the configuration, the number of CPUs would be the size
  645. # of the processor set. Currently there's no way of easily
  646. # determining this so use the system's notion of the total number
  647. # of online CPUs.
  648. return os.sysconf('SC_NPROCESSORS_ONLN')
  649. def _kstat_data(self, uri):
  650. """Return Kstat snapshot data via RAD as a dictionary."""
  651. if not isinstance(uri, str):
  652. raise exception.NovaException("kstat URI must be string type: "
  653. "%s is %s" % (uri, type(uri)))
  654. if not uri.startswith("kstat:/"):
  655. uri = "kstat:/" + uri
  656. try:
  657. self.kstat_control.update()
  658. kstat_obj = self.rad_connection.get_object(
  659. kstat.Kstat(), rad.client.ADRGlobPattern({"uri": uri}))
  660. except Exception as reason:
  661. LOG.info(_("Unable to retrieve kstat object '%s' via kstat(3RAD): "
  662. "%s") % (uri, reason))
  663. return None
  664. ks_data = {}
  665. for name, data in kstat_obj.getMap().items():
  666. ks_data[name] = getattr(data, KSTAT_TYPE[str(data.type)])
  667. return ks_data
  668. def _sum_kstat_statistic(self, kstat_data, statistic):
  669. total = 0
  670. for ks in kstat_data.values():
  671. data = ks.getMap()[statistic]
  672. value = getattr(data, KSTAT_TYPE[str(data.type)])
  673. try:
  674. total += value
  675. except TypeError:
  676. LOG.error(_("Unable to aggregate non-summable kstat %s;%s "
  677. " of type %s") % (ks.getParent().uri, statistic,
  678. type(value)))
  679. return None
  680. return total
  681. def _get_kstat_statistic(self, ks, statistic):
  682. if not isinstance(ks, kstat.Kstat):
  683. reason = (_("Attempted to get a kstat from %s type.") % (type(ks)))
  684. raise TypeError(reason)
  685. try:
  686. data = ks.getMap()[statistic]
  687. value = getattr(data, KSTAT_TYPE[str(data.type)])
  688. except TypeError:
  689. value = None
  690. return value
  691. def _get_cpu_time(self, zone):
  692. """Return the CPU time used in nanoseconds."""
  693. if zone.id == -1:
  694. return 0
  695. # The retry value of 3 was determined by the "we shouldn't hit this
  696. # often, but if we do it should resolve quickly so try again"+1
  697. # algorithm.
  698. for _attempt in range(3):
  699. total = 0
  700. accum_uri = "kstat:/zones/cpu/sys_zone_accum/%d" % zone.id
  701. uri = "kstat:/zones/cpu/sys_zone_%d" % zone.id
  702. initial = self._kstat_data(accum_uri)
  703. cpus = self._kstat_data(uri)
  704. total += self._sum_kstat_statistic(cpus, 'cpu_nsec_kernel_cur')
  705. total += self._sum_kstat_statistic(cpus, 'cpu_nsec_user_cur')
  706. final = self._kstat_data(accum_uri)
  707. if initial['gen_num'] == final['gen_num']:
  708. total += initial['cpu_nsec_user'] + initial['cpu_nsec_kernel']
  709. return total
  710. LOG.error(_("Unable to get accurate cpu usage beacuse cpu list "
  711. "keeps changing"))
  712. return 0
  713. def get_info(self, instance):
  714. """Get the current status of an instance, by name (not ID!)
  715. :param instance: nova.objects.instance.Instance object
  716. Returns a InstanceInfo object
  717. """
  718. # TODO(Vek): Need to pass context in for access to auth_token
  719. name = instance['name']
  720. zone = self._get_zone_by_name(name)
  721. if zone is None:
  722. raise exception.InstanceNotFound(instance_id=name)
  723. return hardware.InstanceInfo(state=self._get_state(zone),
  724. max_mem_kb=self._get_max_mem(zone),
  725. mem_kb=self._get_mem(zone),
  726. num_cpu=self._get_num_cpu(zone),
  727. cpu_time_ns=self._get_cpu_time(zone))
  728. def get_num_instances(self):
  729. """Return the total number of virtual machines.
  730. Return the number of virtual machines that the hypervisor knows
  731. about.
  732. .. note::
  733. This implementation works for all drivers, but it is
  734. not particularly efficient. Maintainers of the virt drivers are
  735. encouraged to override this method with something more
  736. efficient.
  737. """
  738. return len(self.list_instances())
  739. def instance_exists(self, instance):
  740. """Checks existence of an instance on the host.
  741. :param instance: The instance to lookup
  742. Returns True if an instance with the supplied ID exists on
  743. the host, False otherwise.
  744. .. note::
  745. This implementation works for all drivers, but it is
  746. not particularly efficient. Maintainers of the virt drivers are
  747. encouraged to override this method with something more
  748. efficient.
  749. """
  750. try:
  751. return instance.uuid in self.list_instance_uuids()
  752. except NotImplementedError:
  753. return instance.name in self.list_instances()
  754. def estimate_instance_overhead(self, instance_info):
  755. """Estimate the virtualization overhead required to build an instance
  756. of the given flavor.
  757. Defaults to zero, drivers should override if per-instance overhead
  758. calculations are desired.
  759. :param instance_info: Instance/flavor to calculate overhead for.
  760. :returns: Dict of estimated overhead values.
  761. """
  762. return {'memory_mb': 0}
  763. def _get_list_zone_object(self):
  764. """Return a list of all Solaris Zones objects via RAD."""
  765. return self.rad_connection.list_objects(zonemgr.Zone())
  766. def list_instances(self):
  767. """Return the names of all the instances known to the virtualization
  768. layer, as a list.
  769. """
  770. # TODO(Vek): Need to pass context in for access to auth_token
  771. instances_list = []
  772. for zone in self._get_list_zone_object():
  773. instances_list.append(self.rad_connection.get_object(zone).name)
  774. return instances_list
  775. def list_instance_uuids(self):
  776. """Return the UUIDS of all the instances known to the virtualization
  777. layer, as a list.
  778. """
  779. raise NotImplementedError()
  780. def _rebuild_block_devices(self, context, instance, bdms, recreate):
  781. root_ci = None
  782. rootmp = instance['root_device_name']
  783. for entry in bdms:
  784. if entry['connection_info'] is None:
  785. continue
  786. if entry['device_name'] == rootmp:
  787. root_ci = jsonutils.loads(entry['connection_info'])
  788. # Let's make sure this is a well formed connection_info, by
  789. # checking if it has a serial key that represents the
  790. # volume_id. If not check to see if the block device has a
  791. # volume_id, if so then assign this to the root_ci.serial.
  792. #
  793. # If we cannot repair the connection_info then simply do not
  794. # return a root_ci and let the caller decide if they want to
  795. # fail or not.
  796. if root_ci.get('serial') is None:
  797. if entry.get('volume_id') is not None:
  798. root_ci['serial'] = entry['volume_id']
  799. else:
  800. LOG.debug(_("Unable to determine the volume id for "
  801. "the connection info for the root device "
  802. "for instance '%s'") % instance['name'])
  803. root_ci = None
  804. continue
  805. if not recreate:
  806. ci = jsonutils.loads(entry['connection_info'])
  807. self.detach_volume(ci, instance, entry['device_name'])
  808. if root_ci is None and recreate:
  809. msg = (_("Unable to find the root device for instance '%s'.")
  810. % instance['name'])
  811. raise exception.NovaException(msg)
  812. return root_ci
  813. def _set_instance_metahostid(self, instance):
  814. """Attempt to get the hostid from the current configured zone and
  815. return the hostid. Otherwise return None, and do not set the hostid in
  816. the instance
  817. """
  818. hostid = instance.system_metadata.get('hostid')
  819. if hostid is not None:
  820. return hostid
  821. zone = self._get_zone_by_name(instance['name'])
  822. if zone is None:
  823. return None
  824. hostid = lookup_resource_property(zone, 'global', 'hostid')
  825. if hostid:
  826. instance.system_metadata['hostid'] = hostid
  827. return hostid
  828. def rebuild(self, context, instance, image_meta, injected_files,
  829. admin_password, bdms, detach_block_devices,
  830. attach_block_devices, network_info=None,
  831. recreate=False, block_device_info=None,
  832. preserve_ephemeral=False):
  833. """Destroy and re-make this instance.
  834. A 'rebuild' effectively purges all existing data from the system and
  835. remakes the VM with given 'metadata' and 'personalities'.
  836. This base class method shuts down the VM, detaches all block devices,
  837. then spins up the new VM afterwards. It may be overridden by
  838. hypervisors that need to - e.g. for optimisations, or when the 'VM'
  839. is actually proxied and needs to be held across the shutdown + spin
  840. up steps.
  841. :param context: security context
  842. :param instance: nova.objects.instance.Instance
  843. This function should use the data there to guide
  844. the creation of the new instance.
  845. :param nova.objects.ImageMeta image_meta:
  846. The metadata of the image of the instance.
  847. :param injected_files: User files to inject into instance.
  848. :param admin_password: Administrator password to set in instance.
  849. :param bdms: block-device-mappings to use for rebuild
  850. :param detach_block_devices: function to detach block devices. See
  851. nova.compute.manager.ComputeManager:_rebuild_default_impl for
  852. usage.
  853. :param attach_block_devices: function to attach block devices. See
  854. nova.compute.manager.ComputeManager:_rebuild_default_impl for
  855. usage.
  856. :param network_info:
  857. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  858. :param recreate: True if the instance is being recreated on a new
  859. hypervisor - all the cleanup of old state is skipped.
  860. :param block_device_info: Information about block devices to be
  861. attached to the instance.
  862. :param preserve_ephemeral: True if the default ephemeral storage
  863. partition must be preserved on rebuild
  864. """
  865. if recreate:
  866. instance.system_metadata['evac_from'] = instance['launched_on']
  867. instance.save()
  868. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  869. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  870. if brand == ZONE_BRAND_SOLARIS:
  871. msg = (_("'%s' branded zones do not currently support "
  872. "evacuation.") % brand)
  873. raise exception.NovaException(msg)
  874. else:
  875. self._power_off(instance, "HALT")
  876. instance.task_state = task_states.REBUILD_BLOCK_DEVICE_MAPPING
  877. instance.save(expected_task_state=[task_states.REBUILDING])
  878. root_ci = self._rebuild_block_devices(context, instance, bdms,
  879. recreate)
  880. if recreate:
  881. if root_ci is not None:
  882. driver_type = root_ci['driver_volume_type']
  883. else:
  884. driver_type = 'local'
  885. if driver_type not in shared_storage:
  886. msg = (_("Root device is not on shared storage for instance "
  887. "'%s'.") % instance['name'])
  888. raise exception.NovaException(msg)
  889. if not recreate:
  890. self.destroy(context, instance, network_info, block_device_info)
  891. if root_ci is not None:
  892. self._volume_api.detach(context, root_ci['serial'])
  893. self._volume_api.delete(context, root_ci['serial'])
  894. # Go ahead and remove the root bdm from the bdms so that we do
  895. # not trip up spawn either checking against the use of c1d0 or
  896. # attempting to re-attach the root device.
  897. bdms.objects.remove(bdms.root_bdm())
  898. rootdevname = block_device_info.get('root_device_name')
  899. if rootdevname is not None:
  900. bdi_bdms = block_device_info.get('block_device_mapping')
  901. for entry in bdi_bdms:
  902. if entry['mount_device'] == rootdevname:
  903. bdi_bdms.remove(entry)
  904. break
  905. instance.task_state = task_states.REBUILD_SPAWNING
  906. instance.save(
  907. expected_task_state=[task_states.REBUILD_BLOCK_DEVICE_MAPPING])
  908. # Instead of using a boolean for 'rebuilding' scratch data, use a
  909. # string because the object will translate it to a string anyways.
  910. if recreate:
  911. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  912. instance.system_metadata['rebuilding'] = 'false'
  913. self._create_config(context, instance, network_info, root_ci, None)
  914. del instance.system_metadata['evac_from']
  915. instance.save()
  916. else:
  917. instance.system_metadata['rebuilding'] = 'true'
  918. self.spawn(context, instance, image_meta, injected_files,
  919. admin_password, network_info, block_device_info)
  920. del instance.system_metadata['rebuilding']
  921. name = instance['name']
  922. zone = self._get_zone_by_name(name)
  923. if zone is None:
  924. raise exception.InstanceNotFound(instance_id=name)
  925. if recreate:
  926. zone.attach(['-x', 'initialize-hostdata'])
  927. rootmp = instance['root_device_name']
  928. for entry in bdms:
  929. if (entry['connection_info'] is None or
  930. rootmp == entry['device_name']):
  931. continue
  932. connection_info = jsonutils.loads(entry['connection_info'])
  933. mount = entry['device_name']
  934. self.attach_volume(context, connection_info, instance, mount)
  935. self._power_on(instance, network_info)
  936. if admin_password is not None:
  937. # Because there is no way to make sure a zone is ready upon
  938. # returning from a boot request. We must give the zone a few
  939. # seconds to boot before attempting to set the admin password.
  940. greenthread.sleep(15)
  941. self.set_admin_password(instance, admin_password)
  942. def _get_flavor(self, instance):
  943. """Retrieve the flavor object as specified in the instance object"""
  944. return flavor_obj.Flavor.get_by_id(
  945. nova_context.get_admin_context(read_deleted='yes'),
  946. instance['instance_type_id'])
  947. def _fetch_image(self, context, instance):
  948. """Fetch an image using Glance given the instance's image_ref."""
  949. glancecache_dirname = CONF.solariszones.glancecache_dirname
  950. fileutils.ensure_tree(glancecache_dirname)
  951. iref = instance['image_ref']
  952. image = os.path.join(glancecache_dirname, iref)
  953. downloading = image + '.downloading'
  954. with lockutils.lock('glance-image-%s' % iref):
  955. if os.path.isfile(downloading):
  956. LOG.debug(_('Cleaning partial download of %s' % iref))
  957. os.unlink(image)
  958. os.unlink(downloading)
  959. elif os.path.exists(image):
  960. LOG.debug(_("Using existing, cached Glance image: id %s")
  961. % iref)
  962. return image
  963. LOG.debug(_("Fetching new Glance image: id %s") % iref)
  964. try:
  965. # touch the empty .downloading file
  966. with open(downloading, 'w'):
  967. pass
  968. images.fetch(context, iref, image, instance['user_id'],
  969. instance['project_id'])
  970. os.unlink(downloading)
  971. return image
  972. except Exception as reason:
  973. LOG.exception(_("Unable to fetch Glance image: id %s: %s")
  974. % (iref, reason))
  975. raise
  976. @lockutils.synchronized('validate_image')
  977. def _validate_image(self, context, image, instance):
  978. """Validate a glance image for compatibility with the instance."""
  979. # Skip if the image was already checked and confirmed as valid.
  980. if instance['image_ref'] in self._validated_archives:
  981. return
  982. try:
  983. ua = self.archive_manager.getArchive(image)
  984. except Exception as ex:
  985. if isinstance(ex, rad.client.ObjectError):
  986. reason = ex.get_payload().info
  987. else:
  988. reason = str(ex)
  989. raise exception.ImageUnacceptable(image_id=instance['image_ref'],
  990. reason=reason)
  991. # Validate the image at this point to ensure:
  992. # - contains one deployable system
  993. deployables = ua.getArchivedSystems()
  994. if len(deployables) != 1:
  995. reason = _("Image must contain only a single deployable system.")
  996. raise exception.ImageUnacceptable(image_id=instance['image_ref'],
  997. reason=reason)
  998. # - matching architecture
  999. deployable_arch = str(ua.isa)
  1000. compute_arch = platform.processor()
  1001. if deployable_arch.lower() != compute_arch:
  1002. reason = (_("Unified Archive architecture '%s' is incompatible "
  1003. "with this compute host's architecture, '%s'.")
  1004. % (deployable_arch, compute_arch))
  1005. # For some reason we have gotten the wrong architecture image,
  1006. # which should have been filtered by the scheduler. One reason this
  1007. # could happen is because the images architecture type is
  1008. # incorrectly set. Check for this and report a better reason.
  1009. glanceapi = glance_api()
  1010. image_meta = glanceapi.get(context, instance['image_ref'])
  1011. image_properties = image_meta.get('properties')
  1012. if image_properties.get('architecture') is None:
  1013. reason = reason + (_(" The 'architecture' property is not set "
  1014. "on the Glance image."))
  1015. raise exception.ImageUnacceptable(image_id=instance['image_ref'],
  1016. reason=reason)
  1017. # - single root pool only
  1018. if not deployables[0].rootOnly:
  1019. reason = _("Image contains more than one ZFS pool.")
  1020. raise exception.ImageUnacceptable(image_id=instance['image_ref'],
  1021. reason=reason)
  1022. # - looks like it's OK
  1023. self._validated_archives.append(instance['image_ref'])
  1024. def _validate_flavor(self, instance):
  1025. """Validate the flavor for compatibility with zone brands"""
  1026. flavor = self._get_flavor(instance)
  1027. extra_specs = flavor['extra_specs'].copy()
  1028. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  1029. if brand == ZONE_BRAND_SOLARIS_KZ:
  1030. # verify the memory is 256mb aligned
  1031. test_size = Size('256MB')
  1032. instance_size = Size('%sMB' % instance['memory_mb'])
  1033. if instance_size.byte_value % test_size.byte_value:
  1034. # non-zero result so it doesn't align
  1035. raise MemoryAlignmentIncorrect(
  1036. flavor=flavor['name'],
  1037. memsize=str(instance['memory_mb']),
  1038. align='256')
  1039. def _suri_from_volume_info(self, connection_info):
  1040. """Returns a suri(5) formatted string based on connection_info.
  1041. Currently supports local ZFS volume, NFS, Fibre Channel and iSCSI
  1042. driver types.
  1043. """
  1044. driver_type = connection_info['driver_volume_type']
  1045. if driver_type not in ['iscsi', 'fibre_channel', 'local', 'nfs']:
  1046. raise exception.VolumeDriverNotFound(driver_type=driver_type)
  1047. if driver_type == 'local':
  1048. suri = 'dev:/dev/zvol/dsk/%s' % connection_info['volume_path']
  1049. elif driver_type == 'iscsi':
  1050. data = connection_info['data']
  1051. # suri(5) format:
  1052. # iscsi://<host>[:<port>]/target.<IQN>,lun.<LUN>
  1053. # luname-only URI format for the multipathing:
  1054. # iscsi://<host>[:<port>]/luname.naa.<ID>
  1055. # Sample iSCSI connection data values:
  1056. # target_portal: 192.168.1.244:3260
  1057. # target_iqn: iqn.2010-10.org.openstack:volume-a89c.....
  1058. # target_lun: 1
  1059. suri = None
  1060. if 'target_iqns' in data:
  1061. target = data['target_iqns'][0]
  1062. target_lun = data['target_luns'][0]
  1063. try:
  1064. utils.execute('/usr/sbin/iscsiadm', 'list', 'target',
  1065. '-vS', target)
  1066. out, err = utils.execute('/usr/sbin/suriadm', 'lookup-uri',
  1067. '-t', 'iscsi',
  1068. '-p', 'target=%s' % target,
  1069. '-p', 'lun=%s' % target_lun)
  1070. for line in [l.strip() for l in out.splitlines()]:
  1071. if "luname.naa." in line:
  1072. LOG.debug(_("The found luname-only URI for the "
  1073. "LUN '%s' is '%s'.") %
  1074. (target_lun, line))
  1075. suri = line
  1076. except processutils.ProcessExecutionError as ex:
  1077. reason = ex.stderr
  1078. LOG.debug(_("Failed to lookup-uri for volume '%s', lun "
  1079. "'%s': '%s'.") % (target, target_lun, reason))
  1080. if suri is None:
  1081. suri = 'iscsi://%s/target.%s,lun.%d' % (data['target_portal'],
  1082. data['target_iqn'],
  1083. data['target_lun'])
  1084. # TODO(npower): need to handle CHAP authentication also
  1085. elif driver_type == 'nfs':
  1086. data = connection_info['data']
  1087. suri = (
  1088. 'nfs://cinder:cinder@%s/%s' %
  1089. (data['export'].replace(':', ''), data['name'])
  1090. )
  1091. elif driver_type == 'fibre_channel':
  1092. data = connection_info['data']
  1093. target_wwn = data['target_wwn']
  1094. # Ensure there's a fibre channel HBA.
  1095. hbas = self._get_fc_hbas()
  1096. if not hbas:
  1097. LOG.error(_("Cannot attach Fibre Channel volume because "
  1098. "no Fibre Channel HBA initiators were found"))
  1099. raise exception.InvalidVolume(
  1100. reason="No host Fibre Channel initiator found")
  1101. target_lun = data['target_lun']
  1102. # If the volume was exported just a few seconds previously then
  1103. # it will probably not be visible to the local adapter yet.
  1104. # Invoke 'fcinfo remote-port' on all local HBA ports to trigger
  1105. # a refresh.
  1106. for wwpn in self._get_fc_wwpns():
  1107. utils.execute('/usr/sbin/fcinfo', 'remote-port', '-p', wwpn)
  1108. suri = self._lookup_fc_volume_suri(target_wwn, target_lun)
  1109. return suri
  1110. def _lookup_fc_volume_suri(self, target_wwn, target_lun):
  1111. """Searching the LU based URI for the FC LU. """
  1112. wwns = []
  1113. if isinstance(target_wwn, list):
  1114. wwns = target_wwn
  1115. else:
  1116. wwns.append(target_wwn)
  1117. for _none in range(3):
  1118. for wwn in wwns:
  1119. try:
  1120. out, err = utils.execute('/usr/sbin/suriadm', 'lookup-uri',
  1121. '-p', 'target=naa.%s' % wwn,
  1122. '-p', 'lun=%s' % target_lun)
  1123. for line in [l.strip() for l in out.splitlines()]:
  1124. if line.startswith("lu:luname.naa."):
  1125. return line
  1126. except processutils.ProcessExecutionError as ex:
  1127. reason = ex.stderr
  1128. LOG.debug(_("Failed to lookup-uri for volume '%s', lun "
  1129. "%s: %s") % (wwn, target_lun, reason))
  1130. greenthread.sleep(2)
  1131. else:
  1132. msg = _("Unable to lookup URI of Fibre Channel volume "
  1133. "with lun '%s'." % target_lun)
  1134. raise exception.InvalidVolume(reason=msg)
  1135. def _set_global_properties(self, name, extra_specs, brand):
  1136. """Set Solaris Zone's global properties if supplied via flavor."""
  1137. zone = self._get_zone_by_name(name)
  1138. if zone is None:
  1139. raise exception.InstanceNotFound(instance_id=name)
  1140. # TODO(dcomay): Should figure this out via the brands themselves.
  1141. zonecfg_items = [
  1142. 'bootargs',
  1143. 'brand',
  1144. 'hostid'
  1145. ]
  1146. if brand == ZONE_BRAND_SOLARIS:
  1147. zonecfg_items.extend(
  1148. ['file-mac-profile', 'fs-allowed', 'limitpriv'])
  1149. else:
  1150. zonecfg_items.extend(['cpu-arch'])
  1151. with ZoneConfig(zone) as zc:
  1152. for key, value in extra_specs.iteritems():
  1153. # Ignore not-zonecfg-scoped brand properties.
  1154. if not key.startswith('zonecfg:'):
  1155. continue
  1156. _scope, prop = key.split(':', 1)
  1157. # Ignore the 'brand' property if present.
  1158. if prop == 'brand':
  1159. continue
  1160. # Ignore but warn about unsupported zonecfg-scoped properties.
  1161. if prop not in zonecfg_items:
  1162. LOG.warning(_("Ignoring unsupported zone property '%s' "
  1163. "set on flavor for instance '%s'")
  1164. % (prop, name))
  1165. continue
  1166. zc.setprop('global', prop, value)
  1167. def _create_boot_volume(self, context, instance):
  1168. """Create a (Cinder) volume service backed boot volume"""
  1169. boot_vol_az = CONF.solariszones.boot_volume_az
  1170. boot_vol_type = CONF.solariszones.boot_volume_type
  1171. try:
  1172. vol = self._volume_api.create(
  1173. context, instance['root_gb'],
  1174. instance['hostname'] + "-" + self._rootzpool_suffix,
  1175. "Boot volume for instance '%s' (%s)"
  1176. % (instance['name'], instance['uuid']),
  1177. volume_type=boot_vol_type, availability_zone=boot_vol_az)
  1178. # TODO(npower): Polling is what nova/compute/manager also does when
  1179. # creating a new volume, so we do likewise here.
  1180. while True:
  1181. volume = self._volume_api.get(context, vol['id'])
  1182. if volume['status'] != 'creating':
  1183. return volume
  1184. greenthread.sleep(1)
  1185. except Exception as reason:
  1186. LOG.exception(_("Unable to create root zpool volume for instance "
  1187. "'%s': %s") % (instance['name'], reason))
  1188. raise
  1189. def _connect_boot_volume(self, volume, mountpoint, context, instance):
  1190. """Connect a (Cinder) volume service backed boot volume"""
  1191. instance_uuid = instance['uuid']
  1192. volume_id = volume['id']
  1193. connector = self.get_volume_connector(instance)
  1194. connection_info = self._volume_api.initialize_connection(context,
  1195. volume_id,
  1196. connector)
  1197. connection_info['serial'] = volume_id
  1198. # Check connection_info to determine if the provided volume is
  1199. # local to this compute node. If it is, then don't use it for
  1200. # Solaris branded zones in order to avoid a known ZFS deadlock issue
  1201. # when using a zpool within another zpool on the same system.
  1202. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  1203. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  1204. if brand == ZONE_BRAND_SOLARIS:
  1205. driver_type = connection_info['driver_volume_type']
  1206. if driver_type == 'local':
  1207. msg = _("Detected 'local' zvol driver volume type "
  1208. "from volume service, which should not be "
  1209. "used as a boot device for 'solaris' "
  1210. "branded zones.")
  1211. raise exception.InvalidVolume(reason=msg)
  1212. elif driver_type == 'iscsi':
  1213. # Check for a potential loopback iSCSI situation
  1214. data = connection_info['data']
  1215. target_portal = data['target_portal']
  1216. # Strip off the port number (eg. 127.0.0.1:3260)
  1217. host = target_portal.rsplit(':', 1)
  1218. # Strip any enclosing '[' and ']' brackets for
  1219. # IPv6 addresses.
  1220. target_host = host[0].strip('[]')
  1221. # Check if target_host is an IP or hostname matching the
  1222. # connector host or IP, which would mean the provisioned
  1223. # iSCSI LUN is on the same host as the instance.
  1224. if target_host in [connector['ip'], connector['host']]:
  1225. msg = _("iSCSI connection info from volume "
  1226. "service indicates that the target is a "
  1227. "local volume, which should not be used "
  1228. "as a boot device for 'solaris' branded "
  1229. "zones.")
  1230. raise exception.InvalidVolume(reason=msg)
  1231. # Assuming that fibre_channel is non-local
  1232. elif driver_type != 'fibre_channel':
  1233. # Some other connection type that we don't understand
  1234. # Let zone use some local fallback instead.
  1235. msg = _("Unsupported volume driver type '%s' can not be used "
  1236. "as a boot device for zones." % driver_type)
  1237. raise exception.InvalidVolume(reason=msg)
  1238. # Volume looks OK to use. Notify Cinder of the attachment.
  1239. self._volume_api.attach(context, volume_id, instance_uuid, mountpoint)
  1240. return connection_info
  1241. def _set_boot_device(self, name, connection_info, brand):
  1242. """Set the boot device specified by connection_info"""
  1243. zone = self._get_zone_by_name(name)
  1244. if zone is None:
  1245. raise exception.InstanceNotFound(instance_id=name)
  1246. suri = self._suri_from_volume_info(connection_info)
  1247. with ZoneConfig(zone) as zc:
  1248. # ZOSS device configuration is different for the solaris-kz brand
  1249. if brand == ZONE_BRAND_SOLARIS_KZ:
  1250. zc.zone.setResourceProperties(
  1251. zonemgr.Resource("device",
  1252. [zonemgr.Property("bootpri", "0")]),
  1253. [zonemgr.Property("storage", suri)])
  1254. else:
  1255. zc.addresource(ROOTZPOOL_RESOURCE,
  1256. [zonemgr.Property("storage", listvalue=[suri])],
  1257. ignore_exists=True)
  1258. def _set_num_cpu(self, name, vcpus, brand):
  1259. """Set number of VCPUs in a Solaris Zone configuration."""
  1260. zone = self._get_zone_by_name(name)
  1261. if zone is None:
  1262. raise exception.InstanceNotFound(instance_id=name)
  1263. # The Solaris Zone brand type is used to specify the type of
  1264. # 'cpu' resource set in the Solaris Zone configuration.
  1265. if brand == ZONE_BRAND_SOLARIS:
  1266. vcpu_resource = 'capped-cpu'
  1267. else:
  1268. vcpu_resource = 'virtual-cpu'
  1269. # TODO(dcomay): Until 17881862 is resolved, this should be turned into
  1270. # an appropriate 'rctl' resource for the 'capped-cpu' case.
  1271. with ZoneConfig(zone) as zc:
  1272. zc.setprop(vcpu_resource, 'ncpus', str(vcpus))
  1273. def _set_memory_cap(self, name, memory_mb, brand):
  1274. """Set memory cap in a Solaris Zone configuration."""
  1275. zone = self._get_zone_by_name(name)
  1276. if zone is None:
  1277. raise exception.InstanceNotFound(instance_id=name)
  1278. # The Solaris Zone brand type is used to specify the type of
  1279. # 'memory' cap set in the Solaris Zone configuration.
  1280. if brand == ZONE_BRAND_SOLARIS:
  1281. mem_resource = 'swap'
  1282. else:
  1283. mem_resource = 'physical'
  1284. with ZoneConfig(zone) as zc:
  1285. zc.setprop('capped-memory', mem_resource, '%dM' % memory_mb)
  1286. def _ovs_add_port(self, instance, vif, port):
  1287. if vif['type'] == 'binding_failed':
  1288. LOG.error(_('Port binding has failed for VIF %s. Ensure that '
  1289. 'OVS agent is running and/or bridge_mappings are '
  1290. 'correctly configured. VM will not have network '
  1291. 'connectivity') % vif)
  1292. ovs_bridge = CONF.neutron.ovs_bridge
  1293. cmd = ['/usr/sbin/ovs-vsctl',
  1294. '--timeout=%s' % CONF.ovs_vsctl_timeout,
  1295. '--', '--if-exists', 'del-port', ovs_bridge, port,
  1296. '--', 'add-port', ovs_bridge, port,
  1297. '--', 'set', 'Interface', port,
  1298. 'external-ids:iface-id=%s' % vif['id'],
  1299. 'external-ids:iface-status=active',
  1300. 'external-ids:attached-mac=%s' % vif['address'],
  1301. 'external-ids:vm-uuid=%s' % instance['uuid']
  1302. ]
  1303. try:
  1304. out, err = utils.execute(*cmd)
  1305. except Exception as reason:
  1306. msg = (_("Failed to add port '%s' with MAC address '%s' to "
  1307. "OVS Bridge '%s': %s")
  1308. % (port, vif['address'], ovs_bridge, reason))
  1309. raise exception.NovaException(msg)
  1310. LOG.debug(_('Successfully added port %s with MAC adddress %s') %
  1311. (port, vif['address']))
  1312. def _ovs_delete_port(self, port, log_warnings=False):
  1313. ovs_bridge = CONF.neutron.ovs_bridge
  1314. cmd = ['/usr/sbin/ovs-vsctl',
  1315. '--timeout=%s' % CONF.ovs_vsctl_timeout,
  1316. '--', '--if-exists', 'del-port', ovs_bridge, port]
  1317. try:
  1318. out, err = utils.execute(*cmd)
  1319. LOG.debug(_('Removed port %s from the OVS bridge %s') %
  1320. (port, ovs_bridge))
  1321. except Exception as reason:
  1322. msg = (_("Unable to remove port '%s' from the OVS "
  1323. "bridge '%s': %s") % (port, ovs_bridge, reason))
  1324. if log_warnings:
  1325. LOG.warning(msg)
  1326. else:
  1327. raise nova.exception.NovaException(msg)
  1328. def _plug_vifs(self, instance, network_info):
  1329. if not network_info:
  1330. LOG.debug(_("Instance has no VIF. Nothing to plug."))
  1331. return
  1332. # first find out all the anets for a given instance
  1333. try:
  1334. out, err = utils.execute('/usr/sbin/dladm', 'show-vnic',
  1335. '-z', instance['name'],
  1336. '-po', 'link,macaddress')
  1337. except Exception as reason:
  1338. msg = (_("Unable to get interfaces for instance '%s': %s")
  1339. % (instance['name'], reason))
  1340. raise exception.NovaException(msg)
  1341. anetdict = {}
  1342. for anet_maddr in out.strip().splitlines():
  1343. anet, maddr = anet_maddr.strip().split(':', 1)
  1344. maddr = maddr.replace('\\', '')
  1345. maddr = ''.join(['%02x' % int(b, 16) for b in maddr.split(':')])
  1346. anetdict[maddr] = anet
  1347. LOG.debug(_("List of instance %s's anets: %s")
  1348. % (instance['name'], anetdict))
  1349. # we now have a list of VNICs that belong to the VM
  1350. # we need to map the VNIC to the bridge
  1351. for vif in network_info:
  1352. vif_maddr = ''.join(['%02x' % int(b, 16) for b in
  1353. vif['address'].split(':')])
  1354. anet = anetdict.get(vif_maddr)
  1355. if anet is None:
  1356. LOG.error(_('Failed to add port %s connected to network %s '
  1357. 'to instance %s')
  1358. % (vif['ovs_interfaceid'], vif['network']['id'],
  1359. instance['name']))
  1360. continue
  1361. self._ovs_add_port(instance, vif, anet)
  1362. def _unplug_vifs(self, instance):
  1363. ovs_bridge = CONF.neutron.ovs_bridge
  1364. # remove the anets from the OVS bridge
  1365. cmd = ['/usr/sbin/ovs-vsctl', '--timeout=%s' % CONF.ovs_vsctl_timeout,
  1366. 'list-ports', ovs_bridge]
  1367. try:
  1368. out, err = utils.execute(*cmd)
  1369. except Exception as reason:
  1370. msg = (_("Unable to get interfaces for instance '%s': %s")
  1371. % (instance['name'], reason))
  1372. raise exception.NovaException(msg)
  1373. for port in out.strip().splitlines():
  1374. if port.split('/')[0] != instance['name']:
  1375. continue
  1376. self._ovs_delete_port(port, log_warnings=True)
  1377. def _set_ovs_info(self, context, zone, brand, first_anet, vif):
  1378. # Need to be admin to retrieve provider:network_type attribute
  1379. network_plugin = neutronv2_api.get_client(context, admin=True)
  1380. network = network_plugin.show_network(
  1381. vif['network']['id'])['network']
  1382. network_type = network['provider:network_type']
  1383. lower_link = None
  1384. if network_type == 'vxlan':
  1385. lower_link = 'ovs.vxlan1'
  1386. elif network_type in ['vlan', 'flat']:
  1387. physical_network = network['provider:physical_network']
  1388. # retrieve the other_config information from Open_vSwitch table
  1389. try:
  1390. results = get_ovsdb_info('Open_vSwitch', ['other_config'])
  1391. except Exception as err:
  1392. LOG.exception(_("Failed to retrieve other_config: %s"), err)
  1393. raise
  1394. other_config = results[0]['other_config']
  1395. if not other_config:
  1396. msg = (_("'other_config' column in 'Open_vSwitch' OVSDB table "
  1397. "is not configured. Please configure it so that the "
  1398. "lower-link can be determined for the instance's "
  1399. "interface."))
  1400. LOG.error(msg)
  1401. raise exception.NovaException(msg)
  1402. bridge_mappings = other_config.get('bridge_mappings')
  1403. if not bridge_mappings:
  1404. msg = (_("'bridge_mappings' info is not set in the "
  1405. "'other_config' column of 'Open_vSwitch' OVSDB "
  1406. "table. Please configure it so that the lower-link "
  1407. "can be determined for the instance's interface."))
  1408. LOG.error(msg)
  1409. raise exception.NovaException(msg)
  1410. for bridge_mapping in bridge_mappings.split(','):
  1411. if physical_network in bridge_mapping:
  1412. lower_link = bridge_mapping.split(':')[1]
  1413. break
  1414. if not lower_link:
  1415. msg = (_("Failed to determine the lower_link for vif '%s'.") %
  1416. (vif))
  1417. LOG.error(msg)
  1418. raise exception.NovaException(msg)
  1419. else:
  1420. # TYPE_GRE and TYPE_LOCAL
  1421. msg = (_("Unsupported network type: %s") % network_type)
  1422. LOG.error(msg)
  1423. raise exception.NovaException(msg)
  1424. mtu = network['mtu']
  1425. with ZoneConfig(zone) as zc:
  1426. if first_anet:
  1427. zc.setprop('anet', 'lower-link', lower_link)
  1428. zc.setprop('anet', 'configure-allowed-address', 'false')
  1429. zc.setprop('anet', 'mac-address', vif['address'])
  1430. if mtu > 0:
  1431. zc.setprop('anet', 'mtu', str(mtu))
  1432. else:
  1433. props = [zonemgr.Property('lower-link', lower_link),
  1434. zonemgr.Property('configure-allowed-address',
  1435. 'false'),
  1436. zonemgr.Property('mac-address', vif['address'])]
  1437. if mtu > 0:
  1438. props.append(zonemgr.Property('mtu', str(mtu)))
  1439. zc.addresource('anet', props)
  1440. prop_filter = [zonemgr.Property('mac-address', vif['address'])]
  1441. if brand == ZONE_BRAND_SOLARIS:
  1442. anetname = lookup_resource_property(zc.zone, 'anet',
  1443. 'linkname', prop_filter)
  1444. else:
  1445. anetid = lookup_resource_property(zc.zone, 'anet', 'id',
  1446. prop_filter)
  1447. anetname = 'net%s' % anetid
  1448. return anetname
  1449. def _set_network(self, context, name, instance, network_info, brand,
  1450. sc_dir):
  1451. """add networking information to the zone."""
  1452. zone = self._get_zone_by_name(name)
  1453. if zone is None:
  1454. raise exception.InstanceNotFound(instance_id=name)
  1455. if not network_info:
  1456. with ZoneConfig(zone) as zc:
  1457. if brand == ZONE_BRAND_SOLARIS:
  1458. zc.removeresources("anet",
  1459. [zonemgr.Property("linkname", "net0")])
  1460. else:
  1461. zc.removeresources("anet", [zonemgr.Property("id", "0")])
  1462. return
  1463. for vifid, vif in enumerate(network_info):
  1464. LOG.debug("%s", jsonutils.dumps(vif, indent=5))
  1465. ip = vif['network']['subnets'][0]['ips'][0]['address']
  1466. cidr = vif['network']['subnets'][0]['cidr']
  1467. ip_cidr = "%s/%s" % (ip, cidr.split('/')[1])
  1468. ip_version = vif['network']['subnets'][0]['version']
  1469. dhcp_server = \
  1470. vif['network']['subnets'][0]['meta'].get('dhcp_server')
  1471. enable_dhcp = dhcp_server is not None
  1472. route = vif['network']['subnets'][0]['gateway']['address']
  1473. dns_list = vif['network']['subnets'][0]['dns']
  1474. nameservers = []
  1475. for dns in dns_list:
  1476. if dns['type'] == 'dns':
  1477. nameservers.append(dns['address'])
  1478. anetname = self._set_ovs_info(context, zone, brand, vifid == 0,
  1479. vif)
  1480. # create the required sysconfig file (or skip if this is part of a
  1481. # resize or evacuate process)
  1482. tstate = instance['task_state']
  1483. if tstate not in [task_states.RESIZE_FINISH,
  1484. task_states.RESIZE_REVERTING,
  1485. task_states.RESIZE_MIGRATING,
  1486. task_states.REBUILD_SPAWNING] or \
  1487. (tstate == task_states.REBUILD_SPAWNING and
  1488. instance.system_metadata['rebuilding'] == 'true'):
  1489. if enable_dhcp:
  1490. tree = sysconfig.create_ncp_defaultfixed('dhcp',
  1491. anetname, vifid,
  1492. ip_version)
  1493. else:
  1494. host_routes = vif['network']['subnets'][0]['routes']
  1495. tree = sysconfig.create_ncp_defaultfixed('static',
  1496. anetname, vifid,
  1497. ip_version,
  1498. ip_cidr, route,
  1499. nameservers,
  1500. host_routes)
  1501. fp = os.path.join(sc_dir, 'zone-network-%d.xml' % vifid)
  1502. sysconfig.create_sc_profile(fp, tree)
  1503. def _set_suspend(self, instance):
  1504. """Use the instance name to specify the pathname for the suspend image.
  1505. """
  1506. name = instance['name']
  1507. zone = self._get_zone_by_name(name)
  1508. if zone is None:
  1509. raise exception.InstanceNotFound(instance_id=name)
  1510. path = os.path.join(CONF.solariszones.zones_suspend_path,
  1511. '%{zonename}')
  1512. with ZoneConfig(zone) as zc:
  1513. zc.addresource('suspend', [zonemgr.Property('path', path)])
  1514. def _verify_sysconfig(self, sc_dir, instance, admin_password=None):
  1515. """verify the SC profile(s) passed in contain an entry for
  1516. system/config-user to configure the root account. If an SSH key is
  1517. specified, configure root's profile to use it.
  1518. """
  1519. usercheck = lambda e: e.attrib.get('name') == 'system/config-user'
  1520. hostcheck = lambda e: e.attrib.get('name') == 'system/identity'
  1521. root_account_needed = True
  1522. hostname_needed = True
  1523. sshkey = instance.get('key_data')
  1524. name = instance.get('hostname')
  1525. encrypted_password = None
  1526. # encrypt admin password, using SHA-256 as default
  1527. if admin_password is not None:
  1528. encrypted_password = sha256_crypt.encrypt(admin_password)
  1529. # find all XML files in sc_dir
  1530. for root, dirs, files in os.walk(sc_dir):
  1531. for fname in [f for f in files if f.endswith(".xml")]:
  1532. fileroot = etree.parse(os.path.join(root, fname))
  1533. # look for config-user properties
  1534. if filter(usercheck, fileroot.findall('service')):
  1535. # a service element was found for config-user. Verify
  1536. # root's password is set, the admin account name is set and
  1537. # the admin's password is set
  1538. pgs = fileroot.iter('property_group')
  1539. for pg in pgs:
  1540. if pg.attrib.get('name') == 'root_account':
  1541. root_account_needed = False
  1542. # look for identity properties
  1543. if filter(hostcheck, fileroot.findall('service')):
  1544. for props in fileroot.iter('propval'):
  1545. if props.attrib.get('name') == 'nodename':
  1546. hostname_needed = False
  1547. # Verify all of the requirements were met. Create the required SMF
  1548. # profile(s) if needed.
  1549. if root_account_needed:
  1550. fp = os.path.join(sc_dir, 'config-root.xml')
  1551. if admin_password is not None and sshkey is not None:
  1552. # store password for horizon retrieval
  1553. ctxt = nova_context.get_admin_context()
  1554. enc = crypto.ssh_encrypt_text(sshkey, admin_password)
  1555. instance.system_metadata.update(
  1556. password.convert_password(ctxt, base64.b64encode(enc)))
  1557. instance.save()
  1558. if encrypted_password is not None or sshkey is not None:
  1559. # set up the root account as 'normal' with no expiration,
  1560. # an ssh key, and a root password
  1561. tree = sysconfig.create_default_root_account(
  1562. sshkey=sshkey, password=encrypted_password)
  1563. else:
  1564. # sets up root account with expiration if sshkey is None
  1565. # and password is none
  1566. tree = sysconfig.create_default_root_account(expire='0')
  1567. sysconfig.create_sc_profile(fp, tree)
  1568. elif sshkey is not None:
  1569. fp = os.path.join(sc_dir, 'config-root-ssh-keys.xml')
  1570. tree = sysconfig.create_root_ssh_keys(sshkey)
  1571. sysconfig.create_sc_profile(fp, tree)
  1572. if hostname_needed and name is not None:
  1573. fp = os.path.join(sc_dir, 'hostname.xml')
  1574. sysconfig.create_sc_profile(fp, sysconfig.create_hostname(name))
  1575. def _create_config(self, context, instance, network_info, connection_info,
  1576. sc_dir, admin_password=None):
  1577. """Create a new Solaris Zone configuration."""
  1578. name = instance['name']
  1579. if self._get_zone_by_name(name) is not None:
  1580. raise exception.InstanceExists(name=name)
  1581. flavor = self._get_flavor(instance)
  1582. extra_specs = flavor['extra_specs'].copy()
  1583. # If unspecified, default zone brand is ZONE_BRAND_SOLARIS
  1584. brand = extra_specs.get('zonecfg:brand')
  1585. if brand is None:
  1586. LOG.warning(_("'zonecfg:brand' key not found in extra specs for "
  1587. "flavor '%s'. Defaulting to 'solaris'"
  1588. % flavor['name']))
  1589. brand = ZONE_BRAND_SOLARIS
  1590. template = ZONE_BRAND_TEMPLATE.get(brand)
  1591. # TODO(dcomay): Detect capability via libv12n(3LIB) or virtinfo(1M).
  1592. if template is None:
  1593. msg = (_("Invalid brand '%s' specified for instance '%s'"
  1594. % (brand, name)))
  1595. raise exception.NovaException(msg)
  1596. tstate = instance['task_state']
  1597. if tstate not in [task_states.RESIZE_FINISH,
  1598. task_states.RESIZE_REVERTING,
  1599. task_states.RESIZE_MIGRATING,
  1600. task_states.REBUILD_SPAWNING] or \
  1601. (tstate == task_states.REBUILD_SPAWNING and
  1602. instance.system_metadata['rebuilding'] == 'true'):
  1603. sc_profile = extra_specs.get('install:sc_profile')
  1604. if sc_profile is not None:
  1605. if os.path.isfile(sc_profile):
  1606. shutil.copy(sc_profile, sc_dir)
  1607. elif os.path.isdir(sc_profile):
  1608. shutil.copytree(sc_profile,
  1609. os.path.join(sc_dir, 'sysconfig'))
  1610. self._verify_sysconfig(sc_dir, instance, admin_password)
  1611. LOG.debug(_("Creating zone configuration for '%s' (%s)")
  1612. % (name, instance['display_name']))
  1613. try:
  1614. self.zone_manager.create(name, None, template)
  1615. self._set_global_properties(name, extra_specs, brand)
  1616. hostid = instance.system_metadata.get('hostid')
  1617. if hostid:
  1618. zone = self._get_zone_by_name(name)
  1619. with ZoneConfig(zone) as zc:
  1620. zc.setprop('global', 'hostid', hostid)
  1621. if connection_info is not None:
  1622. self._set_boot_device(name, connection_info, brand)
  1623. self._set_num_cpu(name, instance['vcpus'], brand)
  1624. self._set_memory_cap(name, instance['memory_mb'], brand)
  1625. self._set_network(context, name, instance, network_info, brand,
  1626. sc_dir)
  1627. except Exception as ex:
  1628. reason = zonemgr_strerror(ex)
  1629. LOG.exception(_("Unable to create configuration for instance '%s' "
  1630. "via zonemgr(3RAD): %s") % (name, reason))
  1631. raise
  1632. def _create_vnc_console_service(self, instance):
  1633. """Create a VNC console SMF service for a Solaris Zone"""
  1634. # Basic environment checks first: vncserver and xterm
  1635. if not os.path.exists(VNC_SERVER_PATH):
  1636. LOG.warning(_("Zone VNC console SMF service not available on this "
  1637. "compute node. %s is missing. Run 'pkg install "
  1638. "x11/server/xvnc'") % VNC_SERVER_PATH)
  1639. raise exception.ConsoleTypeUnavailable(console_type='vnc')
  1640. if not os.path.exists(XTERM_PATH):
  1641. LOG.warning(_("Zone VNC console SMF service not available on this "
  1642. "compute node. %s is missing. Run 'pkg install "
  1643. "terminal/xterm'") % XTERM_PATH)
  1644. raise exception.ConsoleTypeUnavailable(console_type='vnc')
  1645. name = instance['name']
  1646. # TODO(npower): investigate using RAD instead of CLI invocation
  1647. try:
  1648. out, err = utils.execute('/usr/sbin/svccfg',
  1649. '-s', VNC_CONSOLE_BASE_FMRI, 'add', name)
  1650. except processutils.ProcessExecutionError as ex:
  1651. if self._has_vnc_console_service(instance):
  1652. LOG.debug(_("Ignoring attempt to create existing zone VNC "
  1653. "console SMF service for instance '%s'") % name)
  1654. return
  1655. reason = ex.stderr
  1656. LOG.exception(_("Unable to create zone VNC console SMF service "
  1657. "'{0}': {1}").format(VNC_CONSOLE_BASE_FMRI + ':' +
  1658. name, reason))
  1659. raise
  1660. def _delete_vnc_console_service(self, instance):
  1661. """Delete a VNC console SMF service for a Solaris Zone"""
  1662. name = instance['name']
  1663. self._disable_vnc_console_service(instance)
  1664. # TODO(npower): investigate using RAD instead of CLI invocation
  1665. try:
  1666. out, err = utils.execute('/usr/sbin/svccfg',
  1667. '-s', VNC_CONSOLE_BASE_FMRI, 'delete',
  1668. name)
  1669. except processutils.ProcessExecutionError as ex:
  1670. if not self._has_vnc_console_service(instance):
  1671. LOG.debug(_("Ignoring attempt to delete a non-existent zone "
  1672. "VNC console SMF service for instance '%s'")
  1673. % name)
  1674. return
  1675. reason = ex.stderr
  1676. LOG.exception(_("Unable to delete zone VNC console SMF service "
  1677. "'%s': %s")
  1678. % (VNC_CONSOLE_BASE_FMRI + ':' + name, reason))
  1679. raise
  1680. def _enable_vnc_console_service(self, instance):
  1681. """Enable a zone VNC console SMF service"""
  1682. name = instance['name']
  1683. console_fmri = VNC_CONSOLE_BASE_FMRI + ':' + name
  1684. # TODO(npower): investigate using RAD instead of CLI invocation
  1685. try:
  1686. # The console SMF service exits with SMF_TEMP_DISABLE to prevent
  1687. # unnecessarily coming online at boot. Tell it to really bring
  1688. # it online.
  1689. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  1690. 'setprop', 'vnc/nova-enabled=true')
  1691. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  1692. 'refresh')
  1693. out, err = utils.execute('/usr/sbin/svcadm', 'enable',
  1694. console_fmri)
  1695. except processutils.ProcessExecutionError as ex:
  1696. if not self._has_vnc_console_service(instance):
  1697. LOG.debug(_("Ignoring attempt to enable a non-existent zone "
  1698. "VNC console SMF service for instance '%s'")
  1699. % name)
  1700. return
  1701. reason = ex.stderr
  1702. LOG.exception(_("Unable to start zone VNC console SMF service "
  1703. "'%s': %s") % (console_fmri, reason))
  1704. raise
  1705. # Allow some time for the console service to come online.
  1706. greenthread.sleep(2)
  1707. while True:
  1708. try:
  1709. out, err = utils.execute('/usr/bin/svcs', '-H', '-o', 'state',
  1710. console_fmri)
  1711. state = out.strip()
  1712. if state == 'online':
  1713. break
  1714. elif state in ['maintenance', 'offline']:
  1715. LOG.error(_("Zone VNC console SMF service '%s' is in the "
  1716. "'%s' state. Run 'svcs -x %s' for details.")
  1717. % (console_fmri, state, console_fmri))
  1718. raise exception.ConsoleNotFoundForInstance(
  1719. instance_uuid=instance['uuid'])
  1720. # Wait for service state to transition to (hopefully) online
  1721. # state or offline/maintenance states.
  1722. greenthread.sleep(2)
  1723. except processutils.ProcessExecutionError as ex:
  1724. reason = ex.stderr
  1725. LOG.exception(_("Error querying state of zone VNC console SMF "
  1726. "service '%s': %s") % (console_fmri, reason))
  1727. raise
  1728. # TODO(npower): investigate using RAD instead of CLI invocation
  1729. try:
  1730. # The console SMF service exits with SMF_TEMP_DISABLE to prevent
  1731. # unnecessarily coming online at boot. Make that happen.
  1732. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  1733. 'setprop', 'vnc/nova-enabled=false')
  1734. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  1735. 'refresh')
  1736. except processutils.ProcessExecutionError as ex:
  1737. reason = ex.stderr
  1738. LOG.exception(_("Unable to update 'vnc/nova-enabled' property for "
  1739. "zone VNC console SMF service '%s': %s")
  1740. % (console_fmri, reason))
  1741. raise
  1742. def _disable_vnc_console_service(self, instance):
  1743. """Disable a zone VNC console SMF service"""
  1744. name = instance['name']
  1745. if not self._has_vnc_console_service(instance):
  1746. LOG.debug(_("Ignoring attempt to disable a non-existent zone VNC "
  1747. "console SMF service for instance '%s'") % name)
  1748. return
  1749. console_fmri = VNC_CONSOLE_BASE_FMRI + ':' + name
  1750. # TODO(npower): investigate using RAD instead of CLI invocation
  1751. try:
  1752. out, err = utils.execute('/usr/sbin/svcadm', 'disable',
  1753. '-s', console_fmri)
  1754. except processutils.ProcessExecutionError as ex:
  1755. reason = ex.stderr
  1756. LOG.exception(_("Unable to disable zone VNC console SMF service "
  1757. "'%s': %s") % (console_fmri, reason))
  1758. # The console service sets a SMF instance property for the port
  1759. # on which the VNC service is listening. The service needs to be
  1760. # refreshed to reset the property value
  1761. try:
  1762. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  1763. 'refresh')
  1764. except processutils.ProcessExecutionError as ex:
  1765. reason = ex.stderr
  1766. LOG.exception(_("Unable to refresh zone VNC console SMF service "
  1767. "'%s': %s") % (console_fmri, reason))
  1768. def _get_vnc_console_service_state(self, instance):
  1769. """Returns state of the instance zone VNC console SMF service"""
  1770. name = instance['name']
  1771. if not self._has_vnc_console_service(instance):
  1772. LOG.warning(_("Console state requested for a non-existent zone "
  1773. "VNC console SMF service for instance '%s'")
  1774. % name)
  1775. return None
  1776. console_fmri = VNC_CONSOLE_BASE_FMRI + ':' + name
  1777. # TODO(npower): investigate using RAD instead of CLI invocation
  1778. try:
  1779. state, err = utils.execute('/usr/sbin/svcs', '-H', '-o', 'state',
  1780. console_fmri)
  1781. return state.strip()
  1782. except processutils.ProcessExecutionError as ex:
  1783. reason = ex.stderr
  1784. LOG.exception(_("Console state request failed for zone VNC "
  1785. "console SMF service for instance '%s': %s")
  1786. % (name, reason))
  1787. raise
  1788. def _has_vnc_console_service(self, instance):
  1789. """Returns True if the instance has a zone VNC console SMF service"""
  1790. name = instance['name']
  1791. console_fmri = VNC_CONSOLE_BASE_FMRI + ':' + name
  1792. # TODO(npower): investigate using RAD instead of CLI invocation
  1793. try:
  1794. utils.execute('/usr/bin/svcs', '-H', '-o', 'state', console_fmri)
  1795. return True
  1796. except Exception:
  1797. return False
  1798. def _install(self, instance, image, sc_dir):
  1799. """Install a new Solaris Zone root file system."""
  1800. name = instance['name']
  1801. zone = self._get_zone_by_name(name)
  1802. if zone is None:
  1803. raise exception.InstanceNotFound(instance_id=name)
  1804. # log the zone's configuration
  1805. with ZoneConfig(zone) as zc:
  1806. LOG.debug("-" * 80)
  1807. LOG.debug(zc.zone.exportConfig(True))
  1808. LOG.debug("-" * 80)
  1809. options = ['-a', image]
  1810. if os.listdir(sc_dir):
  1811. # the directory isn't empty so pass it along to install
  1812. options.extend(['-c', sc_dir])
  1813. try:
  1814. LOG.debug(_("Installing instance '%s' (%s)") %
  1815. (name, instance['display_name']))
  1816. zone.install(options=options)
  1817. except Exception as ex:
  1818. reason = zonemgr_strerror(ex)
  1819. LOG.exception(_("Unable to install root file system for instance "
  1820. "'%s' via zonemgr(3RAD): %s") % (name, reason))
  1821. raise
  1822. self._set_instance_metahostid(instance)
  1823. LOG.debug(_("Installation of instance '%s' (%s) complete") %
  1824. (name, instance['display_name']))
  1825. def _power_on(self, instance, network_info):
  1826. """Power on a Solaris Zone."""
  1827. name = instance['name']
  1828. zone = self._get_zone_by_name(name)
  1829. if zone is None:
  1830. raise exception.InstanceNotFound(instance_id=name)
  1831. # Attempt to update the zones hostid in the instance data, to catch
  1832. # those instances that might have been created without a hostid stored.
  1833. self._set_instance_metahostid(instance)
  1834. bootargs = []
  1835. if CONF.solariszones.solariszones_boot_options:
  1836. reset_bootargs = False
  1837. persistent = 'False'
  1838. # Get any bootargs already set in the zone
  1839. cur_bootargs = lookup_resource_property(zone, 'global', 'bootargs')
  1840. # Get any bootargs set in the instance metadata by the user
  1841. meta_bootargs = instance.metadata.get('bootargs')
  1842. if meta_bootargs:
  1843. bootargs = ['--', str(meta_bootargs)]
  1844. persistent = str(
  1845. instance.metadata.get('bootargs_persist', 'False'))
  1846. if cur_bootargs is not None and meta_bootargs != cur_bootargs:
  1847. with ZoneConfig(zone) as zc:
  1848. reset_bootargs = True
  1849. # Temporarily clear bootargs in zone config
  1850. zc.clear_resource_props('global', ['bootargs'])
  1851. try:
  1852. zone.boot(bootargs)
  1853. self._plug_vifs(instance, network_info)
  1854. except Exception as ex:
  1855. reason = zonemgr_strerror(ex)
  1856. LOG.exception(_("Unable to power on instance '%s' via "
  1857. "zonemgr(3RAD): %s") % (name, reason))
  1858. raise exception.InstancePowerOnFailure(reason=reason)
  1859. finally:
  1860. if CONF.solariszones.solariszones_boot_options:
  1861. if meta_bootargs and persistent.lower() == 'false':
  1862. # We have consumed the metadata bootargs and
  1863. # the user asked for them not to be persistent so
  1864. # clear them out now.
  1865. instance.metadata.pop('bootargs', None)
  1866. instance.metadata.pop('bootargs_persist', None)
  1867. if reset_bootargs:
  1868. with ZoneConfig(zone) as zc:
  1869. # restore original boot args in zone config
  1870. zc.setprop('global', 'bootargs', cur_bootargs)
  1871. def _uninstall(self, instance):
  1872. """Uninstall an existing Solaris Zone root file system."""
  1873. name = instance['name']
  1874. zone = self._get_zone_by_name(name)
  1875. if zone is None:
  1876. raise exception.InstanceNotFound(instance_id=name)
  1877. if zone.state == ZONE_STATE_CONFIGURED:
  1878. LOG.debug(_("Uninstall not required for zone '%s' in state '%s'")
  1879. % (name, zone.state))
  1880. return
  1881. try:
  1882. zone.uninstall(['-F'])
  1883. except Exception as ex:
  1884. reason = zonemgr_strerror(ex)
  1885. LOG.exception(_("Unable to uninstall root file system for "
  1886. "instance '%s' via zonemgr(3RAD): %s")
  1887. % (name, reason))
  1888. raise
  1889. def _delete_config(self, instance):
  1890. """Delete an existing Solaris Zone configuration."""
  1891. name = instance['name']
  1892. if self._get_zone_by_name(name) is None:
  1893. raise exception.InstanceNotFound(instance_id=name)
  1894. try:
  1895. self.zone_manager.delete(name)
  1896. except Exception as ex:
  1897. reason = zonemgr_strerror(ex)
  1898. LOG.exception(_("Unable to delete configuration for instance '%s' "
  1899. "via zonemgr(3RAD): %s") % (name, reason))
  1900. raise
  1901. def spawn(self, context, instance, image_meta, injected_files,
  1902. admin_password, network_info=None, block_device_info=None):
  1903. """Create a new instance/VM/domain on the virtualization platform.
  1904. Once this successfully completes, the instance should be
  1905. running (power_state.RUNNING).
  1906. If this fails, any partial instance should be completely
  1907. cleaned up, and the virtualization platform should be in the state
  1908. that it was before this call began.
  1909. :param context: security context
  1910. :param instance: nova.objects.instance.Instance
  1911. This function should use the data there to guide
  1912. the creation of the new instance.
  1913. :param nova.objects.ImageMeta image_meta:
  1914. The metadata of the image of the instance.
  1915. :param injected_files: User files to inject into instance.
  1916. :param admin_password: Administrator password to set in instance.
  1917. :param network_info:
  1918. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  1919. :param block_device_info: Information about block devices to be
  1920. attached to the instance.
  1921. """
  1922. image = self._fetch_image(context, instance)
  1923. self._validate_image(context, image, instance)
  1924. self._validate_flavor(instance)
  1925. # c1d0 is the standard dev for the default boot device.
  1926. # Irrelevant value for ZFS, but Cinder gets stroppy without it.
  1927. mountpoint = "c1d0"
  1928. # Ensure no block device mappings attempt to use the reserved boot
  1929. # device (c1d0).
  1930. for entry in block_device_info.get('block_device_mapping'):
  1931. if entry['connection_info'] is None:
  1932. continue
  1933. mount_device = entry['mount_device']
  1934. if mount_device == '/dev/' + mountpoint:
  1935. msg = (_("Unable to assign '%s' to block device as it is"
  1936. "reserved for the root file system") % mount_device)
  1937. raise exception.InvalidDiskInfo(msg)
  1938. # Attempt to provision a (Cinder) volume service backed boot volume
  1939. volume = self._create_boot_volume(context, instance)
  1940. volume_id = volume['id']
  1941. name = instance['name']
  1942. try:
  1943. connection_info = self._connect_boot_volume(volume, mountpoint,
  1944. context, instance)
  1945. except exception.InvalidVolume as reason:
  1946. # This Cinder volume is not usable for ZOSS so discard it.
  1947. # zonecfg will apply default zonepath dataset configuration
  1948. # instead. Carry on
  1949. LOG.warning(_("Volume '%s' is being discarded: %s")
  1950. % (volume_id, reason))
  1951. self._volume_api.delete(context, volume_id)
  1952. connection_info = None
  1953. except Exception as reason:
  1954. # Something really bad happened. Don't pass Go.
  1955. LOG.exception(_("Unable to attach root zpool volume '%s' to "
  1956. "instance %s: %s") % (volume['id'], name, reason))
  1957. self._volume_api.delete(context, volume_id)
  1958. raise
  1959. # create a new directory for SC profiles
  1960. sc_dir = tempfile.mkdtemp(prefix="nova-sysconfig-",
  1961. dir=CONF.state_path)
  1962. os.chmod(sc_dir, 0755)
  1963. try:
  1964. self._create_config(context, instance, network_info,
  1965. connection_info, sc_dir, admin_password)
  1966. self._install(instance, image, sc_dir)
  1967. for entry in block_device_info.get('block_device_mapping'):
  1968. if entry['connection_info'] is not None:
  1969. self.attach_volume(context, entry['connection_info'],
  1970. instance, entry['mount_device'])
  1971. self._power_on(instance, network_info)
  1972. except Exception as ex:
  1973. reason = zonemgr_strerror(ex)
  1974. LOG.exception(_("Unable to spawn instance '%s' via zonemgr(3RAD): "
  1975. "'%s'") % (name, reason))
  1976. # At least attempt to uninstall the instance, depending on where
  1977. # the installation got to there could be things left behind that
  1978. # need to be cleaned up, e.g a root zpool etc.
  1979. try:
  1980. self._uninstall(instance)
  1981. except Exception as ex:
  1982. reason = zonemgr_strerror(ex)
  1983. LOG.debug(_("Unable to uninstall instance '%s' via "
  1984. "zonemgr(3RAD): %s") % (name, reason))
  1985. try:
  1986. self._delete_config(instance)
  1987. except Exception as ex:
  1988. reason = zonemgr_strerror(ex)
  1989. LOG.debug(_("Unable to unconfigure instance '%s' via "
  1990. "zonemgr(3RAD): %s") % (name, reason))
  1991. if connection_info is not None:
  1992. self._volume_api.detach(context, volume_id)
  1993. self._volume_api.delete(context, volume_id)
  1994. raise
  1995. finally:
  1996. # remove the sc_profile temp directory
  1997. shutil.rmtree(sc_dir)
  1998. if connection_info is not None:
  1999. bdm_obj = objects.BlockDeviceMappingList()
  2000. # there's only one bdm for this instance at this point
  2001. bdm = bdm_obj.get_by_instance_uuid(
  2002. context, instance.uuid).objects[0]
  2003. # update the required attributes
  2004. bdm['connection_info'] = jsonutils.dumps(connection_info)
  2005. bdm['source_type'] = 'volume'
  2006. bdm['destination_type'] = 'volume'
  2007. bdm['device_name'] = mountpoint
  2008. bdm['delete_on_termination'] = True
  2009. bdm['volume_id'] = volume_id
  2010. bdm['volume_size'] = instance['root_gb']
  2011. bdm.save()
  2012. def _power_off(self, instance, halt_type):
  2013. """Power off a Solaris Zone."""
  2014. name = instance['name']
  2015. zone = self._get_zone_by_name(name)
  2016. if zone is None:
  2017. raise exception.InstanceNotFound(instance_id=name)
  2018. # Attempt to update the zones hostid in the instance data, to catch
  2019. # those instances that might have been created without a hostid stored.
  2020. self._set_instance_metahostid(instance)
  2021. try:
  2022. self._unplug_vifs(instance)
  2023. if halt_type == 'SOFT':
  2024. zone.shutdown()
  2025. else:
  2026. # 'HARD'
  2027. zone.halt()
  2028. except Exception as ex:
  2029. reason = zonemgr_strerror(ex)
  2030. # A shutdown state could still be reached if the error was
  2031. # informational and ignorable.
  2032. if self._get_state(zone) == power_state.SHUTDOWN:
  2033. LOG.warning(_("Ignoring command error returned while "
  2034. "trying to power off instance '%s' via "
  2035. "zonemgr(3RAD): %s" % (name, reason)))
  2036. return
  2037. LOG.exception(_("Unable to power off instance '%s' "
  2038. "via zonemgr(3RAD): %s") % (name, reason))
  2039. raise exception.InstancePowerOffFailure(reason=reason)
  2040. def _samehost_revert_resize(self, context, instance, network_info,
  2041. block_device_info):
  2042. """Reverts the zones configuration to pre-resize config
  2043. """
  2044. self.power_off(instance)
  2045. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2046. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2047. name = instance['name']
  2048. self._set_num_cpu(name, instance.vcpus, brand)
  2049. self._set_memory_cap(name, instance.memory_mb, brand)
  2050. rgb = instance.root_gb
  2051. old_rvid = instance.system_metadata.get('old_instance_volid')
  2052. if old_rvid:
  2053. new_rvid = instance.system_metadata.get('new_instance_volid')
  2054. mount_dev = instance['root_device_name']
  2055. del instance.system_metadata['old_instance_volid']
  2056. self._resize_disk_migration(context, instance, new_rvid, old_rvid,
  2057. rgb, mount_dev)
  2058. def destroy(self, context, instance, network_info, block_device_info=None,
  2059. destroy_disks=True, migrate_data=None):
  2060. """Destroy the specified instance from the Hypervisor.
  2061. If the instance is not found (for example if networking failed), this
  2062. function should still succeed. It's probably a good idea to log a
  2063. warning in that case.
  2064. :param context: security context
  2065. :param instance: Instance object as returned by DB layer.
  2066. :param network_info:
  2067. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  2068. :param block_device_info: Information about block devices that should
  2069. be detached from the instance.
  2070. :param destroy_disks: Indicates if disks should be destroyed
  2071. :param migrate_data: implementation specific params
  2072. """
  2073. if (instance['task_state'] == task_states.RESIZE_REVERTING and
  2074. instance.system_metadata['old_vm_state'] == vm_states.RESIZED):
  2075. return
  2076. # A destroy is issued for the original zone for an evac case. If
  2077. # the evac fails we need to protect the zone from deletion when
  2078. # power comes back on.
  2079. evac_from = instance.system_metadata.get('evac_from')
  2080. if evac_from is not None and instance['task_state'] is None:
  2081. instance.host = evac_from
  2082. instance.node = evac_from
  2083. del instance.system_metadata['evac_from']
  2084. instance.save()
  2085. return
  2086. try:
  2087. # These methods log if problems occur so no need to double log
  2088. # here. Just catch any stray exceptions and allow destroy to
  2089. # proceed.
  2090. if self._has_vnc_console_service(instance):
  2091. self._disable_vnc_console_service(instance)
  2092. self._delete_vnc_console_service(instance)
  2093. except Exception:
  2094. pass
  2095. name = instance['name']
  2096. zone = self._get_zone_by_name(name)
  2097. # If instance cannot be found, just return.
  2098. if zone is None:
  2099. LOG.warning(_("Unable to find instance '%s' via zonemgr(3RAD)")
  2100. % name)
  2101. return
  2102. try:
  2103. if self._get_state(zone) == power_state.RUNNING:
  2104. self._power_off(instance, 'HARD')
  2105. if self._get_state(zone) == power_state.SHUTDOWN:
  2106. self._uninstall(instance)
  2107. if self._get_state(zone) == power_state.NOSTATE:
  2108. self._delete_config(instance)
  2109. except Exception as ex:
  2110. reason = zonemgr_strerror(ex)
  2111. LOG.warning(_("Unable to destroy instance '%s' via zonemgr(3RAD): "
  2112. "%s") % (name, reason))
  2113. # One last point of house keeping. If we are deleting the instance
  2114. # during a resize operation we want to make sure the cinder volumes are
  2115. # properly cleaned up. We need to do this here, because the periodic
  2116. # task that comes along and cleans these things up isn't nice enough to
  2117. # pass a context in so that we could simply do the work there. But
  2118. # because we have access to a context, we can handle the work here and
  2119. # let the periodic task simply clean up the left over zone
  2120. # configuration that might be left around. Note that the left over
  2121. # zone will only show up in zoneadm list, not nova list.
  2122. #
  2123. # If the task state is RESIZE_REVERTING do not process these because
  2124. # the cinder volume cleanup is taken care of in
  2125. # finish_revert_migration.
  2126. if instance['task_state'] == task_states.RESIZE_REVERTING:
  2127. return
  2128. tags = ['old_instance_volid', 'new_instance_volid']
  2129. for tag in tags:
  2130. volid = instance.system_metadata.get(tag)
  2131. if volid:
  2132. try:
  2133. LOG.debug(_("Deleting volume %s"), volid)
  2134. self._volume_api.delete(context, volid)
  2135. del instance.system_metadata[tag]
  2136. except Exception:
  2137. pass
  2138. def cleanup(self, context, instance, network_info, block_device_info=None,
  2139. destroy_disks=True, migrate_data=None, destroy_vifs=True):
  2140. """Cleanup the instance resources .
  2141. Instance should have been destroyed from the Hypervisor before calling
  2142. this method.
  2143. :param context: security context
  2144. :param instance: Instance object as returned by DB layer.
  2145. :param network_info:
  2146. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  2147. :param block_device_info: Information about block devices that should
  2148. be detached from the instance.
  2149. :param destroy_disks: Indicates if disks should be destroyed
  2150. :param migrate_data: implementation specific params
  2151. """
  2152. raise NotImplementedError()
  2153. def reboot(self, context, instance, network_info, reboot_type,
  2154. block_device_info=None, bad_volumes_callback=None):
  2155. """Reboot the specified instance.
  2156. After this is called successfully, the instance's state
  2157. goes back to power_state.RUNNING. The virtualization
  2158. platform should ensure that the reboot action has completed
  2159. successfully even in cases in which the underlying domain/vm
  2160. is paused or halted/stopped.
  2161. :param instance: nova.objects.instance.Instance
  2162. :param network_info:
  2163. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  2164. :param reboot_type: Either a HARD or SOFT reboot
  2165. :param block_device_info: Info pertaining to attached volumes
  2166. :param bad_volumes_callback: Function to handle any bad volumes
  2167. encountered
  2168. """
  2169. name = instance['name']
  2170. zone = self._get_zone_by_name(name)
  2171. if zone is None:
  2172. raise exception.InstanceNotFound(instance_id=name)
  2173. if self._get_state(zone) == power_state.SHUTDOWN:
  2174. self._power_on(instance, network_info)
  2175. return
  2176. bootargs = []
  2177. if CONF.solariszones.solariszones_boot_options:
  2178. reset_bootargs = False
  2179. persistent = 'False'
  2180. # Get any bootargs already set in the zone
  2181. cur_bootargs = lookup_resource_property(zone, 'global', 'bootargs')
  2182. # Get any bootargs set in the instance metadata by the user
  2183. meta_bootargs = instance.metadata.get('bootargs')
  2184. if meta_bootargs:
  2185. bootargs = ['--', str(meta_bootargs)]
  2186. persistent = str(
  2187. instance.metadata.get('bootargs_persist', 'False'))
  2188. if cur_bootargs is not None and meta_bootargs != cur_bootargs:
  2189. with ZoneConfig(zone) as zc:
  2190. reset_bootargs = True
  2191. # Temporarily clear bootargs in zone config
  2192. zc.clear_resource_props('global', ['bootargs'])
  2193. try:
  2194. self._unplug_vifs(instance)
  2195. if reboot_type == 'SOFT':
  2196. bootargs.insert(0, '-r')
  2197. zone.shutdown(bootargs)
  2198. else:
  2199. zone.reboot(bootargs)
  2200. self._plug_vifs(instance, network_info)
  2201. except Exception as ex:
  2202. reason = zonemgr_strerror(ex)
  2203. LOG.exception(_("Unable to reboot instance '%s' via "
  2204. "zonemgr(3RAD): %s") % (name, reason))
  2205. raise exception.InstanceRebootFailure(reason=reason)
  2206. finally:
  2207. if CONF.solariszones.solariszones_boot_options:
  2208. if meta_bootargs and persistent.lower() == 'false':
  2209. # We have consumed the metadata bootargs and
  2210. # the user asked for them not to be persistent so
  2211. # clear them out now.
  2212. instance.metadata.pop('bootargs', None)
  2213. instance.metadata.pop('bootargs_persist', None)
  2214. if reset_bootargs:
  2215. with ZoneConfig(zone) as zc:
  2216. # restore original boot args in zone config
  2217. zc.setprop('global', 'bootargs', cur_bootargs)
  2218. def get_console_pool_info(self, console_type):
  2219. # TODO(Vek): Need to pass context in for access to auth_token
  2220. raise NotImplementedError()
  2221. def _get_console_output(self, instance):
  2222. """Builds a string containing the console output (capped at
  2223. MAX_CONSOLE_BYTES characters) by reassembling the log files
  2224. that Solaris Zones framework maintains for each zone.
  2225. """
  2226. console_str = ""
  2227. avail = MAX_CONSOLE_BYTES
  2228. # Examine the log files in most-recently modified order, keeping
  2229. # track of the size of each file and of how many characters have
  2230. # been seen. If there are still characters left to incorporate,
  2231. # then the contents of the log file in question are prepended to
  2232. # the console string built so far. When the number of characters
  2233. # available has run out, the last fragment under consideration
  2234. # will likely begin within the middle of a line. As such, the
  2235. # start of the fragment up to the next newline is thrown away.
  2236. # The remainder constitutes the start of the resulting console
  2237. # output which is then prepended to the console string built so
  2238. # far and the result returned.
  2239. logfile_pattern = '/var/log/zones/%s.console*' % instance['name']
  2240. logfiles = sorted(glob.glob(logfile_pattern), key=os.path.getmtime,
  2241. reverse=True)
  2242. for file in logfiles:
  2243. size = os.path.getsize(file)
  2244. if size == 0:
  2245. continue
  2246. avail -= size
  2247. with open(file, 'r') as log:
  2248. if avail < 0:
  2249. (fragment, _) = utils.last_bytes(log, avail + size)
  2250. remainder = fragment.find('\n') + 1
  2251. console_str = fragment[remainder:] + console_str
  2252. break
  2253. fragment = ''
  2254. for line in log.readlines():
  2255. fragment += line
  2256. console_str = fragment + console_str
  2257. return console_str
  2258. def get_console_output(self, context, instance):
  2259. """Get console output for an instance
  2260. :param context: security context
  2261. :param instance: nova.objects.instance.Instance
  2262. """
  2263. return self._get_console_output(instance)
  2264. def get_vnc_console(self, context, instance):
  2265. """Get connection info for a vnc console.
  2266. :param context: security context
  2267. :param instance: nova.objects.instance.Instance
  2268. :returns an instance of console.type.ConsoleVNC
  2269. """
  2270. # Do not provide console access prematurely. Zone console access is
  2271. # exclusive and zones that are still installing require their console.
  2272. # Grabbing the zone console will break installation.
  2273. name = instance['name']
  2274. if instance['vm_state'] == vm_states.BUILDING:
  2275. LOG.info(_("VNC console not available until zone '%s' has "
  2276. "completed installation. Try again later.") % name)
  2277. raise exception.InstanceNotReady(instance_id=instance['uuid'])
  2278. if not self._has_vnc_console_service(instance):
  2279. LOG.debug(_("Creating zone VNC console SMF service for "
  2280. "instance '%s'") % name)
  2281. self._create_vnc_console_service(instance)
  2282. self._enable_vnc_console_service(instance)
  2283. console_fmri = VNC_CONSOLE_BASE_FMRI + ':' + name
  2284. # The console service sets an SMF instance property for the port
  2285. # on which the VNC service is listening. The service needs to be
  2286. # refreshed to reflect the current property value
  2287. # TODO(npower): investigate using RAD instead of CLI invocation
  2288. try:
  2289. out, err = utils.execute('/usr/sbin/svccfg', '-s', console_fmri,
  2290. 'refresh')
  2291. except processutils.ProcessExecutionError as ex:
  2292. reason = ex.stderr
  2293. LOG.exception(_("Unable to refresh zone VNC console SMF service "
  2294. "'%s': %s" % (console_fmri, reason)))
  2295. raise
  2296. host = CONF.vnc.vncserver_proxyclient_address
  2297. try:
  2298. out, err = utils.execute('/usr/bin/svcprop', '-p', 'vnc/port',
  2299. console_fmri)
  2300. port = int(out.strip())
  2301. return ctype.ConsoleVNC(host=host, port=port,
  2302. internal_access_path=None)
  2303. except processutils.ProcessExecutionError as ex:
  2304. reason = ex.stderr
  2305. LOG.exception(_("Unable to read VNC console port from zone VNC "
  2306. "console SMF service '%s': %s"
  2307. % (console_fmri, reason)))
  2308. def get_spice_console(self, context, instance):
  2309. """Get connection info for a spice console.
  2310. :param context: security context
  2311. :param instance: nova.objects.instance.Instance
  2312. :returns an instance of console.type.ConsoleSpice
  2313. """
  2314. raise NotImplementedError()
  2315. def get_rdp_console(self, context, instance):
  2316. """Get connection info for a rdp console.
  2317. :param context: security context
  2318. :param instance: nova.objects.instance.Instance
  2319. :returns an instance of console.type.ConsoleRDP
  2320. """
  2321. raise NotImplementedError()
  2322. def get_serial_console(self, context, instance):
  2323. """Get connection info for a serial console.
  2324. :param context: security context
  2325. :param instance: nova.objects.instance.Instance
  2326. :returns an instance of console.type.ConsoleSerial
  2327. """
  2328. raise NotImplementedError()
  2329. def get_mks_console(self, context, instance):
  2330. """Get connection info for a MKS console.
  2331. :param context: security context
  2332. :param instance: nova.objects.instance.Instance
  2333. :returns an instance of console.type.ConsoleMKS
  2334. """
  2335. raise NotImplementedError()
  2336. def _get_zone_diagnostics(self, zone):
  2337. """Return data about Solaris Zone diagnostics."""
  2338. if zone.id == -1:
  2339. return None
  2340. diagnostics = defaultdict(lambda: 0)
  2341. for stat in ['lockedmem', 'nprocs', 'swapresv']:
  2342. uri = "kstat:/zone_caps/caps/%s_zone_%d/%d" % (stat, zone.id,
  2343. zone.id)
  2344. diagnostics[stat] = self._kstat_data(uri)['usage']
  2345. # Get the inital accumulated data kstat, then get the sys_zone kstat
  2346. # and sum all the "*_cur" statistics in it. Then re-get the accumulated
  2347. # kstat, and if the generation number hasn't changed, add its values.
  2348. # If it has changed, try again a few times then give up because
  2349. # something keeps pulling cpus out from under us.
  2350. accum_uri = "kstat:/zones/cpu/sys_zone_accum/%d" % zone.id
  2351. uri = "kstat:/zones/cpu/sys_zone_%d" % zone.id
  2352. for _attempt in range(3):
  2353. initial = self._kstat_data(accum_uri)
  2354. data = self._kstat_data(uri)
  2355. # The list of cpu kstats in data must contain at least one element
  2356. # and all elements have the same map of statistics, since they're
  2357. # all the same kstat type. This gets a list of all the statistics
  2358. # which end in "_cur" from the first (guaranteed) kstat element.
  2359. stats = [k for k in data[data.keys()[0]].getMap().keys() if
  2360. k.endswith("_cur")]
  2361. for stat in stats:
  2362. diagnostics[stat[:-4]] += self._sum_kstat_statistic(data, stat)
  2363. final = self._kstat_data(accum_uri)
  2364. if initial['gen_num'] == final['gen_num']:
  2365. for stat in stats:
  2366. # Remove the '_cur' from the statistic
  2367. diagnostics[stat[:-4]] += initial[stat[:-4]]
  2368. break
  2369. else:
  2370. reason = (_("Could not get diagnostic info for instance '%s' "
  2371. "because the cpu list keeps changing.") % zone.name)
  2372. raise nova.exception.MaxRetriesExceeded(reason)
  2373. # Remove any None valued elements from diagnostics and return it
  2374. return {k: v for k, v in diagnostics.items() if v is not None}
  2375. def get_diagnostics(self, instance):
  2376. """Return diagnostics data about the given instance.
  2377. :param nova.objects.instance.Instance instance:
  2378. The instance to which the diagnostic data should be returned.
  2379. :return: Has a big overlap to the return value of the newer interface
  2380. :func:`get_instance_diagnostics`
  2381. :rtype: dict
  2382. """
  2383. # TODO(Vek): Need to pass context in for access to auth_token
  2384. name = instance['name']
  2385. zone = self._get_zone_by_name(name)
  2386. if zone is None:
  2387. raise exception.InstanceNotFound(instance_id=name)
  2388. return self._get_zone_diagnostics(zone)
  2389. def get_instance_diagnostics(self, instance):
  2390. """Return diagnostics data about the given instance.
  2391. :param nova.objects.instance.Instance instance:
  2392. The instance to which the diagnostic data should be returned.
  2393. :return: Has a big overlap to the return value of the older interface
  2394. :func:`get_diagnostics`
  2395. :rtype: nova.virt.diagnostics.Diagnostics
  2396. """
  2397. raise NotImplementedError()
  2398. def get_all_bw_counters(self, instances):
  2399. """Return bandwidth usage counters for each interface on each
  2400. running VM.
  2401. :param instances: nova.objects.instance.InstanceList
  2402. """
  2403. raise NotImplementedError()
  2404. def get_all_volume_usage(self, context, compute_host_bdms):
  2405. """Return usage info for volumes attached to vms on
  2406. a given host.-
  2407. """
  2408. raise NotImplementedError()
  2409. def get_host_ip_addr(self):
  2410. """Retrieves the IP address of the dom0
  2411. """
  2412. # TODO(Vek): Need to pass context in for access to auth_token
  2413. return CONF.my_ip
  2414. def attach_volume(self, context, connection_info, instance, mountpoint,
  2415. disk_bus=None, device_type=None, encryption=None):
  2416. """Attach the disk to the instance at mountpoint using info."""
  2417. # TODO(npower): Apply mountpoint in a meaningful way to the zone
  2418. # For security reasons this is not permitted in a Solaris branded zone.
  2419. name = instance['name']
  2420. zone = self._get_zone_by_name(name)
  2421. if zone is None:
  2422. raise exception.InstanceNotFound(instance_id=name)
  2423. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2424. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2425. if brand != ZONE_BRAND_SOLARIS_KZ:
  2426. # Only Solaris kernel zones are currently supported.
  2427. reason = (_("'%s' branded zones are not currently supported")
  2428. % brand)
  2429. raise NotImplementedError(reason)
  2430. suri = self._suri_from_volume_info(connection_info)
  2431. resource_scope = [zonemgr.Property("storage", suri)]
  2432. if connection_info.get('serial') is not None:
  2433. volume = self._volume_api.get(context, connection_info['serial'])
  2434. if volume['bootable']:
  2435. resource_scope.append(zonemgr.Property("bootpri", "1"))
  2436. with ZoneConfig(zone) as zc:
  2437. zc.addresource("device", resource_scope)
  2438. # apply the configuration to the running zone
  2439. if zone.state == ZONE_STATE_RUNNING:
  2440. try:
  2441. zone.apply()
  2442. except Exception as ex:
  2443. reason = zonemgr_strerror(ex)
  2444. LOG.exception(_("Unable to attach '%s' to instance '%s' via "
  2445. "zonemgr(3RAD): %s") % (suri, name, reason))
  2446. with ZoneConfig(zone) as zc:
  2447. zc.removeresources("device", resource_scope)
  2448. raise
  2449. def detach_volume(self, connection_info, instance, mountpoint,
  2450. encryption=None):
  2451. """Detach the disk attached to the instance."""
  2452. name = instance['name']
  2453. zone = self._get_zone_by_name(name)
  2454. if zone is None:
  2455. raise exception.InstanceNotFound(instance_id=name)
  2456. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2457. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2458. if brand != ZONE_BRAND_SOLARIS_KZ:
  2459. # Only Solaris kernel zones are currently supported.
  2460. reason = (_("'%s' branded zones are not currently supported")
  2461. % brand)
  2462. raise NotImplementedError(reason)
  2463. suri = self._suri_from_volume_info(connection_info)
  2464. # Check if the specific property value exists before attempting removal
  2465. resource = lookup_resource_property_value(zone, "device", "storage",
  2466. suri)
  2467. if not resource:
  2468. LOG.warning(_("Storage resource '%s' is not attached to instance "
  2469. "'%s'") % (suri, name))
  2470. return
  2471. with ZoneConfig(zone) as zc:
  2472. zc.removeresources("device", [zonemgr.Property("storage", suri)])
  2473. # apply the configuration to the running zone
  2474. if zone.state == ZONE_STATE_RUNNING:
  2475. try:
  2476. zone.apply()
  2477. except:
  2478. LOG.exception(_("Unable to apply the detach of resource '%s' "
  2479. "to running instance '%s' because the "
  2480. "resource is most likely in use.")
  2481. % (suri, name))
  2482. # re-add the entry to the zone configuration so that the
  2483. # configuration will reflect what is in cinder before we raise
  2484. # the exception, therefore failing the detach and leaving the
  2485. # volume in-use.
  2486. needed_props = ["storage", "bootpri"]
  2487. props = filter(lambda prop: prop.name in needed_props,
  2488. resource.properties)
  2489. with ZoneConfig(zone) as zc:
  2490. zc.addresource("device", props)
  2491. raise
  2492. def swap_volume(self, old_connection_info, new_connection_info,
  2493. instance, mountpoint, resize_to):
  2494. """Replace the volume attached to the given `instance`.
  2495. :param dict old_connection_info:
  2496. The volume for this connection gets detached from the given
  2497. `instance`.
  2498. :param dict new_connection_info:
  2499. The volume for this connection gets attached to the given
  2500. 'instance'.
  2501. :param nova.objects.instance.Instance instance:
  2502. The instance whose volume gets replaced by another one.
  2503. :param str mountpoint:
  2504. The mountpoint in the instance where the volume for
  2505. `old_connection_info` is attached to.
  2506. :param int resize_to:
  2507. If the new volume is larger than the old volume, it gets resized
  2508. to the given size (in Gigabyte) of `resize_to`.
  2509. :return: None
  2510. """
  2511. raise NotImplementedError()
  2512. def attach_interface(self, instance, image_meta, vif):
  2513. """Use hotplug to add a network interface to a running instance.
  2514. The counter action to this is :func:`detach_interface`.
  2515. :param nova.objects.instance.Instance instance:
  2516. The instance which will get an additional network interface.
  2517. :param nova.objects.ImageMeta image_meta:
  2518. The metadata of the image of the instance.
  2519. :param nova.network.model.NetworkInfo vif:
  2520. The object which has the information about the interface to attach.
  2521. :raise nova.exception.NovaException: If the attach fails.
  2522. :return: None
  2523. """
  2524. name = instance['name']
  2525. zone = self._get_zone_by_name(name)
  2526. if zone is None:
  2527. raise exception.InstanceNotFound(instance_id=name)
  2528. ctxt = nova_context.get_admin_context()
  2529. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2530. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2531. anetname = self._set_ovs_info(ctxt, zone, brand, False, vif)
  2532. # apply the configuration if the vm is ACTIVE
  2533. if instance['vm_state'] == vm_states.ACTIVE:
  2534. try:
  2535. zone.apply()
  2536. except Exception as ex:
  2537. reason = zonemgr_strerror(ex)
  2538. msg = (_("Unable to attach interface to instance '%s' via "
  2539. "zonemgr(3RAD): %s") % (name, reason))
  2540. with ZoneConfig(zone) as zc:
  2541. prop_filter = [zonemgr.Property('mac-address',
  2542. vif['address'])]
  2543. zc.removeresources('anet', prop_filter)
  2544. raise nova.exception.NovaException(msg)
  2545. # add port to ovs bridge
  2546. anet = ''.join([name, '/', anetname])
  2547. self._ovs_add_port(instance, vif, anet)
  2548. def detach_interface(self, instance, vif):
  2549. """Use hotunplug to remove a network interface from a running instance.
  2550. The counter action to this is :func:`attach_interface`.
  2551. :param nova.objects.instance.Instance instance:
  2552. The instance which gets a network interface removed.
  2553. :param nova.network.model.NetworkInfo vif:
  2554. The object which has the information about the interface to detach.
  2555. :raise nova.exception.NovaException: If the detach fails.
  2556. :return: None
  2557. """
  2558. name = instance['name']
  2559. zone = self._get_zone_by_name(name)
  2560. if zone is None:
  2561. raise exception.InstanceNotFound(instance_id=name)
  2562. # Check if the specific property value exists before attempting removal
  2563. resource = lookup_resource_property_value(zone, 'anet',
  2564. 'mac-address',
  2565. vif['address'])
  2566. if not resource:
  2567. msg = (_("Interface with MAC address '%s' is not attached to "
  2568. "instance '%s'.") % (vif['address'], name))
  2569. raise nova.exception.NovaException(msg)
  2570. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2571. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2572. for prop in resource.properties:
  2573. if brand == ZONE_BRAND_SOLARIS and prop.name == 'linkname':
  2574. anetname = prop.value
  2575. break
  2576. elif brand != ZONE_BRAND_SOLARIS and prop.name == 'id':
  2577. anetname = 'net%s' % prop.value
  2578. break
  2579. with ZoneConfig(zone) as zc:
  2580. zc.removeresources('anet', [zonemgr.Property('mac-address',
  2581. vif['address'])])
  2582. # apply the configuration if the vm is ACTIVE
  2583. if instance['vm_state'] == vm_states.ACTIVE:
  2584. try:
  2585. zone.apply()
  2586. except:
  2587. msg = (_("Unable to detach interface '%s' from running "
  2588. "instance '%s' because the resource is most likely "
  2589. "in use.") % (anetname, name))
  2590. needed_props = ["lower-link", "configure-allowed-address",
  2591. "mac-address", "mtu"]
  2592. if brand == ZONE_BRAND_SOLARIS:
  2593. needed_props.append("linkname")
  2594. else:
  2595. needed_props.append("id")
  2596. props = filter(lambda prop: prop.name in needed_props,
  2597. resource.properties)
  2598. with ZoneConfig(zone) as zc:
  2599. zc.addresource('anet', props)
  2600. raise nova.exception.NovaException(msg)
  2601. # remove anet from OVS bridge
  2602. port = ''.join([name, '/', anetname])
  2603. self._ovs_delete_port(port)
  2604. def _cleanup_migrate_disk(self, context, instance, volume):
  2605. """Make a best effort at cleaning up the volume that was created to
  2606. hold the new root disk
  2607. :param context: the context for the migration/resize
  2608. :param instance: nova.objects.instance.Instance being migrated/resized
  2609. :param volume: new volume created by the call to cinder create
  2610. """
  2611. try:
  2612. self._volume_api.delete(context, volume['id'])
  2613. except Exception as err:
  2614. LOG.exception(_("Unable to cleanup the resized volume: %s" % err))
  2615. def migrate_disk_and_power_off(self, context, instance, dest,
  2616. flavor, network_info,
  2617. block_device_info=None,
  2618. timeout=0, retry_interval=0):
  2619. """Transfers the disk of a running instance in multiple phases, turning
  2620. off the instance before the end.
  2621. :param nova.objects.instance.Instance instance:
  2622. The instance whose disk should be migrated.
  2623. :param str dest:
  2624. The IP address of the destination host.
  2625. :param nova.objects.flavor.Flavor flavor:
  2626. The flavor of the instance whose disk get migrated.
  2627. :param nova.network.model.NetworkInfo network_info:
  2628. The network information of the given `instance`.
  2629. :param dict block_device_info:
  2630. Information about the block devices.
  2631. :param int timeout:
  2632. The time in seconds to wait for the guest OS to shutdown.
  2633. :param int retry_interval:
  2634. How often to signal guest while waiting for it to shutdown.
  2635. :return: A list of disk information dicts in JSON format.
  2636. :rtype: str
  2637. """
  2638. LOG.debug("Starting migrate_disk_and_power_off", instance=instance)
  2639. samehost = (dest == self.get_host_ip_addr())
  2640. if samehost:
  2641. instance.system_metadata['resize_samehost'] = samehost
  2642. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2643. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2644. if brand != ZONE_BRAND_SOLARIS_KZ and not samehost:
  2645. reason = (_("'%s' branded zones do not currently support resize "
  2646. "to a different host.") % brand)
  2647. raise exception.MigrationPreCheckError(reason=reason)
  2648. if brand != flavor['extra_specs'].get('zonecfg:brand'):
  2649. reason = (_("Unable to change brand of zone during resize."))
  2650. raise exception.MigrationPreCheckError(reason=reason)
  2651. orgb = instance['root_gb']
  2652. nrgb = flavor.root_gb
  2653. if orgb > nrgb:
  2654. msg = (_("Unable to resize to a smaller boot volume."))
  2655. raise exception.ResizeError(reason=msg)
  2656. self.power_off(instance, timeout, retry_interval)
  2657. disk_info = None
  2658. if nrgb > orgb or not samehost:
  2659. bmap = block_device_info.get('block_device_mapping')
  2660. rootmp = instance.root_device_name
  2661. for entry in bmap:
  2662. mountdev = entry['mount_device'].rpartition('/')[2]
  2663. if mountdev == rootmp:
  2664. root_ci = entry['connection_info']
  2665. break
  2666. else:
  2667. # If this is a non-global zone that is on the same host and is
  2668. # simply using a dataset, the disk size is purely an OpenStack
  2669. # quota. We can continue without doing any disk work.
  2670. if samehost and brand == ZONE_BRAND_SOLARIS:
  2671. return disk_info
  2672. else:
  2673. msg = (_("Cannot find an attached root device."))
  2674. raise exception.ResizeError(reason=msg)
  2675. if root_ci['driver_volume_type'] == 'iscsi':
  2676. volume_id = root_ci['data']['volume_id']
  2677. else:
  2678. volume_id = root_ci['serial']
  2679. if volume_id is None:
  2680. msg = (_("Cannot find an attached root device."))
  2681. raise exception.ResizeError(reason=msg)
  2682. vinfo = self._volume_api.get(context, volume_id)
  2683. newvolume = self._volume_api.create(
  2684. context, orgb, vinfo['display_name'] + '-resized',
  2685. vinfo['display_description'], source_volume=vinfo)
  2686. instance.system_metadata['old_instance_volid'] = volume_id
  2687. instance.system_metadata['new_instance_volid'] = newvolume['id']
  2688. # TODO(npower): Polling is what nova/compute/manager also does when
  2689. # creating a new volume, so we do likewise here.
  2690. while True:
  2691. volume = self._volume_api.get(context, newvolume['id'])
  2692. if volume['status'] != 'creating':
  2693. break
  2694. greenthread.sleep(1)
  2695. if nrgb > orgb:
  2696. try:
  2697. self._volume_api.extend(context, newvolume['id'], nrgb)
  2698. except Exception:
  2699. LOG.exception(_("Failed to extend the new volume"))
  2700. self._cleanup_migrate_disk(context, instance, newvolume)
  2701. raise
  2702. disk_info = newvolume
  2703. return disk_info
  2704. def snapshot(self, context, instance, image_id, update_task_state):
  2705. """Snapshots the specified instance.
  2706. :param context: security context
  2707. :param instance: nova.objects.instance.Instance
  2708. :param image_id: Reference to a pre-created image that will
  2709. hold the snapshot.
  2710. """
  2711. name = instance['name']
  2712. zone = self._get_zone_by_name(name)
  2713. if zone is None:
  2714. raise exception.InstanceNotFound(instance_id=name)
  2715. # look to see if the zone is a kernel zone and is powered off. If it
  2716. # is raise an exception before trying to archive it
  2717. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2718. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2719. if zone.state != ZONE_STATE_RUNNING and \
  2720. brand == ZONE_BRAND_SOLARIS_KZ:
  2721. raise exception.InstanceNotRunning(instance_id=name)
  2722. # Get original base image info
  2723. (base_service, base_id) = glance.get_remote_image_service(
  2724. context, instance['image_ref'])
  2725. try:
  2726. base = base_service.show(context, base_id)
  2727. except exception.ImageNotFound:
  2728. base = {}
  2729. snapshot_service, snapshot_id = glance.get_remote_image_service(
  2730. context, image_id)
  2731. # Build updated snapshot image metadata
  2732. snapshot = snapshot_service.show(context, snapshot_id)
  2733. metadata = {
  2734. 'is_public': False,
  2735. 'status': 'active',
  2736. 'name': snapshot['name'],
  2737. 'properties': {
  2738. 'image_location': 'snapshot',
  2739. 'image_state': 'available',
  2740. 'owner_id': instance['project_id'],
  2741. 'instance_uuid': instance['uuid'],
  2742. 'image_type': snapshot['properties']['image_type'],
  2743. }
  2744. }
  2745. # Match architecture, hypervisor_type and vm_mode properties to base
  2746. # image.
  2747. for prop in ['architecture', 'hypervisor_type', 'vm_mode']:
  2748. if prop in base.get('properties', {}):
  2749. base_prop = base['properties'][prop]
  2750. metadata['properties'][prop] = base_prop
  2751. # Set generic container and disk formats initially in case the glance
  2752. # service rejects Unified Archives (uar) and ZFS in metadata.
  2753. metadata['container_format'] = 'ovf'
  2754. metadata['disk_format'] = 'raw'
  2755. update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD)
  2756. snapshot_directory = CONF.solariszones.solariszones_snapshots_directory
  2757. fileutils.ensure_tree(snapshot_directory)
  2758. snapshot_name = uuid.uuid4().hex
  2759. with utils.tempdir(dir=snapshot_directory) as tmpdir:
  2760. out_path = os.path.join(tmpdir, snapshot_name)
  2761. zone_name = instance['name']
  2762. utils.execute('/usr/sbin/archiveadm', 'create', '--root-only',
  2763. '-z', zone_name, out_path)
  2764. LOG.info(_("Snapshot extracted, beginning image upload"),
  2765. instance=instance)
  2766. try:
  2767. # Upload the archive image to the image service
  2768. update_task_state(
  2769. task_state=task_states.IMAGE_UPLOADING,
  2770. expected_state=task_states.IMAGE_PENDING_UPLOAD)
  2771. with open(out_path, 'r') as image_file:
  2772. snapshot_service.update(context, image_id, metadata,
  2773. image_file)
  2774. LOG.info(_("Snapshot image upload complete"),
  2775. instance=instance)
  2776. try:
  2777. # Try to update the image metadata container and disk
  2778. # formats more suitably for a unified archive if the
  2779. # glance server recognises them.
  2780. metadata['container_format'] = 'uar'
  2781. metadata['disk_format'] = 'zfs'
  2782. snapshot_service.update(context, image_id, metadata, None)
  2783. except exception.Invalid:
  2784. LOG.warning(_("Image service rejected image metadata "
  2785. "container and disk formats 'uar' and "
  2786. "'zfs'. Using generic values 'ovf' and "
  2787. "'raw' as fallbacks."))
  2788. finally:
  2789. # Delete the snapshot image file source
  2790. os.unlink(out_path)
  2791. def post_interrupted_snapshot_cleanup(self, context, instance):
  2792. """Cleans up any resources left after an interrupted snapshot.
  2793. :param context: security context
  2794. :param instance: nova.objects.instance.Instance
  2795. """
  2796. pass
  2797. def _cleanup_finish_migration(self, context, instance, disk_info,
  2798. network_info, samehost):
  2799. """Best effort attempt at cleaning up any additional resources that are
  2800. not directly managed by Nova or Cinder so as not to leak these
  2801. resources.
  2802. """
  2803. if disk_info:
  2804. self._volume_api.detach(context, disk_info['id'])
  2805. self._volume_api.delete(context, disk_info['id'])
  2806. old_rvid = instance.system_metadata.get('old_instance_volid')
  2807. if old_rvid:
  2808. connector = self.get_volume_connector(instance)
  2809. connection_info = self._volume_api.initialize_connection(
  2810. context, old_rvid, connector)
  2811. new_rvid = instance.system_metadata['new_instance_volid']
  2812. rootmp = instance.root_device_name
  2813. self._volume_api.attach(context, old_rvid, instance['uuid'],
  2814. rootmp)
  2815. bdmobj = objects.BlockDeviceMapping()
  2816. bdm = bdmobj.get_by_volume_id(context, new_rvid)
  2817. bdm['connection_info'] = jsonutils.dumps(connection_info)
  2818. bdm['volume_id'] = old_rvid
  2819. bdm.save()
  2820. del instance.system_metadata['new_instance_volid']
  2821. del instance.system_metadata['old_instance_volid']
  2822. if not samehost:
  2823. self.destroy(context, instance, network_info)
  2824. instance['host'] = instance['launched_on']
  2825. instance['node'] = instance['launched_on']
  2826. def finish_migration(self, context, migration, instance, disk_info,
  2827. network_info, image_meta, resize_instance,
  2828. block_device_info=None, power_on=True):
  2829. """Completes a resize/migration.
  2830. :param context: the context for the migration/resize
  2831. :param migration: the migrate/resize information
  2832. :param instance: nova.objects.instance.Instance being migrated/resized
  2833. :param disk_info: the newly transferred disk information
  2834. :param network_info:
  2835. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  2836. :param nova.objects.ImageMeta image_meta:
  2837. The metadata of the image of the instance.
  2838. :param resize_instance: True if the instance is being resized,
  2839. False otherwise
  2840. :param block_device_info: instance volume block device info
  2841. :param power_on: True if the instance should be powered on, False
  2842. otherwise
  2843. """
  2844. samehost = (migration['dest_node'] == migration['source_node'])
  2845. if samehost:
  2846. instance.system_metadata['old_vm_state'] = vm_states.RESIZED
  2847. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  2848. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  2849. name = instance['name']
  2850. if disk_info:
  2851. bmap = block_device_info.get('block_device_mapping')
  2852. rootmp = instance['root_device_name']
  2853. for entry in bmap:
  2854. if entry['mount_device'] == rootmp:
  2855. mount_dev = entry['mount_device']
  2856. root_ci = entry['connection_info']
  2857. break
  2858. try:
  2859. if samehost:
  2860. cpu = instance.vcpus
  2861. mem = instance.memory_mb
  2862. self._set_num_cpu(name, cpu, brand)
  2863. self._set_memory_cap(name, mem, brand)
  2864. # Add the new disk to the volume if the size of the disk
  2865. # changed
  2866. if disk_info:
  2867. rgb = instance.root_gb
  2868. self._resize_disk_migration(context, instance,
  2869. root_ci['serial'],
  2870. disk_info['id'], rgb,
  2871. mount_dev)
  2872. else:
  2873. # No need to check disk_info here, because when not on the
  2874. # same host a disk_info is always passed in.
  2875. mount_dev = 'c1d0'
  2876. root_serial = root_ci['serial']
  2877. connection_info = self._resize_disk_migration(context,
  2878. instance,
  2879. root_serial,
  2880. disk_info['id'],
  2881. 0, mount_dev,
  2882. samehost)
  2883. self._create_config(context, instance, network_info,
  2884. connection_info, None)
  2885. zone = self._get_zone_by_name(name)
  2886. if zone is None:
  2887. raise exception.InstanceNotFound(instance_id=name)
  2888. zone.attach(['-x', 'initialize-hostdata'])
  2889. bmap = block_device_info.get('block_device_mapping')
  2890. for entry in bmap:
  2891. if entry['mount_device'] != rootmp:
  2892. self.attach_volume(context, entry['connection_info'],
  2893. instance, entry['mount_device'])
  2894. if power_on:
  2895. self._power_on(instance, network_info)
  2896. if brand == ZONE_BRAND_SOLARIS:
  2897. return
  2898. # Toggle the autoexpand to extend the size of the rpool.
  2899. # We need to sleep for a few seconds to make sure the zone
  2900. # is in a state to accept the toggle. Once bugs are fixed
  2901. # around the autoexpand and the toggle is no longer needed
  2902. # or zone.boot() returns only after the zone is ready we
  2903. # can remove this hack.
  2904. greenthread.sleep(15)
  2905. out, err = utils.execute('/usr/sbin/zlogin', '-S', name,
  2906. '/usr/sbin/zpool', 'set',
  2907. 'autoexpand=off', 'rpool')
  2908. out, err = utils.execute('/usr/sbin/zlogin', '-S', name,
  2909. '/usr/sbin/zpool', 'set',
  2910. 'autoexpand=on', 'rpool')
  2911. except Exception:
  2912. # Attempt to cleanup the new zone and new volume to at least
  2913. # give the user a chance to recover without too many hoops
  2914. self._cleanup_finish_migration(context, instance, disk_info,
  2915. network_info, samehost)
  2916. raise
  2917. def confirm_migration(self, context, migration, instance, network_info):
  2918. """Confirms a resize/migration, destroying the source VM.
  2919. :param instance: nova.objects.instance.Instance
  2920. """
  2921. samehost = (migration['dest_host'] == self.get_host_ip_addr())
  2922. old_rvid = instance.system_metadata.get('old_instance_volid')
  2923. new_rvid = instance.system_metadata.get('new_instance_volid')
  2924. if new_rvid and old_rvid:
  2925. new_vname = instance['display_name'] + "-" + self._rootzpool_suffix
  2926. del instance.system_metadata['old_instance_volid']
  2927. del instance.system_metadata['new_instance_volid']
  2928. self._volume_api.delete(context, old_rvid)
  2929. self._volume_api.update(context, new_rvid,
  2930. {'display_name': new_vname})
  2931. if not samehost:
  2932. self.destroy(context, instance, network_info)
  2933. else:
  2934. del instance.system_metadata['resize_samehost']
  2935. def _resize_disk_migration(self, context, instance, configured,
  2936. replacement, newvolumesz, mountdev,
  2937. samehost=True):
  2938. """Handles the zone root volume switch-over or simply
  2939. initializing the connection for the new zone if not resizing to the
  2940. same host
  2941. :param context: the context for the _resize_disk_migration
  2942. :param instance: nova.objects.instance.Instance being resized
  2943. :param configured: id of the current configured volume
  2944. :param replacement: id of the new volume
  2945. :param newvolumesz: size of the new volume
  2946. :param mountdev: the mount point of the device
  2947. :param samehost: is the resize happening on the same host
  2948. """
  2949. connector = self.get_volume_connector(instance)
  2950. connection_info = self._volume_api.initialize_connection(context,
  2951. replacement,
  2952. connector)
  2953. connection_info['serial'] = replacement
  2954. rootmp = instance.root_device_name
  2955. if samehost:
  2956. name = instance['name']
  2957. zone = self._get_zone_by_name(name)
  2958. if zone is None:
  2959. raise exception.InstanceNotFound(instance_id=name)
  2960. # Need to detach the zone and re-attach the zone if this is a
  2961. # non-global zone so that the update of the rootzpool resource does
  2962. # not fail.
  2963. if zone.brand == ZONE_BRAND_SOLARIS:
  2964. zone.detach()
  2965. try:
  2966. self._set_boot_device(name, connection_info, zone.brand)
  2967. finally:
  2968. if zone.brand == ZONE_BRAND_SOLARIS:
  2969. zone.attach()
  2970. try:
  2971. self._volume_api.detach(context, configured)
  2972. except Exception:
  2973. LOG.exception(_("Failed to detach the volume"))
  2974. raise
  2975. try:
  2976. self._volume_api.attach(context, replacement, instance['uuid'],
  2977. rootmp)
  2978. except Exception:
  2979. LOG.exception(_("Failed to attach the volume"))
  2980. raise
  2981. bdmobj = objects.BlockDeviceMapping()
  2982. bdm = bdmobj.get_by_volume_id(context, configured)
  2983. bdm['connection_info'] = jsonutils.dumps(connection_info)
  2984. bdm['volume_id'] = replacement
  2985. bdm.save()
  2986. if not samehost:
  2987. return connection_info
  2988. def finish_revert_migration(self, context, instance, network_info,
  2989. block_device_info=None, power_on=True):
  2990. """Finish reverting a resize/migration.
  2991. :param context: the context for the finish_revert_migration
  2992. :param instance: nova.objects.instance.Instance being migrated/resized
  2993. :param network_info:
  2994. :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
  2995. :param block_device_info: instance volume block device info
  2996. :param power_on: True if the instance should be powered on, False
  2997. otherwise
  2998. """
  2999. # If this is not a samehost migration then we need to re-attach the
  3000. # original volume to the instance. Otherwise we need to update the
  3001. # original zone configuration.
  3002. samehost = instance.system_metadata.get('resize_samehost')
  3003. if samehost:
  3004. self._samehost_revert_resize(context, instance, network_info,
  3005. block_device_info)
  3006. del instance.system_metadata['resize_samehost']
  3007. old_rvid = instance.system_metadata.get('old_instance_volid')
  3008. if old_rvid:
  3009. connector = self.get_volume_connector(instance)
  3010. connection_info = self._volume_api.initialize_connection(context,
  3011. old_rvid,
  3012. connector)
  3013. new_rvid = instance.system_metadata['new_instance_volid']
  3014. self._volume_api.detach(context, new_rvid)
  3015. self._volume_api.delete(context, new_rvid)
  3016. rootmp = instance.root_device_name
  3017. self._volume_api.attach(context, old_rvid, instance['uuid'],
  3018. rootmp)
  3019. bdmobj = objects.BlockDeviceMapping()
  3020. bdm = bdmobj.get_by_volume_id(context, new_rvid)
  3021. bdm['connection_info'] = jsonutils.dumps(connection_info)
  3022. bdm['volume_id'] = old_rvid
  3023. bdm.save()
  3024. del instance.system_metadata['new_instance_volid']
  3025. del instance.system_metadata['old_instance_volid']
  3026. else:
  3027. new_rvid = instance.system_metadata.get('new_instance_volid')
  3028. if new_rvid:
  3029. del instance.system_metadata['new_instance_volid']
  3030. self._volume_api.delete(context, new_rvid)
  3031. self._power_on(instance, network_info)
  3032. def pause(self, instance):
  3033. """Pause the given instance.
  3034. A paused instance doesn't use CPU cycles of the host anymore. The
  3035. state of the VM could be stored in the memory or storage space of the
  3036. host, depending on the underlying hypervisor technology.
  3037. A "stronger" version of `pause` is :func:'suspend'.
  3038. The counter action for `pause` is :func:`unpause`.
  3039. :param nova.objects.instance.Instance instance:
  3040. The instance which should be paused.
  3041. :return: None
  3042. """
  3043. # TODO(Vek): Need to pass context in for access to auth_token
  3044. raise NotImplementedError()
  3045. def unpause(self, instance):
  3046. """Unpause the given paused instance.
  3047. The paused instance gets unpaused and will use CPU cycles of the
  3048. host again. The counter action for 'unpause' is :func:`pause`.
  3049. Depending on the underlying hypervisor technology, the guest has the
  3050. same state as before the 'pause'.
  3051. :param nova.objects.instance.Instance instance:
  3052. The instance which should be unpaused.
  3053. :return: None
  3054. """
  3055. # TODO(Vek): Need to pass context in for access to auth_token
  3056. raise NotImplementedError()
  3057. def suspend(self, context, instance):
  3058. """Suspend the specified instance.
  3059. A suspended instance doesn't use CPU cycles or memory of the host
  3060. anymore. The state of the instance could be persisted on the host
  3061. and allocate storage space this way. A "softer" way of `suspend`
  3062. is :func:`pause`. The counter action for `suspend` is :func:`resume`.
  3063. :param nova.context.RequestContext context:
  3064. The context for the suspend.
  3065. :param nova.objects.instance.Instance instance:
  3066. The instance to suspend.
  3067. :return: None
  3068. """
  3069. name = instance['name']
  3070. zone = self._get_zone_by_name(name)
  3071. if zone is None:
  3072. raise exception.InstanceNotFound(instance_id=name)
  3073. if zone.brand != ZONE_BRAND_SOLARIS_KZ:
  3074. # Only Solaris kernel zones are currently supported.
  3075. reason = (_("'%s' branded zones do not currently support "
  3076. "suspend. Use 'nova reset-state --active %s' "
  3077. "to reset instance state back to 'active'.")
  3078. % (zone.brand, instance['display_name']))
  3079. raise exception.InstanceSuspendFailure(reason=reason)
  3080. if self._get_state(zone) != power_state.RUNNING:
  3081. reason = (_("Instance '%s' is not running.") % name)
  3082. raise exception.InstanceSuspendFailure(reason=reason)
  3083. try:
  3084. new_path = os.path.join(CONF.solariszones.zones_suspend_path,
  3085. '%{zonename}')
  3086. if not lookup_resource(zone, 'suspend'):
  3087. # add suspend if not configured
  3088. self._set_suspend(instance)
  3089. elif lookup_resource_property(zone, 'suspend', 'path') != new_path:
  3090. # replace the old suspend resource with the new one
  3091. with ZoneConfig(zone) as zc:
  3092. zc.removeresources('suspend')
  3093. self._set_suspend(instance)
  3094. zone.suspend()
  3095. self._unplug_vifs(instance)
  3096. except Exception as ex:
  3097. reason = zonemgr_strerror(ex)
  3098. LOG.exception(_("Unable to suspend instance '%s' via "
  3099. "zonemgr(3RAD): %s") % (name, reason))
  3100. raise exception.InstanceSuspendFailure(reason=reason)
  3101. def resume(self, context, instance, network_info, block_device_info=None):
  3102. """resume the specified suspended instance.
  3103. The suspended instance gets resumed and will use CPU cycles and memory
  3104. of the host again. The counter action for 'resume' is :func:`suspend`.
  3105. Depending on the underlying hypervisor technology, the guest has the
  3106. same state as before the 'suspend'.
  3107. :param nova.context.RequestContext context:
  3108. The context for the resume.
  3109. :param nova.objects.instance.Instance instance:
  3110. The suspended instance to resume.
  3111. :param nova.network.model.NetworkInfo network_info:
  3112. Necessary network information for the resume.
  3113. :param dict block_device_info:
  3114. Instance volume block device info.
  3115. :return: None
  3116. """
  3117. name = instance['name']
  3118. zone = self._get_zone_by_name(name)
  3119. if zone is None:
  3120. raise exception.InstanceNotFound(instance_id=name)
  3121. if zone.brand != ZONE_BRAND_SOLARIS_KZ:
  3122. # Only Solaris kernel zones are currently supported.
  3123. reason = (_("'%s' branded zones do not currently support "
  3124. "resume.") % zone.brand)
  3125. raise exception.InstanceResumeFailure(reason=reason)
  3126. # check that the instance is suspended
  3127. if self._get_state(zone) != power_state.SHUTDOWN:
  3128. reason = (_("Instance '%s' is not suspended.") % name)
  3129. raise exception.InstanceResumeFailure(reason=reason)
  3130. try:
  3131. zone.boot()
  3132. self._plug_vifs(instance, network_info)
  3133. except Exception as ex:
  3134. reason = zonemgr_strerror(ex)
  3135. LOG.exception(_("Unable to resume instance '%s' via "
  3136. "zonemgr(3RAD): %s") % (name, reason))
  3137. raise exception.InstanceResumeFailure(reason=reason)
  3138. def resume_state_on_host_boot(self, context, instance, network_info,
  3139. block_device_info=None):
  3140. """resume guest state when a host is booted.
  3141. :param instance: nova.objects.instance.Instance
  3142. """
  3143. name = instance['name']
  3144. zone = self._get_zone_by_name(name)
  3145. if zone is None:
  3146. raise exception.InstanceNotFound(instance_id=name)
  3147. # TODO(dcomay): Should reconcile with value of zone's autoboot
  3148. # property.
  3149. if self._get_state(zone) not in (power_state.CRASHED,
  3150. power_state.SHUTDOWN):
  3151. return
  3152. self._power_on(instance, network_info)
  3153. def rescue(self, context, instance, network_info, image_meta,
  3154. rescue_password):
  3155. """Rescue the specified instance.
  3156. :param nova.context.RequestContext context:
  3157. The context for the rescue.
  3158. :param nova.objects.instance.Instance instance:
  3159. The instance being rescued.
  3160. :param nova.network.model.NetworkInfo network_info:
  3161. Necessary network information for the resume.
  3162. :param nova.objects.ImageMeta image_meta:
  3163. The metadata of the image of the instance.
  3164. :param rescue_password: new root password to set for rescue.
  3165. """
  3166. raise NotImplementedError()
  3167. def set_bootable(self, instance, is_bootable):
  3168. """Set the ability to power on/off an instance.
  3169. :param instance: nova.objects.instance.Instance
  3170. """
  3171. raise NotImplementedError()
  3172. def unrescue(self, instance, network_info):
  3173. """Unrescue the specified instance.
  3174. :param instance: nova.objects.instance.Instance
  3175. """
  3176. # TODO(Vek): Need to pass context in for access to auth_token
  3177. raise NotImplementedError()
  3178. def power_off(self, instance, timeout=0, retry_interval=0):
  3179. """Power off the specified instance.
  3180. :param instance: nova.objects.instance.Instance
  3181. :param timeout: time to wait for GuestOS to shutdown
  3182. :param retry_interval: How often to signal guest while
  3183. waiting for it to shutdown
  3184. """
  3185. self._power_off(instance, 'SOFT')
  3186. def power_on(self, context, instance, network_info,
  3187. block_device_info=None):
  3188. """Power on the specified instance.
  3189. :param instance: nova.objects.instance.Instance
  3190. """
  3191. self._power_on(instance, network_info)
  3192. def trigger_crash_dump(self, instance):
  3193. """Trigger crash dump mechanism on the given instance.
  3194. Stalling instances can be triggered to dump the crash data. How the
  3195. guest OS reacts in details, depends on the configuration of it.
  3196. :param nova.objects.instance.Instance instance:
  3197. The instance where the crash dump should be triggered.
  3198. :return: None
  3199. """
  3200. raise NotImplementedError()
  3201. def soft_delete(self, instance):
  3202. """Soft delete the specified instance.
  3203. A soft-deleted instance doesn't allocate any resources anymore, but is
  3204. still available as a database entry. The counter action :func:`restore`
  3205. uses the database entry to create a new instance based on that.
  3206. :param nova.objects.instance.Instance instance:
  3207. The instance to soft-delete.
  3208. :return: None
  3209. """
  3210. raise NotImplementedError()
  3211. def restore(self, instance):
  3212. """Restore the specified soft-deleted instance.
  3213. The restored instance will be automatically booted. The counter action
  3214. for `restore` is :func:`soft_delete`.
  3215. :param nova.objects.instance.Instance instance:
  3216. The soft-deleted instance which should be restored from the
  3217. soft-deleted data.
  3218. :return: None
  3219. """
  3220. raise NotImplementedError()
  3221. def _get_zpool_property(self, prop, zpool):
  3222. """Get the value of property from the zpool."""
  3223. try:
  3224. value = None
  3225. (out, _err) = utils.execute('/usr/sbin/zpool', 'get', prop, zpool)
  3226. except processutils.ProcessExecutionError as ex:
  3227. reason = ex.stderr
  3228. LOG.exception(_("Failed to get property '%s' from zpool '%s': %s")
  3229. % (prop, zpool, reason))
  3230. return value
  3231. zpool_prop = out.splitlines()[1].split()
  3232. if zpool_prop[1] == prop:
  3233. value = zpool_prop[2]
  3234. return value
  3235. def _update_host_stats(self):
  3236. """Update currently known host stats."""
  3237. host_stats = {}
  3238. host_stats['vcpus'] = os.sysconf('SC_NPROCESSORS_ONLN')
  3239. pages = os.sysconf('SC_PHYS_PAGES')
  3240. host_stats['memory_mb'] = self._pages_to_kb(pages) / 1024
  3241. out, err = utils.execute('/usr/sbin/zfs', 'list', '-Ho', 'name', '/')
  3242. root_zpool = out.split('/')[0]
  3243. size = self._get_zpool_property('size', root_zpool)
  3244. if size is not None:
  3245. host_stats['local_gb'] = Size(size).get(Size.gb_units)
  3246. else:
  3247. host_stats['local_gb'] = 0
  3248. # Account for any existing processor sets by looking at the the number
  3249. # of CPUs not assigned to any processor sets.
  3250. uri = "kstat:/misc/unix/pset/0"
  3251. data = self._kstat_data(uri)
  3252. if data is not None:
  3253. host_stats['vcpus_used'] = host_stats['vcpus'] - data['ncpus']
  3254. else:
  3255. host_stats['vcpus_used'] = 0
  3256. # Subtract the number of free pages from the total to get the used.
  3257. uri = "kstat:/pages/unix/system_pages"
  3258. data = self._kstat_data(uri)
  3259. if data is not None:
  3260. free_ram = data['freemem']
  3261. free_ram = self._pages_to_kb(free_ram) / 1024
  3262. host_stats['memory_mb_used'] = host_stats['memory_mb'] - free_ram
  3263. else:
  3264. host_stats['memory_mb_used'] = 0
  3265. free = self._get_zpool_property('free', root_zpool)
  3266. if free is not None:
  3267. free_disk_gb = Size(free).get(Size.gb_units)
  3268. else:
  3269. free_disk_gb = 0
  3270. host_stats['local_gb_used'] = host_stats['local_gb'] - free_disk_gb
  3271. host_stats['hypervisor_type'] = 'solariszones'
  3272. host_stats['hypervisor_version'] = \
  3273. versionutils.convert_version_to_int(HYPERVISOR_VERSION)
  3274. host_stats['hypervisor_hostname'] = self._uname[1]
  3275. if self._uname[4] == 'i86pc':
  3276. architecture = arch.X86_64
  3277. else:
  3278. architecture = arch.SPARC64
  3279. cpu_info = {
  3280. 'arch': architecture
  3281. }
  3282. host_stats['cpu_info'] = jsonutils.dumps(cpu_info)
  3283. host_stats['disk_available_least'] = free_disk_gb
  3284. host_stats['supported_instances'] = [
  3285. (architecture, hv_type.SOLARISZONES, vm_mode.SOL)
  3286. ]
  3287. host_stats['numa_topology'] = None
  3288. self._host_stats = host_stats
  3289. def get_available_resource(self, nodename):
  3290. """Retrieve resource information.
  3291. This method is called when nova-compute launches, and
  3292. as part of a periodic task that records the results in the DB.
  3293. :param nodename:
  3294. node which the caller want to get resources from
  3295. a driver that manages only one node can safely ignore this
  3296. :returns: Dictionary describing resources
  3297. """
  3298. self._update_host_stats()
  3299. host_stats = self._host_stats
  3300. resources = {}
  3301. resources['vcpus'] = host_stats['vcpus']
  3302. resources['memory_mb'] = host_stats['memory_mb']
  3303. resources['local_gb'] = host_stats['local_gb']
  3304. resources['vcpus_used'] = host_stats['vcpus_used']
  3305. resources['memory_mb_used'] = host_stats['memory_mb_used']
  3306. resources['local_gb_used'] = host_stats['local_gb_used']
  3307. resources['hypervisor_type'] = host_stats['hypervisor_type']
  3308. resources['hypervisor_version'] = host_stats['hypervisor_version']
  3309. resources['hypervisor_hostname'] = host_stats['hypervisor_hostname']
  3310. resources['cpu_info'] = host_stats['cpu_info']
  3311. resources['disk_available_least'] = host_stats['disk_available_least']
  3312. resources['supported_instances'] = host_stats['supported_instances']
  3313. resources['numa_topology'] = host_stats['numa_topology']
  3314. return resources
  3315. def pre_live_migration(self, context, instance, block_device_info,
  3316. network_info, disk_info, migrate_data=None):
  3317. """Prepare an instance for live migration
  3318. :param context: security context
  3319. :param instance: nova.objects.instance.Instance object
  3320. :param block_device_info: instance block device information
  3321. :param network_info: instance network information
  3322. :param disk_info: instance disk information
  3323. :param migrate_data: a LiveMigrateData object
  3324. """
  3325. return migrate_data
  3326. def _live_migration(self, name, dest, dry_run=False):
  3327. """Live migration of a Solaris kernel zone to another host."""
  3328. zone = self._get_zone_by_name(name)
  3329. if zone is None:
  3330. raise exception.InstanceNotFound(instance_id=name)
  3331. options = []
  3332. live_migration_cipher = CONF.solariszones.live_migration_cipher
  3333. if live_migration_cipher is not None:
  3334. options.extend(['-c', live_migration_cipher])
  3335. if dry_run:
  3336. options.append('-nq')
  3337. options.append('ssh://nova@' + dest)
  3338. zone.migrate(options)
  3339. def live_migration(self, context, instance, dest,
  3340. post_method, recover_method, block_migration=False,
  3341. migrate_data=None):
  3342. """Live migration of an instance to another host.
  3343. :param context: security context
  3344. :param instance:
  3345. nova.db.sqlalchemy.models.Instance object
  3346. instance object that is migrated.
  3347. :param dest: destination host
  3348. :param post_method:
  3349. post operation method.
  3350. expected nova.compute.manager._post_live_migration.
  3351. :param recover_method:
  3352. recovery method when any exception occurs.
  3353. expected nova.compute.manager._rollback_live_migration.
  3354. :param block_migration: if true, migrate VM disk.
  3355. :param migrate_data: a LiveMigrateData object
  3356. """
  3357. name = instance['name']
  3358. try:
  3359. self._live_migration(name, dest, dry_run=False)
  3360. except Exception as ex:
  3361. with excutils.save_and_reraise_exception():
  3362. reason = zonemgr_strerror(ex)
  3363. LOG.exception(_("Unable to live migrate instance '%s' to host "
  3364. "'%s' via zonemgr(3RAD): %s")
  3365. % (name, dest, reason))
  3366. recover_method(context, instance, dest, block_migration)
  3367. post_method(context, instance, dest, block_migration, migrate_data)
  3368. def live_migration_force_complete(self, instance):
  3369. """Force live migration to complete
  3370. :param instance: Instance being live migrated
  3371. """
  3372. raise NotImplementedError()
  3373. def live_migration_abort(self, instance):
  3374. """Abort an in-progress live migration.
  3375. :param instance: instance that is live migrating
  3376. """
  3377. raise NotImplementedError()
  3378. def rollback_live_migration_at_destination(self, context, instance,
  3379. network_info,
  3380. block_device_info,
  3381. destroy_disks=True,
  3382. migrate_data=None):
  3383. """Clean up destination node after a failed live migration.
  3384. :param context: security context
  3385. :param instance: instance object that was being migrated
  3386. :param network_info: instance network information
  3387. :param block_device_info: instance block device information
  3388. :param destroy_disks:
  3389. if true, destroy disks at destination during cleanup
  3390. :param migrate_data: a LiveMigrateData object
  3391. """
  3392. pass
  3393. def post_live_migration(self, context, instance, block_device_info,
  3394. migrate_data=None):
  3395. """Post operation of live migration at source host.
  3396. :param context: security context
  3397. :instance: instance object that was migrated
  3398. :block_device_info: instance block device information
  3399. :param migrate_data: a LiveMigrateData object
  3400. """
  3401. try:
  3402. # These methods log if problems occur so no need to double log
  3403. # here. Just catch any stray exceptions and allow destroy to
  3404. # proceed.
  3405. if self._has_vnc_console_service(instance):
  3406. self._disable_vnc_console_service(instance)
  3407. self._delete_vnc_console_service(instance)
  3408. except Exception:
  3409. pass
  3410. name = instance['name']
  3411. zone = self._get_zone_by_name(name)
  3412. # If instance cannot be found, just return.
  3413. if zone is None:
  3414. LOG.warning(_("Unable to find instance '%s' via zonemgr(3RAD)")
  3415. % name)
  3416. return
  3417. try:
  3418. self._delete_config(instance)
  3419. except Exception as ex:
  3420. reason = zonemgr_strerror(ex)
  3421. LOG.exception(_("Unable to delete configuration for instance '%s' "
  3422. "via zonemgr(3RAD): %s") % (name, reason))
  3423. raise
  3424. def post_live_migration_at_source(self, context, instance, network_info):
  3425. """Unplug VIFs from networks at source.
  3426. :param context: security context
  3427. :param instance: instance object reference
  3428. :param network_info: instance network information
  3429. """
  3430. self._unplug_vifs(instance)
  3431. def post_live_migration_at_destination(self, context, instance,
  3432. network_info,
  3433. block_migration=False,
  3434. block_device_info=None):
  3435. """Post operation of live migration at destination host.
  3436. :param context: security context
  3437. :param instance: instance object that is migrated
  3438. :param network_info: instance network information
  3439. :param block_migration: if true, post operation of block_migration.
  3440. """
  3441. self._plug_vifs(instance, network_info)
  3442. def check_instance_shared_storage_local(self, context, instance):
  3443. """Check if instance files located on shared storage.
  3444. This runs check on the destination host, and then calls
  3445. back to the source host to check the results.
  3446. :param context: security context
  3447. :param instance: nova.objects.instance.Instance object
  3448. """
  3449. raise NotImplementedError()
  3450. def check_instance_shared_storage_remote(self, context, data):
  3451. """Check if instance files located on shared storage.
  3452. :param context: security context
  3453. :param data: result of check_instance_shared_storage_local
  3454. """
  3455. raise NotImplementedError()
  3456. def check_instance_shared_storage_cleanup(self, context, data):
  3457. """Do cleanup on host after check_instance_shared_storage calls
  3458. :param context: security context
  3459. :param data: result of check_instance_shared_storage_local
  3460. """
  3461. pass
  3462. def check_can_live_migrate_destination(self, context, instance,
  3463. src_compute_info, dst_compute_info,
  3464. block_migration=False,
  3465. disk_over_commit=False):
  3466. """Check if it is possible to execute live migration.
  3467. This runs checks on the destination host, and then calls
  3468. back to the source host to check the results.
  3469. :param context: security context
  3470. :param instance: nova.db.sqlalchemy.models.Instance
  3471. :param src_compute_info: Info about the sending machine
  3472. :param dst_compute_info: Info about the receiving machine
  3473. :param block_migration: if true, prepare for block migration
  3474. :param disk_over_commit: if true, allow disk over commit
  3475. :returns: a LiveMigrateData object (hypervisor-dependent)
  3476. """
  3477. src_cpu_info = jsonutils.loads(src_compute_info['cpu_info'])
  3478. src_cpu_arch = src_cpu_info['arch']
  3479. dst_cpu_info = jsonutils.loads(dst_compute_info['cpu_info'])
  3480. dst_cpu_arch = dst_cpu_info['arch']
  3481. if src_cpu_arch != dst_cpu_arch:
  3482. reason = (_("CPU architectures between source host '%s' (%s) and "
  3483. "destination host '%s' (%s) are incompatible.")
  3484. % (src_compute_info['hypervisor_hostname'], src_cpu_arch,
  3485. dst_compute_info['hypervisor_hostname'],
  3486. dst_cpu_arch))
  3487. raise exception.MigrationPreCheckError(reason=reason)
  3488. extra_specs = self._get_flavor(instance)['extra_specs'].copy()
  3489. brand = extra_specs.get('zonecfg:brand', ZONE_BRAND_SOLARIS)
  3490. if brand != ZONE_BRAND_SOLARIS_KZ:
  3491. # Only Solaris kernel zones are currently supported.
  3492. reason = (_("'%s' branded zones do not currently support live "
  3493. "migration.") % brand)
  3494. raise exception.MigrationPreCheckError(reason=reason)
  3495. if block_migration:
  3496. reason = (_('Block migration is not currently supported.'))
  3497. raise exception.MigrationPreCheckError(reason=reason)
  3498. if disk_over_commit:
  3499. reason = (_('Disk overcommit is not currently supported.'))
  3500. raise exception.MigrationPreCheckError(reason=reason)
  3501. dest_check_data = objects.SolarisZonesLiveMigrateData()
  3502. dest_check_data.hypervisor_hostname = \
  3503. dst_compute_info['hypervisor_hostname']
  3504. return dest_check_data
  3505. def check_can_live_migrate_destination_cleanup(self, context,
  3506. dest_check_data):
  3507. """Do required cleanup on dest host after check_can_live_migrate calls
  3508. :param context: security context
  3509. :param dest_check_data: result of check_can_live_migrate_destination
  3510. """
  3511. pass
  3512. def _check_local_volumes_present(self, block_device_info):
  3513. """Check if local volumes are attached to the instance."""
  3514. bmap = block_device_info.get('block_device_mapping')
  3515. for entry in bmap:
  3516. connection_info = entry['connection_info']
  3517. driver_type = connection_info['driver_volume_type']
  3518. if driver_type == 'local':
  3519. reason = (_("Instances with attached '%s' volumes are not "
  3520. "currently supported.") % driver_type)
  3521. raise exception.MigrationPreCheckError(reason=reason)
  3522. def check_can_live_migrate_source(self, context, instance,
  3523. dest_check_data, block_device_info=None):
  3524. """Check if it is possible to execute live migration.
  3525. This checks if the live migration can succeed, based on the
  3526. results from check_can_live_migrate_destination.
  3527. :param context: security context
  3528. :param instance: nova.db.sqlalchemy.models.Instance
  3529. :param dest_check_data: result of check_can_live_migrate_destination
  3530. :param block_device_info: result of _get_instance_block_device_info
  3531. :returns: a LiveMigrateData object
  3532. """
  3533. if not isinstance(dest_check_data, migrate_data_obj.LiveMigrateData):
  3534. obj = objects.SolarisZonesLiveMigrateData()
  3535. obj.from_legacy_dict(dest_check_data)
  3536. dest_check_data = obj
  3537. self._check_local_volumes_present(block_device_info)
  3538. name = instance['name']
  3539. dest = dest_check_data.hypervisor_hostname
  3540. try:
  3541. self._live_migration(name, dest, dry_run=True)
  3542. except Exception as ex:
  3543. reason = zonemgr_strerror(ex)
  3544. raise exception.MigrationPreCheckError(reason=reason)
  3545. return dest_check_data
  3546. def get_instance_disk_info(self, instance,
  3547. block_device_info=None):
  3548. """Retrieve information about actual disk sizes of an instance.
  3549. :param instance: nova.objects.Instance
  3550. :param block_device_info:
  3551. Optional; Can be used to filter out devices which are
  3552. actually volumes.
  3553. :return:
  3554. json strings with below format::
  3555. "[{'path':'disk',
  3556. 'type':'raw',
  3557. 'virt_disk_size':'10737418240',
  3558. 'backing_file':'backing_file',
  3559. 'disk_size':'83886080'
  3560. 'over_committed_disk_size':'10737418240'},
  3561. ...]"
  3562. """
  3563. raise NotImplementedError()
  3564. def refresh_security_group_rules(self, security_group_id):
  3565. """This method is called after a change to security groups.
  3566. All security groups and their associated rules live in the datastore,
  3567. and calling this method should apply the updated rules to instances
  3568. running the specified security group.
  3569. An error should be raised if the operation cannot complete.
  3570. """
  3571. # TODO(Vek): Need to pass context in for access to auth_token
  3572. raise NotImplementedError()
  3573. def refresh_instance_security_rules(self, instance):
  3574. """Refresh security group rules
  3575. Gets called when an instance gets added to or removed from
  3576. the security group the instance is a member of or if the
  3577. group gains or loses a rule.
  3578. """
  3579. raise NotImplementedError()
  3580. def reset_network(self, instance):
  3581. """reset networking for specified instance."""
  3582. # TODO(Vek): Need to pass context in for access to auth_token
  3583. pass
  3584. def ensure_filtering_rules_for_instance(self, instance, network_info):
  3585. """Setting up filtering rules and waiting for its completion.
  3586. To migrate an instance, filtering rules to hypervisors
  3587. and firewalls are inevitable on destination host.
  3588. ( Waiting only for filtering rules to hypervisor,
  3589. since filtering rules to firewall rules can be set faster).
  3590. Concretely, the below method must be called.
  3591. - setup_basic_filtering (for nova-basic, etc.)
  3592. - prepare_instance_filter(for nova-instance-instance-xxx, etc.)
  3593. to_xml may have to be called since it defines PROJNET, PROJMASK.
  3594. but libvirt migrates those value through migrateToURI(),
  3595. so , no need to be called.
  3596. Don't use thread for this method since migration should
  3597. not be started when setting-up filtering rules operations
  3598. are not completed.
  3599. :param instance: nova.objects.instance.Instance object
  3600. """
  3601. # TODO(Vek): Need to pass context in for access to auth_token
  3602. pass
  3603. def filter_defer_apply_on(self):
  3604. """Defer application of IPTables rules."""
  3605. pass
  3606. def filter_defer_apply_off(self):
  3607. """Turn off deferral of IPTables rules and apply the rules now."""
  3608. pass
  3609. def unfilter_instance(self, instance, network_info):
  3610. """Stop filtering instance."""
  3611. # TODO(Vek): Need to pass context in for access to auth_token
  3612. pass
  3613. def set_admin_password(self, instance, new_pass):
  3614. """Set the root password on the specified instance.
  3615. :param instance: nova.objects.instance.Instance
  3616. :param new_pass: the new password
  3617. """
  3618. name = instance['name']
  3619. zone = self._get_zone_by_name(name)
  3620. if zone is None:
  3621. raise exception.InstanceNotFound(instance_id=name)
  3622. if zone.state == ZONE_STATE_RUNNING:
  3623. out, err = utils.execute('/usr/sbin/zlogin', '-S', name,
  3624. '/usr/bin/passwd', '-p',
  3625. "'%s'" % sha256_crypt.encrypt(new_pass))
  3626. else:
  3627. raise exception.InstanceNotRunning(instance_id=name)
  3628. def inject_file(self, instance, b64_path, b64_contents):
  3629. """Writes a file on the specified instance.
  3630. The first parameter is an instance of nova.compute.service.Instance,
  3631. and so the instance is being specified as instance.name. The second
  3632. parameter is the base64-encoded path to which the file is to be
  3633. written on the instance; the third is the contents of the file, also
  3634. base64-encoded.
  3635. NOTE(russellb) This method is deprecated and will be removed once it
  3636. can be removed from nova.compute.manager.
  3637. """
  3638. # TODO(Vek): Need to pass context in for access to auth_token
  3639. raise NotImplementedError()
  3640. def change_instance_metadata(self, context, instance, diff):
  3641. """Applies a diff to the instance metadata.
  3642. This is an optional driver method which is used to publish
  3643. changes to the instance's metadata to the hypervisor. If the
  3644. hypervisor has no means of publishing the instance metadata to
  3645. the instance, then this method should not be implemented.
  3646. :param context: security context
  3647. :param instance: nova.objects.instance.Instance
  3648. """
  3649. pass
  3650. def inject_network_info(self, instance, nw_info):
  3651. """inject network info for specified instance."""
  3652. # TODO(Vek): Need to pass context in for access to auth_token
  3653. pass
  3654. def poll_rebooting_instances(self, timeout, instances):
  3655. """Perform a reboot on all given 'instances'.
  3656. Reboots the given `instances` which are longer in the rebooting state
  3657. than `timeout` seconds.
  3658. :param int timeout:
  3659. The timeout (in seconds) for considering rebooting instances
  3660. to be stuck.
  3661. :param list instances:
  3662. A list of nova.objects.instance.Instance objects that have been
  3663. in rebooting state longer than the configured timeout.
  3664. :return: None
  3665. """
  3666. # TODO(Vek): Need to pass context in for access to auth_token
  3667. raise NotImplementedError()
  3668. def host_power_action(self, action):
  3669. """Reboots, shuts down or powers up the host.
  3670. :param str action:
  3671. The action the host should perform. The valid actions are:
  3672. ""startup", "shutdown" and "reboot".
  3673. :return: The result of the power action
  3674. :rtype: : str
  3675. """
  3676. raise NotImplementedError()
  3677. def host_maintenance_mode(self, host, mode):
  3678. """Start/Stop host maintenance window.
  3679. On start, it triggers the migration of all instances to other hosts.
  3680. Consider the combination with :func:`set_host_enabled`.
  3681. :param str host:
  3682. The name of the host whose maintenance mode should be changed.
  3683. :param bool mode:
  3684. If `True`, go into maintenance mode. If `False`, leave the
  3685. maintenance mode.
  3686. :return: "on_maintenance" if switched to maintenance mode or
  3687. "off_maintenance" if maintenance mode got left.
  3688. :rtype: str
  3689. """
  3690. raise NotImplementedError()
  3691. def set_host_enabled(self, enabled):
  3692. """Sets the ability of this host to accept new instances.
  3693. :param bool enabled:
  3694. If this is `True`, the host will accept new instances. If it is
  3695. `False`, the host won't accept new instances.
  3696. :return: If the host can accept further instances, return "enabled",
  3697. if further instances shouldn't be scheduled to this host,
  3698. return "disabled".
  3699. :rtype: str
  3700. """
  3701. # TODO(Vek): Need to pass context in for access to auth_token
  3702. raise NotImplementedError()
  3703. def get_host_uptime(self):
  3704. """Returns the result of calling the Linux command `uptime` on this
  3705. host.
  3706. :return: A text which contains the uptime of this host since the
  3707. last boot.
  3708. :rtype: str
  3709. """
  3710. # TODO(Vek): Need to pass context in for access to auth_token
  3711. return utils.execute('/usr/bin/uptime')[0]
  3712. def plug_vifs(self, instance, network_info):
  3713. """Plug virtual interfaces (VIFs) into the given `instance` at
  3714. instance boot time.
  3715. The counter action is :func:`unplug_vifs`.
  3716. :param nova.objects.instance.Instance instance:
  3717. The instance which gets VIFs plugged.
  3718. :param nova.network.model.NetworkInfo network_info:
  3719. The object which contains information about the VIFs to plug.
  3720. :return: None
  3721. """
  3722. # TODO(Vek): Need to pass context in for access to auth_token
  3723. pass
  3724. def unplug_vifs(self, instance, network_info):
  3725. # NOTE(markus_z): 2015-08-18
  3726. # The compute manager doesn't use this interface, which seems odd
  3727. # since the manager should be the controlling thing here.
  3728. """Unplug virtual interfaces (VIFs) from networks.
  3729. The counter action is :func:`plug_vifs`.
  3730. :param nova.objects.instance.Instance instance:
  3731. The instance which gets VIFs unplugged.
  3732. :param nova.network.model.NetworkInfo network_info:
  3733. The object which contains information about the VIFs to unplug.
  3734. :return: None
  3735. """
  3736. raise NotImplementedError()
  3737. def get_host_cpu_stats(self):
  3738. """Get the currently known host CPU stats.
  3739. :returns: a dict containing the CPU stat info, eg:
  3740. | {'kernel': kern,
  3741. | 'idle': idle,
  3742. | 'user': user,
  3743. | 'iowait': wait,
  3744. | 'frequency': freq},
  3745. where kern and user indicate the cumulative CPU time
  3746. (nanoseconds) spent by kernel and user processes
  3747. respectively, idle indicates the cumulative idle CPU time
  3748. (nanoseconds), wait indicates the cumulative I/O wait CPU
  3749. time (nanoseconds), since the host is booting up; freq
  3750. indicates the current CPU frequency (MHz). All values are
  3751. long integers.
  3752. """
  3753. raise NotImplementedError()
  3754. def block_stats(self, instance, disk_id):
  3755. """Return performance counters associated with the given disk_id on the
  3756. given instance. These are returned as [rd_req, rd_bytes, wr_req,
  3757. wr_bytes, errs], where rd indicates read, wr indicates write, req is
  3758. the total number of I/O requests made, bytes is the total number of
  3759. bytes transferred, and errs is the number of requests held up due to a
  3760. full pipeline.
  3761. All counters are long integers.
  3762. This method is optional. On some platforms (e.g. XenAPI) performance
  3763. statistics can be retrieved directly in aggregate form, without Nova
  3764. having to do the aggregation. On those platforms, this method is
  3765. unused.
  3766. Note that this function takes an instance ID.
  3767. """
  3768. raise NotImplementedError()
  3769. def deallocate_networks_on_reschedule(self, instance):
  3770. """Does the driver want networks deallocated on reschedule?"""
  3771. return False
  3772. def macs_for_instance(self, instance):
  3773. """What MAC addresses must this instance have?
  3774. Some hypervisors (such as bare metal) cannot do freeform virtualisation
  3775. of MAC addresses. This method allows drivers to return a set of MAC
  3776. addresses that the instance is to have. allocate_for_instance will take
  3777. this into consideration when provisioning networking for the instance.
  3778. Mapping of MAC addresses to actual networks (or permitting them to be
  3779. freeform) is up to the network implementation layer. For instance,
  3780. with openflow switches, fixed MAC addresses can still be virtualised
  3781. onto any L2 domain, with arbitrary VLANs etc, but regular switches
  3782. require pre-configured MAC->network mappings that will match the
  3783. actual configuration.
  3784. Most hypervisors can use the default implementation which returns None.
  3785. Hypervisors with MAC limits should return a set of MAC addresses, which
  3786. will be supplied to the allocate_for_instance call by the compute
  3787. manager, and it is up to that call to ensure that all assigned network
  3788. details are compatible with the set of MAC addresses.
  3789. This is called during spawn_instance by the compute manager.
  3790. :return: None, or a set of MAC ids (e.g. set(['12:34:56:78:90:ab'])).
  3791. None means 'no constraints', a set means 'these and only these
  3792. MAC addresses'.
  3793. """
  3794. return None
  3795. def dhcp_options_for_instance(self, instance):
  3796. """Get DHCP options for this instance.
  3797. Some hypervisors (such as bare metal) require that instances boot from
  3798. the network, and manage their own TFTP service. This requires passing
  3799. the appropriate options out to the DHCP service. Most hypervisors can
  3800. use the default implementation which returns None.
  3801. This is called during spawn_instance by the compute manager.
  3802. Note that the format of the return value is specific to the Neutron
  3803. client API.
  3804. :return: None, or a set of DHCP options, eg:
  3805. | [{'opt_name': 'bootfile-name',
  3806. | 'opt_value': '/tftpboot/path/to/config'},
  3807. | {'opt_name': 'server-ip-address',
  3808. | 'opt_value': '1.2.3.4'},
  3809. | {'opt_name': 'tftp-server',
  3810. | 'opt_value': '1.2.3.4'}
  3811. | ]
  3812. """
  3813. return None
  3814. def manage_image_cache(self, context, all_instances):
  3815. """Manage the driver's local image cache.
  3816. Some drivers chose to cache images for instances on disk. This method
  3817. is an opportunity to do management of that cache which isn't directly
  3818. related to other calls into the driver. The prime example is to clean
  3819. the cache and remove images which are no longer of interest.
  3820. :param all_instances: nova.objects.instance.InstanceList
  3821. """
  3822. pass
  3823. def add_to_aggregate(self, context, aggregate, host, **kwargs):
  3824. """Add a compute host to an aggregate.
  3825. The counter action to this is :func:`remove_from_aggregate`
  3826. :param nova.context.RequestContext context:
  3827. The security context.
  3828. :param nova.objects.aggregate.Aggregate aggregate:
  3829. The aggregate which should add the given `host`
  3830. :param str host:
  3831. The name of the host to add to the given `aggregate`.
  3832. :param dict kwargs:
  3833. A free-form thingy...
  3834. :return: None
  3835. """
  3836. # NOTE(jogo) Currently only used for XenAPI-Pool
  3837. raise NotImplementedError()
  3838. def remove_from_aggregate(self, context, aggregate, host, **kwargs):
  3839. """Remove a compute host from an aggregate.
  3840. The counter action to this is :func:`add_to_aggregate`
  3841. :param nova.context.RequestContext context:
  3842. The security context.
  3843. :param nova.objects.aggregate.Aggregate aggregate:
  3844. The aggregate which should remove the given `host`
  3845. :param str host:
  3846. The name of the host to remove from the given `aggregate`.
  3847. :param dict kwargs:
  3848. A free-form thingy...
  3849. :return: None
  3850. """
  3851. raise NotImplementedError()
  3852. def undo_aggregate_operation(self, context, op, aggregate,
  3853. host, set_error=True):
  3854. """Undo for Resource Pools."""
  3855. raise NotImplementedError()
  3856. def get_volume_connector(self, instance):
  3857. """Get connector information for the instance for attaching to volumes.
  3858. Connector information is a dictionary representing the ip of the
  3859. machine that will be making the connection, the name of the iscsi
  3860. initiator, the WWPN and WWNN values of the Fibre Channel initiator,
  3861. and the hostname of the machine as follows::
  3862. {
  3863. 'ip': ip,
  3864. 'initiator': initiator,
  3865. 'wwnns': wwnns,
  3866. 'wwpns': wwpns,
  3867. 'host': hostname
  3868. }
  3869. """
  3870. connector = {
  3871. 'ip': self.get_host_ip_addr(),
  3872. 'host': CONF.host
  3873. }
  3874. if not self._initiator:
  3875. self._initiator = self._get_iscsi_initiator()
  3876. if self._initiator:
  3877. connector['initiator'] = self._initiator
  3878. else:
  3879. LOG.debug(_("Could not determine iSCSI initiator name"),
  3880. instance=instance)
  3881. if not self._fc_wwnns:
  3882. self._fc_wwnns = self._get_fc_wwnns()
  3883. if not self._fc_wwnns or len(self._fc_wwnns) == 0:
  3884. LOG.debug(_('Could not determine Fibre Channel '
  3885. 'World Wide Node Names'),
  3886. instance=instance)
  3887. if not self._fc_wwpns:
  3888. self._fc_wwpns = self._get_fc_wwpns()
  3889. if not self._fc_wwpns or len(self._fc_wwpns) == 0:
  3890. LOG.debug(_('Could not determine Fibre Channel '
  3891. 'World Wide Port Names'),
  3892. instance=instance)
  3893. if self._fc_wwnns and self._fc_wwpns:
  3894. connector["wwnns"] = self._fc_wwnns
  3895. connector["wwpns"] = self._fc_wwpns
  3896. return connector
  3897. def get_available_nodes(self, refresh=False):
  3898. """Returns nodenames of all nodes managed by the compute service.
  3899. This method is for multi compute-nodes support. If a driver supports
  3900. multi compute-nodes, this method returns a list of nodenames managed
  3901. by the service. Otherwise, this method should return
  3902. [hypervisor_hostname].
  3903. """
  3904. if refresh or not self._host_stats:
  3905. self._update_host_stats()
  3906. stats = self._host_stats
  3907. if not isinstance(stats, list):
  3908. stats = [stats]
  3909. return [s['hypervisor_hostname'] for s in stats]
  3910. def node_is_available(self, nodename):
  3911. """Return whether this compute service manages a particular node."""
  3912. if nodename in self.get_available_nodes():
  3913. return True
  3914. # Refresh and check again.
  3915. return nodename in self.get_available_nodes(refresh=True)
  3916. def get_per_instance_usage(self):
  3917. """Get information about instance resource usage.
  3918. :returns: dict of nova uuid => dict of usage info
  3919. """
  3920. return {}
  3921. def instance_on_disk(self, instance):
  3922. """Checks access of instance files on the host.
  3923. :param instance: nova.objects.instance.Instance to lookup
  3924. Returns True if files of an instance with the supplied ID accessible on
  3925. the host, False otherwise.
  3926. .. note::
  3927. Used in rebuild for HA implementation and required for validation
  3928. of access to instance shared disk files
  3929. """
  3930. bdmobj = objects.BlockDeviceMappingList
  3931. bdms = bdmobj.get_by_instance_uuid(nova_context.get_admin_context(),
  3932. instance['uuid'])
  3933. root_ci = None
  3934. rootmp = instance['root_device_name']
  3935. for entry in bdms:
  3936. if entry['connection_info'] is None:
  3937. continue
  3938. if entry['device_name'] == rootmp:
  3939. root_ci = jsonutils.loads(entry['connection_info'])
  3940. break
  3941. if root_ci is None:
  3942. msg = (_("Unable to find the root device for instance '%s'.")
  3943. % instance['name'])
  3944. raise exception.NovaException(msg)
  3945. driver_type = root_ci['driver_volume_type']
  3946. return driver_type in shared_storage
  3947. def register_event_listener(self, callback):
  3948. """Register a callback to receive events.
  3949. Register a callback to receive asynchronous event
  3950. notifications from hypervisors. The callback will
  3951. be invoked with a single parameter, which will be
  3952. an instance of the nova.virt.event.Event class.
  3953. """
  3954. self._compute_event_callback = callback
  3955. def emit_event(self, event):
  3956. """Dispatches an event to the compute manager.
  3957. Invokes the event callback registered by the
  3958. compute manager to dispatch the event. This
  3959. must only be invoked from a green thread.
  3960. """
  3961. if not self._compute_event_callback:
  3962. LOG.debug("Discarding event %s", str(event))
  3963. return
  3964. if not isinstance(event, virtevent.Event):
  3965. raise ValueError(
  3966. _("Event must be an instance of nova.virt.event.Event"))
  3967. try:
  3968. LOG.debug("Emitting event %s", str(event))
  3969. self._compute_event_callback(event)
  3970. except Exception as ex:
  3971. LOG.error(_LE("Exception dispatching event %(event)s: %(ex)s"),
  3972. {'event': event, 'ex': ex})
  3973. def delete_instance_files(self, instance):
  3974. """Delete any lingering instance files for an instance.
  3975. :param instance: nova.objects.instance.Instance
  3976. :returns: True if the instance was deleted from disk, False otherwise.
  3977. """
  3978. # Delete the zone configuration for the instance using destroy, because
  3979. # it will simply take care of the work, and we don't need to duplicate
  3980. # the code here.
  3981. LOG.debug(_("Cleaning up for instance %s"), instance['name'])
  3982. try:
  3983. self.destroy(None, instance, None)
  3984. except Exception:
  3985. return False
  3986. return True
  3987. @property
  3988. def need_legacy_block_device_info(self):
  3989. """Tell the caller if the driver requires legacy block device info.
  3990. Tell the caller whether we expect the legacy format of block
  3991. device info to be passed in to methods that expect it.
  3992. """
  3993. return True
  3994. def volume_snapshot_create(self, context, instance, volume_id,
  3995. create_info):
  3996. """Snapshots volumes attached to a specified instance.
  3997. The counter action to this is :func:`volume_snapshot_delete`
  3998. :param nova.context.RequestContext context:
  3999. The security context.
  4000. :param nova.objects.instance.Instance instance:
  4001. The instance that has the volume attached
  4002. :param uuid volume_id:
  4003. Volume to be snapshotted
  4004. :param create_info: The data needed for nova to be able to attach
  4005. to the volume. This is the same data format returned by
  4006. Cinder's initialize_connection() API call. In the case of
  4007. doing a snapshot, it is the image file Cinder expects to be
  4008. used as the active disk after the snapshot operation has
  4009. completed. There may be other data included as well that is
  4010. needed for creating the snapshot.
  4011. """
  4012. raise NotImplementedError()
  4013. def volume_snapshot_delete(self, context, instance, volume_id,
  4014. snapshot_id, delete_info):
  4015. """Deletes a snapshot of a volume attached to a specified instance.
  4016. The counter action to this is :func:`volume_snapshot_create`
  4017. :param nova.context.RequestContext context:
  4018. The security context.
  4019. :param nova.objects.instance.Instance instance:
  4020. The instance that has the volume attached.
  4021. :param uuid volume_id:
  4022. Attached volume associated with the snapshot
  4023. :param uuid snapshot_id:
  4024. The snapshot to delete.
  4025. :param dict delete_info:
  4026. Volume backend technology specific data needed to be able to
  4027. complete the snapshot. For example, in the case of qcow2 backed
  4028. snapshots, this would include the file being merged, and the file
  4029. being merged into (if appropriate).
  4030. :return: None
  4031. """
  4032. raise NotImplementedError()
  4033. def default_root_device_name(self, instance, image_meta, root_bdm):
  4034. """Provide a default root device name for the driver.
  4035. :param nova.objects.instance.Instance instance:
  4036. The instance to get the root device for.
  4037. :param nova.objects.ImageMeta image_meta:
  4038. The metadata of the image of the instance.
  4039. :param nova.objects.BlockDeviceMapping root_bdm:
  4040. The description of the root device.
  4041. """
  4042. raise NotImplementedError()
  4043. def default_device_names_for_instance(self, instance, root_device_name,
  4044. *block_device_lists):
  4045. """Default the missing device names in the block device mapping."""
  4046. raise NotImplementedError()
  4047. def get_device_name_for_instance(self, instance,
  4048. bdms, block_device_obj):
  4049. """Get the next device name based on the block device mapping.
  4050. :param instance: nova.objects.instance.Instance that volume is
  4051. requesting a device name
  4052. :param bdms: a nova.objects.BlockDeviceMappingList for the instance
  4053. :param block_device_obj: A nova.objects.BlockDeviceMapping instance
  4054. with all info about the requested block
  4055. device. device_name does not need to be set,
  4056. and should be decided by the driver
  4057. implementation if not set.
  4058. :returns: The chosen device name.
  4059. """
  4060. raise NotImplementedError()
  4061. def is_supported_fs_format(self, fs_type):
  4062. """Check whether the file format is supported by this driver
  4063. :param fs_type: the file system type to be checked,
  4064. the validate values are defined at disk API module.
  4065. """
  4066. # NOTE(jichenjc): Return False here so that every hypervisor
  4067. # need to define their supported file system
  4068. # type and implement this function at their
  4069. # virt layer.
  4070. return False
  4071. def quiesce(self, context, instance, image_meta):
  4072. """Quiesce the specified instance to prepare for snapshots.
  4073. If the specified instance doesn't support quiescing,
  4074. InstanceQuiesceNotSupported is raised. When it fails to quiesce by
  4075. other errors (e.g. agent timeout), NovaException is raised.
  4076. :param context: request context
  4077. :param instance: nova.objects.instance.Instance to be quiesced
  4078. :param nova.objects.ImageMeta image_meta:
  4079. The metadata of the image of the instance.
  4080. """
  4081. raise NotImplementedError()
  4082. def unquiesce(self, context, instance, image_meta):
  4083. """Unquiesce the specified instance after snapshots.
  4084. If the specified instance doesn't support quiescing,
  4085. InstanceQuiesceNotSupported is raised. When it fails to quiesce by
  4086. other errors (e.g. agent timeout), NovaException is raised.
  4087. :param context: request context
  4088. :param instance: nova.objects.instance.Instance to be unquiesced
  4089. :param nova.objects.ImageMeta image_meta:
  4090. The metadata of the image of the instance.
  4091. """
  4092. raise NotImplementedError()
  4093. def network_binding_host_id(self, context, instance):
  4094. """Get host ID to associate with network ports.
  4095. :param context: request context
  4096. :param instance: nova.objects.instance.Instance that the network
  4097. ports will be associated with
  4098. :returns: a string representing the host ID
  4099. """
  4100. return instance.get('host')