Skip to content

Commit e3e8daf

Browse files
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 <[email protected]>
1 parent 4193fd8 commit e3e8daf

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

drivers/cleanup.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,7 @@ def abort(srUuid, soft=False):
39483948
"""Abort GC/coalesce if we are currently GC'ing or coalescing a VDI pair.
39493949
"""
39503950
if _abort(srUuid, soft):
3951+
stop_gc_service(srUuid)
39513952
Util.log("abort: releasing the process lock")
39523953
lockGCActive.release()
39533954
return True
@@ -4021,6 +4022,18 @@ def start_gc(session, sr_uuid):
40214022
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
40224023
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
40234024

4025+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
4026+
"""
4027+
Build and run the systemctl command for the GC service using util.doexec.
4028+
"""
4029+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
4030+
cmd=["/usr/bin/systemctl", "--quiet"]
4031+
if extra_args:
4032+
cmd.extend(extra_args)
4033+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
4034+
return util.doexec(cmd)
4035+
4036+
40244037
def start_gc_service(sr_uuid, wait=False):
40254038
"""
40264039
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -4031,14 +4044,18 @@ def start_gc_service(sr_uuid, wait=False):
40314044
run has finished. This is used to force a run of the GC instead of just kicking it
40324045
in the background.
40334046
"""
4034-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
40354047
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
4036-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
4037-
if not wait:
4038-
cmd.append("--no-block")
4039-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
4040-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
4048+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
4049+
40414050

4051+
def stop_gc_service(sr_uuid):
4052+
"""
4053+
Stops the templated systemd service which runs GC on the given SR UUID.
4054+
"""
4055+
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
4056+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop")
4057+
if rc != 0:
4058+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
40424059

40434060
def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):
40444061
"""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)