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

Fix LVM2 volume names containing hyphens #245

Merged
merged 3 commits into from
May 2, 2023
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
8 changes: 5 additions & 3 deletions dissect/target/plugins/os/unix/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import uuid
from struct import unpack
from typing import Iterator, Optional, Tuple, Union
from typing import Iterator, Optional, Union

from dissect.target.filesystem import Filesystem
from dissect.target.helpers.fsutil import TargetPath
Expand Down Expand Up @@ -277,7 +277,7 @@ def _get_architecture(self, os: str = "unix") -> Optional[str]:
def parse_fstab(
fstab: TargetPath,
log: logging.Logger = log,
) -> Iterator[Tuple[Union[uuid.UUID, str], str, str, str]]:
) -> Iterator[tuple[Union[uuid.UUID, str], str, str, str]]:
"""Parse fstab file and return a generator that streams the details of entries,
with unsupported FS types and block devices filtered away.
"""
Expand Down Expand Up @@ -315,7 +315,9 @@ def parse_fstab(
if dev.startswith(("/dev/mapper", "/dev/gpt")):
volume_name = dev.rsplit("/")[-1]
elif dev.startswith("/dev/") and dev.count("/") == 3:
volume_name = "-".join(dev.rsplit("/")[-2:])
# When composing a vg-lv name, LVM2 replaces hyphens with double hyphens in the vg and lv names
# Emulate that here when combining the vg and lv names
volume_name = "-".join(part.replace("-", "--") for part in dev.rsplit("/")[-2:])
Schamper marked this conversation as resolved.
Show resolved Hide resolved
elif dev.startswith("UUID="):
dev_id = dev.split("=")[1]
try:
Expand Down
4 changes: 3 additions & 1 deletion dissect/target/volumes/lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@

def _volumes(self) -> Iterator[Volume]:
for num, lv in enumerate(self.lvm.volume_group.logical_volumes):
name = f"{lv.vg.name}-{lv.metadata.name}"
# When composing a vg-lv name, LVM2 replaces hyphens with double hyphens in the vg and lv names
# Emulate that here for the volume name
name = f"{lv.vg.name.replace('-', '--')}-{lv.metadata.name.replace('-', '--')}"

Check warning on line 53 in dissect/target/volumes/lvm.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/volumes/lvm.py#L53

Added line #L53 was not covered by tests
yield Volume(lv, num, None, lv.size, None, name, raw=lv, vs=self)
10 changes: 8 additions & 2 deletions tests/test_plugins_os_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
UUID=F631-BECA /boot/efi vfat defaults,discard,umask=0077 0 0

/dev/disk/cloud/azure_resource-part1 /mnt auto defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig 0 2

/dev/mapper/vg--main-lv--var /var auto default 0 2

/dev/vg-main/lv-data /data auto default 0 2
""" # noqa


Expand All @@ -41,14 +45,16 @@ def test_parse_fstab():

records = list(parse_fstab(fs.path("/etc/fstab")))

# 8 input records minus
# 10 input records minus
# 2 unsupported mount devices (proc, /dev/disk/cloud/azure_resource-part1)
# 2 swap partitions
# 1 root partition
# = 3 expected results
# = 5 expected results

assert set(records) == {
(UUID("5d1f1508-069b-4274-9bfa-ae2bf7ffb5e0"), None, "ext4", "/home"),
(UUID("28a25297-9825-4f87-ac41-f9c20cd5db4f"), None, "ext4", "/boot"),
("F631-BECA", None, "vfat", "/boot/efi"),
(None, "vg--main-lv--var", "auto", "/var"),
(None, "vg--main-lv--data", "auto", "/data"),
}