From 1ecfa032ba0e0e320f279a79394b6118c775dfb7 Mon Sep 17 00:00:00 2001 From: Kevin Faulkner Date: Sun, 21 Apr 2019 20:35:45 -0400 Subject: [PATCH 1/2] Qemu builder disk size as a string --- builder/qemu/builder.go | 10 ++++------ builder/qemu/builder_test.go | 10 +++++----- builder/qemu/step_create_disk.go | 2 +- builder/qemu/step_resize_disk.go | 3 +-- website/source/docs/builders/qemu.html.md.erb | 2 +- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index a277e1d45..0ee680f56 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -148,7 +148,7 @@ type Config struct { DiskInterface string `mapstructure:"disk_interface" required:"false"` // The size, in megabytes, of the hard disk to create // for the VM. By default, this is 40960 (40 GB). - DiskSize uint `mapstructure:"disk_size" required:"false"` + DiskSize string `mapstructure:"disk_size" required:"false"` // The cache mode to use for disk. Allowed values include any of // `writethrough`, `writeback`, `none`, `unsafe` or `directsync`. By // default, this is set to `writeback`. @@ -316,7 +316,6 @@ type Config struct { // "BUILDNAME" is the name of the build. Currently, no file extension will be // used unless it is specified in this option. VMName string `mapstructure:"vm_name" required:"false"` - // These are deprecated, but we keep them around for BC // TODO(@mitchellh): remove SSHWaitTimeout time.Duration `mapstructure:"ssh_wait_timeout" required:"false"` @@ -344,11 +343,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { var errs *packer.MultiError warnings := make([]string, 0) - errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) - if b.config.DiskSize == 0 { - b.config.DiskSize = 40960 + if b.config.DiskSize == "" || b.config.DiskSize == "0" { + b.config.DiskSize = "40960M" } if b.config.DiskCache == "" { @@ -700,7 +698,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack artifact.state["diskPaths"] = diskpaths } artifact.state["diskType"] = b.config.Format - artifact.state["diskSize"] = uint64(b.config.DiskSize) + artifact.state["diskSize"] = b.config.DiskSize artifact.state["domainType"] = b.config.Accelerator return artifact, nil diff --git a/builder/qemu/builder_test.go b/builder/qemu/builder_test.go index 122caa754..15f3db8b3 100644 --- a/builder/qemu/builder_test.go +++ b/builder/qemu/builder_test.go @@ -169,11 +169,11 @@ func TestBuilderPrepare_DiskSize(t *testing.T) { t.Fatalf("bad err: %s", err) } - if b.config.DiskSize != 40960 { - t.Fatalf("bad size: %d", b.config.DiskSize) + if b.config.DiskSize != "40960M" { + t.Fatalf("bad size: %s", b.config.DiskSize) } - config["disk_size"] = 60000 + config["disk_size"] = "60000M" b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { @@ -183,8 +183,8 @@ func TestBuilderPrepare_DiskSize(t *testing.T) { t.Fatalf("should not have error: %s", err) } - if b.config.DiskSize != 60000 { - t.Fatalf("bad size: %d", b.config.DiskSize) + if b.config.DiskSize != "60000M" { + t.Fatalf("bad size: %s", b.config.DiskSize) } } diff --git a/builder/qemu/step_create_disk.go b/builder/qemu/step_create_disk.go index 8fb18d025..12041565c 100644 --- a/builder/qemu/step_create_disk.go +++ b/builder/qemu/step_create_disk.go @@ -29,7 +29,7 @@ func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) mult ui.Say("Creating required virtual machine disks") // The 'main' or 'default' disk diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, name)) - diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize))) + diskSizes = append(diskSizes, fmt.Sprintf("%s", config.DiskSize)) // Additional disks if len(config.AdditionalDiskSize) > 0 { for i, diskSize := range config.AdditionalDiskSize { diff --git a/builder/qemu/step_resize_disk.go b/builder/qemu/step_resize_disk.go index d25ffaff4..f9dd5693e 100644 --- a/builder/qemu/step_resize_disk.go +++ b/builder/qemu/step_resize_disk.go @@ -23,9 +23,8 @@ func (s *stepResizeDisk) Run(ctx context.Context, state multistep.StateBag) mult "resize", "-f", config.Format, path, - fmt.Sprintf("%vM", config.DiskSize), + fmt.Sprintf("%s", config.DiskSize), } - if config.DiskImage == false { return multistep.ActionContinue } diff --git a/website/source/docs/builders/qemu.html.md.erb b/website/source/docs/builders/qemu.html.md.erb index 78cf5f013..2e7db1dfc 100644 --- a/website/source/docs/builders/qemu.html.md.erb +++ b/website/source/docs/builders/qemu.html.md.erb @@ -37,7 +37,7 @@ to files, URLS for ISOs and checksums. "iso_checksum_type": "md5", "output_directory": "output_centos_tdhtest", "shutdown_command": "echo 'packer' | sudo -S shutdown -P now", - "disk_size": 5000, + "disk_size": "5000M", "format": "qcow2", "accelerator": "kvm", "http_directory": "path/to/httpdir", From 19a9d7149f2c902e0ae3e6b1681438224ebd9999 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 1 Oct 2019 13:36:55 -0700 Subject: [PATCH 2/2] fix structs and regenerate partials --- builder/qemu/builder.go | 6 ++++-- .../partials/builder/qemu/_Config-not-required.html.md | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 0ee680f56..10c821b7e 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -146,8 +146,10 @@ type Config struct { // one of the other listed interfaces. Using the `scsi` interface under // these circumstances will cause the build to fail. DiskInterface string `mapstructure:"disk_interface" required:"false"` - // The size, in megabytes, of the hard disk to create - // for the VM. By default, this is 40960 (40 GB). + // The size in bytes, suffixes of the first letter of common byte types + // like "k" or "K", "M" for megabytes, G for gigabytes, T for terabytes. + // Will create the of the hard disk of the VM. By default, this is + // `40960M` (40 GB). DiskSize string `mapstructure:"disk_size" required:"false"` // The cache mode to use for disk. Allowed values include any of // `writethrough`, `writeback`, `none`, `unsafe` or `directsync`. By diff --git a/website/source/partials/builder/qemu/_Config-not-required.html.md b/website/source/partials/builder/qemu/_Config-not-required.html.md index 07ac4256a..723c53276 100644 --- a/website/source/partials/builder/qemu/_Config-not-required.html.md +++ b/website/source/partials/builder/qemu/_Config-not-required.html.md @@ -47,8 +47,10 @@ one of the other listed interfaces. Using the `scsi` interface under these circumstances will cause the build to fail. -- `disk_size` (uint) - The size, in megabytes, of the hard disk to create - for the VM. By default, this is 40960 (40 GB). +- `disk_size` (string) - The size in bytes, suffixes of the first letter of common byte types + like "k" or "K", "M" for megabytes, G for gigabytes, T for terabytes. + Will create the of the hard disk of the VM. By default, this is + `40960M` (40 GB). - `disk_cache` (string) - The cache mode to use for disk. Allowed values include any of `writethrough`, `writeback`, `none`, `unsafe` or `directsync`. By