Skip to content

Commit

Permalink
Add Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Heavybullets8 committed Oct 30, 2024
1 parent bb8547e commit f48ec1c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.20

RUN apk add --no-cache zfs
RUN apk add --no-cache zfs bash

COPY entrypoint.sh /entrypoint.sh

Expand Down
96 changes: 90 additions & 6 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,101 @@
#!/bin/sh
#!/bin/bash

# entrypoint.sh
set -e

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

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

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

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

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

echo "Scrub completed on pool: $ZFS_POOL"
while true; do
sleep 15
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 "===================================================="

# Find snapshots with dependent clones
snapshots=$(zfs list -H -o name -t snapshot -r "$ZFS_POOL")
for snapshot in $snapshots; do
# Check if snapshot has dependent clones
clones=$(zfs get -H -o value clones "$snapshot")
if [ "$clones" != "-" ]; then
echo "Processing snapshot with dependent clones: $snapshot"
# Get list of dependent clones
clone_list=$(zfs get -H -o value clones "$snapshot" | tr ',' ' ')
for clone in $clone_list; do
echo "Promoting clone: $clone"
if ! zfs promote "$clone"; then
echo "Error: Failed to promote clone: $clone"
continue
fi
done
echo "Destroying snapshot: $snapshot"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to destroy snapshot: $snapshot"
continue
fi
fi
done

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

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

0 comments on commit f48ec1c

Please sign in to comment.