diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 3fc3ff678..96f1609fc 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -154,8 +154,20 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { if runtime.GOOS == "windows" { b.config.Accelerator = "tcg" } else { - b.config.Accelerator = "kvm" + // /dev/kvm is a kernel module that may be loaded if kvm is + // installed and the host supports VT-x extensions. To make sure + // this will actually work we need to os.Open() it. If os.Open fails + // the kernel module was not installed or loaded correctly. + if fp, err := os.Open("/dev/kvm"); err != nil { + b.config.Accelerator = "tcg" + } else { + fp.Close() + b.config.Accelerator = "kvm" + } } + log.Printf("use detected accelerator: %s", b.config.Accelerator) + } else { + log.Printf("use specified accelerator: %s", b.config.Accelerator) } if b.config.MachineType == "" { diff --git a/website/source/docs/builders/qemu.html.md b/website/source/docs/builders/qemu.html.md index 44b34d264..015845551 100644 --- a/website/source/docs/builders/qemu.html.md +++ b/website/source/docs/builders/qemu.html.md @@ -108,9 +108,10 @@ builder. ### Optional: - `accelerator` (string) - The accelerator type to use when running the VM. - This may have a value of either "none", "kvm", "tcg", or "xen" and you must - have that support in on the machine on which you run the builder. By default - "kvm" is used. + This may be `none`, `kvm`, `tcg`, or `xen`. The appropriate software must + already been installed on your build machine to use the accelerator you + specified. When no accelerator is specified, Packer will try to use `kvm` + if it is available but will default to `tcg` otherwise. - `boot_command` (array of strings) - This is an array of commands to type when the virtual machine is first booted. The goal of these commands should