From 88c94cc987b327cf7d12ac52e5ff62b7da052804 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 17 Jun 2020 02:04:45 -0700 Subject: [PATCH] Fix 8904 (#9435) --- builder/virtualbox/common/driver_4_2.go | 2 +- builder/virtualbox/vm/step_create_snapshot.go | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index a10d45871..b710720dc 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -325,7 +325,7 @@ func (d *VBox42Driver) LoadSnapshots(vmName string) (*VBoxSnapshot, error) { var rootNode *VBoxSnapshot stdoutString, err := d.VBoxManageWithOutput("snapshot", vmName, "list", "--machinereadable") if stdoutString == "This machine does not have any snapshots" { - return rootNode, fmt.Errorf("VM %s does not have any snapshots.", vmName) + return rootNode, nil } if nil != err { return nil, err diff --git a/builder/virtualbox/vm/step_create_snapshot.go b/builder/virtualbox/vm/step_create_snapshot.go index 95c676069..8252c6f82 100644 --- a/builder/virtualbox/vm/step_create_snapshot.go +++ b/builder/virtualbox/vm/step_create_snapshot.go @@ -40,18 +40,22 @@ func (s *StepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu return multistep.ActionHalt } - currentSnapshot := snapshotTree.GetCurrentSnapshot() - targetSnapshot := currentSnapshot.GetChildWithName(s.TargetSnapshot) - if nil != targetSnapshot { - log.Printf("Deleting existing target snapshot %s", s.TargetSnapshot) - err = driver.DeleteSnapshot(s.Name, targetSnapshot) - if nil != err { - err = fmt.Errorf("Unable to delete snapshot %s from VM %s: %s", s.TargetSnapshot, s.Name, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + // Remove any snapshot with the target's name, if present. + if snapshotTree != nil { + currentSnapshot := snapshotTree.GetCurrentSnapshot() + targetSnapshot := currentSnapshot.GetChildWithName(s.TargetSnapshot) + if nil != targetSnapshot { + log.Printf("Deleting existing target snapshot %s", s.TargetSnapshot) + err = driver.DeleteSnapshot(s.Name, targetSnapshot) + if nil != err { + err = fmt.Errorf("Unable to delete snapshot %s from VM %s: %s", s.TargetSnapshot, s.Name, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } } + err = driver.CreateSnapshot(s.Name, s.TargetSnapshot) if err != nil { err := fmt.Errorf("Error creating snaphot VM: %s", err)