Skip to content

Commit

Permalink
Mix up some things and format
Browse files Browse the repository at this point in the history
  • Loading branch information
Heavybullets8 committed Oct 30, 2024
1 parent 7986c94 commit c4dcd8e
Showing 1 changed file with 80 additions and 62 deletions.
142 changes: 80 additions & 62 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,94 +1,112 @@
#!/bin/bash

# entrypoint.sh
set -e

if [ -z "$ZFS_POOL" ]; then
echo "Error: No ZFS_POOL specified. Exiting."
exit 1
echo "Error: No ZFS_POOL specified. Exiting."
exit 1
fi

if [ -z "$ACTION" ]; then
echo "Error: No ACTION specified. Exiting."
exit 1
echo "Error: No ACTION specified. Exiting."
exit 1
fi

scrub_pool() {
echo "===================================================="
echo "Starting scrub on pool: $ZFS_POOL"
echo "===================================================="
echo "===================================================="
echo "Starting scrub on pool: $ZFS_POOL"
echo "===================================================="

if ! zpool scrub "$ZFS_POOL"; then
echo "Error: Failed to start scrub on pool: $ZFS_POOL"
exit 1
fi

echo "Scrub started on pool: $ZFS_POOL"
echo "Monitoring scrub progress..."

while true; do
sleep 5
status=$(zpool status "$ZFS_POOL")
scrub_line=$(echo "$status" | grep "scan:")
echo "$scrub_line"
if echo "$scrub_line" | grep -q "scrub repaired"; then
echo "===================================================="
echo "Scrub completed on pool: $ZFS_POOL"
echo "===================================================="
break
elif echo "$scrub_line" | grep -q "scrub in progress"; then
continue
elif echo "$scrub_line" | grep -q "resilver in progress"; then
echo "Resilver in progress on pool: $ZFS_POOL"
continue
else
echo "Error: Unexpected scrub status."
exit 1
if ! zpool scrub "$ZFS_POOL"; then
echo "Error: Failed to start scrub on pool: $ZFS_POOL"
exit 1
fi
done

echo "Scrub started on pool: $ZFS_POOL"
echo "Monitoring scrub progress..."

while true; do
sleep 5
status=$(zpool status "$ZFS_POOL")
scrub_line=$(echo "$status" | grep "scan:")
echo "$scrub_line"
if echo "$scrub_line" | grep -q "scrub repaired"; then
echo "===================================================="
echo "Scrub completed on pool: $ZFS_POOL"
echo "===================================================="
break
elif echo "$scrub_line" | grep -q "scrub in progress"; then
continue
elif echo "$scrub_line" | grep -q "resilver in progress"; then
echo "Resilver in progress on pool: $ZFS_POOL"
continue
else
echo "Error: Unexpected scrub status."
exit 1
fi
done
}

cleanup_snapshots() {
echo "===================================================="
echo "Starting cleanup on pool: $ZFS_POOL"
echo "===================================================="

mapfile -t snapshots < <(zfs list -H -o name -t snapshot -s clones -r "$ZFS_POOL")

for snapshot in "${snapshots[@]}"; do
mapfile -t clones < <(zfs get -o value -H clones "$snapshot")

if [ "${#clones[@]}" -gt 0 ]; then
echo "Processing snapshot with dependent clones: $snapshot"
for clone in "${clones[@]}"; do
# Promote, but dont delete since openebs should be handling that
echo "Promoting clone: $clone"
if ! zfs promote "$clone"; then
echo "Error: Failed to promote clone: $clone"
continue
echo "===================================================="
echo "Starting cleanup on pool: $ZFS_POOL"
echo "===================================================="

mapfile -t snapshot_clone_pairs < <(zfs list -H -t snapshot -o name,clones -r "$ZFS_POOL")

for line in "${snapshot_clone_pairs[@]}"; do
snapshot=$(echo "$line" | awk -F'\t' '{print $1}')
clone=$(echo "$line" | awk -F'\t' '{print $2}')

if [ "$clone" != "-" ]; then
echo "Processing snapshot: $snapshot with clone: $clone"

echo "Promoting clone: $clone"
if ! zfs promote "$clone"; then
echo "Error: Failed to promote clone: $clone"
continue
fi

echo "Destroying snapshot: $snapshot"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to destroy snapshot: $snapshot"
continue
fi

echo "Destroying clone dataset: $clone"
if ! zfs destroy "$clone"; then
echo "Error: Failed to destroy clone dataset: $clone"
continue
fi
else
echo "Destroying snapshot: $snapshot (no clones)"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to destroy snapshot: $snapshot"
continue
fi
fi
done
fi
done
echo
done

echo "===================================================="
echo "Cleanup completed on pool: $ZFS_POOL"
echo "===================================================="
echo "===================================================="
echo "Cleanup completed on pool: $ZFS_POOL"
echo "===================================================="
}

case "$ACTION" in
scrub)
scrub)
scrub_pool
;;
cleanup)
cleanup)
cleanup_snapshots
;;
all)
all)
scrub_pool
echo
echo
cleanup_snapshots
;;
*)
*)
echo "Error: Invalid ACTION specified. Use 'scrub', 'cleanup', or 'all'."
exit 1
;;
Expand Down

0 comments on commit c4dcd8e

Please sign in to comment.