blob: 5571ce516a7fda60e0d8dd136b472f5b041aa645 [file] [log] [blame]
Peter Pentchevea354462023-07-18 11:15:56 +03001From 6e24ec90deb5e5977a4654c0e9f7f02e99ddb131 Mon Sep 17 00:00:00 2001
Peter Pentchev9c24be92022-09-26 22:35:24 +03002From: Peter Penchev <openstack-dev@storpool.com>
3Date: Mon, 12 Mar 2018 12:00:10 +0200
Peter Pentchevacaaa382023-02-28 11:26:13 +02004Subject: [PATCH 10/10] Add iSCSI export support to the StorPool driver
Peter Pentchev9c24be92022-09-26 22:35:24 +03005
6Add four new driver options:
7- iscsi_cinder_volume: use StorPool iSCSI attachments whenever
8 the cinder-volume service needs to attach a volume to the controller,
9 e.g. for copying an image to a volume or vice versa
10- iscsi_export_to:
11 - an empty string to use the StorPool native protocol for exporting volumes
12 protocol for exporting volumes)
13 - the string "*" to always use iSCSI for exporting volumes
14 - an experimental, not fully supported list of IQN patterns to export
15 volumes to using iSCSI; this results in a Cinder driver that exports
16 different volumes using different storage protocols
17- iscsi_portal_group: the name of the iSCSI portal group defined in
18 the StorPool configuration to use for these export
19- iscsi_learn_initiator_iqns: automatically create StorPool configuration
20 records for an initiator when a volume is first exported to it
21
22When exporting volumes via iSCSI, report the storage protocol as "iSCSI" and
23disable multiattach (the StorPool CI failures with iSCSI multiattach may need
24further investigation).
25
26Change-Id: I9de64306e0e6976268df782053b0651dd1cca96f
27---
Peter Pentchevea354462023-07-18 11:15:56 +030028 .../unit/volume/drivers/test_storpool.py | 435 +++++++++++++++++-
29 cinder/volume/drivers/storpool.py | 374 ++++++++++++++-
30 .../drivers/storpool-volume-driver.rst | 60 ++-
31 .../storpool-iscsi-cefcfe590a07c5c7.yaml | 10 +
32 4 files changed, 870 insertions(+), 9 deletions(-)
33 create mode 100644 releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml
Peter Pentchev9c24be92022-09-26 22:35:24 +030034
Peter Pentchevacaaa382023-02-28 11:26:13 +020035diff --git a/cinder/tests/unit/volume/drivers/test_storpool.py b/cinder/tests/unit/volume/drivers/test_storpool.py
Peter Pentchevea354462023-07-18 11:15:56 +030036index 95a1ffffd..842790ab4 100644
Peter Pentchev9c24be92022-09-26 22:35:24 +030037--- a/cinder/tests/unit/volume/drivers/test_storpool.py
38+++ b/cinder/tests/unit/volume/drivers/test_storpool.py
Peter Pentchevea354462023-07-18 11:15:56 +030039@@ -14,15 +14,25 @@
40 # under the License.
41
42
43+from __future__ import annotations
44+
45+import dataclasses
46 import itertools
47 import re
48 import sys
49+from typing import Any, NamedTuple, TYPE_CHECKING # noqa: H301
50 from unittest import mock
51
52 import ddt
53 from oslo_utils import units
54 import six
55
56+if TYPE_CHECKING:
57+ if sys.version_info >= (3, 11):
58+ from typing import Self
59+ else:
60+ from typing_extensions import Self
61+
62
63 fakeStorPool = mock.Mock()
64 fakeStorPool.spopenstack = mock.Mock()
65@@ -32,12 +42,21 @@ fakeStorPool.sptypes = mock.Mock()
Peter Pentchev9c24be92022-09-26 22:35:24 +030066 sys.modules['storpool'] = fakeStorPool
67
68
69+from cinder.common import constants
70 from cinder import exception
Peter Pentchevea354462023-07-18 11:15:56 +030071+from cinder.tests.unit import fake_constants as fconst
Peter Pentchev9c24be92022-09-26 22:35:24 +030072 from cinder.tests.unit import test
73 from cinder.volume import configuration as conf
Peter Pentchevea354462023-07-18 11:15:56 +030074 from cinder.volume.drivers import storpool as driver
75
76
77+_ISCSI_IQN_OURS = 'beleriand'
78+_ISCSI_IQN_OTHER = 'rohan'
79+_ISCSI_IQN_THIRD = 'gondor'
80+_ISCSI_PAT_OTHER = 'roh*'
81+_ISCSI_PAT_BOTH = '*riand roh*'
82+_ISCSI_PORTAL_GROUP = 'openstack_pg'
83+
84 volume_types = {
85 1: {},
86 2: {'storpool_template': 'ssd'},
87@@ -71,6 +90,10 @@ def snapshotName(vtype, vid):
88 return 'os--snap--{t}--{id}'.format(t=vtype, id=vid)
89
90
91+def targetName(vid):
92+ return 'iqn.2012-11.storpool:{id}'.format(id=vid)
93+
94+
95 class MockDisk(object):
96 def __init__(self, diskId):
97 self.id = diskId
98@@ -181,6 +204,273 @@ def MockVolumeUpdateDesc(size):
99 return {'size': size}
100
101
102+@dataclasses.dataclass(frozen=True)
103+class MockIscsiNetwork:
104+ """Mock a StorPool IP CIDR network definition (partially)."""
105+
106+ address: str
107+
108+
109+@dataclasses.dataclass(frozen=True)
110+class MockIscsiPortalGroup:
111+ """Mock a StorPool iSCSI portal group definition (partially)."""
112+
113+ name: str
114+ networks: list[MockIscsiNetwork]
115+
116+
117+@dataclasses.dataclass(frozen=True)
118+class MockIscsiExport:
119+ """Mock a StorPool iSCSI exported volume/target definition."""
120+
121+ portalGroup: str
122+ target: str
123+
124+
125+@dataclasses.dataclass(frozen=True)
126+class MockIscsiInitiator:
127+ """Mock a StorPool iSCSI initiator definition."""
128+
129+ name: str
130+ exports: list[MockIscsiExport]
131+
132+
133+@dataclasses.dataclass(frozen=True)
134+class MockIscsiTarget:
135+ """Mock a StorPool iSCSI volume-to-target mapping definition."""
136+
137+ name: str
138+ volume: str
139+
140+
141+class IscsiTestCase(NamedTuple):
142+ """A single test case for the iSCSI config and export methods."""
143+
144+ initiator: str | None
145+ volume: str | None
146+ exported: bool
147+ commands_count: int
148+
149+
150+@dataclasses.dataclass(frozen=True)
151+class MockIscsiConfig:
152+ """Mock the structure returned by the "get current config" query."""
153+
154+ portalGroups: dict[str, MockIscsiPortalGroup]
155+ initiators: dict[str, MockIscsiInitiator]
156+ targets: dict[str, MockIscsiTarget]
157+
158+ @classmethod
159+ def build(cls, tcase: IscsiTestCase) -> Self:
160+ """Build a test config structure."""
161+ initiators = {
162+ '0': MockIscsiInitiator(name=_ISCSI_IQN_OTHER, exports=[]),
163+ }
164+ if tcase.initiator is not None:
165+ initiators['1'] = MockIscsiInitiator(
166+ name=tcase.initiator,
167+ exports=(
168+ [
169+ MockIscsiExport(
170+ portalGroup=_ISCSI_PORTAL_GROUP,
171+ target=targetName(tcase.volume),
172+ ),
173+ ]
174+ if tcase.exported
175+ else []
176+ ),
177+ )
178+
179+ targets = {
180+ '0': MockIscsiTarget(
181+ name=targetName(fconst.VOLUME2_ID),
182+ volume=volumeName(fconst.VOLUME2_ID),
183+ ),
184+ }
185+ if tcase.volume is not None:
186+ targets['1'] = MockIscsiTarget(
187+ name=targetName(tcase.volume),
188+ volume=volumeName(tcase.volume),
189+ )
190+
191+ return cls(
192+ portalGroups={
193+ '0': MockIscsiPortalGroup(
194+ name=_ISCSI_PORTAL_GROUP + '-not',
195+ networks=[],
196+ ),
197+ '1': MockIscsiPortalGroup(
198+ name=_ISCSI_PORTAL_GROUP,
199+ networks=[
200+ MockIscsiNetwork(address="192.0.2.0"),
201+ MockIscsiNetwork(address="195.51.100.0"),
202+ ],
203+ ),
204+ },
205+ initiators=initiators,
206+ targets=targets,
207+ )
208+
209+
210+@dataclasses.dataclass(frozen=True)
211+class MockIscsiConfigTop:
212+ """Mock the top level of the "get the iSCSI configuration" response."""
213+
214+ iscsi: MockIscsiConfig
215+
216+
217+class MockIscsiAPI:
218+ """Mock only the iSCSI-related calls of the StorPool API bindings."""
219+
220+ _asrt: test.TestCase
221+ _configs: list[MockIscsiConfig]
222+
223+ def __init__(
224+ self,
225+ configs: list[MockIscsiConfig],
226+ asrt: test.TestCase,
227+ ) -> None:
228+ """Store the reference to the list of iSCSI config objects."""
229+ self._asrt = asrt
230+ self._configs = configs
231+
232+ def iSCSIConfig(self) -> MockIscsiConfigTop:
233+ """Return the last version of the iSCSI configuration."""
234+ return MockIscsiConfigTop(iscsi=self._configs[-1])
235+
236+ def _handle_export(
237+ self,
238+ cfg: MockIscsiConfig, cmd: dict[str, Any],
239+ ) -> MockIscsiConfig:
240+ """Add an export for an initiator."""
241+ self._asrt.assertDictEqual(
242+ cmd,
243+ {
244+ 'initiator': _ISCSI_IQN_OURS,
245+ 'portalGroup': _ISCSI_PORTAL_GROUP,
246+ 'volumeName': volumeName(fconst.VOLUME_ID),
247+ },
248+ )
249+ self._asrt.assertEqual(cfg.initiators['1'].name, cmd['initiator'])
250+ self._asrt.assertListEqual(cfg.initiators['1'].exports, [])
251+
252+ return dataclasses.replace(
253+ cfg,
254+ initiators={
255+ **cfg.initiators,
256+ '1': dataclasses.replace(
257+ cfg.initiators['1'],
258+ exports=[
259+ MockIscsiExport(
260+ portalGroup=cmd['portalGroup'],
261+ target=targetName(fconst.VOLUME_ID),
262+ ),
263+ ],
264+ ),
265+ },
266+ )
267+
268+ def _handle_create_initiator(
269+ self,
270+ cfg: MockIscsiConfig,
271+ cmd: dict[str, Any],
272+ ) -> MockIscsiConfig:
273+ """Add a whole new initiator."""
274+ self._asrt.assertDictEqual(
275+ cmd,
276+ {
277+ 'name': _ISCSI_IQN_OURS,
278+ 'username': '',
279+ 'secret': '',
280+ },
281+ )
282+ self._asrt.assertNotIn(
283+ cmd['name'],
284+ [init.name for init in cfg.initiators.values()],
285+ )
286+ self._asrt.assertListEqual(sorted(cfg.initiators), ['0'])
287+
288+ return dataclasses.replace(
289+ cfg,
290+ initiators={
291+ **cfg.initiators,
292+ '1': MockIscsiInitiator(name=cmd['name'], exports=[]),
293+ },
294+ )
295+
296+ def _handle_create_target(
297+ self,
298+ cfg: MockIscsiConfig,
299+ cmd: dict[str, Any],
300+ ) -> MockIscsiConfig:
301+ """Add a target for a volume so that it may be exported."""
302+ self._asrt.assertDictEqual(
303+ cmd,
304+ {'volumeName': volumeName(fconst.VOLUME_ID)},
305+ )
306+ self._asrt.assertListEqual(sorted(cfg.targets), ['0'])
307+ return dataclasses.replace(
308+ cfg,
309+ targets={
310+ **cfg.targets,
311+ '1': MockIscsiTarget(
312+ name=targetName(fconst.VOLUME_ID),
313+ volume=volumeName(fconst.VOLUME_ID),
314+ ),
315+ },
316+ )
317+
318+ def _handle_initiator_add_network(
319+ self,
320+ cfg: MockIscsiConfig,
321+ cmd: dict[str, Any],
322+ ) -> MockIscsiConfig:
323+ """Add a network that an initiator is allowed to log in from."""
324+ self._asrt.assertDictEqual(
325+ cmd,
326+ {
327+ 'initiator': _ISCSI_IQN_OURS,
328+ 'net': '0.0.0.0/0',
329+ },
330+ )
331+ return dataclasses.replace(cfg)
332+
333+ _CMD_HANDLERS = {
334+ 'createInitiator': _handle_create_initiator,
335+ 'createTarget': _handle_create_target,
336+ 'export': _handle_export,
337+ 'initiatorAddNetwork': _handle_initiator_add_network,
338+ }
339+
340+ def iSCSIConfigChange(
341+ self,
342+ commands: dict[str, list[dict[str, dict[str, Any]]]],
343+ ) -> None:
344+ """Apply the requested changes to the iSCSI configuration.
345+
346+ This method adds a new config object to the configs list,
347+ making a shallow copy of the last one and applying the changes
348+ specified in the list of commands.
349+ """
350+ self._asrt.assertListEqual(sorted(commands), ['commands'])
351+ self._asrt.assertGreater(len(commands['commands']), 0)
352+ for cmd in commands['commands']:
353+ keys = sorted(cmd.keys())
354+ cmd_name = keys[0]
355+ self._asrt.assertListEqual(keys, [cmd_name])
356+ handler = self._CMD_HANDLERS[cmd_name]
357+ new_cfg = handler(self, self._configs[-1], cmd[cmd_name])
358+ self._configs.append(new_cfg)
359+
360+
361+_ISCSI_TEST_CASES = [
362+ IscsiTestCase(None, None, False, 4),
363+ IscsiTestCase(_ISCSI_IQN_OURS, None, False, 2),
364+ IscsiTestCase(_ISCSI_IQN_OURS, fconst.VOLUME_ID, False, 1),
365+ IscsiTestCase(_ISCSI_IQN_OURS, fconst.VOLUME_ID, True, 0),
366+]
367+
368+
369 def MockSPConfig(section = 's01'):
370 res = {}
371 m = re.match('^s0*([A-Za-z0-9]+)$', section)
372@@ -222,7 +512,15 @@ class StorPoolTestCase(test.TestCase):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300373 self.cfg.volume_backend_name = 'storpool_test'
374 self.cfg.storpool_template = None
375 self.cfg.storpool_replication = 3
376+ self.cfg.iscsi_cinder_volume = False
377+ self.cfg.iscsi_export_to = ''
Peter Pentchevea354462023-07-18 11:15:56 +0300378+ self.cfg.iscsi_learn_initiator_iqns = True
379+ self.cfg.iscsi_portal_group = _ISCSI_PORTAL_GROUP
Peter Pentchev9c24be92022-09-26 22:35:24 +0300380+
Peter Pentchevea354462023-07-18 11:15:56 +0300381+ self._setup_test_driver()
382
Peter Pentchev9c24be92022-09-26 22:35:24 +0300383+ def _setup_test_driver(self):
384+ """Initialize a StorPool driver as per the current configuration."""
385 mock_exec = mock.Mock()
386 mock_exec.return_value = ('', '')
387
Peter Pentchevea354462023-07-18 11:15:56 +0300388@@ -231,7 +529,7 @@ class StorPoolTestCase(test.TestCase):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300389 self.driver.check_for_setup_error()
390
391 @ddt.data(
392- (5, TypeError),
393+ (5, (TypeError, AttributeError)),
394 ({'no-host': None}, KeyError),
395 ({'host': 'sbad'}, driver.StorPoolConfigurationInvalid),
396 ({'host': 's01'}, None),
Peter Pentchevea354462023-07-18 11:15:56 +0300397@@ -247,7 +545,7 @@ class StorPoolTestCase(test.TestCase):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300398 conn)
399
400 @ddt.data(
401- (5, TypeError),
402+ (5, (TypeError, AttributeError)),
403 ({'no-host': None}, KeyError),
404 ({'host': 'sbad'}, driver.StorPoolConfigurationInvalid),
405 )
Peter Pentchevea354462023-07-18 11:15:56 +0300406@@ -644,3 +942,136 @@ class StorPoolTestCase(test.TestCase):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300407 self.driver.get_pool({
408 'volume_type': volume_type
409 }))
410+
411+ @ddt.data(
412+ # The default values
Peter Pentchevea354462023-07-18 11:15:56 +0300413+ ('', False, constants.STORPOOL, _ISCSI_IQN_OURS, False),
Peter Pentchev9c24be92022-09-26 22:35:24 +0300414+
415+ # Export to all
Peter Pentchevea354462023-07-18 11:15:56 +0300416+ ('*', True, constants.ISCSI, _ISCSI_IQN_OURS, True),
417+ ('*', True, constants.ISCSI, _ISCSI_IQN_OURS, True),
Peter Pentchev9c24be92022-09-26 22:35:24 +0300418+
419+ # Only export to the controller
Peter Pentchevea354462023-07-18 11:15:56 +0300420+ ('', False, constants.STORPOOL, _ISCSI_IQN_OURS, False),
Peter Pentchev9c24be92022-09-26 22:35:24 +0300421+
422+ # Some of the not-fully-supported pattern lists
Peter Pentchevea354462023-07-18 11:15:56 +0300423+ (_ISCSI_PAT_OTHER, False, constants.STORPOOL, _ISCSI_IQN_OURS, False),
424+ (_ISCSI_PAT_OTHER, False, constants.STORPOOL, _ISCSI_IQN_OTHER, True),
425+ (_ISCSI_PAT_BOTH, False, constants.STORPOOL, _ISCSI_IQN_OURS, True),
426+ (_ISCSI_PAT_BOTH, False, constants.STORPOOL, _ISCSI_IQN_OTHER, True),
Peter Pentchev9c24be92022-09-26 22:35:24 +0300427+ )
428+ @ddt.unpack
429+ def test_wants_iscsi(self, iscsi_export_to, use_iscsi, storage_protocol,
430+ hostname, expected):
431+ """Check the "should this export use iSCSI?" detection."""
432+ self.cfg.iscsi_export_to = iscsi_export_to
433+ self._setup_test_driver()
434+ self.assertEqual(self.driver._use_iscsi, use_iscsi)
435+
436+ # Make sure the driver reports the correct protocol in the stats
437+ self.driver._update_volume_stats()
438+ self.assertEqual(self.driver._stats["vendor_name"], "StorPool")
439+ self.assertEqual(self.driver._stats["storage_protocol"],
440+ storage_protocol)
441+
442+ def check(conn, forced, expected):
443+ """Pass partially or completely valid connector info."""
444+ for initiator in (None, hostname):
Peter Pentchevea354462023-07-18 11:15:56 +0300445+ for host in (None, _ISCSI_IQN_THIRD):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300446+ self.assertEqual(
447+ self.driver._connector_wants_iscsi({
448+ "host": host,
449+ "initiator": initiator,
450+ **conn,
451+ }),
452+ expected if initiator is not None and host is not None
453+ else forced)
454+
455+ # If iscsi_cinder_volume is set and this is the controller, then yes.
456+ check({"storpool_wants_iscsi": True}, True, True)
457+
458+ # If iscsi_cinder_volume is not set or this is not the controller, then
459+ # look at the specified expected value.
460+ check({"storpool_wants_iscsi": False}, use_iscsi, expected)
461+ check({}, use_iscsi, expected)
Peter Pentchevea354462023-07-18 11:15:56 +0300462+
463+ def _validate_iscsi_config(
464+ self,
465+ cfg: MockIscsiConfig,
466+ res: dict[str, Any],
467+ tcase: IscsiTestCase,
468+ ) -> None:
469+ """Make sure the returned structure makes sense."""
470+ initiator = res['initiator']
471+ cfg_initiator = cfg.initiators.get('1')
472+
473+ self.assertIs(res['cfg'].iscsi, cfg)
474+ self.assertEqual(res['pg'].name, _ISCSI_PORTAL_GROUP)
475+
476+ if tcase.initiator is None:
477+ self.assertIsNone(initiator)
478+ else:
479+ self.assertIsNotNone(initiator)
480+ self.assertEqual(initiator, cfg_initiator)
481+
482+ if tcase.volume is None:
483+ self.assertIsNone(res['target'])
484+ else:
485+ self.assertIsNotNone(res['target'])
486+ self.assertEqual(res['target'], cfg.targets.get('1'))
487+
488+ if tcase.initiator is None:
489+ self.assertIsNone(cfg_initiator)
490+ self.assertIsNone(res['export'])
491+ else:
492+ self.assertIsNotNone(cfg_initiator)
493+ if tcase.exported:
494+ self.assertIsNotNone(res['export'])
495+ self.assertEqual(res['export'], cfg_initiator.exports[0])
496+ else:
497+ self.assertIsNone(res['export'])
498+
499+ @ddt.data(*_ISCSI_TEST_CASES)
500+ def test_iscsi_get_config(self, tcase: IscsiTestCase) -> None:
501+ """Make sure the StorPool iSCSI configuration is parsed correctly."""
502+ cfg_orig = MockIscsiConfig.build(tcase)
503+ configs = [cfg_orig]
504+ iapi = MockIscsiAPI(configs, self)
505+ with mock.patch.object(self.driver._attach, 'api', new=lambda: iapi):
506+ res = self.driver._get_iscsi_config(
507+ _ISCSI_IQN_OURS,
508+ fconst.VOLUME_ID,
509+ )
510+
511+ self._validate_iscsi_config(cfg_orig, res, tcase)
512+
513+ @ddt.data(*_ISCSI_TEST_CASES)
514+ def test_iscsi_create_export(self, tcase: IscsiTestCase) -> None:
515+ """Make sure _create_iscsi_export() makes the right API calls."""
516+ cfg_orig = MockIscsiConfig.build(tcase)
517+ configs = [cfg_orig]
518+ iapi = MockIscsiAPI(configs, self)
519+ with mock.patch.object(self.driver._attach, 'api', new=lambda: iapi):
520+ self.driver._create_iscsi_export(
521+ {
522+ 'id': fconst.VOLUME_ID,
523+ 'display_name': fconst.VOLUME_NAME,
524+ },
525+ {
526+ # Yeah, okay, so we cheat a little bit here...
527+ 'host': _ISCSI_IQN_OURS + '.hostname',
528+ 'initiator': _ISCSI_IQN_OURS,
529+ },
530+ )
531+
532+ self.assertEqual(len(configs), tcase.commands_count + 1)
533+ cfg_final = configs[-1]
534+ self.assertEqual(cfg_final.initiators['1'].name, _ISCSI_IQN_OURS)
535+ self.assertEqual(
536+ cfg_final.initiators['1'].exports[0].target,
537+ targetName(fconst.VOLUME_ID),
538+ )
539+ self.assertEqual(
540+ cfg_final.targets['1'].volume,
541+ volumeName(fconst.VOLUME_ID),
542+ )
Peter Pentchevacaaa382023-02-28 11:26:13 +0200543diff --git a/cinder/volume/drivers/storpool.py b/cinder/volume/drivers/storpool.py
Peter Pentchevea354462023-07-18 11:15:56 +0300544index b15a201c3..ba5aa10c3 100644
Peter Pentchev9c24be92022-09-26 22:35:24 +0300545--- a/cinder/volume/drivers/storpool.py
546+++ b/cinder/volume/drivers/storpool.py
547@@ -15,6 +15,7 @@
548
549 """StorPool block device driver"""
550
551+import fnmatch
552 import platform
553
554 from oslo_config import cfg
Peter Pentchevacaaa382023-02-28 11:26:13 +0200555@@ -44,6 +45,31 @@ if storpool:
Peter Pentchev9c24be92022-09-26 22:35:24 +0300556
557
558 storpool_opts = [
559+ cfg.BoolOpt('iscsi_cinder_volume',
560+ default=False,
561+ help='Let the cinder-volume service use iSCSI instead of '
562+ 'the StorPool block device driver for accessing '
563+ 'StorPool volumes, e.g. when creating a volume from '
564+ 'an image or vice versa.'),
565+ cfg.StrOpt('iscsi_export_to',
566+ default='',
567+ help='Whether to export volumes using iSCSI. '
568+ 'An empty string (the default) makes the driver export '
569+ 'all volumes using the StorPool native network protocol. '
570+ 'The value "*" makes the driver export all volumes using '
571+ 'iSCSI. '
572+ 'Any other value leads to an experimental not fully '
573+ 'supported configuration and is interpreted as '
574+ 'a whitespace-separated list of patterns for IQNs for '
575+ 'hosts that need volumes to be exported via iSCSI, e.g. '
576+ '"iqn.1991-05.com.microsoft:\\*" for Windows hosts.'),
577+ cfg.BoolOpt('iscsi_learn_initiator_iqns',
578+ default=True,
579+ help='Create a StorPool record for a new initiator as soon as '
580+ 'Cinder asks for a volume to be exported to it.'),
581+ cfg.StrOpt('iscsi_portal_group',
582+ default=None,
583+ help='The portal group to export volumes via iSCSI in.'),
584 cfg.StrOpt('storpool_template',
585 default=None,
586 help='The StorPool template for volumes with no type.'),
Peter Pentchevacaaa382023-02-28 11:26:13 +0200587@@ -105,6 +131,7 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300588 self._ourId = None
589 self._ourIdInt = None
590 self._attach = None
591+ self._use_iscsi = None
592
593 @staticmethod
594 def get_driver_options():
Peter Pentchevacaaa382023-02-28 11:26:13 +0200595@@ -162,10 +189,326 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300596 raise StorPoolConfigurationInvalid(
597 section=hostname, param='SP_OURID', error=e)
598
599+ def _connector_wants_iscsi(self, connector):
600+ """Should we do this export via iSCSI?
601+
602+ Check the configuration to determine whether this connector is
603+ expected to provide iSCSI exports as opposed to native StorPool
604+ protocol ones. Match the initiator's IQN against the list of
605+ patterns supplied in the "iscsi_export_to" configuration setting.
606+ """
607+ if connector is None:
608+ return False
609+ if self._use_iscsi:
610+ LOG.debug(' - forcing iSCSI for all exported volumes')
611+ return True
612+ if connector.get('storpool_wants_iscsi'):
613+ LOG.debug(' - forcing iSCSI for the controller')
614+ return True
615+
616+ try:
617+ iqn = connector.get('initiator')
618+ except Exception:
619+ iqn = None
620+ try:
621+ host = connector.get('host')
622+ except Exception:
623+ host = None
624+ if iqn is None or host is None:
625+ LOG.debug(' - this connector certainly does not want iSCSI')
626+ return False
627+
628+ LOG.debug(' - check whether %(host)s (%(iqn)s) wants iSCSI',
629+ {
630+ 'host': host,
631+ 'iqn': iqn,
632+ })
633+
634+ export_to = self.configuration.iscsi_export_to
635+ if export_to is None:
636+ return False
637+
638+ for pat in export_to.split():
639+ LOG.debug(' - matching against %(pat)s', {'pat': pat})
640+ if fnmatch.fnmatch(iqn, pat):
641+ LOG.debug(' - got it!')
642+ return True
643+ LOG.debug(' - nope')
644+ return False
645+
646 def validate_connector(self, connector):
647+ if self._connector_wants_iscsi(connector):
648+ return True
649 return self._storpool_client_id(connector) >= 0
650
651+ def _get_iscsi_config(self, iqn, volume_id):
652+ """Get the StorPool iSCSI config items pertaining to this volume.
653+
654+ Find the elements of the StorPool iSCSI configuration tree that
655+ will be needed to create, ensure, or remove the iSCSI export of
656+ the specified volume to the specified initiator.
657+ """
658+ cfg = self._attach.api().iSCSIConfig()
659+
660+ pg_name = self.configuration.iscsi_portal_group
661+ pg_found = [
662+ pg for pg in cfg.iscsi.portalGroups.values() if pg.name == pg_name
663+ ]
664+ if not pg_found:
665+ raise Exception('StorPool Cinder iSCSI configuration error: '
666+ 'no portal group "{pg}"'.format(pg=pg_name))
667+ pg = pg_found[0]
668+
669+ # Do we know about this initiator?
670+ i_found = [
671+ init for init in cfg.iscsi.initiators.values() if init.name == iqn
672+ ]
673+ if i_found:
674+ initiator = i_found[0]
675+ else:
676+ initiator = None
677+
678+ # Is this volume already being exported?
679+ volname = self._attach.volumeName(volume_id)
680+ t_found = [
681+ tgt for tgt in cfg.iscsi.targets.values() if tgt.volume == volname
682+ ]
683+ if t_found:
684+ target = t_found[0]
685+ else:
686+ target = None
687+
688+ # OK, so is this volume being exported to this initiator?
689+ export = None
690+ if initiator is not None and target is not None:
691+ e_found = [
692+ exp for exp in initiator.exports
693+ if exp.portalGroup == pg.name and exp.target == target.name
694+ ]
695+ if e_found:
696+ export = e_found[0]
697+
698+ return {
699+ 'cfg': cfg,
700+ 'pg': pg,
701+ 'initiator': initiator,
702+ 'target': target,
703+ 'export': export,
704+ 'volume_name': volname,
705+ 'volume_id': volume_id,
706+ }
707+
708+ def _create_iscsi_export(self, volume, connector):
709+ """Create (if needed) an iSCSI export for the StorPool volume."""
710+ LOG.debug(
711+ '_create_iscsi_export() invoked for volume '
712+ '"%(vol_name)s" (%(vol_id)s) connector %(connector)s',
713+ {
714+ 'vol_name': volume['display_name'],
715+ 'vol_id': volume['id'],
716+ 'connector': connector,
717+ }
718+ )
719+ iqn = connector['initiator']
720+ try:
721+ cfg = self._get_iscsi_config(iqn, volume['id'])
722+ except Exception as exc:
723+ LOG.error(
724+ 'Could not fetch the iSCSI config: %(exc)s', {'exc': exc}
725+ )
726+ raise
727+
728+ if cfg['initiator'] is None:
729+ if not (self.configuration.iscsi_learn_initiator_iqns or
730+ self.configuration.iscsi_cinder_volume and
731+ connector.get('storpool_wants_iscsi')):
732+ raise Exception('The "{iqn}" initiator IQN for the "{host}" '
733+ 'host is not defined in the StorPool '
734+ 'configuration.'
735+ .format(iqn=iqn, host=connector['host']))
736+ else:
737+ LOG.info('Creating a StorPool iSCSI initiator '
738+ 'for "{host}s" ({iqn}s)',
739+ {'host': connector['host'], 'iqn': iqn})
740+ self._attach.api().iSCSIConfigChange({
741+ 'commands': [
742+ {
743+ 'createInitiator': {
744+ 'name': iqn,
745+ 'username': '',
746+ 'secret': '',
747+ },
748+ },
749+ {
750+ 'initiatorAddNetwork': {
751+ 'initiator': iqn,
752+ 'net': '0.0.0.0/0',
753+ },
754+ },
755+ ]
756+ })
757+
758+ if cfg['target'] is None:
759+ LOG.info(
760+ 'Creating a StorPool iSCSI target '
761+ 'for the "%(vol_name)s" volume (%(vol_id)s)',
762+ {
763+ 'vol_name': volume['display_name'],
764+ 'vol_id': volume['id'],
765+ }
766+ )
767+ self._attach.api().iSCSIConfigChange({
768+ 'commands': [
769+ {
770+ 'createTarget': {
771+ 'volumeName': cfg['volume_name'],
772+ },
773+ },
774+ ]
775+ })
776+ cfg = self._get_iscsi_config(iqn, volume['id'])
777+
778+ if cfg['export'] is None:
779+ LOG.info('Creating a StorPool iSCSI export '
780+ 'for the "{vol_name}s" volume ({vol_id}s) '
781+ 'to the "{host}s" initiator ({iqn}s) '
782+ 'in the "{pg}s" portal group',
783+ {
784+ 'vol_name': volume['display_name'],
785+ 'vol_id': volume['id'],
786+ 'host': connector['host'],
787+ 'iqn': iqn,
788+ 'pg': cfg['pg'].name
789+ })
790+ self._attach.api().iSCSIConfigChange({
791+ 'commands': [
792+ {
793+ 'export': {
794+ 'initiator': iqn,
795+ 'portalGroup': cfg['pg'].name,
796+ 'volumeName': cfg['volume_name'],
797+ },
798+ },
799+ ]
800+ })
801+
Peter Pentchevc53e6c02023-02-08 15:13:56 +0200802+ target_portals = [
803+ "{addr}:3260".format(addr=net.address)
804+ for net in cfg['pg'].networks
805+ ]
806+ target_iqns = [cfg['target'].name] * len(target_portals)
807+ target_luns = [0] * len(target_portals)
808+ if connector.get('multipath', False):
809+ multipath_settings = {
810+ 'target_iqns': target_iqns,
811+ 'target_portals': target_portals,
812+ 'target_luns': target_luns,
813+ }
814+ else:
815+ multipath_settings = {}
816+
Peter Pentchev9c24be92022-09-26 22:35:24 +0300817+ res = {
818+ 'driver_volume_type': 'iscsi',
819+ 'data': {
Peter Pentchevc53e6c02023-02-08 15:13:56 +0200820+ **multipath_settings,
Peter Pentchev9c24be92022-09-26 22:35:24 +0300821+ 'target_discovered': False,
Peter Pentchevc53e6c02023-02-08 15:13:56 +0200822+ 'target_iqn': target_iqns[0],
823+ 'target_portal': target_portals[0],
824+ 'target_lun': target_luns[0],
Peter Pentchev9c24be92022-09-26 22:35:24 +0300825+ 'volume_id': volume['id'],
826+ 'discard': True,
827+ },
828+ }
829+ LOG.debug('returning %(res)s', {'res': res})
830+ return res
831+
832+ def _remove_iscsi_export(self, volume, connector):
833+ """Remove an iSCSI export for the specified StorPool volume."""
834+ LOG.debug(
835+ '_remove_iscsi_export() invoked for volume '
836+ '"%(vol_name)s" (%(vol_id)s) connector %(conn)s',
837+ {
838+ 'vol_name': volume['display_name'],
839+ 'vol_id': volume['id'],
840+ 'conn': connector,
841+ }
842+ )
843+ try:
844+ cfg = self._get_iscsi_config(connector['initiator'], volume['id'])
845+ except Exception as exc:
846+ LOG.error(
847+ 'Could not fetch the iSCSI config: %(exc)s', {'exc': exc}
848+ )
849+ raise
850+
851+ if cfg['export'] is not None:
852+ LOG.info('Removing the StorPool iSCSI export '
853+ 'for the "%(vol_name)s" volume (%(vol_id)s) '
854+ 'to the "%(host)s" initiator (%(iqn)s) '
855+ 'in the "%(pg)s" portal group',
856+ {
857+ 'vol_name': volume['display_name'],
858+ 'vol_id': volume['id'],
859+ 'host': connector['host'],
860+ 'iqn': connector['initiator'],
861+ 'pg': cfg['pg'].name,
862+ })
863+ try:
864+ self._attach.api().iSCSIConfigChange({
865+ 'commands': [
866+ {
867+ 'exportDelete': {
868+ 'initiator': cfg['initiator'].name,
869+ 'portalGroup': cfg['pg'].name,
870+ 'volumeName': cfg['volume_name'],
871+ },
872+ },
873+ ]
874+ })
875+ except spapi.ApiError as e:
876+ if e.name not in ('objectExists', 'objectDoesNotExist'):
877+ raise
878+ LOG.info('Looks like somebody beat us to it')
879+
880+ if cfg['target'] is not None:
881+ last = True
882+ for initiator in cfg['cfg'].iscsi.initiators.values():
883+ if initiator.name == cfg['initiator'].name:
884+ continue
885+ for exp in initiator.exports:
886+ if exp.target == cfg['target'].name:
887+ last = False
888+ break
889+ if not last:
890+ break
891+
892+ if last:
893+ LOG.info(
894+ 'Removing the StorPool iSCSI target '
895+ 'for the "{vol_name}s" volume ({vol_id}s)',
896+ {
897+ 'vol_name': volume['display_name'],
898+ 'vol_id': volume['id'],
899+ }
900+ )
901+ try:
902+ self._attach.api().iSCSIConfigChange({
903+ 'commands': [
904+ {
905+ 'deleteTarget': {
906+ 'volumeName': cfg['volume_name'],
907+ },
908+ },
909+ ]
910+ })
911+ except spapi.ApiError as e:
912+ if e.name not in ('objectDoesNotExist', 'invalidParam'):
913+ raise
914+ LOG.info('Looks like somebody beat us to it')
915+
916 def initialize_connection(self, volume, connector):
917+ if self._connector_wants_iscsi(connector):
918+ return self._create_iscsi_export(volume, connector)
919 return {'driver_volume_type': 'storpool',
920 'data': {
921 'client_id': self._storpool_client_id(connector),
Peter Pentchevacaaa382023-02-28 11:26:13 +0200922@@ -174,6 +517,9 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300923 }}
924
925 def terminate_connection(self, volume, connector, **kwargs):
926+ if self._connector_wants_iscsi(connector):
927+ LOG.debug('- removing an iSCSI export')
928+ self._remove_iscsi_export(volume, connector)
929 pass
930
931 def create_snapshot(self, snapshot):
Peter Pentchevacaaa382023-02-28 11:26:13 +0200932@@ -275,11 +621,20 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300933 )
934
935 def create_export(self, context, volume, connector):
936- pass
937+ if self._connector_wants_iscsi(connector):
938+ LOG.debug('- creating an iSCSI export')
939+ self._create_iscsi_export(volume, connector)
940
941 def remove_export(self, context, volume):
942 pass
943
944+ def _attach_volume(self, context, volume, properties, remote=False):
945+ if self.configuration.iscsi_cinder_volume and not remote:
946+ LOG.debug('- adding the "storpool_wants_iscsi" flag')
947+ properties['storpool_wants_iscsi'] = True
948+
949+ return super()._attach_volume(context, volume, properties, remote)
950+
951 def delete_volume(self, volume):
952 name = self._attach.volumeName(volume['id'])
953 try:
Peter Pentchevacaaa382023-02-28 11:26:13 +0200954@@ -316,6 +671,17 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300955 LOG.error("StorPoolDriver API initialization failed: %s", e)
956 raise
957
958+ export_to = self.configuration.iscsi_export_to
959+ export_to_set = export_to is not None and export_to.split()
960+ vol_iscsi = self.configuration.iscsi_cinder_volume
961+ pg_name = self.configuration.iscsi_portal_group
962+ if (export_to_set or vol_iscsi) and pg_name is None:
963+ msg = _('The "iscsi_portal_group" option is required if '
964+ 'any patterns are listed in "iscsi_export_to"')
965+ raise exception.VolumeDriverException(message=msg)
966+
967+ self._use_iscsi = export_to == "*"
968+
969 def _update_volume_stats(self):
970 try:
971 dl = self._attach.api().disksList()
Peter Pentchevacaaa382023-02-28 11:26:13 +0200972@@ -341,7 +707,7 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300973 'total_capacity_gb': total / units.Gi,
974 'free_capacity_gb': free / units.Gi,
975 'reserved_percentage': 0,
976- 'multiattach': True,
977+ 'multiattach': not self._use_iscsi,
978 'QoS_support': False,
979 'thick_provisioning_support': False,
980 'thin_provisioning_support': True,
Peter Pentchevacaaa382023-02-28 11:26:13 +0200981@@ -360,7 +726,9 @@ class StorPoolDriver(driver.VolumeDriver):
Peter Pentchev9c24be92022-09-26 22:35:24 +0300982 'volume_backend_name') or 'storpool',
983 'vendor_name': 'StorPool',
984 'driver_version': self.VERSION,
985- 'storage_protocol': constants.STORPOOL,
986+ 'storage_protocol': (
987+ constants.ISCSI if self._use_iscsi else constants.STORPOOL
988+ ),
Peter Pentchevacaaa382023-02-28 11:26:13 +0200989 # Driver capabilities
Peter Pentchev9c24be92022-09-26 22:35:24 +0300990 'clone_across_pools': True,
991 'sparse_copy_volume': True,
Peter Pentchevacaaa382023-02-28 11:26:13 +0200992diff --git a/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst b/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst
Peter Pentchevea354462023-07-18 11:15:56 +0300993index d2c5895a9..1f3d46cce 100644
Peter Pentchev9c24be92022-09-26 22:35:24 +0300994--- a/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst
995+++ b/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst
Peter Pentchevacaaa382023-02-28 11:26:13 +0200996@@ -19,12 +19,15 @@ Prerequisites
Peter Pentchev9c24be92022-09-26 22:35:24 +0300997 * The controller and all the compute nodes must have access to the StorPool
998 API service.
999
1000-* All nodes where StorPool-backed volumes will be attached must have access to
1001+* If iSCSI is not being used as a transport (see below), all nodes where
1002+ StorPool-backed volumes will be attached must have access to
1003 the StorPool data network and run the ``storpool_block`` service.
1004
1005-* If StorPool-backed Cinder volumes need to be created directly from Glance
1006- images, then the node running the ``cinder-volume`` service must also have
1007- access to the StorPool data network and run the ``storpool_block`` service.
1008+* If Glance uses Cinder as its image store, or if StorPool-backed Cinder
1009+ volumes need to be created directly from Glance images, and iSCSI is not
1010+ being used as a transport, then the node running the ``cinder-volume``
1011+ service must also have access to the StorPool data network and run
1012+ the ``storpool_block`` service.
1013
1014 * All nodes that need to access the StorPool API (the compute nodes and
1015 the node running the ``cinder-volume`` service) must have the following
Peter Pentchevacaaa382023-02-28 11:26:13 +02001016@@ -34,6 +37,29 @@ Prerequisites
Peter Pentchev9c24be92022-09-26 22:35:24 +03001017 * the storpool Python bindings package
1018 * the storpool.spopenstack Python helper package
1019
1020+Using iSCSI as the transport protocol
1021+-------------------------------------
1022+
1023+The StorPool distributed storage system uses its own, highly optimized and
1024+tailored for its specifics, network protocol for communication between
1025+the storage servers and the clients (the OpenStack cluster nodes where
1026+StorPool-backed volumes will be attached). There are cases when granting
1027+various nodes access to the StorPool data network or installing and
1028+running the ``storpool_block`` client service on them may pose difficulties.
1029+The StorPool servers may also expose the user-created volumes and snapshots
1030+using the standard iSCSI protocol that only requires TCP routing and
1031+connectivity between the storage servers and the StorPool clients.
1032+The StorPool Cinder driver may be configured to export volumes and
1033+snapshots via iSCSI using the ``iscsi_export_to`` and ``iscsi_portal_group``
1034+configuration options.
1035+
1036+Additionally, even if e.g. the hypervisor nodes running Nova will use
1037+the StorPool network protocol and run the ``storpool_block`` service
1038+(so the ``iscsi_export_to`` option has its default empty string value),
1039+the ``iscsi_cinder_volume`` option configures the StorPool Cinder driver
1040+so that only the ``cinder-volume`` service will use the iSCSI protocol when
1041+attaching volumes and snapshots to transfer data to and from Glance images.
1042+
1043 Configuring the StorPool volume driver
1044 --------------------------------------
1045
Peter Pentchevea354462023-07-18 11:15:56 +03001046@@ -55,6 +81,32 @@ volume backend definition) and per volume type:
Peter Pentchev9c24be92022-09-26 22:35:24 +03001047 with the default placement constraints for the StorPool cluster.
1048 The default value for the chain replication is 3.
1049
Peter Pentchevea354462023-07-18 11:15:56 +03001050+In addition, if the iSCSI protocol is used to access the StorPool cluster as
1051+described in the previous section, the following options may be defined in
1052+the ``cinder.conf`` volume backend definition:
1053+
Peter Pentchev9c24be92022-09-26 22:35:24 +03001054+- ``iscsi_export_to``: if set to the value ``*``, the StorPool Cinder driver
1055+ will export volumes and snapshots using the iSCSI protocol instead of
1056+ the StorPool network protocol. The ``iscsi_portal_group`` option must also
1057+ be specified.
1058+
1059+- ``iscsi_portal_group``: if the ``iscsi_export_to`` option is set to
1060+ the value ``*`` or the ``iscsi_cinder_volume`` option is turned on,
1061+ this option specifies the name of the iSCSI portal group that Cinder
1062+ volumes will be exported to.
1063+
1064+- ``iscsi_cinder_volume``: if enabled, even if the ``iscsi_export_to`` option
1065+ has its default empty value, the ``cinder-volume`` service will use iSCSI
1066+ to attach the volumes and snapshots for transferring data to and from
1067+ Glance images.
1068+
Peter Pentchevea354462023-07-18 11:15:56 +03001069+- ``iscsi_learn_initiator_iqns``: if enabled, the StorPool Cinder driver will
1070+ automatically use the StorPool API to create definitions for new initiators
1071+ in the StorPool cluster's configuration. This is the default behavior of
1072+ the driver; it may be disabled in the rare case if, e.g. because of site
1073+ policy, OpenStack iSCSI initiators (e.g. Nova hypervisors) need to be
1074+ explicitly allowed to use the StorPool iSCSI targets.
1075+
Peter Pentchev9c24be92022-09-26 22:35:24 +03001076 Using the StorPool volume driver
1077 --------------------------------
1078
Peter Pentchevea354462023-07-18 11:15:56 +03001079diff --git a/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml b/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml
1080new file mode 100644
1081index 000000000..c48686abb
1082--- /dev/null
1083+++ b/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml
1084@@ -0,0 +1,10 @@
1085+---
1086+features:
1087+ - |
1088+ StorPool driver: Added support for exporting the StorPool-backed volumes
1089+ using the iSCSI protocol, so that the Cinder volume service and/or
1090+ the Nova or Glance consumers do not need to have the StorPool block
1091+ device third-party service installed. See the StorPool driver section in
1092+ the Cinder documentation for more information on the ``iscsi_export_to``,
1093+ ``iscsi_portal_group``, ``iscsi_cinder_volume``, and
1094+ ``iscsi_learn_initiator_iqns`` options.
Peter Pentchevacaaa382023-02-28 11:26:13 +02001095--
Peter Pentchevea354462023-07-18 11:15:56 +030010962.40.1
Peter Pentchevacaaa382023-02-28 11:26:13 +02001097