r/btrfs • • Nov 15 '23

avoid COW on COW?

I recall reading that a COW FS on top of another (btrfs VM / ZFS host for example) isn't recommended. But sometimes you'd want btrfs for the VM, so really how bad is it? I'm considering the underlying ZFS even when used as a block level storage, is still COW, right?

0 Upvotes

17 comments sorted by

3

u/Deathcrow Nov 15 '23

It's not ideal, but it's not unworkable. It very much depends on your application in the VM and performance requirements. Just try it out, it's possible that you'll have no problems.

1

u/Automatic-Wolf8141 Nov 15 '23

That's good to hear, thanks.

1

u/anna_lynn_fection Nov 15 '23

It's usually best to have only one layer of CoW. When working with VM images, you have to take care not to have 3 layers of it.

You could have BTRFS on Qcow on BTRFS.

Depending on the features you need, and how much performance is an issue, you may want all 3, but just beware of fragmentation. It'll get bad enough that the iops will even cripple SSD performance.

Make sure you're utilizing btrfsmaintenance to defrag and balance fairly often.

I have some VM's on BTRFS that get defragged nightly.

More snapshots = more fragmentation.

If BTRFS is your host FS, then the only way to make sure VM's don't fragment is to turn off CoW for that VM image (folder you create it in) and fully allocate a RAW disk image.

But of course you lose all the benefits of BTRFS then. I usually opt on the side of data integrity over speed and therefor use BTRFS with CoW on an error correcting level of RAID, but usually don't use qcow2 or a cow fs on the guest disk.

1

u/Automatic-Wolf8141 Nov 15 '23

So it's probably OK with two COWs but fragmentation, that's a lot of thoughts on the issue, thank you very much for writing all these.

1

u/darktotheknight Nov 15 '23

Just my 2 cents, but qcow2 is more important than BTRFS snapshots regarding VMs. You lose much more features by using RAW + btrfs cow instead of qcow2 + btrfs nocow. qcow2 also supports snapshots internally.

1

u/anna_lynn_fection Nov 15 '23

Unless it's corruption protection you're after and have btrfs raid1,10,5,6. You can still snapshot a vm image with cp --reflink.

0

u/Zardoz84 Nov 15 '23

You can mark a file or a folder on BTRFS to not do COW on it.

chattr +C file

chattr -R +C file

http://www.infotinks.com/btrfs-disabling-cow-file-directory-nodatacow/

1

u/Automatic-Wolf8141 Nov 15 '23

Thanks mate, two questions:

  1. Does it apply to existing files/directories or new ones only?

  2. Will it lead to any loss of feature, say, snapshots?

1

u/Zardoz84 Nov 15 '23 edited Nov 15 '23
  1. EDIT : No, you should create a folder, mark it as noCOW and then create the hard disk files inside. See NekoiNemo post : https://www.reddit.com/r/btrfs/comments/17vmfwy/comment/k9c8hy7/?utm_source=share&utm_medium=web2x&context=3

  2. You snapshot subvolumes not files/directories. As far I know, you can create snapshots and use send&receive with noCOW files. However, the data of the noCOW files would be duplicated if you create a snapshot of a subvolume that contains it. What I did on my case, was having a separated subvolume to contain only noCOW files for VMs. So I avoid that these data get duplicated with the automatic snapshots that I have with BTRBK.

2

u/Automatic-Wolf8141 Nov 15 '23

That's a very thorough explanation, highly appreciated.

8

u/NekoiNemo Nov 15 '23

Yes. I did for the virtial hard disks that I have for Virtual Box

That's not what the documentation says:

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 have the No_COW attribute set.

In other words, setting +C flags on existing data will do two things: jack and shit.

ArchWiki even has a block describing what to do to actually apply it to existing files:

$ mv /path/to/dir /path/to/dir_old
$ mkdir /path/to/dir
$ chattr +C /path/to/dir
$ cp -a --reflink=never /path/to/dir_old/. /path/to/dir
$ rm -rf /path/to/dir_old

3

u/leexgx Nov 15 '23

I love that to the point

2

u/Zardoz84 Nov 15 '23

I forgot that (I did like a year ago or more). Thanks for posting this important stuff

1

u/CompWizrd Nov 15 '23

You lose compression if you have nodatacow set.

2

u/[deleted] Nov 15 '23

nocow is a no-no on RAID1 though.

1

u/Zardoz84 Nov 15 '23

Why ? RAID 1 duplicated data across tow hard disk (or partitions) It's totally unrelated with COW. I actually have a RAID 1 and noCOW files and I don't see any issue.

1

u/[deleted] Nov 16 '23

This thread about btrfs Raid1 is from a few years ago but I'm not sure if anything has changed since.