From d8be123a97380aef28a9b1155c57531c231f9e23 Mon Sep 17 00:00:00 2001 From: Sebastian Bachmann Date: Mon, 3 Jan 2022 19:40:39 +0100 Subject: Check loop device before trying to remove If the loop device was already sucessfully removed by kpartx, losetup -d would fail and thus crash the vmdb2 run. A check was added to query the loop device's back-file and only remove the loop device if there is still one. Resolves #60 --- vmdb/plugins/kpartx_plugin.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/vmdb/plugins/kpartx_plugin.py b/vmdb/plugins/kpartx_plugin.py index 7c31cfc..9ed8e26 100644 --- a/vmdb/plugins/kpartx_plugin.py +++ b/vmdb/plugins/kpartx_plugin.py @@ -14,6 +14,7 @@ # along with this program. If not, see . # # =*= License: GPL-3+ =*= +import json import os import re @@ -61,4 +62,16 @@ class KpartxStepRunner(vmdb.StepRunnerInterface): loop_devs.add("/dev/{}".format(loop)) for loop_dev in loop_devs: + # Check if loop device is actually active: + loopdev_info = json.loads( + vmdb.runcmd(["losetup", "--json", "-l", loop_dev]) + )["loopdevices"] + if len(loopdev_info) != 1: + # Sometime is wrong, this should not happen... + raise RuntimeError("losetup returned more than one device") + loopdev_info = loopdev_info.pop() + if loopdev_info["back-file"] is None: + continue + vmdb.progress("Loop device {} is still bound to back-file {}, " + "removing loop device".format(loop_dev, loopdev_info["back-file"])) vmdb.runcmd(["losetup", "-d", loop_dev]) -- cgit v1.2.1