From 54d020b8c3349219a6b88385c4cdb3ff2f72b199 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 30 Jul 2013 11:27:55 -0700 Subject: [PATCH] builder/amazon/chroot: use mountcommand configs --- builder/amazon/chroot/builder.go | 33 +++++++++++++++++++--- builder/amazon/chroot/step_mount_device.go | 13 ++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 9e03e6771..cf8848903 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -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()...) diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go index 4655c37c9..dee71b456 100644 --- a/builder/amazon/chroot/step_mount_device.go +++ b/builder/amazon/chroot/step_mount_device.go @@ -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))