diff options
author | Sebastian Bachmann <hello@reox.at> | 2022-01-03 19:40:39 +0100 |
---|---|---|
committer | Sebastian Bachmann <hello@reox.at> | 2022-01-03 19:44:09 +0100 |
commit | d8be123a97380aef28a9b1155c57531c231f9e23 (patch) | |
tree | a3d5454e563085d72fcdfa7559d87b046fae0190 | |
parent | d2437ac95bc0c346d32feb4acf650656414da7ff (diff) | |
download | vmdb2-d8be123a97380aef28a9b1155c57531c231f9e23.tar.gz |
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
-rw-r--r-- | vmdb/plugins/kpartx_plugin.py | 13 |
1 files changed, 13 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. # # =*= 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]) |