diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 500f246d6..b3d826e8d 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -117,7 +117,6 @@ type Config struct { OutputDir string `mapstructure:"output_directory"` QemuArgs [][]string `mapstructure:"qemuargs"` QemuBinary string `mapstructure:"qemu_binary"` - QMPEnable bool `mapstructure:"qmp_enable"` QMPSocketPath string `mapstructure:"qmp_socket_path"` ShutdownCommand string `mapstructure:"shutdown_command"` SSHHostPortMin int `mapstructure:"ssh_host_port_min"` @@ -358,11 +357,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max")) } - if b.config.VNCUsePassword && !b.config.QMPEnable { - b.config.QMPEnable = true - } - - if b.config.QMPEnable && b.config.QMPSocketPath == "" { + if b.config.VNCUsePassword && b.config.QMPSocketPath == "" { socketName := fmt.Sprintf("%s.monitor", b.config.VMName) b.config.QMPSocketPath = filepath.Join(b.config.OutputDir, socketName) } diff --git a/builder/qemu/builder_test.go b/builder/qemu/builder_test.go index 6e49cbb24..122caa754 100644 --- a/builder/qemu/builder_test.go +++ b/builder/qemu/builder_test.go @@ -114,25 +114,6 @@ func TestBuilderPrepare_VNCBindAddress(t *testing.T) { } } -func TestBuilderPrepare_VNCPassword(t *testing.T) { - var b Builder - config := testConfig() - - // Test a default boot_wait - config["vnc_use_password"] = true - warns, err := b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Fatalf("err: %s", err) - } - - if !b.config.QMPEnable { - t.Fatalf("QMP should be enabled.") - } -} - func TestBuilderPrepare_DiskCompaction(t *testing.T) { var b Builder config := testConfig() @@ -619,12 +600,11 @@ func TestBuilderPrepare_QemuArgs(t *testing.T) { } } -func TestBuilderPrepare_QMP(t *testing.T) { +func TestBuilderPrepare_VNCPassword(t *testing.T) { var b Builder config := testConfig() - // QMP Defaults - config["qmp_enable"] = true + config["vnc_use_password"] = true config["output_directory"] = "not-a-real-directory" b = Builder{} warns, err := b.Prepare(config) diff --git a/builder/qemu/step_configure_qmp.go b/builder/qemu/step_configure_qmp.go index 957807a29..fa988ad0b 100644 --- a/builder/qemu/step_configure_qmp.go +++ b/builder/qemu/step_configure_qmp.go @@ -26,7 +26,7 @@ func (s *stepConfigureQMP) Run(ctx context.Context, state multistep.StateBag) mu config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) - if !config.QMPEnable { + if !config.VNCUsePassword { return multistep.ActionContinue } @@ -35,49 +35,46 @@ func (s *stepConfigureQMP) Run(ctx context.Context, state multistep.StateBag) mu log.Print(msg) // Only initialize and open QMP when we have a use for it. - // Handles cases where user may want the socket, but we don't - if config.VNCUsePassword { - // Open QMP socket - var err error - var cmd []byte - var result []byte - s.monitor, err = qmp.NewSocketMonitor("unix", config.QMPSocketPath, 2*time.Second) - if err != nil { - err := fmt.Errorf("Error opening QMP socket: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - QMPMonitor := s.monitor - vncPassword := state.Get("vnc_password") - - // Connect to QMP - // function automatically calls capabilities so is immediately ready for commands - err = QMPMonitor.Connect() - if err != nil { - err := fmt.Errorf("Error connecting to QMP socket: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - log.Printf("QMP socket open SUCCESS") - - cmd = []byte(fmt.Sprintf("{ \"execute\": \"change-vnc-password\", \"arguments\": { \"password\": \"%s\" } }", - vncPassword)) - result, err = QMPMonitor.Run(cmd) - if err != nil { - err := fmt.Errorf("Error connecting to QMP socket: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - msg = fmt.Sprintf("QMP Command: %s\nResult: %s", cmd, result) - log.Printf(msg) - - // Put QMP monitor in statebag in case there is a use in a following step - // Uncomment for future case as it is unused for now - //state.Put("qmp_monitor", QMPMonitor) + // Open QMP socket + var err error + var cmd []byte + var result []byte + s.monitor, err = qmp.NewSocketMonitor("unix", config.QMPSocketPath, 2*time.Second) + if err != nil { + err := fmt.Errorf("Error opening QMP socket: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } + QMPMonitor := s.monitor + vncPassword := state.Get("vnc_password") + + // Connect to QMP + // function automatically calls capabilities so is immediately ready for commands + err = QMPMonitor.Connect() + if err != nil { + err := fmt.Errorf("Error connecting to QMP socket: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + log.Printf("QMP socket open SUCCESS") + + cmd = []byte(fmt.Sprintf("{ \"execute\": \"change-vnc-password\", \"arguments\": { \"password\": \"%s\" } }", + vncPassword)) + result, err = QMPMonitor.Run(cmd) + if err != nil { + err := fmt.Errorf("Error connecting to QMP socket: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + msg = fmt.Sprintf("QMP Command: %s\nResult: %s", cmd, result) + log.Printf(msg) + + // Put QMP monitor in statebag in case there is a use in a following step + // Uncomment for future case as it is unused for now + //state.Put("qmp_monitor", QMPMonitor) return multistep.ActionContinue } diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index d5f3030ae..55390168c 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -79,6 +79,7 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error vnc = fmt.Sprintf("%s:%d", vncIP, vncPort-5900) } else { vnc = fmt.Sprintf("%s:%d,password", vncIP, vncPort-5900) + defaultArgs["-qmp"] = fmt.Sprintf("unix:%s,server,nowait", config.QMPSocketPath) } defaultArgs["-name"] = vmName @@ -90,10 +91,6 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0") } - if config.QMPEnable { - defaultArgs["-qmp"] = fmt.Sprintf("unix:%s,server,nowait", config.QMPSocketPath) - } - rawVersion, err := driver.Version() if err != nil { return nil, err diff --git a/website/source/docs/builders/qemu.html.md.erb b/website/source/docs/builders/qemu.html.md.erb index 4dc0b5f28..fe0a9143e 100644 --- a/website/source/docs/builders/qemu.html.md.erb +++ b/website/source/docs/builders/qemu.html.md.erb @@ -282,11 +282,7 @@ Linux server and have not enabled X11 forwarding (`ssh -X`). switch/value pairs. Any value specified as an empty string is ignored. All values after the switch are concatenated with no separator. -- `qmp_enable` (bool) - Enable QMP socket. Location is specified by - `qmp_socket_path`. - Defaults to false. - -- `qmp_socket_path` (string) - QMP Socket Path when `qmp_enable` is true. +- `qmp_socket_path` (string) - QMP Socket Path when `vnc_use_password` is true. Defaults to `output_directory`/`vm_name`.monitor. ~> **Warning:** The qemu command line allows extreme flexibility, so beware @@ -398,7 +394,7 @@ default port of `5985` or whatever value you have the service set to listen on. default this is `5900` to `6000`. The minimum and maximum ports are inclusive. - `vnc_use_password` (bool) - Whether or not to set a password on the VNC server. - This option automatically sets `qmp_enable` to true. + This option automatically enables the QMP socket. See `qmp_socket_path`. Defaults to `false`. ## Boot Command