OpenStack Block Storage (Cinder)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver.py 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2010 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # 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. Drivers for volumes.
  19. """
  20. import os
  21. import time
  22. from oslo.config import cfg
  23. from cinder.brick.initiator import connector as initiator
  24. from cinder.brick.iscsi import iscsi
  25. from cinder.brick.iser import iser
  26. from cinder import exception
  27. from cinder.image import image_utils
  28. from cinder.openstack.common import excutils
  29. from cinder.openstack.common import fileutils
  30. from cinder.openstack.common import log as logging
  31. from cinder.openstack.common import processutils
  32. from cinder import utils
  33. from cinder.volume import rpcapi as volume_rpcapi
  34. from cinder.volume import utils as volume_utils
  35. LOG = logging.getLogger(__name__)
  36. volume_opts = [
  37. cfg.IntOpt('num_shell_tries',
  38. default=3,
  39. help='number of times to attempt to run flakey shell commands'),
  40. cfg.IntOpt('reserved_percentage',
  41. default=0,
  42. help='The percentage of backend capacity is reserved'),
  43. cfg.IntOpt('iscsi_num_targets',
  44. default=100,
  45. help='The maximum number of iscsi target ids per host'),
  46. cfg.StrOpt('iscsi_target_prefix',
  47. default='iqn.2010-10.org.openstack:',
  48. help='prefix for iscsi volumes'),
  49. cfg.StrOpt('iscsi_ip_address',
  50. default='$my_ip',
  51. help='The IP address that the iSCSI daemon is listening on'),
  52. cfg.IntOpt('iscsi_port',
  53. default=3260,
  54. help='The port that the iSCSI daemon is listening on'),
  55. cfg.IntOpt('num_volume_device_scan_tries',
  56. deprecated_name='num_iscsi_scan_tries',
  57. default=3,
  58. help='The maximum number of times to rescan targets'
  59. ' to find volume'),
  60. cfg.IntOpt('num_iser_scan_tries',
  61. default=3,
  62. help='The maximum number of times to rescan iSER target'
  63. 'to find volume'),
  64. cfg.IntOpt('iser_num_targets',
  65. default=100,
  66. help='The maximum number of iser target ids per host'),
  67. cfg.StrOpt('iser_target_prefix',
  68. default='iqn.2010-10.org.iser.openstack:',
  69. help='prefix for iser volumes'),
  70. cfg.StrOpt('iser_ip_address',
  71. default='$my_ip',
  72. help='The IP address that the iSER daemon is listening on'),
  73. cfg.IntOpt('iser_port',
  74. default=3260,
  75. help='The port that the iSER daemon is listening on'),
  76. cfg.StrOpt('iser_helper',
  77. default='tgtadm',
  78. help='iser target user-land tool to use'),
  79. cfg.StrOpt('volume_backend_name',
  80. default=None,
  81. help='The backend name for a given driver implementation'),
  82. cfg.BoolOpt('use_multipath_for_image_xfer',
  83. default=False,
  84. help='Do we attach/detach volumes in cinder using multipath '
  85. 'for volume to image and image to volume transfers?'),
  86. cfg.StrOpt('volume_clear',
  87. default='zero',
  88. help='Method used to wipe old voumes (valid options are: '
  89. 'none, zero, shred)'),
  90. cfg.IntOpt('volume_clear_size',
  91. default=0,
  92. help='Size in MiB to wipe at start of old volumes. 0 => all'),
  93. cfg.StrOpt('iscsi_helper',
  94. default='tgtadm',
  95. help='iscsi target user-land tool to use'),
  96. cfg.StrOpt('volumes_dir',
  97. default='$state_path/volumes',
  98. help='Volume configuration file storage '
  99. 'directory'),
  100. cfg.StrOpt('iet_conf',
  101. default='/etc/iet/ietd.conf',
  102. help='IET configuration file'),
  103. cfg.StrOpt('lio_initiator_iqns',
  104. default='',
  105. help=('Comma-separated list of initiator IQNs '
  106. 'allowed to connect to the '
  107. 'iSCSI target. (From Nova compute nodes.)')),
  108. cfg.StrOpt('iscsi_iotype',
  109. default='fileio',
  110. help=('Sets the behavior of the iSCSI target '
  111. 'to either perform blockio or fileio '
  112. 'optionally, auto can be set and Cinder '
  113. 'will autodetect type of backing device'))]
  114. CONF = cfg.CONF
  115. CONF.register_opts(volume_opts)
  116. class VolumeDriver(object):
  117. """Executes commands relating to Volumes."""
  118. VERSION = "N/A"
  119. def __init__(self, execute=utils.execute, *args, **kwargs):
  120. # NOTE(vish): db is set by Manager
  121. self.db = kwargs.get('db')
  122. self.configuration = kwargs.get('configuration', None)
  123. if self.configuration:
  124. self.configuration.append_config_values(volume_opts)
  125. self.set_execute(execute)
  126. self._stats = {}
  127. # set True by manager after succesful check_for_setup
  128. self._initialized = False
  129. def set_execute(self, execute):
  130. self._execute = execute
  131. def set_initialized(self):
  132. self._initialized = True
  133. @property
  134. def initialized(self):
  135. return self._initialized
  136. def get_version(self):
  137. """Get the current version of this driver."""
  138. return self.VERSION
  139. def _is_non_recoverable(self, err, non_recoverable_list):
  140. for item in non_recoverable_list:
  141. if item in err:
  142. return True
  143. return False
  144. def _try_execute(self, *command, **kwargs):
  145. # NOTE(vish): Volume commands can partially fail due to timing, but
  146. # running them a second time on failure will usually
  147. # recover nicely.
  148. non_recoverable = kwargs.pop('no_retry_list', [])
  149. tries = 0
  150. while True:
  151. try:
  152. self._execute(*command, **kwargs)
  153. return True
  154. except processutils.ProcessExecutionError as ex:
  155. tries = tries + 1
  156. if tries >= self.configuration.num_shell_tries or\
  157. self._is_non_recoverable(ex.stderr, non_recoverable):
  158. raise
  159. LOG.exception(_("Recovering from a failed execute. "
  160. "Try number %s"), tries)
  161. time.sleep(tries ** 2)
  162. def check_for_setup_error(self):
  163. raise NotImplementedError()
  164. def create_volume(self, volume):
  165. """Creates a volume. Can optionally return a Dictionary of
  166. changes to the volume object to be persisted.
  167. """
  168. raise NotImplementedError()
  169. def create_volume_from_snapshot(self, volume, snapshot):
  170. """Creates a volume from a snapshot."""
  171. raise NotImplementedError()
  172. def create_cloned_volume(self, volume, src_vref):
  173. """Creates a clone of the specified volume."""
  174. raise NotImplementedError()
  175. def delete_volume(self, volume):
  176. """Deletes a volume."""
  177. raise NotImplementedError()
  178. def create_snapshot(self, snapshot):
  179. """Creates a snapshot."""
  180. raise NotImplementedError()
  181. def delete_snapshot(self, snapshot):
  182. """Deletes a snapshot."""
  183. raise NotImplementedError()
  184. def local_path(self, volume):
  185. raise NotImplementedError()
  186. def ensure_export(self, context, volume):
  187. """Synchronously recreates an export for a volume."""
  188. raise NotImplementedError()
  189. def create_export(self, context, volume):
  190. """Exports the volume. Can optionally return a Dictionary of changes
  191. to the volume object to be persisted.
  192. """
  193. raise NotImplementedError()
  194. def remove_export(self, context, volume):
  195. """Removes an export for a volume."""
  196. raise NotImplementedError()
  197. def initialize_connection(self, volume, connector):
  198. """Allow connection to connector and return connection info."""
  199. raise NotImplementedError()
  200. def terminate_connection(self, volume, connector, **kwargs):
  201. """Disallow connection from connector"""
  202. raise NotImplementedError()
  203. def attach_volume(self, context, volume, instance_uuid, host_name,
  204. mountpoint):
  205. """Callback for volume attached to instance or host."""
  206. pass
  207. def detach_volume(self, context, volume):
  208. """Callback for volume detached."""
  209. pass
  210. def get_volume_stats(self, refresh=False):
  211. """Return the current state of the volume service. If 'refresh' is
  212. True, run the update first.
  213. """
  214. return None
  215. def do_setup(self, context):
  216. """Any initialization the volume driver does while starting"""
  217. pass
  218. def validate_connector(self, connector):
  219. """Fail if connector doesn't contain all the data needed by driver"""
  220. pass
  221. def _copy_volume_data_cleanup(self, context, volume, properties,
  222. attach_info, remote, force=False):
  223. self._detach_volume(attach_info)
  224. if remote:
  225. rpcapi = volume_rpcapi.VolumeAPI()
  226. rpcapi.terminate_connection(context, volume, properties,
  227. force=force)
  228. else:
  229. self.terminate_connection(volume, properties, force=False)
  230. def copy_volume_data(self, context, src_vol, dest_vol, remote=None):
  231. """Copy data from src_vol to dest_vol."""
  232. LOG.debug(_('copy_data_between_volumes %(src)s -> %(dest)s.')
  233. % {'src': src_vol['name'], 'dest': dest_vol['name']})
  234. properties = utils.brick_get_connector_properties()
  235. dest_remote = True if remote in ['dest', 'both'] else False
  236. dest_orig_status = dest_vol['status']
  237. try:
  238. dest_attach_info = self._attach_volume(context,
  239. dest_vol,
  240. properties,
  241. remote=dest_remote)
  242. except Exception:
  243. with excutils.save_and_reraise_exception():
  244. msg = _("Failed to attach volume %(vol)s")
  245. LOG.error(msg % {'vol': dest_vol['id']})
  246. self.db.volume_update(context, dest_vol['id'],
  247. {'status': dest_orig_status})
  248. src_remote = True if remote in ['src', 'both'] else False
  249. src_orig_status = src_vol['status']
  250. try:
  251. src_attach_info = self._attach_volume(context,
  252. src_vol,
  253. properties,
  254. remote=src_remote)
  255. except Exception:
  256. with excutils.save_and_reraise_exception():
  257. msg = _("Failed to attach volume %(vol)s")
  258. LOG.error(msg % {'vol': src_vol['id']})
  259. self.db.volume_update(context, src_vol['id'],
  260. {'status': src_orig_status})
  261. self._copy_volume_data_cleanup(context, dest_vol, properties,
  262. dest_attach_info, dest_remote,
  263. force=True)
  264. try:
  265. size_in_mb = int(src_vol['size']) * 1024 # vol size is in GB
  266. volume_utils.copy_volume(src_attach_info['device']['path'],
  267. dest_attach_info['device']['path'],
  268. size_in_mb)
  269. copy_error = False
  270. except Exception:
  271. with excutils.save_and_reraise_exception():
  272. msg = _("Failed to copy volume %(src)s to %(dest)d")
  273. LOG.error(msg % {'src': src_vol['id'], 'dest': dest_vol['id']})
  274. copy_error = True
  275. finally:
  276. self._copy_volume_data_cleanup(context, dest_vol, properties,
  277. dest_attach_info, dest_remote,
  278. force=copy_error)
  279. self._copy_volume_data_cleanup(context, src_vol, properties,
  280. src_attach_info, src_remote,
  281. force=copy_error)
  282. def copy_image_to_volume(self, context, volume, image_service, image_id):
  283. """Fetch the image from image_service and write it to the volume."""
  284. LOG.debug(_('copy_image_to_volume %s.') % volume['name'])
  285. properties = utils.brick_get_connector_properties()
  286. attach_info = self._attach_volume(context, volume, properties)
  287. try:
  288. image_utils.fetch_to_raw(context,
  289. image_service,
  290. image_id,
  291. attach_info['device']['path'])
  292. finally:
  293. self._detach_volume(attach_info)
  294. self.terminate_connection(volume, properties)
  295. def copy_volume_to_image(self, context, volume, image_service, image_meta):
  296. """Copy the volume to the specified image."""
  297. LOG.debug(_('copy_volume_to_image %s.') % volume['name'])
  298. properties = utils.brick_get_connector_properties()
  299. attach_info = self._attach_volume(context, volume, properties)
  300. try:
  301. image_utils.upload_volume(context,
  302. image_service,
  303. image_meta,
  304. attach_info['device']['path'])
  305. finally:
  306. self._detach_volume(attach_info)
  307. self.terminate_connection(volume, properties)
  308. def _attach_volume(self, context, volume, properties, remote=False):
  309. """Attach the volume."""
  310. if remote:
  311. rpcapi = volume_rpcapi.VolumeAPI()
  312. conn = rpcapi.initialize_connection(context, volume, properties)
  313. else:
  314. conn = self.initialize_connection(volume, properties)
  315. # Use Brick's code to do attach/detach
  316. use_multipath = self.configuration.use_multipath_for_image_xfer
  317. device_scan_attempts = self.configuration.num_volume_device_scan_tries
  318. protocol = conn['driver_volume_type']
  319. connector = utils.brick_get_connector(protocol,
  320. use_multipath=use_multipath,
  321. device_scan_attempts=
  322. device_scan_attempts,
  323. conn=conn)
  324. device = connector.connect_volume(conn['data'])
  325. host_device = device['path']
  326. if not connector.check_valid_device(host_device):
  327. raise exception.DeviceUnavailable(path=host_device,
  328. reason=(_("Unable to access "
  329. "the backend storage "
  330. "via the path "
  331. "%(path)s.") %
  332. {'path': host_device}))
  333. return {'conn': conn, 'device': device, 'connector': connector}
  334. def _detach_volume(self, attach_info):
  335. """Disconnect the volume from the host."""
  336. # Use Brick's code to do attach/detach
  337. connector = attach_info['connector']
  338. connector.disconnect_volume(attach_info['conn']['data'],
  339. attach_info['device'])
  340. def clone_image(self, volume, image_location, image_id):
  341. """Create a volume efficiently from an existing image.
  342. image_location is a string whose format depends on the
  343. image service backend in use. The driver should use it
  344. to determine whether cloning is possible.
  345. image_id is a string which represents id of the image.
  346. It can be used by the driver to introspect internal
  347. stores or registry to do an efficient image clone.
  348. Returns a dict of volume properties eg. provider_location,
  349. boolean indicating whether cloning occurred
  350. """
  351. return None, False
  352. def backup_volume(self, context, backup, backup_service):
  353. """Create a new backup from an existing volume."""
  354. volume = self.db.volume_get(context, backup['volume_id'])
  355. LOG.debug(_('Creating a new backup for volume %s.') %
  356. volume['name'])
  357. properties = utils.brick_get_connector_properties()
  358. attach_info = self._attach_volume(context, volume, properties)
  359. try:
  360. volume_path = attach_info['device']['path']
  361. with utils.temporary_chown(volume_path):
  362. with fileutils.file_open(volume_path) as volume_file:
  363. backup_service.backup(backup, volume_file)
  364. finally:
  365. self._detach_volume(attach_info)
  366. self.terminate_connection(volume, properties)
  367. def restore_backup(self, context, backup, volume, backup_service):
  368. """Restore an existing backup to a new or existing volume."""
  369. LOG.debug(_('Restoring backup %(backup)s to '
  370. 'volume %(volume)s.') %
  371. {'backup': backup['id'],
  372. 'volume': volume['name']})
  373. properties = utils.brick_get_connector_properties()
  374. attach_info = self._attach_volume(context, volume, properties)
  375. try:
  376. volume_path = attach_info['device']['path']
  377. with utils.temporary_chown(volume_path):
  378. with fileutils.file_open(volume_path, 'wb') as volume_file:
  379. backup_service.restore(backup, volume['id'], volume_file)
  380. finally:
  381. self._detach_volume(attach_info)
  382. self.terminate_connection(volume, properties)
  383. def clear_download(self, context, volume):
  384. """Clean up after an interrupted image copy."""
  385. pass
  386. def extend_volume(self, volume, new_size):
  387. msg = _("Extend volume not implemented")
  388. raise NotImplementedError(msg)
  389. def migrate_volume(self, context, volume, host):
  390. """Migrate the volume to the specified host.
  391. Returns a boolean indicating whether the migration occurred, as well as
  392. model_update.
  393. """
  394. return (False, None)
  395. class ISCSIDriver(VolumeDriver):
  396. """Executes commands relating to ISCSI volumes.
  397. We make use of model provider properties as follows:
  398. ``provider_location``
  399. if present, contains the iSCSI target information in the same
  400. format as an ietadm discovery
  401. i.e. '<ip>:<port>,<portal> <target IQN>'
  402. ``provider_auth``
  403. if present, contains a space-separated triple:
  404. '<auth method> <auth username> <auth password>'.
  405. `CHAP` is the only auth_method in use at the moment.
  406. """
  407. def __init__(self, *args, **kwargs):
  408. super(ISCSIDriver, self).__init__(*args, **kwargs)
  409. def _do_iscsi_discovery(self, volume):
  410. #TODO(justinsb): Deprecate discovery and use stored info
  411. #NOTE(justinsb): Discovery won't work with CHAP-secured targets (?)
  412. LOG.warn(_("ISCSI provider_location not stored, using discovery"))
  413. volume_name = volume['name']
  414. (out, _err) = self._execute('iscsiadm', '-m', 'discovery',
  415. '-t', 'sendtargets', '-p', volume['host'],
  416. run_as_root=True)
  417. for target in out.splitlines():
  418. if (self.configuration.iscsi_ip_address in target
  419. and volume_name in target):
  420. return target
  421. return None
  422. def _get_iscsi_properties(self, volume):
  423. """Gets iscsi configuration
  424. We ideally get saved information in the volume entity, but fall back
  425. to discovery if need be. Discovery may be completely removed in future
  426. The properties are:
  427. :target_discovered: boolean indicating whether discovery was used
  428. :target_iqn: the IQN of the iSCSI target
  429. :target_portal: the portal of the iSCSI target
  430. :target_lun: the lun of the iSCSI target
  431. :volume_id: the id of the volume (currently used by xen)
  432. :auth_method:, :auth_username:, :auth_password:
  433. the authentication details. Right now, either auth_method is not
  434. present meaning no authentication, or auth_method == `CHAP`
  435. meaning use CHAP with the specified credentials.
  436. :access_mode: the volume access mode allow client used
  437. ('rw' or 'ro' currently supported)
  438. """
  439. properties = {}
  440. location = volume['provider_location']
  441. if location:
  442. # provider_location is the same format as iSCSI discovery output
  443. properties['target_discovered'] = False
  444. else:
  445. location = self._do_iscsi_discovery(volume)
  446. if not location:
  447. msg = (_("Could not find iSCSI export for volume %s") %
  448. (volume['name']))
  449. raise exception.InvalidVolume(reason=msg)
  450. LOG.debug(_("ISCSI Discovery: Found %s") % (location))
  451. properties['target_discovered'] = True
  452. results = location.split(" ")
  453. properties['target_portal'] = results[0].split(",")[0]
  454. properties['target_iqn'] = results[1]
  455. try:
  456. properties['target_lun'] = int(results[2])
  457. except (IndexError, ValueError):
  458. if (self.configuration.volume_driver in
  459. ['cinder.volume.drivers.lvm.LVMISCSIDriver',
  460. 'cinder.volume.drivers.lvm.ThinLVMVolumeDriver'] and
  461. self.configuration.iscsi_helper == 'tgtadm'):
  462. properties['target_lun'] = 1
  463. else:
  464. properties['target_lun'] = 0
  465. properties['volume_id'] = volume['id']
  466. auth = volume['provider_auth']
  467. if auth:
  468. (auth_method, auth_username, auth_secret) = auth.split()
  469. properties['auth_method'] = auth_method
  470. properties['auth_username'] = auth_username
  471. properties['auth_password'] = auth_secret
  472. geometry = volume.get('provider_geometry', None)
  473. if geometry:
  474. (physical_block_size, logical_block_size) = geometry.split()
  475. properties['physical_block_size'] = physical_block_size
  476. properties['logical_block_size'] = logical_block_size
  477. encryption_key_id = volume.get('encryption_key_id', None)
  478. properties['encrypted'] = encryption_key_id is not None
  479. return properties
  480. def _run_iscsiadm(self, iscsi_properties, iscsi_command, **kwargs):
  481. check_exit_code = kwargs.pop('check_exit_code', 0)
  482. (out, err) = self._execute('iscsiadm', '-m', 'node', '-T',
  483. iscsi_properties['target_iqn'],
  484. '-p', iscsi_properties['target_portal'],
  485. *iscsi_command, run_as_root=True,
  486. check_exit_code=check_exit_code)
  487. LOG.debug("iscsiadm %s: stdout=%s stderr=%s" %
  488. (iscsi_command, out, err))
  489. return (out, err)
  490. def _run_iscsiadm_bare(self, iscsi_command, **kwargs):
  491. check_exit_code = kwargs.pop('check_exit_code', 0)
  492. (out, err) = self._execute('iscsiadm',
  493. *iscsi_command,
  494. run_as_root=True,
  495. check_exit_code=check_exit_code)
  496. LOG.debug("iscsiadm %s: stdout=%s stderr=%s" %
  497. (iscsi_command, out, err))
  498. return (out, err)
  499. def _iscsiadm_update(self, iscsi_properties, property_key, property_value,
  500. **kwargs):
  501. iscsi_command = ('--op', 'update', '-n', property_key,
  502. '-v', property_value)
  503. return self._run_iscsiadm(iscsi_properties, iscsi_command, **kwargs)
  504. def initialize_connection(self, volume, connector):
  505. """Initializes the connection and returns connection info.
  506. The iscsi driver returns a driver_volume_type of 'iscsi'.
  507. The format of the driver data is defined in _get_iscsi_properties.
  508. Example return value::
  509. {
  510. 'driver_volume_type': 'iscsi'
  511. 'data': {
  512. 'target_discovered': True,
  513. 'target_iqn': 'iqn.2010-10.org.openstack:volume-00000001',
  514. 'target_portal': '127.0.0.0.1:3260',
  515. 'volume_id': 1,
  516. 'access_mode': 'rw'
  517. }
  518. }
  519. """
  520. if CONF.iscsi_helper == 'lioadm':
  521. self.tgtadm.initialize_connection(volume, connector)
  522. iscsi_properties = self._get_iscsi_properties(volume)
  523. return {
  524. 'driver_volume_type': 'iscsi',
  525. 'data': iscsi_properties
  526. }
  527. def validate_connector(self, connector):
  528. # iSCSI drivers require the initiator information
  529. if 'initiator' not in connector:
  530. err_msg = (_('The volume driver requires the iSCSI initiator '
  531. 'name in the connector.'))
  532. LOG.error(err_msg)
  533. raise exception.VolumeBackendAPIException(data=err_msg)
  534. def terminate_connection(self, volume, connector, **kwargs):
  535. pass
  536. def _get_iscsi_initiator(self):
  537. """Get iscsi initiator name for this machine"""
  538. # NOTE openiscsi stores initiator name in a file that
  539. # needs root permission to read.
  540. contents = utils.read_file_as_root('/etc/iscsi/initiatorname.iscsi')
  541. for l in contents.split('\n'):
  542. if l.startswith('InitiatorName='):
  543. return l[l.index('=') + 1:].strip()
  544. def get_volume_stats(self, refresh=False):
  545. """Get volume stats.
  546. If 'refresh' is True, run update the stats first.
  547. """
  548. if refresh:
  549. self._update_volume_stats()
  550. return self._stats
  551. def _update_volume_stats(self):
  552. """Retrieve stats info from volume group."""
  553. LOG.debug(_("Updating volume stats"))
  554. data = {}
  555. backend_name = self.configuration.safe_get('volume_backend_name')
  556. data["volume_backend_name"] = backend_name or 'Generic_iSCSI'
  557. data["vendor_name"] = 'Open Source'
  558. data["driver_version"] = '1.0'
  559. data["storage_protocol"] = 'iSCSI'
  560. data['total_capacity_gb'] = 'infinite'
  561. data['free_capacity_gb'] = 'infinite'
  562. data['reserved_percentage'] = 100
  563. data['QoS_support'] = False
  564. self._stats = data
  565. def accept_transfer(self, context, volume, new_user, new_project):
  566. pass
  567. def get_target_admin(self):
  568. root_helper = utils.get_root_helper()
  569. if CONF.iscsi_helper == 'tgtadm':
  570. return iscsi.TgtAdm(root_helper,
  571. CONF.volumes_dir,
  572. CONF.iscsi_target_prefix)
  573. elif CONF.iscsi_helper == 'fake':
  574. return iscsi.FakeIscsiHelper()
  575. elif CONF.iscsi_helper == 'lioadm':
  576. return iscsi.LioAdm(root_helper,
  577. CONF.lio_initiator_iqns,
  578. CONF.iscsi_target_prefix)
  579. else:
  580. return iscsi.IetAdm(root_helper, CONF.iet_conf, CONF.iscsi_iotype)
  581. class FakeISCSIDriver(ISCSIDriver):
  582. """Logs calls instead of executing."""
  583. def __init__(self, *args, **kwargs):
  584. super(FakeISCSIDriver, self).__init__(execute=self.fake_execute,
  585. *args, **kwargs)
  586. def create_volume(self, volume):
  587. pass
  588. def check_for_setup_error(self):
  589. """No setup necessary in fake mode."""
  590. pass
  591. def initialize_connection(self, volume, connector):
  592. return {
  593. 'driver_volume_type': 'iscsi',
  594. 'data': {'access_mode': 'rw'}
  595. }
  596. def terminate_connection(self, volume, connector, **kwargs):
  597. pass
  598. @staticmethod
  599. def fake_execute(cmd, *_args, **_kwargs):
  600. """Execute that simply logs the command."""
  601. LOG.debug(_("FAKE ISCSI: %s"), cmd)
  602. return (None, None)
  603. class ISERDriver(ISCSIDriver):
  604. """Executes commands relating to ISER volumes.
  605. We make use of model provider properties as follows:
  606. ``provider_location``
  607. if present, contains the iSER target information in the same
  608. format as an ietadm discovery
  609. i.e. '<ip>:<port>,<portal> <target IQN>'
  610. ``provider_auth``
  611. if present, contains a space-separated triple:
  612. '<auth method> <auth username> <auth password>'.
  613. `CHAP` is the only auth_method in use at the moment.
  614. """
  615. def __init__(self, *args, **kwargs):
  616. super(ISERDriver, self).__init__(*args, **kwargs)
  617. def _do_iser_discovery(self, volume):
  618. LOG.warn(_("ISER provider_location not stored, using discovery"))
  619. volume_name = volume['name']
  620. (out, _err) = self._execute('iscsiadm', '-m', 'discovery',
  621. '-t', 'sendtargets', '-p', volume['host'],
  622. run_as_root=True)
  623. for target in out.splitlines():
  624. if (self.configuration.iser_ip_address in target
  625. and volume_name in target):
  626. return target
  627. return None
  628. def _get_iser_properties(self, volume):
  629. """Gets iser configuration
  630. We ideally get saved information in the volume entity, but fall back
  631. to discovery if need be. Discovery may be completely removed in future
  632. The properties are:
  633. :target_discovered: boolean indicating whether discovery was used
  634. :target_iqn: the IQN of the iSER target
  635. :target_portal: the portal of the iSER target
  636. :target_lun: the lun of the iSER target
  637. :volume_id: the id of the volume (currently used by xen)
  638. :auth_method:, :auth_username:, :auth_password:
  639. the authentication details. Right now, either auth_method is not
  640. present meaning no authentication, or auth_method == `CHAP`
  641. meaning use CHAP with the specified credentials.
  642. """
  643. properties = {}
  644. location = volume['provider_location']
  645. if location:
  646. # provider_location is the same format as iSER discovery output
  647. properties['target_discovered'] = False
  648. else:
  649. location = self._do_iser_discovery(volume)
  650. if not location:
  651. msg = (_("Could not find iSER export for volume %s") %
  652. (volume['name']))
  653. raise exception.InvalidVolume(reason=msg)
  654. LOG.debug(_("ISER Discovery: Found %s") % (location))
  655. properties['target_discovered'] = True
  656. results = location.split(" ")
  657. properties['target_portal'] = results[0].split(",")[0]
  658. properties['target_iqn'] = results[1]
  659. try:
  660. properties['target_lun'] = int(results[2])
  661. except (IndexError, ValueError):
  662. if (self.configuration.volume_driver in
  663. ['cinder.volume.drivers.lvm.LVMISERDriver',
  664. 'cinder.volume.drivers.lvm.ThinLVMVolumeDriver'] and
  665. self.configuration.iser_helper == 'tgtadm'):
  666. properties['target_lun'] = 1
  667. else:
  668. properties['target_lun'] = 0
  669. properties['volume_id'] = volume['id']
  670. auth = volume['provider_auth']
  671. if auth:
  672. (auth_method, auth_username, auth_secret) = auth.split()
  673. properties['auth_method'] = auth_method
  674. properties['auth_username'] = auth_username
  675. properties['auth_password'] = auth_secret
  676. return properties
  677. def initialize_connection(self, volume, connector):
  678. """Initializes the connection and returns connection info.
  679. The iser driver returns a driver_volume_type of 'iser'.
  680. The format of the driver data is defined in _get_iser_properties.
  681. Example return value::
  682. {
  683. 'driver_volume_type': 'iser'
  684. 'data': {
  685. 'target_discovered': True,
  686. 'target_iqn':
  687. 'iqn.2010-10.org.iser.openstack:volume-00000001',
  688. 'target_portal': '127.0.0.0.1:3260',
  689. 'volume_id': 1,
  690. }
  691. }
  692. """
  693. iser_properties = self._get_iser_properties(volume)
  694. return {
  695. 'driver_volume_type': 'iser',
  696. 'data': iser_properties
  697. }
  698. def _check_valid_device(self, path):
  699. cmd = ('dd', 'if=%(path)s' % {"path": path},
  700. 'of=/dev/null', 'count=1')
  701. out, info = None, None
  702. try:
  703. out, info = self._execute(*cmd, run_as_root=True)
  704. except processutils.ProcessExecutionError as e:
  705. LOG.error(_("Failed to access the device on the path "
  706. "%(path)s: %(error)s.") %
  707. {"path": path, "error": e.stderr})
  708. return False
  709. # If the info is none, the path does not exist.
  710. if info is None:
  711. return False
  712. return True
  713. def _attach_volume(self, context, volume, connector):
  714. """Attach the volume."""
  715. iser_properties = None
  716. host_device = None
  717. init_conn = self.initialize_connection(volume, connector)
  718. iser_properties = init_conn['data']
  719. # code "inspired by" nova/virt/libvirt/volume.py
  720. try:
  721. self._run_iscsiadm(iser_properties, ())
  722. except processutils.ProcessExecutionError as exc:
  723. # iscsiadm returns 21 for "No records found" after version 2.0-871
  724. if exc.exit_code in [21, 255]:
  725. self._run_iscsiadm(iser_properties, ('--op', 'new'))
  726. else:
  727. raise
  728. if iser_properties.get('auth_method'):
  729. self._iscsiadm_update(iser_properties,
  730. "node.session.auth.authmethod",
  731. iser_properties['auth_method'])
  732. self._iscsiadm_update(iser_properties,
  733. "node.session.auth.username",
  734. iser_properties['auth_username'])
  735. self._iscsiadm_update(iser_properties,
  736. "node.session.auth.password",
  737. iser_properties['auth_password'])
  738. host_device = ("/dev/disk/by-path/ip-%s-iser-%s-lun-%s" %
  739. (iser_properties['target_portal'],
  740. iser_properties['target_iqn'],
  741. iser_properties.get('target_lun', 0)))
  742. out = self._run_iscsiadm_bare(["-m", "session"],
  743. run_as_root=True,
  744. check_exit_code=[0, 1, 21])[0] or ""
  745. portals = [{'portal': p.split(" ")[2], 'iqn': p.split(" ")[3]}
  746. for p in out.splitlines() if p.startswith("iser:")]
  747. stripped_portal = iser_properties['target_portal'].split(",")[0]
  748. length_iqn = [s for s in portals
  749. if stripped_portal ==
  750. s['portal'].split(",")[0] and
  751. s['iqn'] == iser_properties['target_iqn']]
  752. if len(portals) == 0 or len(length_iqn) == 0:
  753. try:
  754. self._run_iscsiadm(iser_properties, ("--login",),
  755. check_exit_code=[0, 255])
  756. except processutils.ProcessExecutionError as err:
  757. if err.exit_code in [15]:
  758. self._iscsiadm_update(iser_properties,
  759. "node.startup",
  760. "automatic")
  761. return iser_properties, host_device
  762. else:
  763. raise
  764. self._iscsiadm_update(iser_properties,
  765. "node.startup", "automatic")
  766. tries = 0
  767. while not os.path.exists(host_device):
  768. if tries >= self.configuration.num_iser_scan_tries:
  769. raise exception.CinderException(_("iSER device "
  770. "not found "
  771. "at %s") % (host_device))
  772. LOG.warn(_("ISER volume not yet found at: %(host_device)s. "
  773. "Will rescan & retry. Try number: %(tries)s.") %
  774. {'host_device': host_device, 'tries': tries})
  775. # The rescan isn't documented as being necessary(?),
  776. # but it helps
  777. self._run_iscsiadm(iser_properties, ("--rescan",))
  778. tries = tries + 1
  779. if not os.path.exists(host_device):
  780. time.sleep(tries ** 2)
  781. if tries != 0:
  782. LOG.debug(_("Found iSER node %(host_device)s "
  783. "(after %(tries)s rescans).") %
  784. {'host_device': host_device,
  785. 'tries': tries})
  786. if not self._check_valid_device(host_device):
  787. raise exception.DeviceUnavailable(path=host_device,
  788. reason=(_("Unable to access "
  789. "the backend storage "
  790. "via the path "
  791. "%(path)s.") %
  792. {'path': host_device}))
  793. return iser_properties, host_device
  794. def _update_volume_status(self):
  795. """Retrieve status info from volume group."""
  796. LOG.debug(_("Updating volume status"))
  797. data = {}
  798. backend_name = self.configuration.safe_get('volume_backend_name')
  799. data["volume_backend_name"] = backend_name or 'Generic_iSER'
  800. data["vendor_name"] = 'Open Source'
  801. data["driver_version"] = '1.0'
  802. data["storage_protocol"] = 'iSER'
  803. data['total_capacity_gb'] = 'infinite'
  804. data['free_capacity_gb'] = 'infinite'
  805. data['reserved_percentage'] = 100
  806. data['QoS_support'] = False
  807. self._stats = data
  808. def get_target_admin(self):
  809. root_helper = utils.get_root_helper()
  810. if CONF.iser_helper == 'fake':
  811. return iser.FakeIserHelper()
  812. else:
  813. return iser.TgtAdm(root_helper,
  814. CONF.volumes_dir)
  815. class FakeISERDriver(FakeISCSIDriver):
  816. """Logs calls instead of executing."""
  817. def __init__(self, *args, **kwargs):
  818. super(FakeISERDriver, self).__init__(execute=self.fake_execute,
  819. *args, **kwargs)
  820. def initialize_connection(self, volume, connector):
  821. return {
  822. 'driver_volume_type': 'iser',
  823. 'data': {}
  824. }
  825. @staticmethod
  826. def fake_execute(cmd, *_args, **_kwargs):
  827. """Execute that simply logs the command."""
  828. LOG.debug(_("FAKE ISER: %s"), cmd)
  829. return (None, None)
  830. class FibreChannelDriver(VolumeDriver):
  831. """Executes commands relating to Fibre Channel volumes."""
  832. def __init__(self, *args, **kwargs):
  833. super(FibreChannelDriver, self).__init__(*args, **kwargs)
  834. def initialize_connection(self, volume, connector):
  835. """Initializes the connection and returns connection info.
  836. The driver returns a driver_volume_type of 'fibre_channel'.
  837. The target_wwn can be a single entry or a list of wwns that
  838. correspond to the list of remote wwn(s) that will export the volume.
  839. Example return values:
  840. {
  841. 'driver_volume_type': 'fibre_channel'
  842. 'data': {
  843. 'target_discovered': True,
  844. 'target_lun': 1,
  845. 'target_wwn': '1234567890123',
  846. 'access_mode': 'rw'
  847. }
  848. }
  849. or
  850. {
  851. 'driver_volume_type': 'fibre_channel'
  852. 'data': {
  853. 'target_discovered': True,
  854. 'target_lun': 1,
  855. 'target_wwn': ['1234567890123', '0987654321321'],
  856. 'access_mode': 'rw'
  857. }
  858. }
  859. """
  860. msg = _("Driver must implement initialize_connection")
  861. raise NotImplementedError(msg)