8000 make sure we continue to default to Megabytes for qemu disk size to p… by SwampDragons · Pull Request #8438 · hashicorp/packer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

make sure we continue to default to Megabytes for qemu disk size to p… #8438

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

Merged
merged 2 commits into from
Dec 3, 2019
Merged
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
29 changes: 24 additions & 5 deletions builder/qemu/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"

"github.com/hashicorp/packer/common"
Expand Down Expand Up @@ -147,10 +149,11 @@ 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 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).
// The size in bytes of the hard disk of the VM. Suffix with the first
// letter of common byte types. Use "k" or "K" for kilobytes, "M" for
// megabytes, G for gigabytes, and T for terabytes. If no value is provided
// for disk_size, Packer uses a default of `40960M` (40 GB). If a disk_size
// number is provided with no units, Packer will default to Megabytes.
DiskSize string `mapstructure:"disk_size" required:"false"`
Comment on lines +152 to 157
Copy link
Contributor

Choose a reason for hiding this comment

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

I was about to say that I was amazed that there is not open source package that does this like the time pkg. But then I found this: https://github.com/docker/go-units/blob/master/size.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically a direct passthrough to the qemu-img command call, so I think using an intermediary library would be more work than it's worth.

// The cache mode to use for disk. Allowed values include any of
// `writethrough`, `writeback`, `none`, `unsafe` or `directsync`. By
Expand Down Expand Up @@ -354,6 +357,23 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {

if b.config.DiskSize == "" || b.config.DiskSize == "0" {
b.config.DiskSize = "40960M"
} else {
// Make sure supplied disk size is valid
// (digits, plus an optional valid unit character). e.g. 5000, 40G, 1t
re := regexp.MustCompile(`^[\d]+(b|k|m|g|t){0,1}$`)
matched := re.MatchString(strings.ToLower(b.config.DiskSize))
if !matched {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid disk size."))
} else {
// Okay, it's valid -- if it doesn't alreay have a suffix, then
// append "M" as the default unit.
re = regexp.MustCompile(`^[\d]+$`)
matched = re.MatchString(strings.ToLower(b.config.DiskSize))
if matched {
// Needs M added.
b.config.DiskSize = fmt.Sprintf("%sM", b.config.DiskSize)
}
}
}

if b.config.DiskCache == "" {
Expand Down Expand Up @@ -457,7 +477,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
if b.config.ISOSkipCache {
b.config.ISOChecksumType = "none"
}

isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)
Expand Down
60 changes: 32 additions & 28 deletions builder/qemu/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,38 @@ func TestBuilderPrepare_DiskCompaction(t *testing.T) {
}

func TestBuilderPrepare_DiskSize(t *testing.T) {
var b Builder
config := testConfig()

delete(config, "disk_size")
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("bad err: %s", err)
}

if b.config.DiskSize != "40960M" {
t.Fatalf("bad size: %s", b.config.DiskSize)
}

config["disk_size"] = "60000M"
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}

if b.config.DiskSize != "60000M" {
t.Fatalf("bad size: %s", b.config.DiskSize)
type testcase struct {
InputSize string
OutputSize string
ErrExpected bool
}

testCases := []testcase{
testcase{"", "40960M", false}, // not provided
testcase{"12345", "12345M", false}, // no unit given, defaults to M
testcase{"12345x", "12345x", true}, // invalid unit
testcase{"12345T", "12345T", false}, // terabytes
testcase{"12345b", "12345b", false}, // bytes get preserved when set.
testcase{"60000M", "60000M", false}, // Original test case
}
for _, tc := range testCases {
// Set input disk size
var b Builder
config := testConfig()
delete(config, "disk_size")
config["disk_size"] = tc.InputSize

warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if (err == nil) == tc.ErrExpected {
t.Fatalf("bad: error when providing disk size %s; Err expected: %t; err recieved: %v", tc.InputSize, tc.ErrExpected, err)
}

if b.config.DiskSize != tc.OutputSize {
t.Fatalf("bad size: received: %s but expected %s", b.config.DiskSize, tc.OutputSize)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
one of the other listed interfaces. Using the `scsi` interface under
these circumstances will cause the build to fail.

- `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_size` (string) - The size in bytes of the hard disk of the VM. Suffix with the first
letter of common byte types. Use "k" or "K" for kilobytes, "M" for
megabytes, G for gigabytes, and T for terabytes. If no value is provided
for disk_size, Packer uses a default of `40960M` (40 GB). If a disk_size
number is provided with no units, Packer will default to Megabytes.

- `disk_cache` (string) - The cache mode to use for disk. Allowed values include any of
`writethrough`, `writeback`, `none`, `unsafe` or `directsync`. By
Expand Down
0