|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package chroot |
| 5 | + |
| 6 | +// mostly borrowed from ./builder/amazon/chroot/step_mount_device.go |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + |
| 15 | + "github.com/hashicorp/packer-plugin-azure/builder/azure/common/log" |
| 16 | + |
| 17 | + "github.com/hashicorp/packer-plugin-sdk/common" |
| 18 | + "github.com/hashicorp/packer-plugin-sdk/multistep" |
| 19 | + packersdk "github.com/hashicorp/packer-plugin-sdk/packer" |
| 20 | + "github.com/hashicorp/packer-plugin-sdk/template/interpolate" |
| 21 | +) |
| 22 | + |
| 23 | +var _ multistep.Step = &StepManualMountCommand{} |
| 24 | + |
| 25 | +type StepManualMountCommand struct { |
| 26 | + Command string |
| 27 | + MountPartition string |
| 28 | + MountPath string |
| 29 | + |
| 30 | + mountPath string |
| 31 | +} |
| 32 | + |
| 33 | +func (s *StepManualMountCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { |
| 34 | + ui := state.Get("ui").(packersdk.Ui) |
| 35 | + device := state.Get("device").(string) |
| 36 | + config := state.Get("config").(*Config) |
| 37 | + |
| 38 | + ictx := config.ctx |
| 39 | + |
| 40 | + ictx.Data = &struct{ Device string }{Device: filepath.Base(device)} |
| 41 | + mountPath, err := interpolate.Render(s.MountPath, &ictx) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + err := fmt.Errorf("error preparing mount directory: %s", err) |
| 45 | + state.Put("error", err) |
| 46 | + ui.Error(err.Error()) |
| 47 | + return multistep.ActionHalt |
| 48 | + } |
| 49 | + |
| 50 | + mountPath, err = filepath.Abs(mountPath) |
| 51 | + if err != nil { |
| 52 | + err := fmt.Errorf("error preparing mount directory: %s", err) |
| 53 | + state.Put("error", err) |
| 54 | + ui.Error(err.Error()) |
| 55 | + return multistep.ActionHalt |
| 56 | + } |
| 57 | + |
| 58 | + log.Printf("Mount path: %s", mountPath) |
| 59 | + |
| 60 | + var deviceMount string |
| 61 | + switch runtime.GOOS { |
| 62 | + case "freebsd": |
| 63 | + deviceMount = fmt.Sprintf("%sp%s", device, s.MountPartition) |
| 64 | + default: |
| 65 | + deviceMount = fmt.Sprintf("%s%s", device, s.MountPartition) |
| 66 | + } |
| 67 | + |
| 68 | + state.Put("deviceMount", deviceMount) |
| 69 | + |
| 70 | + ui.Say("Mounting the root device...") |
| 71 | + stderr := new(bytes.Buffer) |
| 72 | + |
| 73 | + log.Printf("[DEBUG] (step mount) mount command is %s", s.Command) |
| 74 | + cmd := common.ShellCommand(fmt.Sprintf("%s %s", s.Command, mountPath)) |
| 75 | + cmd.Stderr = stderr |
| 76 | + if err := cmd.Run(); err != nil { |
| 77 | + err := fmt.Errorf( |
| 78 | + "error mounting root volume: %s\nStderr: %s", err, stderr.String()) |
| 79 | + state.Put("error", err) |
| 80 | + ui.Error(err.Error()) |
| 81 | + return multistep.ActionHalt |
| 82 | + } |
| 83 | + |
| 84 | + // Set the mount path so we remember to unmount it later |
| 85 | + s.mountPath = mountPath |
| 86 | + state.Put("mount_path", s.mountPath) |
| 87 | + state.Put("mount_device_cleanup", s) |
| 88 | + |
| 89 | + return multistep.ActionContinue |
| 90 | +} |
| 91 | + |
| 92 | +func (s *StepManualMountCommand) Cleanup(state multistep.StateBag) { |
| 93 | + ui := state.Get("ui").(packersdk.Ui) |
| 94 | + if err := s.CleanupFunc(state); err != nil { |
| 95 | + ui.Error(err.Error()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (s *StepManualMountCommand) CleanupFunc(state multistep.StateBag) error { |
| 100 | + if s.mountPath == "" { |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + ui := state.Get("ui").(packersdk.Ui) |
| 105 | + |
| 106 | + ui.Say("Skipping Unmounting the root device, it is manually unmounted via manual mount command script...") |
| 107 | + |
| 108 | + s.mountPath = "" |
| 109 | + return nil |
| 110 | +} |
0 commit comments