-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sys-apps/ignition: Use new upstream patch
The patch was reworked to use partx which already is in the initrd and to have safety checks for disks in use.
- Loading branch information
Showing
5 changed files
with
293 additions
and
68 deletions.
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
...overlay/sys-apps/ignition/files/0022-disks-Refuse-to-modify-disks-partitions-in-use.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
From 66f58ea2f20455351ae7dbd3e0529950e75c47d3 Mon Sep 17 00:00:00 2001 | ||
From: Kai Lueke <[email protected]> | ||
Date: Mon, 20 Nov 2023 15:47:24 +0100 | ||
Subject: [PATCH 1/2] disks: Refuse to modify disks/partitions in use | ||
|
||
When a partition or the whole disk is in use, sgdisk should not execute | ||
the destructive operation. | ||
Add a check that errors out when a disk in use or a partition in use is | ||
to be destroyed. | ||
--- | ||
internal/exec/stages/disks/partitions.go | 132 +++++++++++++++++++++++ | ||
1 file changed, 132 insertions(+) | ||
|
||
diff --git a/internal/exec/stages/disks/partitions.go b/internal/exec/stages/disks/partitions.go | ||
index cb55c376..e1cc11d3 100644 | ||
--- a/internal/exec/stages/disks/partitions.go | ||
+++ b/internal/exec/stages/disks/partitions.go | ||
@@ -317,11 +317,122 @@ func (p PartitionList) Swap(i, j int) { | ||
p[i], p[j] = p[j], p[i] | ||
} | ||
|
||
+func blockDevHeld(blockDev string) (bool, error) { | ||
+ blockDevResolved, err := filepath.EvalSymlinks(blockDev) | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to resolve %q: %v", blockDev, err) | ||
+ } | ||
+ _, blockDevNode := filepath.Split(blockDevResolved) | ||
+ | ||
+ holdersDir := fmt.Sprintf("/sys/class/block/%s/holders/", blockDevNode) | ||
+ entries, err := ioutil.ReadDir(holdersDir) | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to retrieve holders of %q: %v", blockDev, err) | ||
+ } | ||
+ return len(entries) > 0, nil | ||
+} | ||
+ | ||
+func blockDevMounted(blockDev string) (bool, error) { | ||
+ blockDevResolved, err := filepath.EvalSymlinks(blockDev) | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to resolve %q: %v", blockDev, err) | ||
+ } | ||
+ | ||
+ mounts, err := os.Open("/proc/mounts") | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to open mounts: %v", err) | ||
+ } | ||
+ scanner := bufio.NewScanner(mounts) | ||
+ for scanner.Scan() { | ||
+ mountSource := strings.Split(scanner.Text(), " ")[0] | ||
+ if strings.Contains(mountSource, "/") { | ||
+ mountSourceResolved, err := filepath.EvalSymlinks(mountSource) | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to resolve %q: %v", mountSource, err) | ||
+ } | ||
+ if mountSourceResolved == blockDevResolved { | ||
+ return true, nil | ||
+ } | ||
+ } | ||
+ } | ||
+ if err := scanner.Err(); err != nil { | ||
+ return false, fmt.Errorf("failed to check mounts for %q: %v", blockDev, err) | ||
+ } | ||
+ return false, nil | ||
+} | ||
+ | ||
+func blockDevPartitions(blockDev string) ([]string, error) { | ||
+ blockDevResolved, err := filepath.EvalSymlinks(blockDev) | ||
+ if err != nil { | ||
+ return nil, fmt.Errorf("failed to resolve %q: %v", blockDev, err) | ||
+ } | ||
+ _, blockDevNode := filepath.Split(blockDevResolved) | ||
+ | ||
+ // This also works for extended MBR partitions | ||
+ sysDir := fmt.Sprintf("/sys/class/block/%s/", blockDevNode) | ||
+ entries, err := ioutil.ReadDir(sysDir) | ||
+ if err != nil { | ||
+ return nil, fmt.Errorf("failed to retrieve sysfs entries of %q: %v", blockDev, err) | ||
+ } | ||
+ var partitions []string | ||
+ for _, entry := range entries { | ||
+ if strings.HasPrefix(entry.Name(), blockDevNode+"p") { | ||
+ partitions = append(partitions, "/dev/"+entry.Name()) | ||
+ } | ||
+ } | ||
+ | ||
+ return partitions, nil | ||
+} | ||
+ | ||
+func blockDevInUse(blockDev string) (bool, []string, error) { | ||
+ // Note: This ignores swap and LVM usage | ||
+ inUse := false | ||
+ held, err := blockDevHeld(blockDev) | ||
+ if err != nil { | ||
+ return false, nil, fmt.Errorf("failed to check if %q is held: %v", blockDev, err) | ||
+ } | ||
+ mounted, err := blockDevMounted(blockDev) | ||
+ if err != nil { | ||
+ return false, nil, fmt.Errorf("failed to check if %q is mounted: %v", blockDev, err) | ||
+ } | ||
+ inUse = held || mounted | ||
+ partitions, err := blockDevPartitions(blockDev) | ||
+ if err != nil { | ||
+ return false, nil, fmt.Errorf("failed to retrieve partitions of %q: %v", blockDev, err) | ||
+ } | ||
+ var activePartitions []string | ||
+ for _, partition := range partitions { | ||
+ held, err := blockDevHeld(partition) | ||
+ if err != nil { | ||
+ return false, nil, fmt.Errorf("failed to check if %q is held: %v", partition, err) | ||
+ } | ||
+ mounted, err := blockDevMounted(partition) | ||
+ if err != nil { | ||
+ return false, nil, fmt.Errorf("failed to check if %q is mounted: %v", partition, err) | ||
+ } | ||
+ if held || mounted { | ||
+ activePartitions = append(activePartitions, partition) | ||
+ inUse = true | ||
+ } | ||
+ } | ||
+ return inUse, activePartitions, nil | ||
+} | ||
+ | ||
// partitionDisk partitions devAlias according to the spec given by dev | ||
func (s stage) partitionDisk(dev types.Disk, devAlias string) error { | ||
+ inUse, activeParts, err := blockDevInUse(devAlias) | ||
+ if err != nil { | ||
+ return fmt.Errorf("failed usage check on %q: %v", devAlias, err) | ||
+ } | ||
+ if inUse && len(activeParts) == 0 { | ||
+ return fmt.Errorf("refusing to operate on directly active disk %q", devAlias) | ||
+ } | ||
if cutil.IsTrue(dev.WipeTable) { | ||
op := sgdisk.Begin(s.Logger, devAlias) | ||
s.Logger.Info("wiping partition table requested on %q", devAlias) | ||
+ if len(activeParts) > 0 { | ||
+ return fmt.Errorf("refusing to wipe active disk %q", devAlias) | ||
+ } | ||
op.WipeTable(true) | ||
if err := op.Commit(); err != nil { | ||
// `sgdisk --zap-all` will exit code 2 if the table was corrupted; retry it | ||
@@ -343,6 +454,11 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { | ||
return err | ||
} | ||
|
||
+ blockDevResolved, err := filepath.EvalSymlinks(devAlias) | ||
+ if err != nil { | ||
+ return false, fmt.Errorf("failed to resolve %q: %v", devAlias, err) | ||
+ } | ||
+ | ||
// get a list of parititions that have size and start 0 replaced with the real sizes | ||
// that would be used if all specified partitions were to be created anew. | ||
// Also calculate sectors for all of the start/size values. | ||
@@ -361,16 +477,26 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { | ||
matches := exists && matchErr == nil | ||
wipeEntry := cutil.IsTrue(part.WipePartitionEntry) | ||
|
||
+ var modification bool | ||
+ var partInUse bool | ||
+ for _, activePart := range activeParts { | ||
+ if activePart == fmt.Sprintf("%sp%d", blockDevResolved, part.Number) { | ||
+ partInUse = true | ||
+ } | ||
+ } | ||
+ | ||
// This is a translation of the matrix in the operator notes. | ||
switch { | ||
case !exists && !shouldExist: | ||
s.Logger.Info("partition %d specified as nonexistant and no partition was found. Success.", part.Number) | ||
case !exists && shouldExist: | ||
op.CreatePartition(part) | ||
+ modification = true | ||
case exists && !shouldExist && !wipeEntry: | ||
return fmt.Errorf("partition %d exists but is specified as nonexistant and wipePartitionEntry is false", part.Number) | ||
case exists && !shouldExist && wipeEntry: | ||
op.DeletePartition(part.Number) | ||
+ modification = true | ||
case exists && shouldExist && matches: | ||
s.Logger.Info("partition %d found with correct specifications", part.Number) | ||
case exists && shouldExist && !wipeEntry && !matches: | ||
@@ -383,6 +509,7 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { | ||
part.Label = &info.Label | ||
part.StartSector = &info.StartSector | ||
op.CreatePartition(part) | ||
+ modification = true | ||
} else { | ||
return fmt.Errorf("Partition %d didn't match: %v", part.Number, matchErr) | ||
} | ||
@@ -390,10 +517,15 @@ func (s stage) partitionDisk(dev types.Disk, devAlias string) error { | ||
s.Logger.Info("partition %d did not meet specifications, wiping partition entry and recreating", part.Number) | ||
op.DeletePartition(part.Number) | ||
op.CreatePartition(part) | ||
+ modification = true | ||
default: | ||
// unfortunatey, golang doesn't check that all cases are handled exhaustively | ||
return fmt.Errorf("Unreachable code reached when processing partition %d. golang--", part.Number) | ||
} | ||
+ | ||
+ if partInUse && modification { | ||
+ return fmt.Errorf("refusing to modfiy active partition %d on %q", part.Number, devAlias) | ||
+ } | ||
} | ||
|
||
if err := op.Commit(); err != nil { | ||
-- | ||
2.42.0 | ||
|
67 changes: 0 additions & 67 deletions
67
...s-overlay/sys-apps/ignition/files/0022-sgdisk-Run-partprobe-after-partition-changes.patch
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
...oreos-overlay/sys-apps/ignition/files/0023-sgdisk-Run-partx-after-partition-changes.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
From f76bf5f82fcefc71d14ae7ef3e7d8cef4cd412f5 Mon Sep 17 00:00:00 2001 | ||
From: Kai Lueke <[email protected]> | ||
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 | 1 + | ||
dracut/30ignition/module-setup.sh | 1 + | ||
internal/distro/distro.go | 2 ++ | ||
internal/exec/stages/disks/partitions.go | 10 ++++++++++ | ||
4 files changed, 14 insertions(+) | ||
|
||
diff --git a/docs/release-notes.md b/docs/release-notes.md | ||
index db49c304..ce97d2d8 100644 | ||
--- a/docs/release-notes.md | ||
+++ b/docs/release-notes.md | ||
@@ -14,6 +14,7 @@ nav_order: 9 | ||
|
||
### Changes | ||
|
||
+- The Dracut module now installs partx | ||
|
||
### Bug fixes | ||
|
||
diff --git a/dracut/30ignition/module-setup.sh b/dracut/30ignition/module-setup.sh | ||
index d7a5cfcd..86fd892e 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 9e96166e..f295a572 100644 | ||
--- a/internal/distro/distro.go | ||
+++ b/internal/distro/distro.go | ||
@@ -44,6 +44,7 @@ var ( | ||
mdadmCmd = "mdadm" | ||
mountCmd = "mount" | ||
sgdiskCmd = "sgdisk" | ||
+ partxCmd = "partx" | ||
modprobeCmd = "modprobe" | ||
udevadmCmd = "udevadm" | ||
usermodCmd = "usermod" | ||
@@ -100,6 +101,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 009dcb86..ac2cb70f 100644 | ||
--- a/internal/exec/stages/disks/partitions.go | ||
+++ b/internal/exec/stages/disks/partitions.go | ||
@@ -527,6 +527,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 := op.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 | ||
-- | ||
2.42.0 | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters