builder/amazon/chroot: use mountcommand configs

This commit is contained in:
Mitchell Hashimoto 2013-07-30 11:27:55 -07:00
parent 1c34e35574
commit 54d020b8c3
2 changed files with 34 additions and 12 deletions

View File

@ -24,10 +24,13 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"`
awscommon.AccessConfig `mapstructure:",squash"`
SourceAmi string `mapstructure:"source_ami"`
AttachedDevicePath string `mapstructure:"attached_device_path"`
DevicePath string `mapstructure:"device_path"`
MountPath string `mapstructure:"mount_path"`
AttachedDevicePath string `mapstructure:"attached_device_path"`
ChrootMounts []string `mapstructure:"chroot_mounts"`
DevicePath string `mapstructure:"device_path"`
MountCommand string `mapstructure:"mount_command"`
MountPath string `mapstructure:"mount_path"`
SourceAmi string `mapstructure:"source_ami"`
UnmountCommand string `mapstructure:"unmount_command"`
}
type Builder struct {
@ -42,14 +45,36 @@ func (b *Builder) Prepare(raws ...interface{}) error {
}
// Defaults
if b.config.ChrootMounts == nil {
b.config.ChrootMounts = make([]string, 0)
}
if len(b.config.ChrootMounts) == 0 {
b.config.ChrootMounts = []string{
"{{.MountCommand}} -t proc proc {{.MountPath}}/proc",
"{{.MountCommand}} -t sysfs sysfs {{.MountPath}}/sys",
"{{.MountCommand}} -t bind /dev {{.MountPath}}/dev",
"{{.MountCommand}} -t devpts devpts {{.MountPath}}/dev/pts",
"{{.MountCommand}} -t binfmt_misc binfmt_misc {{.MountPath}}/proc/sys/fs/binfmt_misc",
}
}
if b.config.DevicePath == "" {
b.config.DevicePath = "/dev/sdh"
}
if b.config.MountCommand == "" {
b.config.MountCommand = "mount"
}
if b.config.MountPath == "" {
b.config.MountPath = "/var/packer-amazon-chroot/volumes/{{.Device}}"
}
if b.config.UnmountCommand == "" {
b.config.UnmountCommand = "umount"
}
// Accumulate any errors
errs := common.CheckUnusedConfig(md)
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)

View File

@ -35,7 +35,8 @@ func (s *StepMountDevice) Run(state map[string]interface{}) multistep.StepAction
ui.Say("Mounting the root device...")
stderr := new(bytes.Buffer)
cmd := exec.Command("mount", device, mountPath)
mountCommand := fmt.Sprintf("%s %s %s", config.MountCommand, device, mountPath)
cmd := exec.Command("/bin/sh", "-c", mountCommand)
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := fmt.Errorf(
@ -53,16 +54,12 @@ func (s *StepMountDevice) Cleanup(state map[string]interface{}) {
return
}
config := state["config"].(*Config)
ui := state["ui"].(packer.Ui)
ui.Say("Unmounting the root device...")
path, err := exec.LookPath("umount")
if err != nil {
ui.Error(fmt.Sprintf("Error umounting root device: %s", err))
return
}
cmd := exec.Command(path, s.mountPath)
unmountCommand := fmt.Sprintf("%s %s", config.UnmountCommand, s.mountPath)
cmd := exec.Command("bin/sh", "-c", unmountCommand)
if err := cmd.Run(); err != nil {
ui.Error(fmt.Sprintf(
"Error unmounting root device: %s", err))