Skip to content

Commit

Permalink
Merge pull request containers#18035 from n1hility/flush-config
Browse files Browse the repository at this point in the history
Update podman to use atomic container and machine config updates
  • Loading branch information
openshift-merge-robot authored Apr 11, 2023
2 parents 820976a + 3b2b144 commit 4857c65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 12 additions & 3 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 4857c65

Please sign in to comment.