Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate volume groups #285

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/thumbtack/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ class DuplicateMountAttemptError(Exception):

class EncryptedImageError(Exception):
pass

class DuplicateVolumeGroupError(Exception):
def __init__(self, msg):
super().__init__(msg)
3 changes: 3 additions & 0 deletions src/thumbtack/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ImageNotInDatabaseError,
DuplicateMountAttemptError,
EncryptedImageError,
DuplicateVolumeGroupError,
)
from .utils import get_mount_info, get_supported_libraries, mount_image, unmount_image, get_images, add_mountpoint

Expand Down Expand Up @@ -85,6 +86,8 @@ def put(self, image_path):
status = f"Unable to mount encrypted image."
except DuplicateMountAttemptError:
status = "Mount attempt is already in progress for this image. Please wait until the current mount attempt completes."
except DuplicateVolumeGroupError as e:
status = f"Unable to mount all volumes. Found duplicate volume group name: {str(e)}. Deactivate the volume group and remount the image."

current_app.mnt_mutex.release()
current_app.logger.error(status)
Expand Down
28 changes: 28 additions & 0 deletions src/thumbtack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ImageNotInDatabaseError,
DuplicateMountAttemptError,
EncryptedImageError,
DuplicateVolumeGroupError,
)

def get_supported_libraries():
Expand Down Expand Up @@ -130,24 +131,43 @@ def process_image_parser(image_parser, relative_image_path):
Mountpoints for LVM volumes are not stored in the main mountpoint variable that we check and display in thumbtack.
This loop identifies the LVM volumes and add them to the main volumes list.
"""
duplicate_vg = False
vgname = ""

num_volumes = len(image_parser.disks[0].volumes.volumes)
for v in image_parser.disks[0].volumes:
if v.duplicate_volume_group:
duplicate_vg = True
vgname = v.vgname
if hasattr(v, 'volumes') and v.mountpoint is None:
current_app.logger.info(f"Volume: {str(v)}")
for vol in v.volumes:
if vol.duplicate_volume_group:
duplicate_vg = True
vgname = vol.vgname
current_app.logger.info(f"Mountpoint: {str(vol.mountpoint)}")
if vol.mountpoint is not None:
vol.index = str(num_volumes)
num_volumes += 1
image_parser.disks[0].volumes.volumes.append(vol)
if hasattr(vol, 'volumes'):
for sub_vol in vol.volumes:
if sub_vol.duplicate_volume_group:
duplicate_vg = True
vgname = sub_vol.vgname
current_app.logger.info(f"Mountpoint: {str(sub_vol.mountpoint)}")
if sub_vol.mountpoint is not None:
sub_vol.index = str(num_volumes)
num_volumes += 1
image_parser.disks[0].volumes.volumes.append(sub_vol)

if duplicate_vg or [v for v in image_parser.disks[0].volumes if v.duplicate_volume_group]:
msg = "* Duplicate Volume groups detected."
if vgname:
msg += f" VG: {vgname}"
current_app.logger.error(msg)
raise DuplicateVolumeGroupError(vgname)

# Fail if we couldn't mount any of the volumes
if not [v for v in image_parser.disks[0].volumes if v.mountpoint]:
image_parser.clean(allow_lazy=True)
Expand Down Expand Up @@ -197,11 +217,15 @@ def mount_image(relative_image_path, creds=None):
update_or_insert_db(sql, [relative_image_path])

no_mountable_volumes = False
duplicate_vg = None
try:
image_parser = imagemounter_mitre.ImageParser(
[full_image_path], pretty=True, mountdir=mount_dir, keys=creds
)
image_parser = process_image_parser(image_parser, relative_image_path)
except DuplicateVolumeGroupError as e:
duplicate_vg = e

except NoMountableVolumesError as e:
current_app.logger.error(f"fstypes: {image_parser.fstypes}.")
no_mountable_volumes = True
Expand Down Expand Up @@ -282,6 +306,10 @@ def mount_image(relative_image_path, creds=None):
sql, [disk_image_id, mount_status_id, v_index, v_mountpoint]
)

if e := duplicate_vg:
current_app.logger.info(str(e))
raise e

return image_parser.disks[0]

def add_mountpoint(relative_image_path, mountpoint_path):
Expand Down
8 changes: 7 additions & 1 deletion src/thumbtack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import os

from .exceptions import UnexpectedDiskError, NoMountableVolumesError, DuplicateMountAttemptError, EncryptedImageError
from .exceptions import UnexpectedDiskError, NoMountableVolumesError, DuplicateMountAttemptError, EncryptedImageError, DuplicateVolumeGroupError
from .resources import Mount, SupportedLibraries, Images, ImageDir, ManualMount
from .utils import (
get_supported_libraries,
Expand Down Expand Up @@ -78,6 +78,7 @@ def mount_form():
creds = None

mounted_disk = None
duplicate_vg = False
try:
mounted_disk = mount_image(rel_path, creds)
except imagemounter_mitre.exceptions.SubsystemError:
Expand All @@ -98,7 +99,12 @@ def mount_form():
status = "Mount failed. Thumbtack server has no mount directory set."
except DuplicateMountAttemptError:
status = "Mount attempt is already in progress for this image. Please wait until the current mount attempt completes."
except DuplicateVolumeGroupError as e:
status = f"Unable to mount all volumes. Found duplicate volume group name: {str(e)}. Deactivate the volume group and remount the image."
duplicate_vg = True
if mounted_disk and mounted_disk.mountpoint is not None:
if duplicate_vg:
status = ' '.join(["Mounted Successfully.", status])
status = "Mounted successfully"

elif operation == "unmount":
Expand Down
Loading