From 91f971d92f5d06bec28e20925098cbd9906b4ab6 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Mon, 15 Sep 2014 11:33:28 +0300 Subject: [PATCH] qemu: Use "unsafe" cache if direct IO is not supported ZFS on Linux, for example, does not support direct I/O. Probe for DIO support before starting a new QEMU instance and fall-back to "unsafe" caching if it's not supported. Suggested by Tomek. Fixes #118. Signed-off-by: Pekka Enberg --- hypervisor/qemu/qemu.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hypervisor/qemu/qemu.go b/hypervisor/qemu/qemu.go index cf2f550..e449449 100644 --- a/hypervisor/qemu/qemu.go +++ b/hypervisor/qemu/qemu.go @@ -21,6 +21,7 @@ import ( "regexp" "runtime" "strconv" + "syscall" ) type VMConfig struct { @@ -226,13 +227,26 @@ func ParseVersion(text string) (*Version, error) { }, nil } +func isDirectIOSupported(path string) bool { + f, err := os.OpenFile(path, syscall.O_DIRECT, 0) + defer f.Close() + return err == nil +} + +func (c *VMConfig) vmDriveCache() string { + if isDirectIOSupported(c.Image) { + return "none" + } + return "unsafe" +} + func (c *VMConfig) vmArguments(version *Version) ([]string, error) { args := make([]string, 0) args = append(args, "-display", "none") args = append(args, "-m", strconv.FormatInt(c.Memory, 10)) args = append(args, "-smp", strconv.Itoa(c.Cpus)) args = append(args, "-device", "virtio-blk-pci,id=blk0,bootindex=0,drive=hd0") - args = append(args, "-drive", "file=" + c.Image + ",if=none,id=hd0,aio=native,cache=none") + args = append(args, "-drive", "file=" + c.Image + ",if=none,id=hd0,aio=native,cache=" + c.vmDriveCache()) if version.Major >= 1 && version.Minor >= 3 { args = append(args, "-device", "virtio-rng-pci") }