Friday, May 8, 2015

Btrfs disable copy on write (cow) for virtual machine disks

In files like vmware virtual disks (vmdk files) is common to disable copy on write (cow) on btrfs filesystems to avoid system slow response due to heavy I/O on disks.

You can verify if a file have disabled the copy on write using lsattr command. If you see a C on the file attributes the copy on write is disabled.

chris@r2d2:~/vmware/Windows 10 x64$ lsattr
---------------- ./Windows 10 x64.vmdk
---------------- ./Windows 10 x64.vmsd
---------------- ./Windows 10 x64.vmxf
---------------- ./vmware.log
---------------- ./Windows 10 x64.nvram
---------------- ./caches
---------------- ./Windows 10 x64.vmx
---------------- ./Windows 10 x64.vmx.lck

You can disable the copy on write for a file using chattr +C filename of for a directory you can use chattr +C -R dirname

chris@r2d2:~/vmware$ chattr +C -R Windows\ 10\ x64

As the man page of chattr says:

A file with the 'C' attribute set will not be subject to copy-on-write updates. This flag is only supported on file systems which perform copy-on-write. (Note: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will the No_COW attribute.)

The trick to disable the copy on write to an existing file with existing data on it is to move it out of the directory, then copy the file inside the directory.

chris@r2d2:~/vmware/Windows 10 x64$ mv Windows\ 10\ x64.vmdk ~/
chris@r2d2:~/vmware/Windows 10 x64$ cp ~/Windows\ 10\ x64.vmdk .
chris@r2d2:~/vmware/Windows 10 x64$ rm ~/Windows\ 10\ x64.vmdk
chris@r2d2:~/vmware/Windows 10 x64$ lsattr
---------------C ./Windows 10 x64.vmsd
---------------- ./Windows 10 x64.vmxf
---------------- ./vmware.log
---------------- ./Windows 10 x64.nvram
---------------C ./caches
---------------- ./Windows 10 x64.vmx
---------------C ./Windows 10 x64.vmx.lck
---------------- ./Windows 10 x64.vmdk

The C attribute shows that copy on write is now disabled for vmdk file.

No comments:

Post a Comment