Add proper SCSI search

This commit is contained in:
Miłosz Smółka 2019-02-05 16:20:42 +01:00
parent f7de056f23
commit 0d8e9f50bf
3 changed files with 42 additions and 17 deletions

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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
}