2019-01-28 06:30:15 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-01-28 06:30:15 -05:00
|
|
|
)
|
|
|
|
|
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{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepPrepareDevice) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-06-26 08:50:39 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
if config.ChrootDevice != "" {
|
|
|
|
state.Put("device", config.ChrootDevice)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
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) {}
|