2019-01-28 06:30:15 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2019-02-05 10:20:42 -05:00
|
|
|
const (
|
2019-02-06 06:27:57 -05:00
|
|
|
vmBusPath = "/sys/bus/vmbus/devices"
|
2019-02-05 10:20:42 -05:00
|
|
|
)
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
type stepPrepareDevice struct{}
|
|
|
|
|
|
|
|
func (s *stepPrepareDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-02-06 06:27:57 -05:00
|
|
|
controllerNumber := state.Get("chroot_controller_number").(string)
|
|
|
|
controllerLocation := state.Get("chroot_controller_location").(int)
|
2019-01-28 06:30:15 -05:00
|
|
|
|
|
|
|
log.Println("Searching for available device...")
|
2019-02-05 10:20:42 -05:00
|
|
|
|
2019-02-06 06:27:57 -05:00
|
|
|
cmd := fmt.Sprintf("find %s/%s/ -path *:%d/block -exec ls {} \\;",
|
|
|
|
vmBusPath, controllerNumber, controllerLocation)
|
2019-02-05 10:20:42 -05:00
|
|
|
|
2019-02-06 06:27:57 -05:00
|
|
|
block, err := captureOutput(cmd, state)
|
2019-01-28 06:30:15 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error finding available device: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-02-06 06:27:57 -05:00
|
|
|
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))
|
2019-01-28 06:30:15 -05:00
|
|
|
state.Put("device", device)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPrepareDevice) Cleanup(state multistep.StateBag) {}
|