From 612571a29602e5ea24d565ae26a6e1b95cb660eb Mon Sep 17 00:00:00 2001 From: Kai Lueke Date: Fri, 29 Sep 2023 18:06:09 +0200 Subject: [PATCH] sgdisk: Run partx after partition changes The sgdisk tool does not update the kernel partition table with BLKPG in contrast to other similar tools but only uses BLKRRPART which fails as soon as one partition of the disk is mounted. Update the kernel partition table with partx when we know that a partition of the disk is in use. --- docs/release-notes.md | 2 ++ dracut/30ignition/module-setup.sh | 1 + internal/distro/distro.go | 2 ++ internal/exec/stages/disks/partitions.go | 12 ++++++++++++ 4 files changed, 17 insertions(+) diff --git a/docs/release-notes.md b/docs/release-notes.md index 73723db59f..a8368e2533 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -19,11 +19,13 @@ nav_order: 9 ### Changes - Require Go 1.19+ +- The Dracut module now installs partx ### Bug fixes - Prevent races with udev after disk editing - Don't fail to wipe partition table if it's corrupted +- Force partition table update after repartitioning, even if one partition is already mounted ## Ignition 2.16.2 (2023-07-12) diff --git a/dracut/30ignition/module-setup.sh b/dracut/30ignition/module-setup.sh index 15bb7276bb..1e8b896c95 100755 --- a/dracut/30ignition/module-setup.sh +++ b/dracut/30ignition/module-setup.sh @@ -33,6 +33,7 @@ install() { mkfs.xfs \ mkswap \ sgdisk \ + partx \ useradd \ userdel \ usermod \ diff --git a/internal/distro/distro.go b/internal/distro/distro.go index 61ca87aed9..15cc1aeafc 100644 --- a/internal/distro/distro.go +++ b/internal/distro/distro.go @@ -37,6 +37,7 @@ var ( mdadmCmd = "mdadm" mountCmd = "mount" sgdiskCmd = "sgdisk" + partxCmd = "partx" modprobeCmd = "modprobe" udevadmCmd = "udevadm" usermodCmd = "usermod" @@ -90,6 +91,7 @@ func GroupdelCmd() string { return groupdelCmd } func MdadmCmd() string { return mdadmCmd } func MountCmd() string { return mountCmd } func SgdiskCmd() string { return sgdiskCmd } +func PartxCmd() string { return partxCmd } func ModprobeCmd() string { return modprobeCmd } func UdevadmCmd() string { return udevadmCmd } func UsermodCmd() string { return usermodCmd } diff --git a/internal/exec/stages/disks/partitions.go b/internal/exec/stages/disks/partitions.go index e982b346ea..f42845f123 100644 --- a/internal/exec/stages/disks/partitions.go +++ b/internal/exec/stages/disks/partitions.go @@ -24,6 +24,7 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "regexp" "sort" @@ -32,6 +33,7 @@ import ( cutil "github.com/coreos/ignition/v2/config/util" "github.com/coreos/ignition/v2/config/v3_5_experimental/types" + "github.com/coreos/ignition/v2/internal/distro" "github.com/coreos/ignition/v2/internal/exec/util" "github.com/coreos/ignition/v2/internal/sgdisk" ) @@ -536,6 +538,16 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { return fmt.Errorf("commit failure: %v", err) } + if len(activeParts) > 0 { + // In contrast to similar tools, sgdisk does not trigger the update of the + // kernel partition table with BLKPG but only uses BLKRRPART which fails + // as soon as one partition of the disk is mounted + cmd := exec.Command(distro.PartxCmd(), "-u", "-", blockDevResolved) + if _, err := s.Logger.LogCmd(cmd, "triggering partition table reread on %q", devAlias); err != nil { + return fmt.Errorf("re-reading partitions failed: %v", err) + } + } + // It's best to wait here for the /dev/ABC entries to be // (re)created, not only for other parts of the initramfs but // also because s.waitOnDevices() can still race with udev's