Skip to content
Open
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
71 changes: 67 additions & 4 deletions pkg/plugins/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type Disk struct {
SectorS uint64
LastS uint64
Parts []Partition
// Mapper is the device-mapper device (for example /dev/dm-0, an opened
// LUKS volume) the label resolved to. It is empty when the label is
// directly on a partition of Device.
Mapper string
}

type Partition struct {
Expand Down Expand Up @@ -305,6 +309,14 @@ func Layout(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) erro

l.Debugf("Checking for layout expansion on device %s", dev.Device)
if s.Layout.Expand != nil {
// Growing a partition under a device-mapper device would also need
// the mapping resized before the filesystem. For LUKS2 that needs
// the volume key, which the layout plugin does not have, so leave
// the device alone instead of failing the stage on every boot.
if dev.Mapper != "" {
l.Infof("%s is on the device-mapper device %s, skipping expansion: growing a partition under device-mapper (such as an encrypted LUKS volume) is not supported", s.Layout.Device.Label, dev.Mapper)
return nil
}
// Expansion always grows the last partition of the disk. When the device
// was selected by label, make sure that label is on the last partition,
// otherwise we would silently grow a different partition over it.
Expand Down Expand Up @@ -614,8 +626,20 @@ func FindDiskFromLabel(label string, fs vfs.FS) (Disk, error) {
return Disk{}, fmt.Errorf("resolve label %q: %w", label, err)
}

// A label can be on a device-mapper device, such as an opened LUKS
// volume. Follow it down to the partition it sits on: that partition
// is what tells us the disk.
backing, isMapper, err := backingDevice(sysRoot, partDev)
if err != nil {
return Disk{}, err
}
mapper := ""
if isMapper {
mapper = partDev
}

// Map partition -> parent disk via sysfs directory structure
diskDev, err := parentDiskFromBlockDev(partDev)
diskDev, err := parentDiskFromBlockDev(sysRoot, backing)

if err != nil && !errors.Is(err, ItsImageFileError{}) {
return Disk{}, err
Expand Down Expand Up @@ -645,6 +669,7 @@ func FindDiskFromLabel(label string, fs vfs.FS) (Disk, error) {
SectorS: uint64(d.LogicalBlocksize),
LastS: uint64(d.Size / d.LogicalBlocksize),
Parts: GetParts(d),
Mapper: mapper,
}, nil
}

Expand All @@ -664,6 +689,10 @@ func labelIsOnLastPartition(dev Disk, label string, fs vfs.FS) (bool, error) {
if err != nil {
return false, fmt.Errorf("resolve label %q: %w", label, err)
}
partDev, _, err = backingDevice(sysRoot, partDev)
if err != nil {
return false, err
}
partNumber, err := PartitionNumberFromDevice(partDev)
if err != nil {
return false, err
Expand All @@ -689,7 +718,41 @@ func PartitionNumberFromDevice(devPath string) (int, error) {
return number, nil
}

func parentDiskFromBlockDev(devPath string) (string, error) {
// sysRoot is the root that sysfs paths are resolved against. Only tests that
// build a fake sysfs tree pass anything else.
const sysRoot = "/"

// maxMapperDepth bounds how many stacked device-mapper devices backingDevice
// follows, so a broken sysfs tree cannot loop forever.
const maxMapperDepth = 8

// backingDevice follows a device-mapper device down to the block device it
// sits on, through /sys/class/block/<dm>/slaves. An opened LUKS volume
// /dev/dm-0 on /dev/sda5 resolves to /dev/sda5. Stacked mappings (LUKS on LVM)
// are followed all the way down. Any other device is returned as is.
// isMapper reports whether devPath was a device-mapper device.
func backingDevice(root, devPath string) (backing string, isMapper bool, err error) {
current := devPath
for depth := 0; depth < maxMapperDepth; depth++ {
sysDev := filepath.Join(root, "sys/class/block", filepath.Base(current))
// Only device-mapper devices have a dm/ directory in sysfs.
if _, err := os.Stat(filepath.Join(sysDev, "dm")); err != nil {
return current, isMapper, nil
}
isMapper = true
slaves, err := os.ReadDir(filepath.Join(sysDev, "slaves"))
if err != nil {
return "", true, fmt.Errorf("list devices under %s: %w", current, err)
}
if len(slaves) != 1 {
return "", true, fmt.Errorf("%s is on %d devices, expected exactly one", current, len(slaves))
}
current = filepath.Join("/dev", slaves[0].Name())
}
return "", true, fmt.Errorf("%s: more than %d stacked device-mapper devices", devPath, maxMapperDepth)
}

func parentDiskFromBlockDev(root, devPath string) (string, error) {
// First check if its some kind of image file instead of a block device
// naively check if the path contains /dev/
if !strings.HasPrefix(devPath, "/dev/") {
Expand All @@ -700,7 +763,7 @@ func parentDiskFromBlockDev(devPath string) (string, error) {
// Get the base name of the device
name := filepath.Base(devPath) // "sda3", "nvme0n1p3", "mmcblk0p2", ...
fmt.Printf("Deriving parent disk for block device %s\n", devPath)
sysClass := filepath.Join("/sys/class/block", name)
sysClass := filepath.Join(root, "sys/class/block", name)
fmt.Printf("Deriving parent disk for block device %s\n", sysClass)

// Read where it points to only if its a symlink
Expand All @@ -715,7 +778,7 @@ func parentDiskFromBlockDev(devPath string) (string, error) {
parentName := filepath.Base(parentDir)

// Sanity check: parent should exist in /sys/class/block
if _, err := os.Stat(filepath.Join("/sys/class/block", parentName)); err != nil {
if _, err := os.Stat(filepath.Join(root, "sys/class/block", parentName)); err != nil {
return "", fmt.Errorf("derive parent disk for %s (got %s): %w", devPath, parentName, err)
}

Expand Down
146 changes: 146 additions & 0 deletions pkg/plugins/layout_block_device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package plugins

import (
"os"
"path/filepath"
"testing"
)

// fakeSysfs builds the parts of /sys that the block device helpers read, under
// a temporary root. It lays out the same symlinks the kernel does: every
// /sys/class/block/<name> points into /sys/devices, a partition lives in the
// directory of its disk, and a device-mapper device has a dm/ directory and
// one slaves/ entry per device it sits on.
type fakeSysfs struct {
t *testing.T
root string
}

func newFakeSysfs(t *testing.T) *fakeSysfs {
t.Helper()
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "sys/class/block"), 0o755); err != nil {
t.Fatal(err)
}
return &fakeSysfs{t: t, root: root}
}

// link points /sys/class/block/<name> at the device directory devDir, given
// relative to /sys/devices.
func (f *fakeSysfs) link(name, devDir string) string {
f.t.Helper()
target := filepath.Join(f.root, "sys/devices", devDir)
if err := os.MkdirAll(target, 0o755); err != nil {
f.t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(f.root, "sys/class/block", name)); err != nil {
f.t.Fatal(err)
}
return target
}

func (f *fakeSysfs) disk(name, parentDir string) {
f.link(name, filepath.Join(parentDir, "block", name))
}

func (f *fakeSysfs) partition(disk, name, parentDir string) {
f.link(name, filepath.Join(parentDir, "block", disk, name))
}

func (f *fakeSysfs) mapper(name string, slaves ...string) {
f.t.Helper()
dir := f.link(name, filepath.Join("virtual/block", name))
for _, sub := range []string{"dm", "slaves"} {
if err := os.MkdirAll(filepath.Join(dir, sub), 0o755); err != nil {
f.t.Fatal(err)
}
}
for _, slave := range slaves {
target, err := filepath.EvalSymlinks(filepath.Join(f.root, "sys/class/block", slave))
if err != nil {
f.t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(dir, "slaves", slave)); err != nil {
f.t.Fatal(err)
}
}
}

func TestBackingDeviceAndParentDisk(t *testing.T) {
f := newFakeSysfs(t)
pci := "pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0"
nvme := "pci0000:00/0000:00:01.0/nvme/nvme0"
f.disk("sda", pci)
f.partition("sda", "sda5", pci)
f.disk("nvme0n1", nvme)
f.partition("nvme0n1", "nvme0n1p3", nvme)
f.disk("sdb", pci)
f.partition("sdb", "sdb1", pci)
// LUKS on a partition, as Kairos sets up an encrypted COS_PERSISTENT.
f.mapper("dm-0", "sda5")
// LUKS on top of an LVM volume on a partition.
f.mapper("dm-1", "nvme0n1p3")
f.mapper("dm-2", "dm-1")
// A mapping that spans two devices has no single partition to grow.
f.mapper("dm-3", "sda5", "sdb1")

tests := []struct {
name string
dev string
wantBacking string
wantMapper bool
wantDisk string
wantErr bool
}{
{name: "plain partition", dev: "/dev/sda5", wantBacking: "/dev/sda5", wantDisk: "/dev/sda"},
{name: "nvme partition", dev: "/dev/nvme0n1p3", wantBacking: "/dev/nvme0n1p3", wantDisk: "/dev/nvme0n1"},
{name: "luks mapping on a partition", dev: "/dev/dm-0", wantBacking: "/dev/sda5", wantMapper: true, wantDisk: "/dev/sda"},
{name: "stacked mappings", dev: "/dev/dm-2", wantBacking: "/dev/nvme0n1p3", wantMapper: true, wantDisk: "/dev/nvme0n1"},
{name: "mapping on two devices", dev: "/dev/dm-3", wantMapper: true, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
backing, isMapper, err := backingDevice(f.root, tt.dev)
if tt.wantErr {
if err == nil {
t.Fatalf("backingDevice(%s) = %s, want an error", tt.dev, backing)
}
return
}
if err != nil {
t.Fatalf("backingDevice(%s): %v", tt.dev, err)
}
if backing != tt.wantBacking || isMapper != tt.wantMapper {
t.Fatalf("backingDevice(%s) = %s, %v; want %s, %v", tt.dev, backing, isMapper, tt.wantBacking, tt.wantMapper)
}
disk, err := parentDiskFromBlockDev(f.root, backing)
if err != nil {
t.Fatalf("parentDiskFromBlockDev(%s): %v", backing, err)
}
if disk != tt.wantDisk {
t.Fatalf("parentDiskFromBlockDev(%s) = %s, want %s", backing, disk, tt.wantDisk)
}
})
}
}

// A device-mapper device has no disk above it in sysfs: its directory is
// /sys/devices/virtual/block/dm-0, so the old code took "block" as the disk.
// That is the error an encrypted persistent partition logged on every boot.
func TestParentDiskOfMapperFails(t *testing.T) {
f := newFakeSysfs(t)
f.disk("sda", "pci0000:00")
f.partition("sda", "sda5", "pci0000:00")
f.mapper("dm-0", "sda5")
if disk, err := parentDiskFromBlockDev(f.root, "/dev/dm-0"); err == nil {
t.Fatalf("parentDiskFromBlockDev(/dev/dm-0) = %s, want an error", disk)
}
}

func TestBackingDeviceOfImageFile(t *testing.T) {
f := newFakeSysfs(t)
backing, isMapper, err := backingDevice(f.root, "/tmp/disk.img")
if err != nil || isMapper || backing != "/tmp/disk.img" {
t.Fatalf("backingDevice(/tmp/disk.img) = %s, %v, %v; want it unchanged", backing, isMapper, err)
}
}
Loading