From f8abd7ff751a0ee40c4b75535d6d579494b5d34e Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 28 Feb 2024 09:44:41 +0100 Subject: [PATCH] machine config: make write atomic As indicated in #21849, loading the machine config can flake/fail with an EOF JSON error indicating an incomplete file. Address the issue by atomically writing the config. This way, it is not possible to load an incomplete or partially written file. The lock can be acquired later on to sync state. [NO NEW TESTS NEEDED] as it's a hard-to-hit race. Fixes: #21849 Signed-off-by: Valentin Rothberg --- pkg/machine/vmconfigs/machine.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/machine/vmconfigs/machine.go b/pkg/machine/vmconfigs/machine.go index ceb710a353..0189fab337 100644 --- a/pkg/machine/vmconfigs/machine.go +++ b/pkg/machine/vmconfigs/machine.go @@ -16,6 +16,7 @@ import ( "github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/lock" "github.com/containers/podman/v5/utils" + "github.com/containers/storage/pkg/ioutils" "github.com/sirupsen/logrus" ) @@ -133,7 +134,7 @@ func (mc *MachineConfig) write() error { return err } logrus.Debugf("writing configuration file %q", mc.configPath.Path) - return os.WriteFile(mc.configPath.GetPath(), b, define.DefaultFilePerm) + return ioutils.AtomicWriteFile(mc.configPath.GetPath(), b, define.DefaultFilePerm) } func (mc *MachineConfig) SetRootful(rootful bool) error {