2013-07-30 14:05:06 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-30 14:05:06 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2013-07-30 14:47:30 -04:00
|
|
|
"path/filepath"
|
2015-06-23 12:26:13 -04:00
|
|
|
"strings"
|
2015-05-27 14:47:45 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-07-30 14:05:06 -04:00
|
|
|
)
|
|
|
|
|
2013-07-30 14:47:30 -04:00
|
|
|
type mountPathData struct {
|
|
|
|
Device string
|
|
|
|
}
|
|
|
|
|
2013-07-30 14:05:06 -04:00
|
|
|
// StepMountDevice mounts the attached device.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// mount_path string - The location where the volume was mounted.
|
2013-07-30 19:41:29 -04:00
|
|
|
// mount_device_cleanup CleanupFunc - To perform early cleanup
|
2013-07-30 14:05:06 -04:00
|
|
|
type StepMountDevice struct {
|
2016-01-06 14:35:01 -05:00
|
|
|
MountOptions []string
|
2018-04-25 13:47:52 -04:00
|
|
|
MountPartition string
|
2015-06-23 12:34:42 -04:00
|
|
|
|
2020-01-20 10:29:38 -05:00
|
|
|
mountPath string
|
2020-11-04 18:44:05 -05:00
|
|
|
GeneratedData *packerbuilderdata.GeneratedData
|
2013-07-30 14:05:06 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
device := state.Get("device").(string)
|
2018-04-25 14:58:04 -04:00
|
|
|
if config.NVMEDevicePath != "" {
|
|
|
|
// customizable device path for mounting NVME block devices on c5 and m5 HVM
|
|
|
|
device = config.NVMEDevicePath
|
|
|
|
}
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-07-30 14:05:06 -04:00
|
|
|
|
2016-08-10 20:24:30 -04:00
|
|
|
var virtualizationType string
|
2019-02-22 20:26:58 -05:00
|
|
|
if config.FromScratch || config.AMIVirtType != "" {
|
2016-08-10 20:24:30 -04:00
|
|
|
virtualizationType = config.AMIVirtType
|
|
|
|
} else {
|
|
|
|
image := state.Get("source_image").(*ec2.Image)
|
|
|
|
virtualizationType = *image.VirtualizationType
|
|
|
|
log.Printf("Source image virtualization type is: %s", virtualizationType)
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
ictx := config.ctx
|
2018-04-25 14:58:04 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
ictx.Data = &mountPathData{Device: filepath.Base(device)}
|
|
|
|
mountPath, err := interpolate.Render(config.MountPath, &ictx)
|
2013-07-30 14:47:30 -04:00
|
|
|
|
2013-08-08 17:54:37 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing mount directory: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-08-08 17:54:37 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-31 01:31:07 -04:00
|
|
|
mountPath, err = filepath.Abs(mountPath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing mount directory: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-31 01:31:07 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-30 14:05:06 -04:00
|
|
|
log.Printf("Mount path: %s", mountPath)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(mountPath, 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 14:05:06 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:55:20 -04:00
|
|
|
deviceMount := device
|
2018-04-25 13:47:52 -04:00
|
|
|
|
|
|
|
if virtualizationType == "hvm" && s.MountPartition != "0" {
|
|
|
|
deviceMount = fmt.Sprintf("%s%s", device, s.MountPartition)
|
2014-07-30 00:55:20 -04:00
|
|
|
}
|
|
|
|
state.Put("deviceMount", deviceMount)
|
|
|
|
|
2013-07-30 14:05:06 -04:00
|
|
|
ui.Say("Mounting the root device...")
|
|
|
|
stderr := new(bytes.Buffer)
|
2015-06-23 12:26:13 -04:00
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
// build mount options from mount_options config, useful for nouuid options
|
2015-06-23 12:26:13 -04:00
|
|
|
// or other specific device type settings for mount
|
|
|
|
opts := ""
|
|
|
|
if len(s.MountOptions) > 0 {
|
|
|
|
opts = "-o " + strings.Join(s.MountOptions, " -o ")
|
|
|
|
}
|
2013-09-30 12:33:57 -04:00
|
|
|
mountCommand, err := wrappedCommand(
|
2015-06-23 12:26:13 -04:00
|
|
|
fmt.Sprintf("mount %s %s %s", opts, deviceMount, mountPath))
|
2013-09-30 12:33:57 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating mount command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-04-25 14:58:04 -04:00
|
|
|
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd := common.ShellCommand(mountCommand)
|
2013-07-30 14:05:06 -04:00
|
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"Error mounting root volume: %s\nStderr: %s", err, stderr.String())
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 14:05:06 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-30 14:47:30 -04:00
|
|
|
// Set the mount path so we remember to unmount it later
|
|
|
|
s.mountPath = mountPath
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("mount_path", s.mountPath)
|
2020-01-20 10:29:38 -05:00
|
|
|
s.GeneratedData.Put("MountPath", s.mountPath)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("mount_device_cleanup", s)
|
2013-07-30 14:47:30 -04:00
|
|
|
|
2013-07-30 14:05:06 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepMountDevice) 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-08-31 15:58:55 -04:00
|
|
|
func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
|
2013-07-30 14:05:06 -04:00
|
|
|
if s.mountPath == "" {
|
2013-07-30 19:41:29 -04:00
|
|
|
return nil
|
2013-07-30 14:05:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-09-30 12:33:57 -04:00
|
|
|
|
2013-07-30 14:05:06 -04:00
|
|
|
ui.Say("Unmounting the root device...")
|
2013-09-30 12:33:57 -04:00
|
|
|
unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating unmount command: %s", err)
|
|
|
|
}
|
2013-07-30 14:05:06 -04:00
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd := common.ShellCommand(unmountCommand)
|
2013-07-30 14:05:06 -04:00
|
|
|
if err := cmd.Run(); err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return fmt.Errorf("Error unmounting root device: %s", err)
|
2013-07-30 14:05:06 -04:00
|
|
|
}
|
2013-07-30 19:41:29 -04:00
|
|
|
|
|
|
|
s.mountPath = ""
|
|
|
|
return nil
|
2013-07-30 14:05:06 -04:00
|
|
|
}
|