Skip to content

fix: correctly resolve /dev/mapper paths with hyphens in VG/LV names#368

Closed
lawrence3699 wants to merge 2 commits intomuesli:masterfrom
lawrence3699:fix/lvm-mapper-hyphen-parsing
Closed

fix: correctly resolve /dev/mapper paths with hyphens in VG/LV names#368
lawrence3699 wants to merge 2 commits intomuesli:masterfrom
lawrence3699:fix/lvm-mapper-hyphen-parsing

Conversation

@lawrence3699
Copy link
Copy Markdown

Summary

Fixes #329/dev/mapper/ paths containing hyphens in VG or LV names were incorrectly resolved.

LVM escapes hyphens in volume group and logical volume names by doubling them (--) in /dev/mapper/ paths. A single hyphen separates the VG name from the LV name. The previous greedy regex (.*)-(.*) split on the last hyphen, which broke paths like:

Input Before (wrong) After (correct)
/dev/mapper/vg0-var--log /dev/vg0-var-/log /dev/vg0/var-log
/dev/mapper/my--vg-my--lv /dev/my--vg-my-/lv /dev/my-vg/my-lv

Approach

Replaced the regex with a string-based approach:

  1. Replace escaped -- with a null-byte placeholder
  2. Split on the first single - (the real VG–LV separator)
  3. Restore - from placeholders in both captured parts

This also removes the regexp import (no longer needed).

Extracted helper

The logic is now in a standalone resolveMapperDevice() function with dedicated test cases covering:

  • Simple VG-LV names (no hyphens)
  • Hyphens in LV name only
  • Hyphens in VG name only
  • Hyphens in both VG and LV names
  • Non-mapper devices (returned unchanged)
  • Names without a separator (returned unchanged)

Test plan

  • go build ./... passes
  • go vet ./... passes
  • go test ./... passes on Linux CI (tests are //go:build linux)

LVM escapes hyphens in volume group and logical volume names by
doubling them (--) in /dev/mapper/ paths. A single hyphen separates
the VG name from the LV name.

The previous greedy regex `(.*)-(.*)`split on the last hyphen, which
broke paths like `/dev/mapper/vg0-var--log` (resolved to
`/dev/vg0-var-/log` instead of `/dev/vg0/var-log`).

Replace the regex with a string-based approach that correctly handles
LVM's double-hyphen escaping convention:
1. Replace `--` with a placeholder
2. Split on the first single `-` (the real VG-LV separator)
3. Restore `-` from placeholders in both parts

Fixes muesli#329
Copilot AI review requested due to automatic review settings April 12, 2026 17:24
Return the original device path unchanged when the VG or LV name is
empty after splitting (e.g. `/dev/mapper/-lv` or `/dev/mapper/vg-`).
These are invalid LVM names but should not silently produce truncated
paths.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect resolution of LVM /dev/mapper/* device paths when VG/LV names contain hyphens by replacing the previous greedy regex approach with a deterministic string-based parser.

Changes:

  • Replace regex-based /dev/mapper/<vg>-<lv> parsing with resolveMapperDevice() that properly handles LVM’s -- hyphen escaping.
  • Remove the now-unused regexp import from Linux mounts parsing.
  • Add Linux-only unit tests covering several LVM mapper name variants and non-mapper passthrough behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
mounts_linux.go Introduces resolveMapperDevice() and uses it during mount parsing to correctly resolve LVM mapper device names with escaped hyphens.
mounts_linux_test.go Adds dedicated test coverage for resolveMapperDevice() across common LVM naming scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mounts_linux.go
Comment on lines +118 to +124
// resolveMapperDevice resolves /dev/mapper/* device names to /dev/<VG>/<LV>.
// LVM escapes hyphens in VG/LV names by doubling them (--).
// A single hyphen separates the VG name from the LV name.
func resolveMapperDevice(device string) string {
if !strings.HasPrefix(device, "/dev/mapper/") {
return device
}
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring implies all /dev/mapper/* devices are resolved to /dev/<VG>/<LV>, but the function only rewrites LVM-style /dev/mapper/<vg>-<lv> names and returns the input unchanged for non-mapper devices and mapper names without a VG–LV separator (e.g. /dev/mapper/control). Consider updating the comment to describe the conditional behavior to avoid misleading future changes.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: /dev/mapper mount paths incorrectly parsed

2 participants