github.com/rothwerx/packer@v0.9.0/builder/amazon/chroot/step_prepare_device.go (about)

     1  package chroot
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  	"os"
     9  )
    10  
    11  // StepPrepareDevice finds an available device and sets it.
    12  type StepPrepareDevice struct {
    13  	mounts []string
    14  }
    15  
    16  func (s *StepPrepareDevice) Run(state multistep.StateBag) multistep.StepAction {
    17  	config := state.Get("config").(*Config)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	device := config.DevicePath
    21  	if device == "" {
    22  		var err error
    23  		log.Println("Device path not specified, searching for available device...")
    24  		device, err = AvailableDevice()
    25  		if err != nil {
    26  			err := fmt.Errorf("Error finding available device: %s", err)
    27  			state.Put("error", err)
    28  			ui.Error(err.Error())
    29  			return multistep.ActionHalt
    30  		}
    31  	}
    32  
    33  	if _, err := os.Stat(device); err == nil {
    34  		err := fmt.Errorf("Device is in use: %s", device)
    35  		state.Put("error", err)
    36  		ui.Error(err.Error())
    37  		return multistep.ActionHalt
    38  	}
    39  
    40  	log.Printf("Device: %s", device)
    41  	state.Put("device", device)
    42  	return multistep.ActionContinue
    43  }
    44  
    45  func (s *StepPrepareDevice) Cleanup(state multistep.StateBag) {}