QEMU: Minor fix to VNC mapping parameters and output

* Set the QEMU builder vnc_port_min to have a minimum value of 5900,
  with updated documentation to explain why.

* Changed output messages so that the correct port is listed in packer's
  messaging to the user.

LONGER DESCRIPTION

If vnc_min_port is set to anything above 5900, then packer will fail to
connect via VNC to the QEMU instance. This is due to how QEMU parses
the port for listening to VNC:

   host:d
               TCP connections will only be allowed from host on
               display d.  By convention the TCP port is 5900+d.

Previously the calculation for the port was vncPort - VNCPortMin,
however this will result in an incorrect port being displayed in
packer's messages and potentially packer being unable to connect via VNC
to the host.

For example if vnc_port_min=5990 nad vnc_port_max=5999:

```
Looking for available port between 5990 and 5999 on 0.0.0.0
Found available port: 5996 on IP: 0.0.0.0
Found available VNC port: 5996 on IP: 0.0.0.0
[....]
==> Starting VM, booting disk image
     view the screen of the VM, connect via VNC without a password to
     vnc://0.0.0.0:6
```

This will cause QEMU to set the listening port to 5906 while packer's
VNC client is attempting to connect to 5996.

I am a touch concerned as this commit undoes pull request #9905
(specfically commit 7335695c), but I am also confused as to how he was
able to get QEMU to get a VNC listening port below 5900, as examining
QEMU's git history has shown that this behavior has been in since at
least 2017, probably older.

Hopefully the more explicit error messaging and documentation can make
it clear why this is being undone.
This commit is contained in:
Darwin Liu 2021-01-07 19:46:07 -07:00
parent 6564ee76e8
commit 4f2e9117c3
3 changed files with 15 additions and 5 deletions

View File

@ -374,6 +374,8 @@ type Config struct {
// the initial boot_command. Because Packer generally runs in parallel,
// Packer uses a randomly chosen port in this range that appears available. By
// default this is 5900 to 6000. The minimum and maximum ports are inclusive.
// The minimum port cannot be set below 5900 due to a quirk in how QEMU parses
// vnc display address.
VNCPortMin int `mapstructure:"vnc_port_min" required:"false"`
VNCPortMax int `mapstructure:"vnc_port_max"`
// This is the name of the image (QCOW2 or IMG) file for
@ -592,6 +594,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}
if c.VNCPortMin < 5900 {
errs = packersdk.MultiErrorAppend(
errs, fmt.Errorf("vnc_port_min cannot be below 5900"))
}
if c.VNCPortMin > c.VNCPortMax {
errs = packersdk.MultiErrorAppend(
errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max"))

View File

@ -120,17 +120,18 @@ func (s *stepRun) getDefaultArgs(config *Config, state multistep.StateBag) map[s
vncPort := state.Get("vnc_port").(int)
vncIP := config.VNCBindAddress
vncPort = vncPort - config.VNCPortMin
vnc := fmt.Sprintf("%s:%d", vncIP, vncPort)
vncRealAddress := fmt.Sprintf("%s:%d", vncIP, vncPort)
vncPort = vncPort - 5900
vncArgs := fmt.Sprintf("%s:%d", vncIP, vncPort)
if config.VNCUsePassword {
vnc = fmt.Sprintf("%s:%d,password", vncIP, vncPort)
vncArgs = fmt.Sprintf("%s:%d,password", vncIP, vncPort)
}
defaultArgs["-vnc"] = vnc
defaultArgs["-vnc"] = vncArgs
// Track the connection for the user
vncPass, _ := state.Get("vnc_password").(string)
message = getVncConnectionMessage(config.Headless, vnc, vncPass)
message = getVncConnectionMessage(config.Headless, vncRealAddress, vncPass)
if message != "" {
s.ui.Message(message)
}

View File

@ -301,6 +301,8 @@
the initial boot_command. Because Packer generally runs in parallel,
Packer uses a randomly chosen port in this range that appears available. By
default this is 5900 to 6000. The minimum and maximum ports are inclusive.
The minimum port cannot be set below 5900 due to a quirk in how QEMU parses
vnc display address.
- `vnc_port_max` (int) - VNC Port Max