Skip to content

Commit

Permalink
Merge pull request hashicorp#2 from mikemccracken/2018-02-01/lxc-lv-c…
Browse files Browse the repository at this point in the history
…leanup

clean up LVM lvs on error in executeContainer
  • Loading branch information
mikemccracken committed Feb 2, 2018
2 parents 542f641 + 0e49c7a commit 0a4a3d6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions client/driver/lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,17 @@ func (d *LxcDriver) executeContainer(ctx *ExecContext, c *lxc.Container, task *s
return nil, fmt.Errorf("could not create thin pool snapshot with cmd '%v': %v: %s", lvCreateCmd.Args, err, err.(*exec.ExitError).Stderr), noCleanup
}

removeLVCleanup := func() error {
lvRemoveCmd := exec.Command("lvremove", "-f", baseLvName)
if err := lvRemoveCmd.Run(); err != nil {
return fmt.Errorf("could not remove thin pool snapshot with cmd '%v': %v: %s", lvRemoveCmd.Args, err, err.(*exec.ExitError).Stderr)
}
return nil
}

vgName := baseLvName[:strings.Index(baseLvName, "/")]
if len(vgName) == 0 {
return nil, fmt.Errorf("could not parse volume group name from '%v':, baseLvName")
return nil, fmt.Errorf("could not parse volume group name from '%v':, baseLvName"), removeLVCleanup
}
tr := func(s string) string {
return strings.Replace(s, "-", "--", -1)
Expand All @@ -463,10 +471,13 @@ func (d *LxcDriver) executeContainer(ctx *ExecContext, c *lxc.Container, task *s
newConfigFilePath := filepath.Join(d.lxcPath, c.Name(), "config")
newConfigFile, err := os.Create(newConfigFilePath)
if err != nil {
return nil, fmt.Errorf("unable to create new config file '%s': %v", newConfigFilePath, err), noCleanup
return nil, fmt.Errorf("unable to create new config file '%s': %v", newConfigFilePath, err), removeLVCleanup
}
defer newConfigFile.Close()
removeConfigCleanup := func() error {
if err := removeLVCleanup(); err != nil {
return err
}
return os.Remove(newConfigFilePath)
}

Expand Down

0 comments on commit 0a4a3d6

Please sign in to comment.