Skip to content

Commit e898f7c

Browse files
committed
fix(storage): take storage ownership when put on the network
Signed-off-by: Cedric Hombourger <[email protected]>
1 parent c022588 commit e898f7c

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

mtda/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def storage_network(self, remote):
113113
if os.path.exists(cmd_nbd) is False:
114114
raise RuntimeError(f'{cmd_nbd} not found')
115115

116-
rdev = self._impl.storage_network()
116+
rdev = self._impl.storage_network(session=self._session)
117117
if rdev is None:
118118
raise RuntimeError('could not put storage on network')
119119

mtda/main.py

+35-20
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,12 @@ def storage_locked(self, session=None):
753753
elif self._target_status() != "OFF":
754754
reason = "target is on"
755755
result = True
756+
elif (self._storage_status == CONSTS.STORAGE.ON_NETWORK and
757+
self._storage_owner is not None and
758+
self._storage_owner != session):
759+
reason = ("storage shared on the network for " +
760+
f"{self._storage_owner}")
761+
result = True
756762
# Lastly, the shared storage device shall not be opened
757763
elif self._storage_opened is True:
758764
reason = "shared storage is in use (opened)"
@@ -820,29 +826,37 @@ def storage_network(self, **kwargs):
820826
result = False
821827
session = kwargs.get("session", None)
822828
self.session_ping(session)
823-
if self.storage_locked(session) is False:
824-
if self.storage.to_host() is True:
825-
conf = os.path.join(NBD_CONF_DIR, NBD_CONF_FILE)
826-
file = None
827-
828-
if hasattr(self.storage, 'path'):
829-
file = self.storage.path()
830829

831-
if file is not None and os.path.exists(NBD_CONF_DIR):
832-
with open(conf, 'w') as f:
833-
f.write('[mtda-storage]\n')
834-
f.write('authfile = /etc/nbd-server/allow\n')
835-
f.write(f'exportname = {file}\n')
836-
f.close()
830+
if self.storage is None:
831+
raise RuntimeError('no shared storage device')
832+
elif hasattr(self.storage, 'path') is False:
833+
raise RuntimeError('path to shared storage not available')
834+
elif os.path.exists(NBD_CONF_DIR) is False:
835+
raise RuntimeError('NBD configuration directory not found')
836+
elif self.storage_locked(session) is True:
837+
raise RuntimeError('shared storage in use')
838+
elif self.storage.to_host() is True:
839+
file = self.storage.path()
840+
if file:
841+
conf = os.path.join(NBD_CONF_DIR, NBD_CONF_FILE)
842+
with open(conf, 'w') as f:
843+
f.write('[mtda-storage]\n')
844+
f.write('authfile = /etc/nbd-server/allow\n')
845+
f.write(f'exportname = {file}\n')
846+
f.close()
837847

838-
cmd = ['systemctl', 'restart', 'nbd-server']
839-
subprocess.check_call(cmd)
848+
cmd = ['systemctl', 'restart', 'nbd-server']
849+
subprocess.check_call(cmd)
840850

841-
cmd = ['systemctl', 'is-active', 'nbd-server']
842-
subprocess.check_call(cmd)
851+
cmd = ['systemctl', 'is-active', 'nbd-server']
852+
subprocess.check_call(cmd)
843853

844-
self._storage_event(CONSTS.STORAGE.ON_NETWORK)
845-
result = True
854+
self._storage_owner = session
855+
self.storage_locked()
856+
self._storage_event(CONSTS.STORAGE.ON_NETWORK)
857+
result = True
858+
else:
859+
raise RuntimeError('no path to shared storage')
846860

847861
self.mtda.debug(3, f"main.storage_network(): {result}")
848862
return result
@@ -1014,7 +1028,8 @@ def systemd_configure_www(self):
10141028
dropin = os.path.join(etcdir, 'www.conf')
10151029
with open(dropin, 'w') as f:
10161030
f.write('[Service]\n')
1017-
f.write(f'Environment=HOST={self._www_host} PORT={self._www_port}\n')
1031+
f.write(f'Environment=HOST={self._www_host} '
1032+
f'PORT={self._www_port}\n')
10181033
else:
10191034
import shutil
10201035
shutil.rmtree(etcdir, ignore_errors=True)

0 commit comments

Comments
 (0)