2013-07-30 15:08:16 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-30 15:08:16 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-04-17 15:35:26 -04:00
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-30 15:08:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepMountExtra mounts the attached device.
|
|
|
|
//
|
|
|
|
// Produces:
|
2013-07-30 19:41:29 -04:00
|
|
|
// mount_extra_cleanup CleanupFunc - To perform early cleanup
|
2013-07-30 15:08:16 -04:00
|
|
|
type StepMountExtra struct {
|
2019-05-31 14:49:35 -04:00
|
|
|
ChrootMounts [][]string
|
|
|
|
mounts []string
|
2013-07-30 15:08:16 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepMountExtra) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
mountPath := state.Get("mount_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-07-30 15:08:16 -04:00
|
|
|
|
2019-05-31 14:49:35 -04:00
|
|
|
s.mounts = make([]string, 0, len(s.ChrootMounts))
|
2013-07-30 15:08:16 -04:00
|
|
|
|
|
|
|
ui.Say("Mounting additional paths within the chroot...")
|
2019-05-31 14:49:35 -04:00
|
|
|
for _, mountInfo := range s.ChrootMounts {
|
2013-07-30 15:08:16 -04:00
|
|
|
innerPath := mountPath + mountInfo[2]
|
|
|
|
|
|
|
|
if err := os.MkdirAll(innerPath, 0755); err != nil {
|
|
|
|
err := fmt.Errorf("Error creating mount directory: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 15:08:16 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-30 15:18:19 -04:00
|
|
|
flags := "-t " + mountInfo[0]
|
|
|
|
if mountInfo[0] == "bind" {
|
|
|
|
flags = "--bind"
|
|
|
|
}
|
|
|
|
|
2013-07-30 15:08:16 -04:00
|
|
|
ui.Message(fmt.Sprintf("Mounting: %s", mountInfo[2]))
|
|
|
|
stderr := new(bytes.Buffer)
|
2013-09-30 12:33:57 -04:00
|
|
|
mountCommand, err := wrappedCommand(fmt.Sprintf(
|
2013-09-27 06:54:53 -04:00
|
|
|
"mount %s %s %s",
|
2013-07-30 15:18:19 -04:00
|
|
|
flags,
|
2013-07-30 15:08:16 -04:00
|
|
|
mountInfo[1],
|
2013-09-30 12:33:57 -04:00
|
|
|
innerPath))
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating mount command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd := common.ShellCommand(mountCommand)
|
2013-07-30 15:08:16 -04:00
|
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"Error mounting: %s\nStderr: %s", err, stderr.String())
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 15:08:16 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mounts = append(s.mounts, innerPath)
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("mount_extra_cleanup", s)
|
2013-07-30 15:08:16 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepMountExtra) Cleanup(state multistep.StateBag) {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 19:41:29 -04:00
|
|
|
|
|
|
|
if err := s.CleanupFunc(state); err != nil {
|
|
|
|
ui.Error(err.Error())
|
2013-07-30 15:08:16 -04:00
|
|
|
return
|
|
|
|
}
|
2013-07-30 19:41:29 -04:00
|
|
|
}
|
2013-07-30 15:08:16 -04:00
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error {
|
2013-07-30 19:41:29 -04:00
|
|
|
if s.mounts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-07-30 15:08:16 -04:00
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-07-30 19:41:29 -04:00
|
|
|
for len(s.mounts) > 0 {
|
|
|
|
var path string
|
|
|
|
lastIndex := len(s.mounts) - 1
|
|
|
|
path, s.mounts = s.mounts[lastIndex], s.mounts[:lastIndex]
|
2015-04-17 15:35:26 -04:00
|
|
|
|
|
|
|
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)
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd := common.ShellCommand(grepCommand)
|
2015-04-17 15:35:26 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 15:37:52 -04:00
|
|
|
|
2013-09-30 12:33:57 -04:00
|
|
|
unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", path))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating unmount command: %s", err)
|
|
|
|
}
|
2013-07-30 16:21:25 -04:00
|
|
|
|
2015-04-17 15:35:26 -04:00
|
|
|
stderr = new(bytes.Buffer)
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd = common.ShellCommand(unmountCommand)
|
2013-07-30 16:21:25 -04:00
|
|
|
cmd.Stderr = stderr
|
2013-07-30 15:08:16 -04:00
|
|
|
if err := cmd.Run(); err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error unmounting device: %s\nStderr: %s", err, stderr.String())
|
2013-07-30 15:08:16 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-30 19:41:29 -04:00
|
|
|
|
|
|
|
s.mounts = nil
|
|
|
|
return nil
|
2013-07-30 15:08:16 -04:00
|
|
|
}
|