Skip to content

Commit

Permalink
Raise exception for duplicate lvm volume group names
Browse files Browse the repository at this point in the history
  • Loading branch information
agibbons27 committed Jul 26, 2024
1 parent 17af766 commit 11e7961
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions imagemounter_mitre/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ class SubsystemError(ImageMounterError):
class CleanupError(ImageMounterError):
"""Raised by the unmounter when cleaning failed."""
pass

class DuplicateVolumeGroupError(ImageMounterError):
"""Raised when the mounted volume group name already exists."""
def __init__(self, msg):
super().__init__(msg)
16 changes: 10 additions & 6 deletions imagemounter_mitre/filesystems.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from imagemounter_mitre import _util, VOLUME_SYSTEM_TYPES, dependencies
from imagemounter_mitre.exceptions import UnsupportedFilesystemError, IncorrectFilesystemError, ArgumentError, \
KeyInvalidError, ImageMounterError, SubsystemError, NoLoopbackAvailableError, NoMountpointAvailableError, \
NoNetworkBlockAvailableError
NoNetworkBlockAvailableError, DuplicateVolumeGroupError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -688,8 +688,9 @@ def mount(self):

try:
# Scan for new lvm volumes
result = _util.check_output_(["lvm", "pvscan"])
for line in result.splitlines():
result = _util.check_output_(["lvm", "pvscan"], stderr=subprocess.STDOUT)
lines = result.splitlines()
for line in lines:
if (self.loopback is not None and self.loopback in line) or self.volume.get_raw_path() in line:
for vg in re.findall(r'VG (\S+)', line):
self.vgname = vg
Expand All @@ -699,11 +700,14 @@ def mount(self):
raise IncorrectFilesystemError()

# Enable lvm volumes
_util.check_call_(["lvm", "vgchange", "-a", "y", self.vgname], stdout=subprocess.PIPE)
except Exception:
try:
_util.check_call_(["lvm", "vgchange", "-a", "y", self.vgname], stdout=subprocess.PIPE)
except Exception:
raise DuplicateVolumeGroupError(self.vgname)
except Exception as e:
self._free_loopback()
self.vgname = None
raise
raise e

self.volume.info['volume_group'] = self.vgname
self.volume.volumes.vstype = 'lvm'
Expand Down
10 changes: 9 additions & 1 deletion imagemounter_mitre/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import warnings

from imagemounter_mitre import _util, filesystems, FILE_SYSTEM_TYPES, VOLUME_SYSTEM_TYPES, dependencies
from imagemounter_mitre.exceptions import SubsystemError, NotMountedError, ImageMounterError
from imagemounter_mitre.exceptions import SubsystemError, NotMountedError, ImageMounterError, DuplicateVolumeGroupError
from imagemounter_mitre.filesystems import FileSystem, CarveFileSystem
from imagemounter_mitre.volume_system import VolumeSystem

Expand Down Expand Up @@ -57,6 +57,10 @@ def __init__(self, disk, parent=None, index="0", size=0, offset=0, flag='alloc',

self._get_fstype_from_parser(fstype)

# LVM debugging
self.duplicate_volume_group = False
self.vgname = ""

if key:
self.key = key
elif self.index in self.disk.parser.keys:
Expand Down Expand Up @@ -459,6 +463,10 @@ def mount(self):
logger.exception("Execution failed due to {} {}".format(type(e), e), exc_info=True)
if not isinstance(e, ImageMounterError):
raise SubsystemError(e)
elif isinstance(e, DuplicateVolumeGroupError):
self.duplicate_volume_group = True
self.vgname = str(e)
raise
else:
raise

Expand Down

0 comments on commit 11e7961

Please sign in to comment.