Skip to content

Commit

Permalink
Merge pull request containers#16681 from n1hility/atomic-writeconfig
Browse files Browse the repository at this point in the history
Improve atomicity of VM state persistence on Windows
  • Loading branch information
openshift-merge-robot authored Dec 1, 2022
2 parents 582394f + 009f5ec commit 51deb32
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,16 @@ func (p *Provider) NewMachine(opts machine.InitOptions) (machine.VM, error) {
}

func getConfigPath(name string) (string, error) {
return getConfigPathExt(name, "json")
}

func getConfigPathExt(name string, extension string) (string, error) {
vmConfigDir, err := machine.GetConfDir(vmtype)
if err != nil {
return "", err
}
return filepath.Join(vmConfigDir, name+".json"), nil

return filepath.Join(vmConfigDir, fmt.Sprintf("%s.%s", name, extension)), nil
}

// LoadByName reads a json file that describes a known qemu vm
Expand Down Expand Up @@ -424,14 +429,24 @@ 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, "", " ")
if err != nil {
return err
return fmt.Errorf(format, err)
}
if err := os.WriteFile(jsonFile, b, 0644); err != nil {
return fmt.Errorf("could not write machine json config: %w", err)

if err := os.WriteFile(tmpFile, b, 0644); err != nil {
return fmt.Errorf(format, err)
}

if err := os.Rename(tmpFile, jsonFile); err != nil {
return fmt.Errorf(format, err)
}

return nil
Expand Down

0 comments on commit 51deb32

Please sign in to comment.