btrfs maintenance scripts

main
Ricard Illa 2022-10-20 15:03:12 +02:00
parent 252ec87fe3
commit 016dd83a52
7 changed files with 82 additions and 1 deletions

View File

@ -111,7 +111,6 @@
hosts:
- pikvm
become: true
hosts: pikvm
vars:
domain: monotremata.xyz

View File

@ -0,0 +1,17 @@
#!/bin/sh
set -e
BTRFS_ROOT="${BTRFS_ROOT:-/mnt/btr_pool}"
btrfs -v balance start --background "$BTRFS_ROOT"
# shellcheck disable=SC2064
trap "btrfs balance cancel $BTRFS_ROOT" INT
printf '=================\n'
while ! btrfs -v balance status "$BTRFS_ROOT"; do
printf '________________\n'
sleep 60
done
printf '=================\n'

View File

@ -0,0 +1,22 @@
#!/bin/sh
set -e
defragment() {
btrfs filesystem defragment -v -r -czstd "$@"
}
list_subvols() {
btrfs subvolume list "$@" | awk '{ print $9 }'
}
is_readonly() {
btrfs subvolume show "$@" | grep -q "readonly"
}
BTRFS_ROOT="${BTRFS_ROOT:-/mnt/btr_pool}"
for subvol in $(list_subvols "$BTRFS_ROOT"); do
subvol_path="$BTRFS_ROOT"/"$subvol"
is_readonly "$subvol_path" || defragment "$subvol_path"
done

View File

@ -0,0 +1,25 @@
#!/bin/sh
set -e
scrub_status() {
btrfs scrub status "$@" |
tee /dev/stderr |
grep "^Status:" |
awk '{ print $2}'
}
BTRFS_ROOT="${BTRFS_ROOT:-/mnt/btr_pool}"
btrfs scrub start "$BTRFS_ROOT"
sleep 10
# shellcheck disable=SC2064
trap "btrfs scrub cancel $BTRFS_ROOT" INT
printf '=================\n'
while [ "$(scrub_status "${BTRFS_ROOT}")" = "running" ]; do
printf '________________\n'
sleep 60
done
printf '=================\n'

View File

@ -0,0 +1,6 @@
#!/bin/sh
set -e
FS_ROOT="${BTRFS_ROOT:-/mnt/btr_pool}"
fstrim -v "$FS_ROOT"

View File

@ -2,6 +2,7 @@
- name: install packages
apk:
name:
- btrbk
- btrfs-progs
- cryptsetup
- gnupg

View File

@ -2,3 +2,14 @@
- name: alpine setup
include_tasks: alpine.yml
when: ansible_distribution == "Alpine"
- name: copy maintainance scripts
copy:
src: "bin/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
mode: "755"
loop:
- "btr_defrag.sh"
- "btr_balance.sh"
- "btr_scrub.sh"
- "fs_trim.sh"