Skip to content

Commit e450fce

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 a73339e commit e450fce

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

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

40694087

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