github.phpd.cn/hashicorp/packer@v1.3.2/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,
    60  		controllerNumber, controllerLocation))
    61  
    62  	return multistep.ActionContinue
    63  }
    64  
    65  func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) {
    66  	if s.GuestAdditionsMode != "attach" {
    67  		return
    68  	}
    69  
    70  	dvdControllerState := state.Get("guest.dvd.properties")
    71  
    72  	if dvdControllerState == nil {
    73  		return
    74  	}
    75  
    76  	dvdController := dvdControllerState.(DvdControllerProperties)
    77  	ui := state.Get("ui").(packer.Ui)
    78  	driver := state.Get("driver").(Driver)
    79  	vmName := state.Get("vmName").(string)
    80  	errorMsg := "Error unmounting Integration Services dvd drive: %s"
    81  
    82  	ui.Say("Cleanup Integration Services dvd drive...")
    83  
    84  	if dvdController.Existing {
    85  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    86  		if err != nil {
    87  			log.Print(fmt.Sprintf(errorMsg, err))
    88  		}
    89  	} else {
    90  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    91  		if err != nil {
    92  			log.Print(fmt.Sprintf(errorMsg, err))
    93  		}
    94  	}
    95  }