diff --git a/deploy.yml b/deploy.yml index e8ec7aa..59bb43f 100644 --- a/deploy.yml +++ b/deploy.yml @@ -111,7 +111,6 @@ hosts: - pikvm become: true - hosts: pikvm vars: domain: monotremata.xyz diff --git a/roles/cryptoraid/files/bin/btr_balance.sh b/roles/cryptoraid/files/bin/btr_balance.sh new file mode 100644 index 0000000..d0f105a --- /dev/null +++ b/roles/cryptoraid/files/bin/btr_balance.sh @@ -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' diff --git a/roles/cryptoraid/files/bin/btr_defrag.sh b/roles/cryptoraid/files/bin/btr_defrag.sh new file mode 100644 index 0000000..bd1b537 --- /dev/null +++ b/roles/cryptoraid/files/bin/btr_defrag.sh @@ -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 diff --git a/roles/cryptoraid/files/bin/btr_scrub.sh b/roles/cryptoraid/files/bin/btr_scrub.sh new file mode 100644 index 0000000..342a31a --- /dev/null +++ b/roles/cryptoraid/files/bin/btr_scrub.sh @@ -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' diff --git a/roles/cryptoraid/files/bin/fs_trim.sh b/roles/cryptoraid/files/bin/fs_trim.sh new file mode 100644 index 0000000..d95641b --- /dev/null +++ b/roles/cryptoraid/files/bin/fs_trim.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -e + +FS_ROOT="${BTRFS_ROOT:-/mnt/btr_pool}" +fstrim -v "$FS_ROOT" diff --git a/roles/cryptoraid/tasks/alpine.yml b/roles/cryptoraid/tasks/alpine.yml index 7961c79..1687468 100644 --- a/roles/cryptoraid/tasks/alpine.yml +++ b/roles/cryptoraid/tasks/alpine.yml @@ -2,6 +2,7 @@ - name: install packages apk: name: + - btrbk - btrfs-progs - cryptsetup - gnupg diff --git a/roles/cryptoraid/tasks/main.yml b/roles/cryptoraid/tasks/main.yml index 0043a2c..ec11423 100644 --- a/roles/cryptoraid/tasks/main.yml +++ b/roles/cryptoraid/tasks/main.yml @@ -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"