Merge "Do dmsetup remove device in rollback"

This commit is contained in:
Zuul 2022-08-23 16:11:30 +00:00 committed by Gerrit Code Review
commit 15430098a0
3 changed files with 30 additions and 6 deletions

View File

@ -20,6 +20,7 @@ from diskimage_builder.block_device.plugin import NodeBase
from diskimage_builder.block_device.plugin import PluginBase
from diskimage_builder.block_device.utils import exec_sudo
from diskimage_builder.block_device.utils import parse_abs_size_spec
from diskimage_builder.block_device.utils import remove_device
PHYSICAL_EXTENT_BYTES = parse_abs_size_spec('4MiB')
@ -211,13 +212,15 @@ class LvsNode(NodeBase):
exec_sudo(cmd)
# save state
device_name = '%s-%s' % (self.base, self.name)
self.state['blockdev'][self.name] = {
'vgs': self.base,
'size': self.size,
'extents': self.extents,
'opts': self.options,
'device': '/dev/mapper/%s-%s' % (self.base, self.name)
'device': '/dev/mapper/%s' % device_name
}
self.add_rollback(remove_device, device_name)
def _umount(self):
exec_sudo(['lvchange', '-an',

View File

@ -23,6 +23,7 @@ from diskimage_builder.block_device.plugin import PluginBase
from diskimage_builder.block_device.utils import exec_sudo
from diskimage_builder.block_device.utils import parse_abs_size_spec
from diskimage_builder.block_device.utils import parse_rel_size_spec
from diskimage_builder.block_device.utils import remove_device
logger = logging.getLogger(__name__)
@ -117,10 +118,11 @@ class Partitioning(PluginBase):
# below once we're done. So the device this partition
# will be seen at becomes "/dev/mapper/loop0pX"
assert self.device_path[:5] == "/dev/"
partition_device_name = "/dev/mapper/%sp%d" % \
(self.device_path[5:], part_no)
device_name = "%sp%d" % (self.device_path[5:], part_no)
device_path = "/dev/mapper/%s" % device_name
self.state['blockdev'][part_name] \
= {'device': partition_device_name}
= {'device': device_path}
part_cfg.add_rollback(remove_device, device_name)
def _create_gpt(self):
"""Create partitions with GPT"""
@ -158,14 +160,16 @@ class Partitioning(PluginBase):
# below once we're done. So the device this partition
# will be seen at becomes "/dev/mapper/loop0pX"
assert self.device_path[:5] == "/dev/"
device_name = "/dev/mapper/%sp%d" % (self.device_path[5:], pnum)
device_name = "%sp%d" % (self.device_path[5:], pnum)
device_path = "/dev/mapper/%s" % device_name
self.state['blockdev'][p.get_name()] \
= {'device': device_name}
= {'device': device_path}
disk_free = disk_free - size
pnum = pnum + 1
logger.debug("Partition %s added, %s remaining in disk",
pnum, disk_free)
p.add_rollback(remove_device, device_name)
logger.debug("cmd: %s", ' '.join(cmd))
exec_sudo(cmd)

View File

@ -143,3 +143,20 @@ def exec_sudo(cmd):
raise e
return out
def remove_device(device_name):
"""Attempt to remove a device-mapper device
Convenience rollback function to attempt to delete a device,
logging a warning if the delete fails.
:param device_name: Name of device to run dmsetup remove on
"""
logger.debug('Removing device %s', device_name)
try:
exec_sudo(["dmsetup", "remove", device_name])
except BlockDeviceSetupException as e:
# Do not raise an error - maybe other cleanup methods
# can at least do some more work.
logger.warning("Removing device failed (%s)", e.returncode)