Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions builder/hyperv/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ type CommonConfig struct {
// built. When this value is set to true, the machine will start without a
// console.
Headless bool `mapstructure:"headless" required:"false"`
// Path to a named pipe for COM1 serial output. When set, the VM's COM1
// port will be configured to output to this pipe, enabling capture of
// boot console output. Example: `\\.\pipe\packer-console`
ComPortPipePath string `mapstructure:"com_port_pipe_path" required:"false"`
// When configured, determines the device or device type that is given preferential
// treatment when choosing a boot device.
//
Expand Down
3 changes: 3 additions & 0 deletions builder/hyperv/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ type Driver interface {

// Disconnect disconnects to a VM specified by the context cancel function.
Disconnect(context.CancelFunc)

// SetVirtualMachineComPort configures a COM port to output to a named pipe.
SetVirtualMachineComPort(string, int, string) error
}
14 changes: 14 additions & 0 deletions builder/hyperv/common/driver_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ type DriverMock struct {

Disconnect_Called bool
Disconnect_Cancel context.CancelFunc

SetVirtualMachineComPort_Called bool
SetVirtualMachineComPort_VmName string
SetVirtualMachineComPort_PortNumber int
SetVirtualMachineComPort_PipePath string
SetVirtualMachineComPort_Err error
}

func (d *DriverMock) IsRunning(vmName string) (bool, error) {
Expand Down Expand Up @@ -673,3 +679,11 @@ func (d *DriverMock) Disconnect(cancel context.CancelFunc) {
d.Disconnect_Called = true
d.Disconnect_Cancel = cancel
}

func (d *DriverMock) SetVirtualMachineComPort(vmName string, portNumber int, pipePath string) error {
d.SetVirtualMachineComPort_Called = true
d.SetVirtualMachineComPort_VmName = vmName
d.SetVirtualMachineComPort_PortNumber = portNumber
d.SetVirtualMachineComPort_PipePath = pipePath
return d.SetVirtualMachineComPort_Err
}
5 changes: 5 additions & 0 deletions builder/hyperv/common/driver_ps_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,8 @@ func (d *HypervPS4Driver) Connect(vmName string) (context.CancelFunc, error) {
func (d *HypervPS4Driver) Disconnect(cancel context.CancelFunc) {
hyperv.DisconnectVirtualMachine(cancel)
}

// SetVirtualMachineComPort configures a COM port to output to a named pipe.
func (d *HypervPS4Driver) SetVirtualMachineComPort(vmName string, portNumber int, pipePath string) error {
return hyperv.SetVMComPort(vmName, portNumber, pipePath)
}
10 changes: 10 additions & 0 deletions builder/hyperv/common/powershell/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,13 @@ func ConnectVirtualMachine(vmName string) (context.CancelFunc, error) {
func DisconnectVirtualMachine(cancel context.CancelFunc) {
cancel()
}

func SetVMComPort(vmName string, number int, pipePath string) error {
var script = `
param([string]$vmName, [int]$number, [string]$pipePath)
Set-VMComPort -VMName $vmName -Number $number -Path $pipePath
`
var ps powershell.PowerShellCmd
err := ps.Run(script, vmName, strconv.Itoa(number), pipePath)
return err
}
11 changes: 11 additions & 0 deletions builder/hyperv/common/step_clone_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type StepCloneVM struct {
KeepRegistered bool
AdditionalDiskSize []uint
DiskBlockSize uint
ComPortPipePath string
}

func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -189,6 +190,16 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
}
}

if s.ComPortPipePath != "" {
err = driver.SetVirtualMachineComPort(s.VMName, 1, s.ComPortPipePath)
if err != nil {
err := fmt.Errorf("Error configuring COM port: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}

// Set the final name in the state bag so others can use it
state.Put("vmName", s.VMName)
// instance_id is the generic term used so that users can have access to the
Expand Down
11 changes: 11 additions & 0 deletions builder/hyperv/common/step_create_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type StepCreateVM struct {
FixedVHD bool
Version string
KeepRegistered bool
ComPortPipePath string
}

func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -189,6 +190,16 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
}
}

if s.ComPortPipePath != "" {
err = driver.SetVirtualMachineComPort(s.VMName, 1, s.ComPortPipePath)
if err != nil {
err := fmt.Errorf("Error configuring COM port: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}

// Set the final name in the state bag so others can use it
state.Put("vmName", s.VMName)
// instance_id is the generic term used so that users can have access to the
Expand Down
1 change: 1 addition & 0 deletions builder/hyperv/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
FixedVHD: b.config.FixedVHD,
Version: b.config.Version,
KeepRegistered: b.config.KeepRegistered,
ComPortPipePath: b.config.ComPortPipePath,
},
&hypervcommon.StepEnableIntegrationService{},

Expand Down
2 changes: 2 additions & 0 deletions builder/hyperv/iso/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/hyperv/vmcx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
KeepRegistered: b.config.KeepRegistered,
AdditionalDiskSize: b.config.AdditionalDiskSize,
DiskBlockSize: b.config.DiskBlockSize,
ComPortPipePath: b.config.ComPortPipePath,
},

&hypervcommon.StepResizeVhd{
Expand Down
2 changes: 2 additions & 0 deletions builder/hyperv/vmcx/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
built. When this value is set to true, the machine will start without a
console.

- `com_port_pipe_path` (string) - Path to a named pipe for COM1 serial output. When set, the VM's COM1
port will be configured to output to this pipe, enabling capture of
boot console output. Example: `\\.\pipe\packer-console`

- `first_boot_device` (string) - When configured, determines the device or device type that is given preferential
treatment when choosing a boot device.

Expand Down