From 22aad9c87cf56b405f2d223827bf8eb26db5bc18 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 30 Jul 2013 11:05:06 -0700 Subject: [PATCH] builder/amazon/chroot: mount the root device --- builder/amazon/chroot/builder.go | 1 + builder/amazon/chroot/step_mount_device.go | 71 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 builder/amazon/chroot/step_mount_device.go diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index ec344d938..9e03e6771 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -96,6 +96,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepSourceAMIInfo{}, &StepCreateVolume{}, &StepAttachVolume{}, + &StepMountDevice{}, } // Run! diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go new file mode 100644 index 000000000..4655c37c9 --- /dev/null +++ b/builder/amazon/chroot/step_mount_device.go @@ -0,0 +1,71 @@ +package chroot + +import ( + "bytes" + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "log" + "os" + "os/exec" +) + +// StepMountDevice mounts the attached device. +// +// Produces: +// mount_path string - The location where the volume was mounted. +type StepMountDevice struct { + mountPath string +} + +func (s *StepMountDevice) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*Config) + ui := state["ui"].(packer.Ui) + device := state["device"].(string) + + mountPath := config.MountPath + log.Printf("Mount path: %s", mountPath) + + if err := os.MkdirAll(mountPath, 0755); err != nil { + err := fmt.Errorf("Error creating mount directory: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + ui.Say("Mounting the root device...") + stderr := new(bytes.Buffer) + cmd := exec.Command("mount", device, mountPath) + cmd.Stderr = stderr + if err := cmd.Run(); err != nil { + err := fmt.Errorf( + "Error mounting root volume: %s\nStderr: %s", err, stderr.String()) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *StepMountDevice) Cleanup(state map[string]interface{}) { + if s.mountPath == "" { + return + } + + 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) + if err := cmd.Run(); err != nil { + ui.Error(fmt.Sprintf( + "Error unmounting root device: %s", err)) + return + } +}