From 1155347b5574f54624dd9ce56b76635ddd9a1d5a Mon Sep 17 00:00:00 2001 From: Konstantin Shloma Date: Wed, 10 Oct 2018 14:38:59 +0300 Subject: [PATCH 1/2] add qemu disk detect-zeroes= option --- builder/qemu/builder.go | 16 ++++++++++++++++ builder/qemu/step_run.go | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 189569288..fb8ad621a 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -75,6 +75,12 @@ var diskDiscard = map[string]bool{ "ignore": true, } +var diskDZeroes = map[string]bool{ + "unmap": true, + "on": true, + "off": true, +} + type Builder struct { config Config runner multistep.Runner @@ -94,6 +100,7 @@ type Config struct { DiskSize uint `mapstructure:"disk_size"` DiskCache string `mapstructure:"disk_cache"` DiskDiscard string `mapstructure:"disk_discard"` + DetectZeroes string `mapstructure:"disk_detect_zeroes"` SkipCompaction bool `mapstructure:"skip_compaction"` DiskCompression bool `mapstructure:"disk_compression"` Format string `mapstructure:"format"` @@ -157,6 +164,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.DiskDiscard = "ignore" } + if b.config.DetectZeroes == "" { + b.config.DetectZeroes = "off" + } + if b.config.Accelerator == "" { if runtime.GOOS == "windows" { b.config.Accelerator = "tcg" @@ -286,6 +297,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("unrecognized disk discard type")) } + if _, ok := diskDZeroes[b.config.DetectZeroes]; !ok { + errs = packer.MultiErrorAppend( + errs, errors.New("unrecognized disk detect zeroes setting")) + } + if !b.config.PackerForce { if _, err := os.Stat(b.config.OutputDir); err == nil { errs = packer.MultiErrorAppend( diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index c524d5514..bf567c5f1 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -97,9 +97,9 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error if qemuMajor >= 2 { if config.DiskInterface == "virtio-scsi" { deviceArgs = append(deviceArgs, "virtio-scsi-pci,id=scsi0", "scsi-hd,bus=scsi0.0,drive=drive0") - driveArgs = append(driveArgs, fmt.Sprintf("if=none,file=%s,id=drive0,cache=%s,discard=%s,format=%s", imgPath, config.DiskCache, config.DiskDiscard, config.Format)) + driveArgs = append(driveArgs, fmt.Sprintf("if=none,file=%s,id=drive0,cache=%s,discard=%s,format=%s,detect-zeroes=%s", imgPath, config.DiskCache, config.DiskDiscard, config.Format, config.DetectZeroes)) } else { - driveArgs = append(driveArgs, fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s,format=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard, config.Format)) + driveArgs = append(driveArgs, fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s,format=%s,detect-zeroes=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard, config.Format, config.DetectZeroes)) } } else { driveArgs = append(driveArgs, fmt.Sprintf("file=%s,if=%s,cache=%s,format=%s", imgPath, config.DiskInterface, config.DiskCache, config.Format)) From fe341e2bd03b4645af0ccdcdedb339f6591a6b2f Mon Sep 17 00:00:00 2001 From: Konstantin Shloma Date: Wed, 10 Oct 2018 16:16:23 +0300 Subject: [PATCH 2/2] fix go source formatting add description to documentation page --- builder/qemu/builder.go | 6 +++--- website/source/docs/builders/qemu.html.md.erb | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index fb8ad621a..1535eda86 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -76,9 +76,9 @@ var diskDiscard = map[string]bool{ } var diskDZeroes = map[string]bool{ - "unmap": true, - "on": true, - "off": true, + "unmap": true, + "on": true, + "off": true, } type Builder struct { diff --git a/website/source/docs/builders/qemu.html.md.erb b/website/source/docs/builders/qemu.html.md.erb index 962b85c41..768a94a49 100644 --- a/website/source/docs/builders/qemu.html.md.erb +++ b/website/source/docs/builders/qemu.html.md.erb @@ -150,6 +150,9 @@ Linux server and have not enabled X11 forwarding (`ssh -X`). - `disk_discard` (string) - The discard mode to use for disk. Allowed values include any of `unmap` or `ignore`. By default, this is set to `ignore`. +- `disk_detect_zeroes` (string) - The detect-zeroes mode to use for disk. + Allowed values include any of `unmap`, `on` or `off`. Defaults to `off`. + - `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 either clone