github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_mount_secondary_dvd_images.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 StepMountSecondaryDvdImages struct {
    13  	IsoPaths   []string
    14  	Generation uint
    15  }
    16  
    17  type DvdControllerProperties struct {
    18  	ControllerNumber   uint
    19  	ControllerLocation uint
    20  	Existing           bool
    21  }
    22  
    23  func (s *StepMountSecondaryDvdImages) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    24  	driver := state.Get("driver").(Driver)
    25  	ui := state.Get("ui").(packer.Ui)
    26  	ui.Say("Mounting secondary DVD images...")
    27  
    28  	vmName := state.Get("vmName").(string)
    29  
    30  	// should be able to mount up to 60 additional iso images using SCSI
    31  	// but Windows would only allow a max of 22 due to available drive letters
    32  	// Will Windows assign DVD drives to A: and B: ?
    33  
    34  	// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
    35  	var dvdProperties []DvdControllerProperties
    36  
    37  	for _, isoPath := range s.IsoPaths {
    38  		var properties DvdControllerProperties
    39  
    40  		controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, isoPath, s.Generation)
    41  		if err != nil {
    42  			state.Put("error", err)
    43  			ui.Error(err.Error())
    44  			return multistep.ActionHalt
    45  		}
    46  
    47  		properties.ControllerNumber = controllerNumber
    48  		properties.ControllerLocation = controllerLocation
    49  		properties.Existing = false
    50  		dvdProperties = append(dvdProperties, properties)
    51  		state.Put("secondary.dvd.properties", dvdProperties)
    52  
    53  		ui.Say(fmt.Sprintf("Mounting secondary dvd drive %s ...", isoPath))
    54  		err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
    55  		if err != nil {
    56  			state.Put("error", err)
    57  			ui.Error(err.Error())
    58  			return multistep.ActionHalt
    59  		}
    60  
    61  		log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", isoPath, controllerNumber,
    62  			controllerLocation))
    63  	}
    64  
    65  	return multistep.ActionContinue
    66  }
    67  
    68  func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) {
    69  	dvdControllersState := state.Get("secondary.dvd.properties")
    70  
    71  	if dvdControllersState == nil {
    72  		return
    73  	}
    74  
    75  	dvdControllers := dvdControllersState.([]DvdControllerProperties)
    76  	driver := state.Get("driver").(Driver)
    77  	ui := state.Get("ui").(packer.Ui)
    78  	vmName := state.Get("vmName").(string)
    79  	errorMsg := "Error unmounting secondary dvd drive: %s"
    80  
    81  	ui.Say("Clean up secondary dvd drives...")
    82  
    83  	for _, dvdController := range dvdControllers {
    84  
    85  		if dvdController.Existing {
    86  			err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    87  			if err != nil {
    88  				log.Print(fmt.Sprintf(errorMsg, err))
    89  			}
    90  		} else {
    91  			err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    92  			if err != nil {
    93  				log.Print(fmt.Sprintf(errorMsg, err))
    94  			}
    95  		}
    96  	}
    97  }