block: Do OFD lock check at runtime
cover block: Do OFD lock check at runtime
This fixes the image opening failure reported by Andrew Baumann:
Quoted 7 lines
I'm running a recent Linux build of qemu on Windows Subsystem for Linux (WSL) which doesn't appear to implement file locking:
$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 -device virtio-blk-pci,drive=hd0 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to lock byte 100
It appears to be that the binary is built for Linux targets, but the WSL runtime doesn't recognize the ops (-EINVAL).
Convert to runtime check to cope with that.
Fam Zheng (2):
osdep: Add runtime OFD lock detection
file-posix: Do runtime check for ofd lock API
block/file-posix.c | 19 ++++++-------- include/qemu/osdep.h | 1 + util/osdep.c | 72 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 20 deletions(-)
Signature · 1 line
2.13.3
PATCH 1/2 osdep: Add runtime OFD lock detection
Build time check of OFD lock is not sufficient and can cause image open errors when the runtime environment doesn't support it.
Add a helper function to probe it at runtime, additionally. Also provide a qemu_has_ofd_lock() for callers to check the status.
--- include/qemu/osdep.h | 1 + util/osdep.c | 72 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 9 deletions(-)
include/qemu/osdep.h +1 -0
@@ -357,6 +357,7 @@ int qemu_dup(int fd);int qemu_lock_fd (int fd,int64_t start,int64_t len,bool exclusive);int qemu_unlock_fd (int fd,int64_t start,int64_t len);int qemu_lock_fd_test (int fd,int64_t start,int64_t len,bool exclusive); +bool qemu_has_ofd_lock (void );#if defined(__HAIKU__) && defined(__i386__)#define FMT_pid "%ld"
util/osdep.c +63 -9
96 lines not on this page · raw message
Signature · 1 line
2.13.3
Re: [Qemu-devel] [PATCH 1/2] osdep: Add runtime OFD lock detection
Quoted 16 lines · Fam Zheng
Build time check of OFD lock is not sufficient and can cause image open errors when the runtime environment doesn't support it.
Add a helper function to probe it at runtime, additionally. Also provide a
qemu_has_ofd_lock()for callers to check the status.Signed-off-by Fam Zheng <[email protected]>--- include/qemu/osdep.h | 1 + util/osdep.c | 72 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 9 deletions(-)util/osdep.c +3
-0+static void qemu_probe_lock_ops (void ) +{ +if (fcntl_op_setlk ==-1 ) {
util/osdep.c static int qemu_parse_fdset(const char *param) · line 159
Quoted 10 lines
util/osdep.c +10
-0+#ifdef F_OFD_SETLK +int fd; +int ret; +const char *tmpdir; +char *filename; +struct flock fl = { + .l_whence =SEEK_SET , + .l_start =0 , + .l_len =0 , + .l_type = F_WRLCK,
Do we actually need to grab a lock over the entire file,...
util/osdep.c static int qemu_parse_fdset(const char *param) · line 167
Quoted 8 lines
util/osdep.c +8
-0+ }; + + tmpdir =getenv ("TMPDIR" ); +if (!tmpdir) { + tmpdir ="/var/tmp" ; + } + filename =g_strdup_printf ("%s/qemu_lock_probe.XXXXXX" , tmpdir); + fd =mkstemp (filename);
Can we skip the temporary file, and just open("/dev/null", O_RDWR), for slightly less work?
util/osdep.c static int qemu_parse_fdset(const char *param) · line 175
Quoted 8 lines
util/osdep.c +8
-0+if (fd <0 ) { +fprintf (stderr,"Failed to create temporary file '%s': %s\n" , + filename,strerror (errno)); + fcntl_op_setlk =F_SETLK ; + fcntl_op_getlk =F_GETLK ; + goto out; + } + ret =fcntl (fd,F_OFD_SETLK , &fl);
...or should we change this to the much-weaker F_OFD_GETLK? I guess it doesn't matter if we use a temporary file, but does if we are trying to simplify by using /dev/null.
At any rate, the rest of the patch looks sane.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 2/2 file-posix: Do runtime check for ofd lock API
In reply to Fam ZhengIt is reported that on Windows Subsystem for Linux, ofd operations fail with -EINVAL. In other words, QEMU binary built with system headers that exports F_OFD_SETLK doesn't necessarily run in an environment that actually supports it:
$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 \
-device virtio-blk-pci,drive=hd0
qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100
qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100
qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to lock byte 100Let's do a runtime check to cope with that.
--- block/file-posix.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
block/file-posix.c +8 -11
@@ -457,22 +457,19 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,switch (locking) {case ON_OFF_AUTO_ON : s->use_lock = true; -#ifndef F_OFD_SETLK -fprintf (stderr, -"File lock requested but OFD locking syscall is unavailable, " -"falling back to POSIX file locks.\n" -"Due to the implementation, locks can be lost unexpectedly.\n" ); -#endif +if (!qemu_has_ofd_lock ()) { +fprintf (stderr, +"File lock requested but OFD locking syscall is " +"unavailable, falling back to POSIX file locks.\n" +"Due to the implementation, locks can be lost " +"unexpectedly.\n" ); + }break ;case ON_OFF_AUTO_OFF : s->use_lock = false;break ;case ON_OFF_AUTO_AUTO : -#ifdef F_OFD_SETLK - s->use_lock = true; -#else - s->use_lock = false; -#endif + s->use_lock =qemu_has_ofd_lock ();break ;default :abort ();
Signature · 1 line
2.13.3
Re: [Qemu-devel] [PATCH 2/2] file-posix: Do runtime check for ofd lock API
Quoted 5 lines · Fam Zheng
It is reported that on Windows Subsystem for Linux, ofd operations fail with
-EINVAL. In other words, QEMU binary built with system headers that exportsF_OFD_SETLKdoesn't necessarily run in an environment that actually supports it:
Quoted 7 lines
$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 \ -device virtio-blk-pci,drive=hd0 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to lock byte 100Let's do a runtime check to cope with that.
You may want to mention that the same is possible on a system with old kernel but new glibc (ie. this issue is not necessarily specific to WSL).
Quoted 7 lines
Reported-by Andrew Baumann <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/file-posix.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 2/2] file-posix: Do runtime check for ofd lock API
Quoted 11 lines · Eric Blake
Quoted 8 lines · Fam Zheng
It is reported that on Windows Subsystem for Linux, ofd operations fail with
-EINVAL. In other words, QEMU binary built with system headers that exportsF_OFD_SETLKdoesn't necessarily run in an environment that actually supports it:$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 \ -device virtio-blk-pci,drive=hd0 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed tounlock byte 100
Quoted 1 line
qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to
Quoted 9 lines
unlock byte 100
Quoted 1 line
qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to
lock byte 100
Quoted 2 lines
Let's do a runtime check to cope with that.
You may want to mention that the same is possible on a system with old kernel but new glibc (ie. this issue is not necessarily specific to WSL).
I first thought that this combination hitting me as I run KVM in containers which can diverge glibc (in container) a lot from kernel (in host).
My issue turned out to be an apparmor block instead.
But since I clearly see how my case could lead to the mentioned old kernel but new glibc I wanted to ping here to refresh/reconsider this change as well.
Also the reply might be worth as documentation if people search for the error
message and get here that the following apparmor block leads to the same.
apparmor="DENIED" operation="file_lock" namespace="root//lxd-testkvm-artful-from_<var-lib-lxd>" profile="libvirt-f687a9b3-5bca-41bc-b206-6e616720cc5e" name="/var/lib/uvtool/libvirt/images/kvmguest-artful-normal.qcow" pid=7001 comm="qemu-system-x86" requested_mask="k" denied_mask="k" fsuid=0 ouid=0
I'll work on libvirt's virt-aa-helper to generate a rule appropriate for the new behavior.
Re: [Qemu-devel] [PATCH 2/2] file-posix: Do runtime check for ofd lock API
In reply to Fam ZhengQuoted 17 lines
From: Fam Zheng [mailto:[email protected]]
Sent: Friday, 21 July 2017 3:21It is reported that on Windows Subsystem for Linux, ofd operations fail with
-EINVAL. In other words, QEMU binary built with system headers that exportsF_OFD_SETLKdoesn't necessarily run in an environment that actually supports it:$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 \ -device virtio-blk-pci,drive=hd0 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to lock byte 100
Quoted 7 lines
Let's do a runtime check to cope with that.
Reported-by Andrew Baumann <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/file-posix.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
If it helps:
Thanks!
Andrew
Am 21.07.2017 um 12:20 hat Fam Zheng geschrieben:
Quoted 10 lines
This fixes the image opening failure reported by Andrew Baumann:
Quoted 7 lines
I'm running a recent Linux build of qemu on Windows Subsystem for Linux (WSL) which doesn't appear to implement file locking:
$ qemu-system-aarch64 ... -drive file=test.vhdx,if=none,id=hd0 -device virtio-blk-pci,drive=hd0 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to unlock byte 100 qemu-system-aarch64: -drive file=test.vhdx,if=none,id=hd0: Failed to lock byte 100
Quoted 4 lines
It appears to be that the binary is built for Linux targets, but the WSL runtime doesn't recognize the ops (
-EINVAL).Convert to runtime check to cope with that.
Fair enough in this specific case because we still support older Linux kernels and we want to fail gracefully if the binary was built against a newer kernel.
However, I think the real problem here is with the WSL ecosystem if qemu is routinely built against a real Linux while WSL doesn't provide the same functionality. WSL should provide kernel headers that match what it can provide (i.e. either remove the unimplemnted constants or implement them).
So for the future, I'm not sure that we should add workarounds for WSL shortcomings when no real Linux is affected.
This is even more true when the reporter is a Microsoft employee.
Kevin
Quoted 6 lines · Kevin Wolf
Quoted 2 lines
Convert to runtime check to cope with that.
Fair enough in this specific case because we still support older Linux kernels and we want to fail gracefully if the binary was built against a newer kernel.
Or, more likely: if we are built against a newer glibc (that has the constants) but an older kernel (that lacks support for the constants).
Quoted 6 lines
However, I think the real problem here is with the WSL ecosystem if qemu is routinely built against a real Linux while WSL doesn't provide the same functionality. WSL should provide kernel headers that match what it can provide (i.e. either remove the unimplemnted constants or implement them).
In other words, you're arguing that binaries built for WSL should be cross-compiled rather than native compiled (similar to how mingw binaries are built in a Cygwin environment), such that the cross-compiler picks up the correct altered headers for WSL limitations.
I agree - but I have no influence on how likely that is to come about.
Quoted 8 lines
So for the future, I'm not sure that we should add workarounds for WSL shortcomings when no real Linux is affected.
This is even more true when the reporter is a Microsoft employee.
Kevin
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 18 lines · Eric Blake
Quoted 6 lines · Kevin Wolf
Quoted 2 lines
Convert to runtime check to cope with that.
Fair enough in this specific case because we still support older Linux kernels and we want to fail gracefully if the binary was built against a newer kernel.
Or, more likely: if we are built against a newer glibc (that has the constants) but an older kernel (that lacks support for the constants).
Quoted 6 lines
However, I think the real problem here is with the WSL ecosystem if qemu is routinely built against a real Linux while WSL doesn't provide the same functionality. WSL should provide kernel headers that match what it can provide (i.e. either remove the unimplemnted constants or implement them).
Quoted 5 lines
In other words, you're arguing that binaries built for WSL should be cross-compiled rather than native compiled (similar to how mingw binaries are built in a Cygwin environment), such that the cross-compiler picks up the correct altered headers for WSL limitations.
I agree - but I have no influence on how likely that is to come about.
IIUC, the core selling point of WSL was that you can take an existing Ubuntu (or now various other Linux) distro and just run it "as is", without recompiling its specially. So this really is best thought of as the build with new glibc+kernel, but run on old kernel scenario.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|