Skip to content

Commit aa95fd6

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 532a891 commit aa95fd6

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
@@ -3967,6 +3967,7 @@ def abort(srUuid, soft=False):
39673967
"""Abort GC/coalesce if we are currently GC'ing or coalescing a VDI pair.
39683968
"""
39693969
if _abort(srUuid, soft):
3970+
stop_gc_service(srUuid)
39703971
Util.log("abort: releasing the process lock")
39713972
lockGCActive.release()
39723973
return True
@@ -4047,6 +4048,18 @@ def start_gc(session, sr_uuid):
40474048
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
40484049
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
40494050

4051+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
4052+
"""
4053+
Build and run the systemctl command for the GC service using util.doexec.
4054+
"""
4055+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
4056+
cmd=["/usr/bin/systemctl", "--quiet"]
4057+
if extra_args:
4058+
cmd.extend(extra_args)
4059+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
4060+
return util.doexec(cmd)
4061+
4062+
40504063
def start_gc_service(sr_uuid, wait=False):
40514064
"""
40524065
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -4057,13 +4070,18 @@ def start_gc_service(sr_uuid, wait=False):
40574070
run has finished. This is used to force a run of the GC instead of just kicking it
40584071
in the background.
40594072
"""
4060-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40614073
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
4062-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4063-
if not wait:
4064-
cmd.append("--no-block")
4065-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
4066-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
4074+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
4075+
4076+
4077+
def stop_gc_service(sr_uuid):
4078+
"""
4079+
Stops the templated systemd service which runs GC on the given SR UUID.
4080+
"""
4081+
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
4082+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop")
4083+
if rc != 0:
4084+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
40674085

40684086

40694087
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)