Search SCSI device in /sys/bus due to issues with by-path

This commit is contained in:
Miłosz Smółka 2019-02-06 12:27:57 +01:00
parent 0d8e9f50bf
commit fdf028c463
2 changed files with 20 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package hyperone
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -78,7 +79,9 @@ 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))
controllerNumber := strings.ToLower(strings.Trim(hdd.ControllerNumber, "{}"))
state.Put("chroot_controller_number", controllerNumber)
state.Put("chroot_controller_location", int(hdd.ControllerLocation))
break
}
}

View File

@ -10,21 +10,22 @@ import (
)
const (
diskByPathPrefix = "/dev/disk/by-path/acpi-VMBUS:01-scsi-0:0:0:"
vmBusPath = "/sys/bus/vmbus/devices"
)
type stepPrepareDevice struct{}
func (s *stepPrepareDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
chrootDiskLocation := state.Get("chroot_disk_location").(int)
controllerNumber := state.Get("chroot_controller_number").(string)
controllerLocation := state.Get("chroot_controller_location").(int)
log.Println("Searching for available device...")
diskByPath := fmt.Sprintf("%s%d", diskByPathPrefix, chrootDiskLocation)
cmd := fmt.Sprintf("readlink -f %s", diskByPath)
cmd := fmt.Sprintf("find %s/%s/ -path *:%d/block -exec ls {} \\;",
vmBusPath, controllerNumber, controllerLocation)
device, err := captureOutput(cmd, state)
block, err := captureOutput(cmd, state)
if err != nil {
err := fmt.Errorf("error finding available device: %s", err)
state.Put("error", err)
@ -32,7 +33,16 @@ func (s *stepPrepareDevice) Run(_ context.Context, state multistep.StateBag) mul
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Found device: %s -> %s", diskByPath, device))
if block == "" {
err := fmt.Errorf("device not found")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
device := fmt.Sprintf("/dev/%s", block)
ui.Say(fmt.Sprintf("Found device: %s", device))
state.Put("device", device)
return multistep.ActionContinue
}