Skip to content

Commit

Permalink
Fix LVM2 volume names containing hyphens (#245)
Browse files Browse the repository at this point in the history
(DIS-1982)
  • Loading branch information
Schamper committed May 2, 2023
1 parent e3a68de commit 6e02f7a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
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:])
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 _detect_volume(fh: BinaryIO) -> bool:

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('-', '--')}"
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"),
}

0 comments on commit 6e02f7a

Please sign in to comment.