From 0d8e9f50bf6df5c0c753e15d0f15a47bd9117b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sm=C3=B3=C5=82ka?= Date: Tue, 5 Feb 2019 16:20:42 +0100 Subject: [PATCH] Add proper SCSI search --- builder/hyperone/step_create_vm.go | 1 + builder/hyperone/step_prepare_device.go | 28 +++++++++-------------- builder/hyperone/utils.go | 30 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/builder/hyperone/step_create_vm.go b/builder/hyperone/step_create_vm.go index b156a6157..2bb3ae84a 100644 --- a/builder/hyperone/step_create_vm.go +++ b/builder/hyperone/step_create_vm.go @@ -78,6 +78,7 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis for _, hdd := range hdds { if hdd.Disk.Name == chrootDiskName { state.Put("chroot_disk_id", hdd.Disk.Id) + state.Put("chroot_disk_location", int(hdd.ControllerLocation)) break } } diff --git a/builder/hyperone/step_prepare_device.go b/builder/hyperone/step_prepare_device.go index b282f1e4b..00a720b68 100644 --- a/builder/hyperone/step_prepare_device.go +++ b/builder/hyperone/step_prepare_device.go @@ -4,21 +4,27 @@ import ( "context" "fmt" "log" - "os" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) +const ( + diskByPathPrefix = "/dev/disk/by-path/acpi-VMBUS:01-scsi-0:0:0:" +) + type stepPrepareDevice struct{} func (s *stepPrepareDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - chrootDiskID := state.Get("chroot_disk_id").(string) + chrootDiskLocation := state.Get("chroot_disk_location").(int) - var err error log.Println("Searching for available device...") - device, err := availableDevice(chrootDiskID) + + diskByPath := fmt.Sprintf("%s%d", diskByPathPrefix, chrootDiskLocation) + cmd := fmt.Sprintf("readlink -f %s", diskByPath) + + device, err := captureOutput(cmd, state) if err != nil { err := fmt.Errorf("error finding available device: %s", err) state.Put("error", err) @@ -26,21 +32,9 @@ func (s *stepPrepareDevice) Run(_ context.Context, state multistep.StateBag) mul return multistep.ActionHalt } - if _, err := os.Stat(device); err == nil { - err := fmt.Errorf("device is in use: %s", device) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - - ui.Say(fmt.Sprintf("Found device: %s", device)) + ui.Say(fmt.Sprintf("Found device: %s -> %s", diskByPath, device)) state.Put("device", device) return multistep.ActionContinue } func (s *stepPrepareDevice) Cleanup(state multistep.StateBag) {} - -func availableDevice(scsiID string) (string, error) { - // TODO proper SCSI search - return "/dev/sdb", nil -} diff --git a/builder/hyperone/utils.go b/builder/hyperone/utils.go index 27d172c68..b6172a685 100644 --- a/builder/hyperone/utils.go +++ b/builder/hyperone/utils.go @@ -1,7 +1,10 @@ package hyperone import ( + "bytes" "fmt" + "log" + "strings" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -54,3 +57,30 @@ func runCommands(commands []string, ctx interpolate.Context, state multistep.Sta } return nil } + +func captureOutput(command string, state multistep.StateBag) (string, error) { + comm := state.Get("communicator").(packer.Communicator) + + var stdout bytes.Buffer + remoteCmd := &packer.RemoteCmd{ + Command: command, + Stdout: &stdout, + } + + log.Println(fmt.Sprintf("Executing command: %s", command)) + + err := comm.Start(remoteCmd) + if err != nil { + return "", fmt.Errorf("error running remote cmd: %s", err) + } + + remoteCmd.Wait() + if remoteCmd.ExitStatus != 0 { + return "", fmt.Errorf( + "received non-zero exit code %d from command: %s", + remoteCmd.ExitStatus, + command) + } + + return strings.TrimSpace(stdout.String()), nil +}