From fd5f4c61ae419f3abe324876a532f011370f1e69 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Fri, 17 Apr 2015 12:35:26 -0700 Subject: [PATCH] Added a call to grep for path in /proc/mounts before attempting to umount. If path is not there, it is already unmounted. --- builder/amazon/chroot/step_mount_extra.go | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/builder/amazon/chroot/step_mount_extra.go b/builder/amazon/chroot/step_mount_extra.go index d589d6c74..aa63b4b61 100644 --- a/builder/amazon/chroot/step_mount_extra.go +++ b/builder/amazon/chroot/step_mount_extra.go @@ -6,6 +6,8 @@ import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" "os" + "os/exec" + "syscall" ) // StepMountExtra mounts the attached device. @@ -90,13 +92,37 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error { var path string lastIndex := len(s.mounts) - 1 path, s.mounts = s.mounts[lastIndex], s.mounts[:lastIndex] + + grepCommand, err := wrappedCommand(fmt.Sprintf("grep %s /proc/mounts", path)) + if err != nil { + return fmt.Errorf("Error creating grep command: %s", err) + } + + // Before attempting to unmount, + // check to see if path is already unmounted + stderr := new(bytes.Buffer) + cmd := ShellCommand(grepCommand) + cmd.Stderr = stderr + if err := cmd.Run(); err != nil { + if exitError, ok := err.(*exec.ExitError); ok { + if status, ok := exitError.Sys().(syscall.WaitStatus); ok { + exitStatus := status.ExitStatus() + if exitStatus == 1 { + // path has already been unmounted + // just skip this path + continue + } + } + } + } + unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", path)) if err != nil { return fmt.Errorf("Error creating unmount command: %s", err) } - stderr := new(bytes.Buffer) - cmd := ShellCommand(unmountCommand) + stderr = new(bytes.Buffer) + cmd = ShellCommand(unmountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { return fmt.Errorf(