(Open)ZFS tips and tricks

(Open)ZFS tips and tricks

I am still very new to zfs, but I wanted to compliment my Cool shell tricks post with zfs stuff so I don't lose commands that I like. I am not going to give more detail than a simple description, but you should always be able to post the commands in https://explainshell.com/ as well as the openzfs man page to get a better understanding of how things are getting executed.

NOTE: proceed with caution, this is your data so I don't claim it won't do damage to your data or you won't have specific snapshots if you run these commands and don't know what they are doing...trust me I had this happen too many times to me to not put a notice...

ZFS rollback script

I made a post here about this script, but here is another example from my zsh history that I used for it.

curl -fsSL 'git.io/JtVB4' | sudo bash -s 'rpool' 'autozsys_mf440q' '<username>_mgspym' |& tee ~elbruto/"zfs-rollback_$(date -I).log"

Snapshot space enumeration

Identify all the snapshots for a given pool that are taking up gigabytes of space, so you can remove them.

# old version
POOL='rpool' ; for i in $(zfs list -Ht snapshot -o name -r "${POOL}" | cut -d '@' -f 2 | sort -u ) ; do zfs destroy -vRn "${POOL}@${i}" ; done | grep -B 1 reclaim | grep -B1 'G$'

# streamlined version
POOL='rpool' ; zfs list -Ht snapshot -o name -r "${POOL}" | cut -d '@' -f 2 | sort -u | xargs -I {} zfs destroy -vRn "${POOL}@{}" | grep -B 1 reclaim | grep -B1 'G$'

Cleanup zfs-auto-snap

delete all the zfs-auto-snap snapshots.

# old version
pool_name='rpool' ; touch ~/"deletion-${pool_name}.log" && rm ~/"deletion-${pool_name}.log" && for i in $(zfs list -t snapshot | grep "${pool_name}" | grep 'zfs-auto-snap' | awk '{print $1}') ; do sudo zfs destroy -Rv "$i" | tee -a ~/"deletion-${pool_name}.log" ; done

# streamlined version
snapshot_grep='autozsys'; pool_name='rpool'; rm -f ~/"deletion-${pool_name}_${snapshot_grep}.log" && zfs list -t snapshot -Ho name | grep "${pool_name}" | grep "${snapshot_grep}" | xargs -t -n 1 sudo zfs destroy -Rv  |& tee -a ~/"deletion-${pool_name}-${snapshot_grep}.log"

exploring differences

unset counterz ; for i in "${zfs_snapshots[@]}" ; do if [[ -z "${counterz}" ]] ; then counterz=0 ; else : $((counterz++)) ; fi ; if [[ "${counterz}" -ne 0 ]] && [[ $(( counterz % 2 )) -eq 0 ]] ; then echo "${i} ${newer_snap}" && sudo zfs diff rpool/ROOT/ubuntu_n0ce53@{$i,${newer_snap}} | less ; else newer_snap="${i}" ; fi ; done