Repository: hashicorp/nomad
File: drivers/qemu/driver.go (Go)

Code before:
	var netdevArgs []string
	if cfg.DNS != nil {
		if len(cfg.DNS.Servers) > 0 {
			netdevArgs = append(netdevArgs, "dns="+cfg.DNS.Servers[0])
		}

		for _, s := range cfg.DNS.Searches {
			netdevArgs = append(netdevArgs, "dnssearch="+s)
		}
	}

	taskDir := filepath.Join(cfg.AllocDir, cfg.Name)

	var monitorPath string
	if driverConfig.GracefulShutdown {
		if runtime.GOOS == "windows" {
			return nil, nil, errors.New("QEMU graceful shutdown is unsupported on the Windows platform")
		}
		// This socket will be used to manage the virtual machine (for example,
		// to perform graceful shutdowns)
		monitorPath = filepath.Join(taskDir, qemuMonitorSocketName)
		if err := validateSocketPath(monitorPath); err != nil {
			return nil, nil, err
		}
		d.logger.Debug("got monitor path", "monitorPath", monitorPath)
		args = append(args, "-qmp", fmt.Sprintf("unix:%s,server=on,wait=off", monitorPath))
	}

	if driverConfig.GuestAgent {
		if runtime.GOOS == "windows" {
			return nil, nil, errors.New("QEMU Guest Agent socket is unsupported on the Windows platform")
		}
		// This socket will be used to communicate with the Guest Agent (if it's running)
		agentSocketPath := filepath.Join(taskDir, qemuGuestAgentSocketName)
		if err := validateSocketPath(agentSocketPath); err != nil {
			return nil, nil, err
		}

		args = append(args, "-chardev", fmt.Sprintf("socket,path=%s,server,nowait,id=qga0", agentSocketPath))
		args = append(args, "-device", "virtio-serial")
		args = append(args, "-device", "virtserialport,chardev=qga0,name=org.qemu.guest_agent.0")
	}

	// Add pass through arguments to qemu executable. A user can specify
	// these arguments in driver task configuration. These arguments are
	// passed directly to the qemu driver as command line options.
	// For example, args = [ "-nodefconfig", "-nodefaults" ]
	// This will allow a VM with embedded configuration to boot successfully.
	args = append(args, driverConfig.Args...)

	// Check the Resources required Networks to add port mappings. If no resources

Review comment (refactor):
I really like the idea of the QEMU driver interacting with QEMU VM's via QMP! However, I have a couple caveats.

1. I don't believe it's necessary to fix the current issue at hand (your `for` loop fixed it in my env using `-monitor`). 
2. We are leaving a lot of references to `monitorPath` around the driver, when we're really now using QMP, not monitor, which will be confusing for maintainers.
3. I think the real benefit of using QMP comes from doing the negotiation earlier (like in StartTask), so a user could use something like Nomad actions to run other QMP commands.. Or even Nomad doing things like using QMP for VM migrations in the future.

All that is to say, I think it's awesome, but it needs a little more work. And if you want to just fix the shutdown logic, maybe it's worth doing the QMP refactor in another PR? I'm open to thoughts here.

Code after:
	if vmPath == "" {
		return nil, nil, fmt.Errorf("image_path must be set")
	}
	vmID := filepath.Base(vmPath)

	if !isAllowedImagePath(d.config.ImagePaths, cfg.AllocDir, vmPath) {
		return nil, nil, fmt.Errorf("image_path is not in the allowed paths")
	}

	// Parse configuration arguments
	// Create the base arguments
	emulator := "x86_64"
	if driverConfig.Emulator != "" {
		emulator = driverConfig.Emulator

	}
	accelerator := "tcg"
	if driverConfig.Accelerator != "" {
		accelerator = driverConfig.Accelerator
	}
	machineType := "pc"
	if driverConfig.MachineType != "" {
		machineType = driverConfig.MachineType
	}

	mb := cfg.Resources.NomadResources.Memory.MemoryMB
	if mb < 128 || mb > 4000000 {
		return nil, nil, fmt.Errorf("QEMU memory assignment out of bounds")
	}
	mem := fmt.Sprintf("%dM", mb)

	absPath, err := GetAbsolutePath(fmt.Sprintf("qemu-system-%s", emulator))
	if err != nil {
		return nil, nil, err
	}

	driveInterface := "ide"
	if driverConfig.DriveInterface != "" {
		driveInterface = driverConfig.DriveInterface
	}
	if !isAllowedDriveInterface(driveInterface) {
		return nil, nil, fmt.Errorf("Unsupported drive_interface")
	}

	args := []string{
		absPath,
		"-machine", "type=" + machineType + ",accel=" + accelerator,
		"-name", vmID,
		"-m", mem,
		// setting a drive ID allows users to attach this to other devices
		"-drive", "file=" + vmPath + ",if=" + driveInterface + ",id=image0",