github.com/rothwerx/packer@v0.9.0/builder/amazon/chroot/step_mount_device.go (about)

     1  package chroot
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/aws/aws-sdk-go/service/ec2"
    12  	"github.com/mitchellh/multistep"
    13  	"github.com/mitchellh/packer/packer"
    14  	"github.com/mitchellh/packer/template/interpolate"
    15  )
    16  
    17  type mountPathData struct {
    18  	Device string
    19  }
    20  
    21  // StepMountDevice mounts the attached device.
    22  //
    23  // Produces:
    24  //   mount_path string - The location where the volume was mounted.
    25  //   mount_device_cleanup CleanupFunc - To perform early cleanup
    26  type StepMountDevice struct {
    27  	MountOptions   []string
    28  	MountPartition int
    29  
    30  	mountPath string
    31  }
    32  
    33  func (s *StepMountDevice) Run(state multistep.StateBag) multistep.StepAction {
    34  	config := state.Get("config").(*Config)
    35  	ui := state.Get("ui").(packer.Ui)
    36  	image := state.Get("source_image").(*ec2.Image)
    37  	device := state.Get("device").(string)
    38  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
    39  
    40  	ctx := config.ctx
    41  	ctx.Data = &mountPathData{Device: filepath.Base(device)}
    42  	mountPath, err := interpolate.Render(config.MountPath, &ctx)
    43  
    44  	if err != nil {
    45  		err := fmt.Errorf("Error preparing mount directory: %s", err)
    46  		state.Put("error", err)
    47  		ui.Error(err.Error())
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	mountPath, err = filepath.Abs(mountPath)
    52  	if err != nil {
    53  		err := fmt.Errorf("Error preparing mount directory: %s", err)
    54  		state.Put("error", err)
    55  		ui.Error(err.Error())
    56  		return multistep.ActionHalt
    57  	}
    58  
    59  	log.Printf("Mount path: %s", mountPath)
    60  
    61  	if err := os.MkdirAll(mountPath, 0755); err != nil {
    62  		err := fmt.Errorf("Error creating mount directory: %s", err)
    63  		state.Put("error", err)
    64  		ui.Error(err.Error())
    65  		return multistep.ActionHalt
    66  	}
    67  
    68  	log.Printf("Source image virtualization type is: %s", *image.VirtualizationType)
    69  	deviceMount := device
    70  	if *image.VirtualizationType == "hvm" {
    71  		deviceMount = fmt.Sprintf("%s%d", device, s.MountPartition)
    72  	}
    73  	state.Put("deviceMount", deviceMount)
    74  
    75  	ui.Say("Mounting the root device...")
    76  	stderr := new(bytes.Buffer)
    77  
    78  	// build mount options from mount_options config, usefull for nouuid options
    79  	// or other specific device type settings for mount
    80  	opts := ""
    81  	if len(s.MountOptions) > 0 {
    82  		opts = "-o " + strings.Join(s.MountOptions, " -o ")
    83  	}
    84  	mountCommand, err := wrappedCommand(
    85  		fmt.Sprintf("mount %s %s %s", opts, deviceMount, mountPath))
    86  	if err != nil {
    87  		err := fmt.Errorf("Error creating mount command: %s", err)
    88  		state.Put("error", err)
    89  		ui.Error(err.Error())
    90  		return multistep.ActionHalt
    91  	}
    92  
    93  	cmd := ShellCommand(mountCommand)
    94  	cmd.Stderr = stderr
    95  	if err := cmd.Run(); err != nil {
    96  		err := fmt.Errorf(
    97  			"Error mounting root volume: %s\nStderr: %s", err, stderr.String())
    98  		state.Put("error", err)
    99  		ui.Error(err.Error())
   100  		return multistep.ActionHalt
   101  	}
   102  
   103  	// Set the mount path so we remember to unmount it later
   104  	s.mountPath = mountPath
   105  	state.Put("mount_path", s.mountPath)
   106  	state.Put("mount_device_cleanup", s)
   107  
   108  	return multistep.ActionContinue
   109  }
   110  
   111  func (s *StepMountDevice) Cleanup(state multistep.StateBag) {
   112  	ui := state.Get("ui").(packer.Ui)
   113  	if err := s.CleanupFunc(state); err != nil {
   114  		ui.Error(err.Error())
   115  	}
   116  }
   117  
   118  func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
   119  	if s.mountPath == "" {
   120  		return nil
   121  	}
   122  
   123  	ui := state.Get("ui").(packer.Ui)
   124  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
   125  
   126  	ui.Say("Unmounting the root device...")
   127  	unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath))
   128  	if err != nil {
   129  		return fmt.Errorf("Error creating unmount command: %s", err)
   130  	}
   131  
   132  	cmd := ShellCommand(unmountCommand)
   133  	if err := cmd.Run(); err != nil {
   134  		return fmt.Errorf("Error unmounting root device: %s", err)
   135  	}
   136  
   137  	s.mountPath = ""
   138  	return nil
   139  }