Added the ability to independently configure switch vlan. This will people to leave the switch in trunk mode and set a vlan for the vm.

This commit is contained in:
Taliesin Sisson 2016-09-29 19:37:07 +01:00
parent 48b6cc0650
commit c2d1f6b617
3 changed files with 42 additions and 24 deletions

View File

@ -6,12 +6,14 @@ package common
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
type StepConfigureVlan struct { type StepConfigureVlan struct {
VlanID string VlanId string
SwitchVlanId string
} }
func (s *StepConfigureVlan) Run(state multistep.StateBag) multistep.StepAction { func (s *StepConfigureVlan) Run(state multistep.StateBag) multistep.StepAction {
@ -21,30 +23,29 @@ func (s *StepConfigureVlan) Run(state multistep.StateBag) multistep.StepAction {
errorMsg := "Error configuring vlan: %s" errorMsg := "Error configuring vlan: %s"
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
switchName := state.Get("SwitchName").(string) switchName := state.Get("SwitchName").(string)
vlanId := s.VlanId
switchVlanId := s.SwitchVlanId
ui.Say("Configuring vlan...") ui.Say("Configuring vlan...")
vlanId := s.VlanID if switchVlanId != "" {
err := driver.SetNetworkAdapterVlanId(switchName, vlanId)
if vlanId == "" { if err != nil {
// If no vlan ID is specified, do not enable Virtual LAN Identification err := fmt.Errorf(errorMsg, err)
return multistep.ActionContinue state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} }
err := driver.SetNetworkAdapterVlanId(switchName, vlanId) if vlanId != "" {
if err != nil { err := driver.SetVirtualMachineVlanId(vmName, vlanId)
err := fmt.Errorf(errorMsg, err) if err != nil {
state.Put("error", err) err := fmt.Errorf(errorMsg, err)
ui.Error(err.Error()) state.Put("error", err)
return multistep.ActionHalt ui.Error(err.Error())
} return multistep.ActionHalt
}
err = driver.SetVirtualMachineVlanId(vmName, vlanId)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -87,7 +87,8 @@ type Config struct {
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
SwitchName string `mapstructure:"switch_name"` SwitchName string `mapstructure:"switch_name"`
VlandID string `mapstructure:"vlan_id"` SwitchVlanId string `mapstructure:"switch_vlan_id"`
VlanId string `mapstructure:"vlan_id"`
Cpu uint `mapstructure:"cpu"` Cpu uint `mapstructure:"cpu"`
Generation uint `mapstructure:"generation"` Generation uint `mapstructure:"generation"`
EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"` EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"`
@ -272,6 +273,13 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
} }
if b.config.SwitchVlanId != "" {
if b.config.SwitchVlanId != b.config.VlanId {
warning = fmt.Sprintf("Switch network adaptor vlan should match virtual machine network adaptor vlan. The switch will not be able to see traffic from the VM.")
warnings = appendWarnings(warnings, warning)
}
}
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return warnings, errs return warnings, errs
} }
@ -356,7 +364,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&hypervcommon.StepConfigureVlan{ &hypervcommon.StepConfigureVlan{
VlanID: b.config.VlandID, VlanId: b.config.VlanId,
SwitchVlanId: b.config.SwitchVlanId,
}, },
&hypervcommon.StepRun{ &hypervcommon.StepRun{

View File

@ -186,7 +186,15 @@ can be configured for this builder.
this to an empty string, Packer will try to determine the switch to use by looking for this to an empty string, Packer will try to determine the switch to use by looking for
external switch that is up and running. external switch that is up and running.
- `vm_name` (string) - This is the name of the virtua machine for the new virtual - `switch_vlan_id` (string) - This is the vlan of the virtual switch's network card.
By default none is set. If none is set then a vlan is not set on the switch's network card.
If this value is set it should match the vlan specified in by `vlan_id`.
- `vlan_id` (string) - This is the vlan of the virtual machine's network card for the new virtual
machine. By default none is set. If none is set then vlans are not set on the virtual machine's
network card.
- `vm_name` (string) - This is the name of the virtual machine for the new virtual
machine, without the file extension. By default this is "packer-BUILDNAME", machine, without the file extension. By default this is "packer-BUILDNAME",
where "BUILDNAME" is the name of the build. where "BUILDNAME" is the name of the build.