Skip to content

Commit a398134

Browse files
fix: stop SMGC service on SR detach to prevent orphaned systemd units
Signed-off-by: Mathieu Labourier <mathieu.labourier@vates.tech>
1 parent 3ecfefa commit a398134

5 files changed

Lines changed: 27 additions & 6 deletions

File tree

libs/sm/cleanup.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,18 @@ def start_gc(session, sr_uuid):
32783278
subprocess.run([__file__, '-b', '-u', sr_uuid, '-g'],
32793279
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
32803280

3281+
def _gc_service_cmd(sr_uuid, action, extra_args=None):
3282+
"""
3283+
Build and run the systemctl command for the GC service using util.doexec.
3284+
"""
3285+
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
3286+
cmd=["/usr/bin/systemctl", "--quiet"]
3287+
if extra_args:
3288+
cmd.extend(extra_args)
3289+
cmd += [action, f"SMGC@{sr_uuid_esc}"]
3290+
return util.doexec(cmd)
3291+
3292+
32813293
def start_gc_service(sr_uuid, wait=False):
32823294
"""
32833295
This starts the templated systemd service which runs GC on the given SR UUID.
@@ -3288,13 +3300,18 @@ def start_gc_service(sr_uuid, wait=False):
32883300
run has finished. This is used to force a run of the GC instead of just kicking it
32893301
in the background.
32903302
"""
3291-
sr_uuid_esc = sr_uuid.replace("-", "\\x2d")
32923303
util.SMlog(f"Kicking SMGC@{sr_uuid}...")
3293-
cmd=[ "/usr/bin/systemctl", "--quiet" ]
3294-
if not wait:
3295-
cmd.append("--no-block")
3296-
cmd += ["start", f"SMGC@{sr_uuid_esc}"]
3297-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
3304+
_gc_service_cmd(sr_uuid, "start", extra_args=None if wait else ["--no-block"])
3305+
3306+
3307+
def stop_gc_service(sr_uuid):
3308+
"""
3309+
Stops the templated systemd service which runs GC on the given SR UUID.
3310+
"""
3311+
util.SMlog(f"Stopping SMGC@{sr_uuid}...")
3312+
(rc, _stdout, stderr) = _gc_service_cmd(sr_uuid, "stop")
3313+
if rc != 0:
3314+
util.SMlog(f"Failed to stop gc service `SMGC@{sr_uuid}`: `{stderr}`")
32983315

32993316

33003317
def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):

libs/sm/drivers/FileSR.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ def attach(self, sr_uuid, vdi_uuid):
635635

636636
def detach(self, sr_uuid, vdi_uuid):
637637
self.attached = False
638+
cleanup.stop_gc_service(sr_uuid)
638639

639640
def resize(self, sr_uuid, vdi_uuid, size):
640641
if not self.exists:

libs/sm/drivers/LVHDSR.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ def detach(self, uuid):
641641
# However, we should still delete lock files on slaves as it is the
642642
# only place to do so.
643643
self._cleanup(self.isMaster)
644+
cleanup.stop_gc_service(sr_uuid)
644645

645646
def forget_vdi(self, uuid):
646647
if not self.legacyMode:

libs/sm/drivers/NFSSR.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def detach(self, sr_uuid):
192192
raise xs_errors.XenError('NFSUnMount', opterr=exc.errstr)
193193

194194
self.attached = False
195+
cleanup.stop_gc_service(sr_uuid)
195196

196197
def create(self, sr_uuid, size):
197198
util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget')

libs/sm/drivers/SMBSR.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def detach(self, sr_uuid):
243243
raise xs_errors.XenError('SMBUnMount', opterr=exc.errstr)
244244

245245
self.attached = False
246+
cleanup.stop_gc_service(sr_uuid)
246247

247248
def create(self, sr_uuid, size):
248249
self.__check_license()

0 commit comments

Comments
 (0)