Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 276] QEMU Agent configuration settings #287

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
56 changes: 55 additions & 1 deletion .web-docs/components/builder/clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,13 @@ boot time.
]
```

- `qemu_agent` (boolean) - Enables QEMU Agent option for this VM. When enabled,
- `qemu_agent` (boolean) - DEPRECATED. Define QEMU Guest Agent settings in a `qemu_guest_agent` block instead.
Enables QEMU Agent option for this VM. When enabled,
then `qemu-guest-agent` must be installed on the guest. When disabled, then
`ssh_host` should be used. Defaults to `true`.

- `qemu_guest_agent` (agentConfig) - QEMU Guest Agent configuration. See [QEMU Guest Agent](#qemu-guest-agent)

- `scsi_controller` (string) - The SCSI controller model to emulate. Can be `lsi`,
`lsi53c810`, `virtio-scsi-pci`, `virtio-scsi-single`, `megasas`, or `pvscsi`.
Defaults to `lsi`.
Expand Down Expand Up @@ -826,6 +829,57 @@ Usage example (JSON):
<!-- End of code generated from the comments of the efiConfig struct in builder/proxmox/common/config.go; -->


### QEMU Guest Agent

<!-- Code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->

Set the QEMU Guest Agent options.

JSON Example:

```json

"qemu_guest_agent": {
"enabled": true,
"type": "isa",
"freeze": false,
"fstrim": false
}

```
HCL2 example:

```hcl

qemu_guest_agent {
enabled = true
type = "isa"
freeze = false
fstrim = false
}

```

<!-- End of code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; -->


#### Optional:

<!-- Code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->

- `enabled` (boolean) - Enable QEMU Agent option for this VM. When enabled
`qemu-guest-agent` must be installed on the guest. When disabled
`ssh_host` should be used. Defaults to `true`.

- `type` (string) - Sets the Agent Type. Must be `isa` or `virtio`. Defaults to `virtio`

- `disable_freeze` (bool) - Disable freeze/thaw of guest filesystem on backup. Defaults to `false`

- `fstrim` (bool) - Run guest-trim after a disk move or VM migration. Defaults to `false`

<!-- End of code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; -->


### VirtIO RNG device

<!-- Code generated from the comments of the rng0Config struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->
Expand Down
56 changes: 55 additions & 1 deletion .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@ in the image's Cloud-Init settings for provisioning.
]
```

- `qemu_agent` (boolean) - Enables QEMU Agent option for this VM. When enabled,
- `qemu_agent` (boolean) - DEPRECATED. Define QEMU Guest Agent settings in a `qemu_guest_agent` block instead.
Enables QEMU Agent option for this VM. When enabled,
then `qemu-guest-agent` must be installed on the guest. When disabled, then
`ssh_host` should be used. Defaults to `true`.

