Skip to content

Commit

Permalink
machine config: make write atomic
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vrothberg committed Feb 28, 2024
1 parent 9d17043 commit f8abd7f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/machine/vmconfigs/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f8abd7f

Please sign in to comment.