Last active
April 29, 2017 14:35
-
-
Save n-st/890dc45be2adf6b12c80 to your computer and use it in GitHub Desktop.
Quick'n'dirty script to display the progress and ETA of a running btrfs scrub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# usage: $0 <path being scrubbed> | |
set -e | |
# adapted from my own oneliner script at http://www.commandlinefu.com/commands/view/12621/ | |
format_filesize () { | |
printf "%d" $1 | awk 'function hr(bytes){hum[1024**4]="TiB";hum[1024**3]="GiB";hum[1024**2]="MiB";hum[1024]="kiB";for(x=1024**4;x>=1024;x/=1024){if(bytes>=x){return sprintf("%8.3f %s",bytes/x,hum[x]);}}return sprintf("%4d B",bytes);}{print hr($1) "\t" $2}' | |
} | |
format_time () { | |
printf "%d min %d s" $(($1/60)) $(($1 % 60)) | |
} | |
btrpath="$1" | |
used=$(($(btrfs fi df $btrpath | sed 's/^.\+ single:/:/gi;s/^.\+ DUP:/2*:/gi;s/: .*used=//g;s/GiB$/*1024*1024*1024/g;s/MiB$/*1024*1024/g;s/KiB$/*1024/g;s/$/+/g;$s/\+//g' | xargs))) | |
seconds=$(btrfs scrub status $btrpath | grep -oP '(?<=running for )[[:digit:]]+(?= seconds)') | |
scrubbed=$(($(btrfs scrub status $btrpath | grep -oP '(?<=total bytes scrubbed: )[0-9.GMKiB]+(?= with)' | sed 's/GiB$/*1024*1024*1024/g;s/MiB$/*1024*1024/g;s/KiB$/*1024/g'))) | |
percent=$((100.0*$scrubbed/$used)) | |
totaltime=$(($seconds/($percent/100.0))) | |
eta=$(($totaltime - $seconds)) | |
printf "scrubbed %s of %s (%.2f%%)\n%s remaining (of %s total)\n" "$(format_filesize $scrubbed)" "$(format_filesize $used)" $percent "$(format_time $eta)" "$(format_time $totaltime)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above did not work with my TiB sized btrfs filesystem. I modified the file to work for me, here are my changes if you are interested.