diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index 677cb16b8b..371daa57a1 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -18,7 +18,6 @@ import ( "github.com/containers/podman/v4/pkg/machine" "github.com/containers/podman/v4/pkg/util" "github.com/containers/podman/v4/utils" - "github.com/containers/storage/pkg/ioutils" "github.com/docker/go-units" "github.com/sirupsen/logrus" ) @@ -673,22 +672,7 @@ func (m *HyperVMachine) forwardSocketPath() (*machine.VMFile, error) { func (m *HyperVMachine) writeConfig() error { // Write the JSON file - opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true} - w, err := ioutils.NewAtomicFileWriterWithOpts(m.ConfigPath.GetPath(), 0644, opts) - if err != nil { - return err - } - defer w.Close() - - enc := json.NewEncoder(w) - enc.SetIndent("", " ") - - if err := enc.Encode(m); err != nil { - return err - } - - // Commit the changes to disk if no errors - return w.Commit() + return machine.WriteConfig(m.ConfigPath.Path, m) } func (m *HyperVMachine) setRootful(rootful bool) error { diff --git a/pkg/machine/machine_common.go b/pkg/machine/machine_common.go index a447de8f12..27eabb5035 100644 --- a/pkg/machine/machine_common.go +++ b/pkg/machine/machine_common.go @@ -4,10 +4,13 @@ package machine import ( + "encoding/json" "fmt" "net/url" "os" "strconv" + + "github.com/containers/storage/pkg/ioutils" ) // getDevNullFiles returns pointers to Read-only and Write-only DevNull files @@ -143,3 +146,23 @@ func SetRootful(rootful bool, name, rootfulName string) error { } return nil } + +// WriteConfig writes the machine's JSON config file +func WriteConfig(configPath string, v VM) error { + opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true} + w, err := ioutils.NewAtomicFileWriterWithOpts(configPath, 0644, opts) + if err != nil { + return err + } + defer w.Close() + + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + + if err := enc.Encode(v); err != nil { + return err + } + + // Commit the changes to disk if no errors + return w.Commit() +} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index 67aac835ea..000a6b3bff 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -25,7 +25,6 @@ import ( "github.com/containers/podman/v4/pkg/machine" "github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/util" - "github.com/containers/storage/pkg/ioutils" "github.com/containers/storage/pkg/lockfile" "github.com/digitalocean/go-qemu/qmp" "github.com/sirupsen/logrus" @@ -1523,22 +1522,7 @@ func (v *MachineVM) writeConfig() error { return err } // Write the JSON file - opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true} - w, err := ioutils.NewAtomicFileWriterWithOpts(v.ConfigPath.GetPath(), 0644, opts) - if err != nil { - return err - } - defer w.Close() - - enc := json.NewEncoder(w) - enc.SetIndent("", " ") - - if err := enc.Encode(v); err != nil { - return err - } - - // Commit the changes to disk if no errors - return w.Commit() + return machine.WriteConfig(v.ConfigPath.Path, v) } // getImageFile wrapper returns the path to the image used diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 2ff886ed2e..65cbcd3629 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -22,7 +22,6 @@ import ( "github.com/containers/podman/v4/pkg/machine/wsl/wutil" "github.com/containers/podman/v4/pkg/util" "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" @@ -405,28 +404,7 @@ 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 - - opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true} - w, err := ioutils.NewAtomicFileWriterWithOpts(jsonFile, 0644, opts) - if err != nil { - return fmt.Errorf(format, err) - } - defer w.Close() - - enc := json.NewEncoder(w) - enc.SetIndent("", " ") - if err := enc.Encode(v); err != nil { - return fmt.Errorf(format, err) - } - - // Commit the changes to disk if no error has occurred - if err := w.Commit(); err != nil { - return fmt.Errorf(format, err) - } - - return nil + return machine.WriteConfig(v.ConfigPath, v) } func setupConnections(v *MachineVM, opts machine.InitOptions) error {