Skip to content

Commit e32c9c5

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 ebd4cb6 commit e32c9c5

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

drivers/cleanup.py

Lines changed: 23 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
@@ -4040,6 +4041,18 @@ def start_gc(session, sr_uuid):
40404041
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
40414042
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
40424043

4044+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
4045+
"""
4046+
Build and run the systemctl command for the GC service using util.doexec.
4047+
"""
4048+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
4049+
cmd=["/usr/bin/systemctl", "--quiet"]
4050+
if extra_args:
4051+
cmd.extend(extra_args)
4052+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
4053+
return util.doexec(cmd)
4054+
4055+
40434056
def start_gc_service(sr_uuid, wait=False):
40444057
"""
40454058
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -4050,14 +4063,18 @@ def start_gc_service(sr_uuid, wait=False):
40504063
run has finished. This is used to force a run of the GC instead of just kicking it
40514064
in the background.
40524065
"""
4053-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40544066
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
4055-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4056-
if not wait:
4057-
cmd.append("--no-block")
4058-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
4059-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
4067+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
4068+
40604069

4070+
def stop_gc_service(sr_uuid):
4071+
"""
4072+
Stops the templated systemd service which runs GC on the given SR UUID.
4073+
"""
4074+
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
4075+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop")
4076+
if rc != 0:
4077+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
40614078

40624079
def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):
40634080
"""Garbage collect all deleted VDIs in SR "srUuid". The caller must ensure

tests/test_cleanup.py

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

410-
ret = cleanup.abort(mock_sr, False)
411-
410+
ret = cleanup.abort(str(mock_sr.uuid), False)
412411
# Pass on the return from _abort.
413412
self.assertEqual(True, ret)
414413

0 commit comments

Comments
 (0)