github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_mount_dvddrive.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  	"github.com/hashicorp/packer/packer"
    12  )
    13  
    14  type StepMountDvdDrive struct {
    15  	Generation uint
    16  }
    17  
    18  func (s *StepMountDvdDrive) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	driver := state.Get("driver").(Driver)
    20  	ui := state.Get("ui").(packer.Ui)
    21  
    22  	errorMsg := "Error mounting dvd drive: %s"
    23  	vmName := state.Get("vmName").(string)
    24  
    25  	// Determine if we even have a dvd disk to attach
    26  	var isoPath string
    27  	if isoPathRaw, ok := state.GetOk("iso_path"); ok {
    28  		isoPath = isoPathRaw.(string)
    29  	} else {
    30  		log.Println("No dvd disk, not attaching.")
    31  		return multistep.ActionContinue
    32  	}
    33  
    34  	// Determine if its a virtual hdd to mount
    35  	if strings.ToLower(filepath.Ext(isoPath)) == ".vhd" || strings.ToLower(filepath.Ext(isoPath)) == ".vhdx" {
    36  		log.Println("Its a hard disk, not attaching.")
    37  		return multistep.ActionContinue
    38  	}
    39  
    40  	// should be able to mount up to 60 additional iso images using SCSI
    41  	// but Windows would only allow a max of 22 due to available drive letters
    42  	// Will Windows assign DVD drives to A: and B: ?
    43  
    44  	// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
    45  
    46  	var dvdControllerProperties DvdControllerProperties
    47  	controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, isoPath, s.Generation)
    48  	if err != nil {
    49  		state.Put("error", err)
    50  		ui.Error(err.Error())
    51  		return multistep.ActionHalt
    52  	}
    53  
    54  	dvdControllerProperties.ControllerNumber = controllerNumber
    55  	dvdControllerProperties.ControllerLocation = controllerLocation
    56  	dvdControllerProperties.Existing = false
    57  
    58  	state.Put("os.dvd.properties", dvdControllerProperties)
    59  
    60  	ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ...", isoPath))
    61  	err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, s.Generation)
    62  	if err != nil {
    63  		err := fmt.Errorf(errorMsg, err)
    64  		state.Put("error", err)
    65  		ui.Error(err.Error())
    66  		return multistep.ActionHalt
    67  	}
    68  
    69  	ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath))
    70  	err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
    71  	if err != nil {
    72  		err := fmt.Errorf(errorMsg, err)
    73  		state.Put("error", err)
    74  		ui.Error(err.Error())
    75  		return multistep.ActionHalt
    76  	}
    77  
    78  	return multistep.ActionContinue
    79  }
    80  
    81  func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) {
    82  	dvdControllerState := state.Get("os.dvd.properties")
    83  
    84  	if dvdControllerState == nil {
    85  		return
    86  	}
    87  
    88  	dvdController := dvdControllerState.(DvdControllerProperties)
    89  	driver := state.Get("driver").(Driver)
    90  	vmName := state.Get("vmName").(string)
    91  	ui := state.Get("ui").(packer.Ui)
    92  	errorMsg := "Error unmounting os dvd drive: %s"
    93  
    94  	ui.Say("Clean up os dvd drive...")
    95  
    96  	if dvdController.Existing {
    97  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    98  		if err != nil {
    99  			err := fmt.Errorf("Error unmounting dvd drive: %s", err)
   100  			log.Print(fmt.Sprintf(errorMsg, err))
   101  		}
   102  	} else {
   103  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
   104  		if err != nil {
   105  			err := fmt.Errorf("Error deleting dvd drive: %s", err)
   106  			log.Print(fmt.Sprintf(errorMsg, err))
   107  		}
   108  	}
   109  }