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 <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2024-02-28 09:44:41 +01:00
parent 9d1704396a
commit f8abd7ff75

View File

@ -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 {