Merge pull request #18035 from n1hility/flush-config

Update podman to use atomic container and machine config updates
This commit is contained in:
OpenShift Merge Robot
2023-04-11 02:53:42 -04:00
committed by GitHub
2 changed files with 21 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/utils"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/ioutils"
"github.com/digitalocean/go-qemu/qmp"
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
@ -1560,14 +1561,22 @@ func (v *MachineVM) writeConfig() error {
return err
}
// Write the JSON file
b, err := json.MarshalIndent(v, "", " ")
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(v.ConfigPath.GetPath(), 0644, opts)
if err != nil {
return err
}
if err := os.WriteFile(v.ConfigPath.GetPath(), b, 0644); err != nil {
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return err
}
return nil
// Commit the changes to disk if no errors
return w.Commit()
}
// getImageFile wrapper returns the path to the image used

View File

@ -22,6 +22,7 @@ import (
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/utils"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/ioutils"
"github.com/sirupsen/logrus"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@ -450,21 +451,22 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {
func (v *MachineVM) writeConfig() error {
const format = "could not write machine json config: %w"
jsonFile := v.ConfigPath
tmpFile, err := getConfigPathExt(v.Name, "tmp")
if err != nil {
return err
}
b, err := json.MarshalIndent(v, "", " ")
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(jsonFile, 0644, opts)
if err != nil {
return fmt.Errorf(format, err)
}
defer w.Close()
if err := os.WriteFile(tmpFile, b, 0644); err != nil {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return fmt.Errorf(format, err)
}
if err := os.Rename(tmpFile, jsonFile); err != nil {
// Commit the changes to disk if no error has occurred
if err := w.Commit(); err != nil {
return fmt.Errorf(format, err)
}