Skip to content

Commit 0d798f4

Browse files
Millefeuille42Wescoeur
authored andcommitted
Stop SMGC service on SR detach to prevent orphaned systemd units (#92)
This is not done on every and each implementation of SR but only on ones that calls cleanup.start_gc_service (like FileSR) and on the classes that inherits from them and don't call super on detach. This is to prevent useless errors logs like Failed to stop xxx.service: Unit xxx.service not loaded. Signed-off-by: Mathieu Labourier <mathieu.labourier@vates.tech>
1 parent b918968 commit 0d798f4

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

drivers/cleanup.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3979,6 +3979,7 @@ def abort(srUuid, soft=False):
39793979
"""Abort GC/coalesce if we are currently GC'ing or coalescing a VDI pair.
39803980
"""
39813981
if _abort(srUuid, soft):
3982+
stop_gc_service(srUuid)
39823983
Util.log("abort: releasing the process lock")
39833984
lockGCActive.release()
39843985
return True
@@ -4059,6 +4060,18 @@ def start_gc(session, sr_uuid):
40594060
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
40604061
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
40614062

4063+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
4064+
"""
4065+
Build and run the systemctl command for the GC service using util.doexec.
4066+
"""
4067+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
4068+
cmd=["/usr/bin/systemctl", "--quiet"]
4069+
if extra_args:
4070+
cmd.extend(extra_args)
4071+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
4072+
return util.doexec(cmd)
4073+
4074+
40624075
def start_gc_service(sr_uuid, wait=False):
40634076
"""
40644077
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -4069,13 +4082,18 @@ def start_gc_service(sr_uuid, wait=False):
40694082
run has finished. This is used to force a run of the GC instead of just kicking it
40704083
in the background.
40714084
"""
4072-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40734085
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
4074-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4075-
if not wait:
4076-
cmd.append("--no-block")
4077-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
4078-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
4086+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
4087+
4088+
4089+
def stop_gc_service(sr_uuid):
4090+
"""
4091+
Stops the templated systemd service which runs GC on the given SR UUID.
4092+
"""
4093+
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
4094+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop")
4095+
if rc != 0:
4096+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
40794097

40804098

40814099
def wait_for_completion(sr_uuid):

tests/test_cleanup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ def test_lock_released_by_abort_when_held(
409409
cleanup.lockGCActive = TestRelease()
410410
cleanup.lockGCActive.release = mock.Mock(return_value=None)
411411

412-
ret = cleanup.abort(mock_sr, False)
413-
412+
ret = cleanup.abort(str(mock_sr.uuid), False)
414413
# Pass on the return from _abort.
415414
self.assertEqual(True, ret)
416415

0 commit comments

Comments
 (0)