Using cache=writeback safely on qemu 1.4.0 and later
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hello,
I am running several qemu-kvm VM servers on Ubuntu 12.04 with qemu-kvm 1.4.0. Most of the guests are also running Ubuntu 12.04. I am using qcow2 disk images with the virtio driver in almost all cases, and am storing the disks on two types of storage devices:
* a shared NFS server mounted on each host
* local ext4 filesystems on each host (on top of an md RAID1 of HDDs)
It seems like the consensus for NFS is to use cache=none in order to support live migration between hosts.
For local ext4 filesystems, I see from this message and elsewhere that cache=writeback is now the default disk cache mode, and is safe on ext4:
http://lists.gnu.org/archive/html/qemu-devel/2012-02/msg02689.html
All of the hosts and guests are using ext4 (with the default data=ordered,barrier). Thus it seems probable that the kernel (3.2.x or newer) should support flushing correctly? Is there a way to check in the guest is utilizing WCE (or any other required components) correctly?
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks and required a fairly long fsck to fix. Afterwards, data that had been written to the disks several hours before the crash was corrupted, which makes me think that it was never fsync()-ed to the non-volatile storage.
Is it safe in this setup to use cache=writeback? Or, should I use cache=writethrough instead?
Thanks,
Andrew Martin
Quoted 12 lines · Andrew Martin
Hello,
I am running several qemu-kvm VM servers on Ubuntu 12.04 with qemu-kvm 1.4.0. Most of the guests are also running Ubuntu 12.04. I am using qcow2 disk images with the virtio driver in almost all cases, and am storing the disks on two types of storage devices:
* a shared NFS server mounted on each host
* local ext4 filesystems on each host (on top of an md RAID1 of HDDs)It seems like the consensus for NFS is to use cache=none in order to support live migration between hosts.
Quoted 8 lines
For local ext4 filesystems, I see from this message and elsewhere that cache=writeback is now the default disk cache mode, and is safe on ext4:
http://lists.gnu.org/archive/html/qemu-devel/2012-02/msg02689.htmlAll of the hosts and guests are using ext4 (with the default data=ordered,barrier). Thus it seems probable that the kernel (3.2.x or newer) should support flushing correctly? Is there a way to check in the guest is utilizing WCE (or any other required components) correctly?
If you strace -f the QEMU process on the host, you will see fdatasync(2) system calls when the guest flushes the disk.
You can find the file descriptor number by checking ls -l /proc/$PID_OF_QEMU/fd and looking for the disk image file.
Quoted 5 lines
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks and required a fairly long fsck to fix. Afterwards, data that had been written to the disks several hours before the crash was corrupted, which makes me think that it was never fsync()-ed to the non-volatile storage.
What exactly was the "corruption" you encountered? Which application, error message, etc.
Quoted 2 lines
Is it safe in this setup to use cache=writeback? Or, should I use cache=writethrough instead?
Ubuntu 12.04 is recent and sends write cache flushes.
Are you sure the file system and/or application workload are flushing the disk cache? Please check the mount options and application-specific configuration.
Stefan
----- Original Message -----
Quoted 11 lines
From: "Stefan Hajnoczi" <[email protected]>
To: "Andrew Martin" <[email protected]>
Cc: [email protected]
Sent: Tuesday, August 19, 2014 9:59:25 AM
Subject: Re: [Qemu-devel] Using cache=writeback safely on qemu 1.4.0 and laterIf you strace -f the QEMU process on the host, you will see fdatasync(2) system calls when the guest flushes the disk.
You can find the file descriptor number by checking ls -l
/proc/$PID_OF_QEMU/fdand looking for the disk image file.
When the disk is set to cache=writethrough on one of the same VMs, I see frequent fdatasync(2) calls (every few seconds). However, when I change the disk over to cache=writeback, since boot I have not yet seen a single fdatasync(2) call, even after writing data 2x the amount of RAM:
# time strace -ft -p4113 2>&1 | grep fdatasync ^C
real 15m39.245s user 0m7.940s sys 0m18.280s
Note that the disk is defined as follows:
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback'/>
<source file='/var/lib/libvirt/images/vm.img'/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/></disk>
Quoted 11 lines
Quoted 8 lines
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks and
required a fairly long fsck to fix. Afterwards, data that had been written to
the disks several hours before the crash was corrupted, which makes me think
that it was never fsync()-ed to the non-volatile storage.What exactly was the "corruption" you encountered? Which application, error message, etc.
Two of the servers are web servers with apache2. In one case, a python daemon copies JPGs onto the server - the last 100 copied onto the server were corrupted. In another case, some files had been uploaded several days prior to the www-root, but after the hard reset said files were no longer present in the filesystem.
Quoted 8 lines
Quoted 2 lines
Is it safe in this setup to use cache=writeback? Or, should I use cache=writethrough instead?
Ubuntu 12.04 is recent and sends write cache flushes.
Are you sure the file system and/or application workload are flushing the disk cache? Please check the mount options and application-specific configuration.
The mount options for the ext4 filesystem in the VM in both cases are:
rw,relatime,errors=remount-ro,data=ordered
Similarly, the host's ext4 filesystem holding the images is mounted with:
rw,relatime,data=ordered
I did not see any errors in the kernel log in the guest, probably because the root filesystem was read-only until the fsck had completed.
Thanks,
Andrew
Quoted 10 lines · Andrew Martin
----- Original Message -----
Quoted 9 lines
From: "Stefan Hajnoczi" <[email protected]>
To: "Andrew Martin" <[email protected]>
Cc: [email protected]
Sent: Tuesday, August 19, 2014 9:59:25 AM
Subject: Re: [Qemu-devel] Using cache=writeback safely on qemu 1.4.0 and laterIf you strace -f the QEMU process on the host, you will see fdatasync(2) system calls when the guest flushes the disk.
Quoted 7 lines
Quoted 2 lines
You can find the file descriptor number by checking ls -l
/proc/$PID_OF_QEMU/fdand looking for the disk image file.When the disk is set to cache=writethrough on one of the same VMs, I see frequent fdatasync(2) calls (every few seconds). However, when I change the disk over to cache=writeback, since boot I have not yet seen a single fdatasync(2) call, even after writing data 2x the amount of RAM:
There is a misconception here. Writing data to disk does not make it persistent across power failure. The behavior you observed is actually expected.
Flushing the disk cache is a deliberate operation that applications must perform to ensure data is safe on disk.
You can use sync(1) to manually flush file system buffers for testing. On the host you should see an fdatasync(2).
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks and
required a fairly long fsck to fix. Afterwards, data that had been written to
the disks several hours before the crash was corrupted, which makes me think
that it was never fsync()-ed to the non-volatile storage.
Quoted 7 lines
Quoted 2 lines
What exactly was the "corruption" you encountered? Which application, error message, etc.
Two of the servers are web servers with apache2. In one case, a python daemon copies JPGs onto the server - the last 100 copied onto the server were corrupted. In another case, some files had been uploaded several days prior to the www-root, but after the hard reset said files were no longer present in the filesystem.
Did the Python daemon fsync the files and directories it modified/created?
Did you sync(1) after copying files to www-root?
Also, you didn't explain what "corrupted" means. Where the jpg files missing, were they zero bytes in size, were they filled with junk, etc?
Quoted 9 lines
Quoted 8 lines
Quoted 2 lines
Is it safe in this setup to use cache=writeback? Or, should I use cache=writethrough instead?
Ubuntu 12.04 is recent and sends write cache flushes.
Are you sure the file system and/or application workload are flushing the disk cache? Please check the mount options and application-specific configuration.
Quoted 8 lines
The mount options for the ext4 filesystem in the VM in both cases are:
rw,relatime,errors=remount-ro,data=orderedSimilarly, the host's ext4 filesystem holding the images is mounted with:
rw,relatime,data=orderedI did not see any errors in the kernel log in the guest, probably because the root filesystem was read-only until the fsck had completed.
Good, that is a sane setup. I guess the behavior you saw would also happen on a physical machine, it is necessary to fsync the file or sync the file system to guarantee that data is safely on disk.
Stefan
----- Original Message -----
Quoted 18 lines
From: "Stefan Hajnoczi" <[email protected]>
To: "Andrew Martin" <[email protected]>
Cc: "qemu-devel" <[email protected]>
Sent: Thursday, August 21, 2014 7:59:50 AM
Subject: Re: [Qemu-devel] Using cache=writeback safely on qemu 1.4.0 and laterQuoted 7 lines
When the disk is set to cache=writethrough on one of the same VMs, I see frequent
fdatasync(2) calls (every few seconds). However, when I change the disk over to
cache=writeback, since boot I have not yet seen a single fdatasync(2) call, even
after writing data 2x the amount of RAM:There is a misconception here. Writing data to disk does not make it persistent across power failure. The behavior you observed is actually expected.
Quoted 6 lines
Flushing the disk cache is a deliberate operation that applications must perform to ensure data is safe on disk.
You can use sync(1) to manually flush file system buffers for testing. On the host you should see an fdatasync(2).
I booted a VM into both writeback and none, and in both cases wrote some data to the disk and then issued sync(1) as root while stracing the QEMU process on the host. In both cases, I did not see any fdatasync, fsync, or sync syscalls passed.
Quoted 23 lines
Quoted 14 lines
Quoted 11 lines
Quoted 10 lines
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks
and
required a fairly long fsck to fix. Afterwards, data that had been written
to
the disks several hours before the crash was corrupted, which makes me think
that it was never fsync()-ed to the non-volatile storage.Quoted 2 lines
What exactly was the "corruption" you encountered? Which application, error message, etc.
Quoted 8 lines
Two of the servers are web servers with apache2. In one case, a python daemon
copies JPGs onto the server - the last 100 copied onto the server were corrupted.
In another case, some files had been uploaded several days prior to the www-root,
but after the hard reset said files were no longer present in the filesystem.
Quoted 8 lines
Did the Python daemon fsync the files and directories it modified/created?
Did you sync(1) after copying files to www-root?
Also, you didn't explain what "corrupted" means. Where the jpg files missing, were they zero bytes in size, were they filled with junk, etc?
The jpgs appeared to be a normal size, but were filled with junk. The files uploaded by apache2 were missing from the filesystem.
Even if the python daemon or apache2 did not fsync the modified files, isn't there some action that the OS takes periodically to flush dirty pages to disk? This seems to be implied in the SuSE documentation:
https://www.suse.com/documentation/sles11/book_kvm/data/sect1_1_chapter_book_kvm.html
"the normal page cache management will handle commitment to the storage device."
In the case of the files uploaded by apache2, they were added to the server days before the power outage, so it seems like there would have been ample time for those changes to have been flushed.
Thanks!
Andrew
Il 25/08/2014 20:13, Andrew Martin ha scritto:
Quoted 5 lines
Even if the python daemon or apache2 did not fsync the modified files, isn't there some action that the OS takes periodically to flush dirty pages to disk? This seems to be implied in the SuSE documentation:
https://www.suse.com/documentation/sles11/book_kvm/data/sect1_1_chapter_book_kvm.html
"the normal page cache management will handle commitment to the storage device."
... using fsync :)
Quoted 3 lines
In the case of the files uploaded by apache2, they were added to the server days before the power outage, so it seems like there would have been ample time for those changes to have been flushed.
Remember that cache=writeback has a huge cache, whose size is several gigabytes.
At some point there was a QEMU bug that caused the guest not to do fsyncs. It was fixed by commit ef5bc96268.
Paolo
----- Original Message -----
Quoted 12 lines
From: "Paolo Bonzini" <[email protected]>
To: "Andrew Martin" <[email protected]>, "Stefan Hajnoczi" <[email protected]>
Cc: "qemu-devel" <[email protected]>
Sent: Tuesday, August 26, 2014 2:03:18 AM
Subject: Re: Using cache=writeback safely on qemu 1.4.0 and laterQuoted 5 lines
In the case of the files uploaded by apache2, they were added to the server days
before the power outage, so it seems like there would have been ample time for
those changes to have been flushed.
Quoted 5 lines
Remember that cache=writeback has a huge cache, whose size is several gigabytes.
At some point there was a QEMU bug that caused the guest not to do fsyncs. It was fixed by commit
ef5bc96268.
Paolo,
Thanks for the info about this bug - I am using QEMU 1.4.0 (released on 02/15/13) and ef5bc96268 was added on 09/20/13, so the bug had not been fixed in the version of QEMU I am using. I have had trouble reproducing it on-demand - was there specific behavior you used to reproduce it, or just the lack of fsyncs or fdatasyncs when stracing the QEMU process when using cache=writeback?
Thanks,
Andrew
Il 27/08/2014 16:23, Andrew Martin ha scritto:
Quoted 5 lines
Thanks for the info about this bug - I am using QEMU 1.4.0 (released on 02/15/13) and
ef5bc96268was added on 09/20/13, so the bug had not been fixed in the version of QEMU I am using. I have had trouble reproducing it on-demand - was there specific behavior you used to reproduce it, or just the lack of fsyncs or fdatasyncs when stracing the QEMU process when using cache=writeback?
Just the lack of fdatasyncs.
Paolo
----- Original Message -----
Quoted 17 lines
From: "Paolo Bonzini" <[email protected]>
To: "Andrew Martin" <[email protected]>
Cc: "Stefan Hajnoczi" <[email protected]>, "qemu-devel" <[email protected]>
Sent: Wednesday, August 27, 2014 9:34:27 AM
Subject: Re: Using cache=writeback safely on qemu 1.4.0 and laterIl 27/08/2014 16:23, Andrew Martin ha scritto:
Quoted 9 lines
Thanks for the info about this bug - I am using QEMU 1.4.0 (released on 02/15/13)
andef5bc96268was added on 09/20/13, so the bug had not been fixed in the version
of QEMU I am using. I have had trouble reproducing it on-demand - was there specific
behavior you used to reproduce it, or just the lack of fsyncs or fdatasyncs when
stracing the QEMU process when using cache=writeback?
Quoted 2 lines
Just the lack of fdatasyncs.
Paolo,
I also did not see any fdatasyncs when using cache=none - would that also be explained by this bug?
Thanks,
Andrew
Quoted 4 lines
Quoted 1 line
Just the lack of fdatasyncs.
I also did not see any fdatasyncs when using cache=none - would that also be explained by this bug?
Yes, cache=none and cache=writeback are the same as far as the _disk_ cache is concerned, and you need fdatasyncs to flush the disk cache too.
Paolo
Quoted 41 lines · Andrew Martin
Quoted 23 lines
Quoted 14 lines
Quoted 11 lines
Quoted 10 lines
I recently experienced UPS failure on several hosts which caused a hard shutdown. After restarting, 3 of the guests had corruption on their disks
and
required a fairly long fsck to fix. Afterwards, data that had been written
to
the disks several hours before the crash was corrupted, which makes me think
that it was never fsync()-ed to the non-volatile storage.Quoted 2 lines
What exactly was the "corruption" you encountered? Which application, error message, etc.
Quoted 8 lines
Two of the servers are web servers with apache2. In one case, a python daemon
copies JPGs onto the server - the last 100 copied onto the server were corrupted.
In another case, some files had been uploaded several days prior to the www-root,
but after the hard reset said files were no longer present in the filesystem.Quoted 8 lines
Did the Python daemon fsync the files and directories it modified/created?
Did you sync(1) after copying files to www-root?
Also, you didn't explain what "corrupted" means. Where the jpg files missing, were they zero bytes in size, were they filled with junk, etc?
The jpgs appeared to be a normal size, but were filled with junk. The files uploaded by apache2 were missing from the filesystem.
Even if the python daemon or apache2 did not fsync the modified files, isn't there some action that the OS takes periodically to flush dirty pages to disk? This seems to be implied in the SuSE documentation:
https://www.suse.com/documentation/sles11/book_kvm/data/sect1_1_chapter_book_kvm.html
"the normal page cache management will handle commitment to the storage device."
Quoted 3 lines
In the case of the files uploaded by apache2, they were added to the server days before the power outage, so it seems like there would have been ample time for those changes to have been flushed.
In the general case of copying/creating some files and hoping that they will be persistent, it usually works. If you want to be 100% sure you still need to flush the cache explicitly.
It doesn't work when updates are made to data on disk and the ordering matters (e.g. wrong ordering could corrupt data or cause it to be lost). In that case relying on the kernel to flush dirty buffers periodically is not a feasible approach because you don't know when the will happen and therefore have no control over ordering.
Stefan