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