diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 9b4abb437..cb0ac6c88 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -66,6 +66,11 @@ var diskCache = map[string]bool{ "directsync": true, } +var diskDiscard = map[string]bool{ + "unmap": true, + "ignore": true, +} + type Builder struct { config Config runner multistep.Runner @@ -79,6 +84,7 @@ type Config struct { DiskInterface string `mapstructure:"disk_interface"` DiskSize uint `mapstructure:"disk_size"` DiskCache string `mapstructure:"disk_cache"` + DiskDiscard string `mapstructure:"disk_discard"` FloppyFiles []string `mapstructure:"floppy_files"` Format string `mapstructure:"format"` Headless bool `mapstructure:"headless"` @@ -144,6 +150,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.DiskCache = "writeback" } + if b.config.DiskDiscard == "" { + b.config.DiskDiscard = "ignore" + } + if b.config.Accelerator == "" { b.config.Accelerator = "kvm" } @@ -237,6 +247,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("unrecognized disk cache type")) } + if _, ok := diskDiscard[b.config.DiskDiscard]; !ok { + errs = packer.MultiErrorAppend( + errs, errors.New("unrecognized disk cache type")) + } + if b.config.HTTPPortMin > b.config.HTTPPortMax { errs = packer.MultiErrorAppend( errs, errors.New("http_port_min must be less than http_port_max")) diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index aa13a1076..4f64c4d8b 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -82,7 +82,7 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error defaultArgs["-machine"] = fmt.Sprintf("type=%s", config.MachineType) defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0,hostfwd=tcp::%v-:22", sshHostPort) defaultArgs["-device"] = fmt.Sprintf("%s,netdev=user.0", config.NetDevice) - defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s", imgPath, config.DiskInterface, config.DiskCache) + defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard) if !config.DiskImage { defaultArgs["-cdrom"] = isoPath } diff --git a/website/source/docs/builders/qemu.html.markdown b/website/source/docs/builders/qemu.html.markdown index 5aa0cf152..0e22ccc3b 100644 --- a/website/source/docs/builders/qemu.html.markdown +++ b/website/source/docs/builders/qemu.html.markdown @@ -115,6 +115,9 @@ each category, the available options are alphabetized and described. values include any of "writethrough", "writeback", "none", "unsafe" or "directsync". +* `disk_discard` (string) - The discard mode to use for disk. Allowed values + include any of "unmap" or "ignore". + * `disk_image` (boolean) - Packer defaults to building from an ISO file, this parameter controls whether the ISO URL supplied is actually a bootable QEMU image. When this value is set to true, the machine will clone the