Skip to content

Commit

Permalink
osutils/disks: work-around old UC 20 bug where udev did not run on de…
Browse files Browse the repository at this point in the history
…vice

There is a bug in old UC20 kernels where udev does not retrieve data
from cold plugged disks initialized in initrd.

It was fixed in:

* canonical/core-initrd#58
* canonical/core-initrd#203
  • Loading branch information
valentindavid authored and pedronis committed Feb 3, 2025
1 parent 53c5204 commit b90d901
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion osutil/disks/disks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,22 @@ func DMCryptUUIDFromMountPoint(mountpoint string) (string, error) {

dmUUID, hasDmUUID := props["DM_UUID"]
if !hasDmUUID {
return "", ErrNoDmUUID
// There is a bug in old UC20 kernels where udev does
// not retrieve data from cold plugged disks
// initialized in initrd.
// https://github.com/canonical/core-initrd/pull/58
// https://github.com/canonical/core-initrd/pull/203
devPath, hasDevPath := props["DEVPATH"]
if !hasDevPath {
return "", ErrNoDmUUID
}
devUUIDPath := filepath.Join(dirs.SysfsDir, devPath, "dm", "uuid")
data, err := os.ReadFile(devUUIDPath)
if err != nil {
return "", ErrNoDmUUID
}
dmUUID = strings.TrimSuffix(string(data), "\n")

}

match := dmUUIDRe.FindStringSubmatchIndex(dmUUID)
Expand Down
30 changes: 30 additions & 0 deletions osutil/disks/disks_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,36 @@ func (s *diskSuite) TestDMCryptUUIDFromMountPoint(c *C) {
c.Assert(uuid, Equals, "5a522809-c87e-4dfa-81a8-8dc5667d1304")
}

func (s *diskSuite) TestDMCryptUUIDFromMountPointFallback(c *C) {
restore := osutil.MockMountInfo(`130 30 42:1 / /run/mnt/point rw,relatime shared:54 - ext4 /dev/mapper/something rw
`)
defer restore()
restore = disks.MockUdevPropertiesForDevice(func(typeOpt, dev string) (map[string]string, error) {
c.Assert(typeOpt, Equals, "--name")
switch dev {
case "/dev/mapper/something":
return map[string]string{
"DEVPATH": "/devices/virtual/mydevice",

}, nil
default:
c.Errorf("unexpected udev device properties requested: %s", dev)
return nil, fmt.Errorf("unexpected udev device: %s", dev)
}
})
defer restore()

err := os.MkdirAll(filepath.Join(dirs.SysfsDir, "devices/virtual/mydevice", "dm"), 0755)
c.Assert(err, IsNil)
b := []byte("CRYPT-LUKS2-5a522809c87e4dfa81a88dc5667d1304-something\n")
err = os.WriteFile(filepath.Join(dirs.SysfsDir, "devices/virtual/mydevice", "dm/uuid"), b, 0644)
c.Assert(err, IsNil)

uuid, err := disks.DMCryptUUIDFromMountPoint("/run/mnt/point")
c.Assert(err, IsNil)
c.Assert(uuid, Equals, "5a522809-c87e-4dfa-81a8-8dc5667d1304")
}

func (s *diskSuite) TestFilesystemTypeForPartition(c *C) {
restore := disks.MockUdevPropertiesForDevice(func(typeOpt, dev string) (map[string]string, error) {
c.Assert(typeOpt, Equals, "--name")
Expand Down

0 comments on commit b90d901

Please sign in to comment.