- `qemu_guest_agent` (agentConfig) - QEMU Guest Agent configuration. See [QEMU Guest Agent](#qemu-guest-agent)

- `scsi_controller` (string) - The SCSI controller model to emulate. Can be `lsi`,
`lsi53c810`, `virtio-scsi-pci`, `virtio-scsi-single`, `megasas`, or `pvscsi`.
Defaults to `lsi`.
Expand Down Expand Up @@ -667,6 +670,57 @@ Usage example (JSON):
<!-- End of code generated from the comments of the efiConfig struct in builder/proxmox/common/config.go; -->


### QEMU Guest Agent

<!-- Code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->

Set the QEMU Guest Agent options.

JSON Example:

```json

"qemu_guest_agent": {
"enabled": true,
"type": "isa",
"freeze": false,
"fstrim": false
}

```
HCL2 example:

```hcl

qemu_guest_agent {
enabled = true
type = "isa"
freeze = false
fstrim = false
}

```

<!-- End of code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; -->


#### Optional:

<!-- Code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->

- `enabled` (boolean) - Enable QEMU Agent option for this VM. When enabled
`qemu-guest-agent` must be installed on the guest. When disabled
`ssh_host` should be used. Defaults to `true`.

- `type` (string) - Sets the Agent Type. Must be `isa` or `virtio`. Defaults to `virtio`

- `disable_freeze` (bool) - Disable freeze/thaw of guest filesystem on backup. Defaults to `false`

- `fstrim` (bool) - Run guest-trim after a disk move or VM migration. Defaults to `false`

<!-- End of code generated from the comments of the agentConfig struct in builder/proxmox/common/config.go; -->


### VirtIO RNG device

<!-- Code generated from the comments of the rng0Config struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->
Expand Down
2 changes: 2 additions & 0 deletions builder/proxmox/clone/config.hcl2spec.go

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

65 changes: 61 additions & 4 deletions builder/proxmox/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MPL-2.0

//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NICConfig,diskConfig,rng0Config,pciDeviceConfig,vgaConfig,ISOsConfig,efiConfig,tpmConfig
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NICConfig,diskConfig,rng0Config,pciDeviceConfig,vgaConfig,ISOsConfig,efiConfig,tpmConfig,agentConfig

package proxmox

Expand Down Expand Up @@ -158,10 +158,13 @@ type Config struct {
// ]
// ```
Serials []string `mapstructure:"serials"`
// DEPRECATED. Define QEMU Guest Agent settings in a `qemu_guest_agent` block instead.
// Enables QEMU Agent option for this VM. When enabled,
// then `qemu-guest-agent` must be installed on the guest. When disabled, then
// `ssh_host` should be used. Defaults to `true`.
Agent config.Trilean `mapstructure:"qemu_agent"`
// QEMU Guest Agent configuration. See [QEMU Guest Agent](#qemu-guest-agent)
GuestAgent agentConfig `mapstructure:"qemu_guest_agent"`
// The SCSI controller model to emulate. Can be `lsi`,
// `lsi53c810`, `virtio-scsi-pci`, `virtio-scsi-single`, `megasas`, or `pvscsi`.
// Defaults to `lsi`.
Expand Down Expand Up @@ -207,6 +210,45 @@ type Config struct {
Ctx interpolate.Context `mapstructure-to-hcl2:",skip"`
}

// Set the QEMU Guest Agent options.
//
// JSON Example:
//
// ```json
//
// "qemu_guest_agent": {
// "enabled": true,
// "type": "isa",
// "freeze": false,
// "fstrim": false
// }
//
// ```
// HCL2 example:
//
// ```hcl
//
// qemu_guest_agent {
// enabled = true
// type = "isa"
// freeze = false
// fstrim = false
// }
//
// ```
type agentConfig struct {
// Enable QEMU Agent option for this VM. When enabled
// `qemu-guest-agent` must be installed on the guest. When disabled
// `ssh_host` should be used. Defaults to `true`.
Enabled config.Trilean `mapstructure:"enabled"`
// Sets the Agent Type. Must be `isa` or `virtio`. Defaults to `virtio`
Type string `mapstructure:"type"`
// Disable freeze/thaw of guest filesystem on backup. Defaults to `false`
DisableFreeze bool `mapstructure:"disable_freeze"`
// Run guest-trim after a disk move or VM migration. Defaults to `false`
FsTrim bool `mapstructure:"fstrim"`
}

// ISO files attached to the virtual machine.
//
// JSON Example:
Expand Down Expand Up @@ -604,9 +646,24 @@ func (c *Config) Prepare(upper interface{}, raws ...interface{}) ([]string, []st
warnings = append(warnings, "proxmox is deprecated, please use proxmox-iso instead")
}

// Default qemu_agent to true
if c.Agent != config.TriFalse {
c.Agent = config.TriTrue
if c.Agent != config.TriUnset {
warnings = append(warnings, "qemu_agent is deprecated and will be removed in a future release. define QEMU agent settings in a qemu_guest_agent block instead")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest we only print that warning if qemu_agent is unset here, not if it's not explicitly false, otherwise we'll see it all the time unless we add qemu_agent = false to the template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurred to me that we don't need to set a default value for c.Agent given the replacement c.GuestAgent.Enabled has a default set further down, so I've changed the logic to warn and convert the value only if the c.Agent is not Unset.

// convert to qemu_guest_agent block value
c.GuestAgent.Enabled = c.Agent
}

// Default qemu_guest_agent.enable to true
if c.GuestAgent.Enabled != config.TriFalse {
c.GuestAgent.Enabled = config.TriTrue
}

switch c.GuestAgent.Type {
case "virtio", "isa":
case "":
log.Printf("qemu_guest_agent type not specified, defaulting to `virtio`")
c.GuestAgent.Type = "virtio"
default:
errs = packersdk.MultiErrorAppend(errs, errors.New("qemu_guest_agent type field must be `virtio` or `isa`"))
}

packersdk.LogSecretFilter.Set(c.Password)
Expand Down
31 changes: 31 additions & 0 deletions builder/proxmox/common/config.hcl2spec.go

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

16 changes: 16 additions & 0 deletions builder/proxmox/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func TestAgentSetToFalse(t *testing.T) {
}
}

func TestQemuAgentInvalidType(t *testing.T) {
cfg := mandatoryConfig(t)

agentCfg := make(map[string]interface{})
agentCfg["type"] = "qwerty"

cfg["qemu_guest_agent"] = agentCfg

var c Config
_, _, err := c.Prepare(&c, cfg)
// expect error, if none returned fail test
if err == nil {
t.Error("expected config preparation to fail, but no error occured")
}
}

func TestPacketQueueSupportForNetworkAdapters(t *testing.T) {
drivertests := []struct {
expectedToFail bool
Expand Down
24 changes: 15 additions & 9 deletions builder/proxmox/common/step_start_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
)

// stepStartVM takes the given configuration and starts a VM on the given Proxmox node.
Expand Down Expand Up @@ -123,7 +122,7 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist

config := proxmox.ConfigQemu{
Name: c.VMName,
Agent: generateAgentConfig(c.Agent),
Agent: generateAgentConfig(c.GuestAgent),
QemuKVM: &kvm,
Tags: generateTags(c.Tags),
Boot: c.Boot, // Boot priority, example: "order=virtio0;ide2;net0", virtio0:Disk0 -> ide0:CDROM -> net0:Network
Expand Down Expand Up @@ -255,16 +254,23 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
return multistep.ActionContinue
}

func generateAgentConfig(agent config.Trilean) *proxmox.QemuGuestAgent {
var enableAgent bool
func generateAgentConfig(agent agentConfig) *proxmox.QemuGuestAgent {
var agentCfg proxmox.QemuGuestAgent
freeze := true

if agent.True() {
enableAgent = true
}
agentCfg.Enable = agent.Enabled.ToBoolPointer()

return &proxmox.QemuGuestAgent{
Enable: &enableAgent,
if agent.DisableFreeze {
freeze = false
}
agentCfg.Freeze = &freeze

agentType := proxmox.QemuGuestAgentType(agent.Type)
agentCfg.Type = &agentType

agentCfg.FsTrim = &agent.FsTrim

return &agentCfg
}

func generateTags(rawTags string) *[]proxmox.Tag {
Expand Down
Loading