fix: correctly resolve /dev/mapper paths with hyphens in VG/LV names#368
fix: correctly resolve /dev/mapper paths with hyphens in VG/LV names#368lawrence3699 wants to merge 2 commits intomuesli:masterfrom
Conversation
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
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.
There was a problem hiding this comment.
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 withresolveMapperDevice()that properly handles LVM’s--hyphen escaping. - Remove the now-unused
regexpimport 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.
| // 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 | ||
| } |
There was a problem hiding this comment.
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.
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:/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-lvApproach
Replaced the regex with a string-based approach:
--with a null-byte placeholder-(the real VG–LV separator)-from placeholders in both captured partsThis also removes the
regexpimport (no longer needed).Extracted helper
The logic is now in a standalone
resolveMapperDevice()function with dedicated test cases covering:Test plan
go build ./...passesgo vet ./...passesgo test ./...passes on Linux CI (tests are//go:build linux)