2013-07-31 00:19:57 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-03-28 21:02:51 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-31 00:19:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepPrepareDevice finds an available device and sets it.
|
|
|
|
type StepPrepareDevice struct {
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepPrepareDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-31 00:19:57 -04:00
|
|
|
|
|
|
|
device := config.DevicePath
|
|
|
|
if device == "" {
|
|
|
|
var err error
|
|
|
|
log.Println("Device path not specified, searching for available device...")
|
|
|
|
device, err = AvailableDevice()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error finding available device: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-31 00:19:57 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(device); err == nil {
|
|
|
|
err := fmt.Errorf("Device is in use: %s", device)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-31 00:19:57 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Device: %s", device)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("device", device)
|
2013-07-31 00:19:57 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepPrepareDevice) Cleanup(state multistep.StateBag) {}
|