github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/hyperv/common/step_mount_guest_additions.go (about)

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