USE AT YOUR OWN RISK – HAVE A BACKUP PRIOR TO RUNNING
Sometimes you dont want COW, because COW is copy on write and causes fragmentation where there are alot of reads and writes all over the place (Torrents, Virtual Disks used by VMWARE & XEN & HYPERV and the like, databases, etc)
My script below will go thru every file in the CURRENT WORKING FOLDER and uncow them (the uncow’ing happens with the python script uncow.py which is not my script but Alex Bennee script – Here is how it uncows: make empty sized file, make new empty file C+attribute meaning COW disabled – because COW can only be enabled and disabled on empty files – then uncow.py copys data from original to this new COW disabled file, then uncow.py deletes original, and renames new COW disabled file to match original – end result: same filename, same data, but nocow). Found script here: https://btrfs.wiki.kernel.org/index.php/FAQ In the section “Can copy-on-write be turned off for data blocks?”
Uncow.py is like a file defragmentor (defrag), and my whole script is like a filesystem/directory defragmentor (defrag) – the difference being that a defragmentor works block by block and does a MUCH better job, but this tool works file by file. With that said my script (and uncow.py) have the disadvantage that there should be enough free space to fit the biggest file in the filesystem (meaning FREESPACE should be bigger than SIZE-OF-BIGGEST-FILE)
Note it would be wise to run uncow.py on files that are the most fragmented (aka have the most extents) – check out my mostfragged script at the bottom of this article to find those files: mostfragged
Actual Script is here: https://raw.githubusercontent.com/stsquad/scripts/master/uncow.py
Requirement for uncow.py (besides having a BTRFS filesystem) is having python:
apt-get update apt-get install python
If you just want the uncow.py script:
wget --no-check-certificate https://raw.githubusercontent.com/stsquad/scripts/master/uncow.py -P /root chmod +x /root/uncow.py
Now to use this uncow.py
# to use uncow.py (use -v for verbosity) /root/uncow.py -v <some file or folder> # or without being verbose: /root/uncow.py <some file or foldeR>
Also note that my copy paste script below will download Alex Bennée uncow.py to /root/ and then run it on every file and folder within the current folder that your in(all the way down recursively). This will not jump across symlinks. This just uses output of find to point files & folders at uncow.
####################### # UNCOW.py COPY PASTE # ####################### # WHAT THIS DOES: This will uncow the current folder and everything within it # HOW THIS HAPPENS IS EXPLAINED HERE (look for yellow highlights): # http://www.infotinks.com/uncow/ # HOW TO USE: Copy and paste below script (script by Alex Bennée) # HOW TO USE - MORE DETAILED: # Step1. Decide which folder you want to uncow # Step2. cd /the-folder-you-want-to-uncow # Step3. Copy this script and paste it and hit enter # REQ: requires programs 'date', 'python', 'wget', 'awk', 'wc', 'echo', and to be inside a btrfs folder # NOTE: Takes a minute to run thru # ADVICE: I hope the folder your in doesnt have any used files (if they do, turn off their usage) # ----------------------------------------------------# (echo "######################################################################################################" echo "Downloading uncow.py & installing to /root" echo "######################################################################################################" wget --no-check-certificate https://raw.githubusercontent.com/stsquad/scripts/master/uncow.py -P /root chmod +x /root/uncow.py echo "######################################################################################################" echo "Running uncow.py unto current folder and everything inside it" echo "######################################################################################################" echo -n "CALCULATING (one sec): "; TOTAL=`find | wc -l`; echo "$TOTAL files&folders to uncow"; # If READYNAS OS6, comment out above line, and uncomment below line (below line will ensure only spend time on uncowing none snapshot folders): # TOTAL=`find -not -iwholename "/*/._share/*/.snapshot/*" -not -iwholename "/*/*/.snapshots/*/snapshot/*" -not -iwholename "/*/*/snapshot/*" -not -iwholename "/*/._snap-*/*" | wc -l`; echo "$TOTAL files&folders to uncow"; N=1; OLDIFS=$IFS; IFS=$'\n'; for i in `find`; do echo "($N/$TOTAL) ############################## $i ###############################[`date +%D-%T`][`date +%s`]"; echo " # /root/uncow.py -v $i"; /root/uncow.py -v $i | awk '{print " " $0;}'; N=$((N+1)); done; IFS=$OLDIFS;)
One thought on “BTRFS – Mass Turning off COW – Uncow.py Copy Paste Script – Recursively Uncow.py everything”