2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
2017-05-29 20:16:03 -04:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepMountDvdDrive struct {
|
2020-02-09 16:22:36 -05:00
|
|
|
Generation uint
|
2020-02-21 02:01:09 -05:00
|
|
|
FirstBootDevice string
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepMountDvdDrive) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
errorMsg := "Error mounting dvd drive: %s"
|
|
|
|
vmName := state.Get("vmName").(string)
|
2017-03-12 19:51:59 -04:00
|
|
|
|
|
|
|
// Determine if we even have a dvd disk to attach
|
|
|
|
var isoPath string
|
|
|
|
if isoPathRaw, ok := state.GetOk("iso_path"); ok {
|
|
|
|
isoPath = isoPathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No dvd disk, not attaching.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2015-10-31 12:53:58 -04:00
|
|
|
|
2017-05-29 20:16:03 -04:00
|
|
|
// Determine if its a virtual hdd to mount
|
|
|
|
if strings.ToLower(filepath.Ext(isoPath)) == ".vhd" || strings.ToLower(filepath.Ext(isoPath)) == ".vhdx" {
|
|
|
|
log.Println("Its a hard disk, not attaching.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
// should be able to mount up to 60 additional iso images using SCSI
|
|
|
|
// but Windows would only allow a max of 22 due to available drive letters
|
|
|
|
// Will Windows assign DVD drives to A: and B: ?
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
|
2015-06-29 16:18:25 -04:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
var dvdControllerProperties DvdControllerProperties
|
2015-12-05 06:03:33 -05:00
|
|
|
controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, isoPath, s.Generation)
|
2015-06-29 16:18:25 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
dvdControllerProperties.ControllerNumber = controllerNumber
|
|
|
|
dvdControllerProperties.ControllerLocation = controllerLocation
|
2015-11-01 11:00:56 -05:00
|
|
|
dvdControllerProperties.Existing = false
|
2015-11-07 08:42:26 -05:00
|
|
|
|
2015-11-01 11:00:56 -05:00
|
|
|
state.Put("os.dvd.properties", dvdControllerProperties)
|
2015-10-31 12:53:58 -04:00
|
|
|
|
2020-02-21 02:01:09 -05:00
|
|
|
// the "first_boot_device" setting has precedence over the legacy boot order
|
|
|
|
// configuration, but only if its been assigned a value.
|
|
|
|
|
|
|
|
if s.FirstBootDevice == "" {
|
|
|
|
|
|
|
|
if s.Generation > 1 {
|
|
|
|
// only print this message for Gen2, it's not a true statement for Gen1 VMs
|
|
|
|
ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ...", isoPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, s.Generation)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2020-02-09 15:26:46 -05:00
|
|
|
|
2015-10-31 12:53:58 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath))
|
2015-11-01 11:00:56 -05:00
|
|
|
err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-31 12:53:58 -04:00
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-11-07 08:42:26 -05:00
|
|
|
func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) {
|
2015-11-01 11:00:56 -05:00
|
|
|
dvdControllerState := state.Get("os.dvd.properties")
|
2015-11-07 08:42:26 -05:00
|
|
|
|
2015-11-01 11:00:56 -05:00
|
|
|
if dvdControllerState == nil {
|
2015-06-21 07:36:07 -04:00
|
|
|
return
|
|
|
|
}
|
2015-11-07 08:42:26 -05:00
|
|
|
|
|
|
|
dvdController := dvdControllerState.(DvdControllerProperties)
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-11-01 11:00:56 -05:00
|
|
|
errorMsg := "Error unmounting os dvd drive: %s"
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
ui.Say("Clean up os dvd drive...")
|
2015-10-31 12:53:58 -04:00
|
|
|
|
2015-11-01 11:00:56 -05:00
|
|
|
if dvdController.Existing {
|
|
|
|
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
|
2015-10-30 13:19:25 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error unmounting dvd drive: %s", err)
|
|
|
|
log.Print(fmt.Sprintf(errorMsg, err))
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-01 11:00:56 -05:00
|
|
|
err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
|
2015-10-30 13:19:25 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error deleting dvd drive: %s", err)
|
|
|
|
log.Print(fmt.Sprintf(errorMsg, err))
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
}
|