github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_mount_guest_additions.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 StepMountGuestAdditions struct {
    11  	GuestAdditionsMode string
    12  	GuestAdditionsPath string
    13  	Generation         uint
    14  }
    15  
    16  func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	if s.GuestAdditionsMode != "attach" {
    20  		ui.Say("Skipping mounting Integration Services Setup Disk...")
    21  		return multistep.ActionContinue
    22  	}
    23  
    24  	driver := state.Get("driver").(Driver)
    25  	ui.Say("Mounting Integration Services Setup Disk...")
    26  
    27  	vmName := state.Get("vmName").(string)
    28  
    29  	// should be able to mount up to 60 additional iso images using SCSI
    30  	// but Windows would only allow a max of 22 due to available drive letters
    31  	// Will Windows assign DVD drives to A: and B: ?
    32  
    33  	// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
    34  
    35  	var dvdControllerProperties DvdControllerProperties
    36  
    37  	controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, s.GuestAdditionsPath, s.Generation)
    38  	if err != nil {
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	dvdControllerProperties.ControllerNumber = controllerNumber
    45  	dvdControllerProperties.ControllerLocation = controllerLocation
    46  	dvdControllerProperties.Existing = false
    47  	state.Put("guest.dvd.properties", dvdControllerProperties)
    48  
    49  	ui.Say(fmt.Sprintf("Mounting Integration Services dvd drive %s ...", s.GuestAdditionsPath))
    50  	err = driver.MountDvdDrive(vmName, s.GuestAdditionsPath, controllerNumber, controllerLocation)
    51  	if err != nil {
    52  		state.Put("error", err)
    53  		ui.Error(err.Error())
    54  		return multistep.ActionHalt
    55  	}
    56  
    57  	log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", s.GuestAdditionsPath, controllerNumber, controllerLocation))
    58  
    59  	return multistep.ActionContinue
    60  }
    61  
    62  func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) {
    63  	if s.GuestAdditionsMode != "attach" {
    64  		return
    65  	}
    66  
    67  	dvdControllerState := state.Get("guest.dvd.properties")
    68  
    69  	if dvdControllerState == nil {
    70  		return
    71  	}
    72  
    73  	dvdController := dvdControllerState.(DvdControllerProperties)
    74  	ui := state.Get("ui").(packer.Ui)
    75  	driver := state.Get("driver").(Driver)
    76  	vmName := state.Get("vmName").(string)
    77  	errorMsg := "Error unmounting Integration Services dvd drive: %s"
    78  
    79  	ui.Say("Cleanup Integration Services dvd drive...")
    80  
    81  	if dvdController.Existing {
    82  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    83  		if err != nil {
    84  			log.Print(fmt.Sprintf(errorMsg, err))
    85  		}
    86  	} else {
    87  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    88  		if err != nil {
    89  			log.Print(fmt.Sprintf(errorMsg, err))
    90  		}
    91  	}
    92  }