github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_mount_dvddrive.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  )
     9  
    10  type StepMountDvdDrive struct {
    11  	Generation uint
    12  }
    13  
    14  func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
    15  	driver := state.Get("driver").(Driver)
    16  	ui := state.Get("ui").(packer.Ui)
    17  
    18  	errorMsg := "Error mounting dvd drive: %s"
    19  	vmName := state.Get("vmName").(string)
    20  	isoPath := state.Get("iso_path").(string)
    21  
    22  	// should be able to mount up to 60 additional iso images using SCSI
    23  	// but Windows would only allow a max of 22 due to available drive letters
    24  	// Will Windows assign DVD drives to A: and B: ?
    25  
    26  	// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
    27  
    28  	var dvdControllerProperties DvdControllerProperties
    29  	controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, isoPath, s.Generation)
    30  	if err != nil {
    31  		state.Put("error", err)
    32  		ui.Error(err.Error())
    33  		return multistep.ActionHalt
    34  	}
    35  
    36  	dvdControllerProperties.ControllerNumber = controllerNumber
    37  	dvdControllerProperties.ControllerLocation = controllerLocation
    38  	dvdControllerProperties.Existing = false
    39  
    40  	state.Put("os.dvd.properties", dvdControllerProperties)
    41  
    42  	ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ...", isoPath))
    43  	err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, s.Generation)
    44  	if err != nil {
    45  		err := fmt.Errorf(errorMsg, err)
    46  		state.Put("error", err)
    47  		ui.Error(err.Error())
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath))
    52  	err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
    53  	if err != nil {
    54  		err := fmt.Errorf(errorMsg, err)
    55  		state.Put("error", err)
    56  		ui.Error(err.Error())
    57  		return multistep.ActionHalt
    58  	}
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) {
    64  	dvdControllerState := state.Get("os.dvd.properties")
    65  
    66  	if dvdControllerState == nil {
    67  		return
    68  	}
    69  
    70  	dvdController := dvdControllerState.(DvdControllerProperties)
    71  	driver := state.Get("driver").(Driver)
    72  	vmName := state.Get("vmName").(string)
    73  	ui := state.Get("ui").(packer.Ui)
    74  	errorMsg := "Error unmounting os dvd drive: %s"
    75  
    76  	ui.Say("Clean up os dvd drive...")
    77  
    78  	if dvdController.Existing {
    79  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    80  		if err != nil {
    81  			err := fmt.Errorf("Error unmounting dvd drive: %s", err)
    82  			log.Print(fmt.Sprintf(errorMsg, err))
    83  		}
    84  	} else {
    85  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    86  		if err != nil {
    87  			err := fmt.Errorf("Error deleting dvd drive: %s", err)
    88  			log.Print(fmt.Sprintf(errorMsg, err))
    89  		}
    90  	}
    91  }