Merge pull request #9 from cnsta/cleanup
modifying cleanup script to comply with hashes in the beginning of fi…
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
# "nix build .#packages.x86_64-linux.cleanup-boot".
|
# "nix build .#packages.x86_64-linux.cleanup-boot".
|
||||||
|
|
||||||
# Number of generations to keep
|
# Number of generations to keep
|
||||||
KEEP_GENERATIONS=5
|
KEEP_GENERATIONS=4
|
||||||
|
|
||||||
# Log file for cleanup actions
|
# Log file for cleanup actions
|
||||||
LOG_FILE="/var/log/cleanup-boot.log"
|
LOG_FILE="/var/log/cleanup-boot.log"
|
||||||
@@ -15,12 +15,12 @@ DRY_RUN=false
|
|||||||
|
|
||||||
# Check for dry run argument
|
# Check for dry run argument
|
||||||
if [ "$1" = "--dry-run" ]; then
|
if [ "$1" = "--dry-run" ]; then
|
||||||
DRY_RUN=true
|
DRY_RUN=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Function to log messages
|
# Function to log messages
|
||||||
log() {
|
log() {
|
||||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Exit on any error
|
# Exit on any error
|
||||||
@@ -29,10 +29,10 @@ set -e
|
|||||||
log "Starting cleanup script. Keeping the latest $KEEP_GENERATIONS generations."
|
log "Starting cleanup script. Keeping the latest $KEEP_GENERATIONS generations."
|
||||||
|
|
||||||
# List the initrd files in /boot/EFI/nixos sorted by modification time (oldest first)
|
# List the initrd files in /boot/EFI/nixos sorted by modification time (oldest first)
|
||||||
mapfile -t initrd_files < <(find /boot/EFI/nixos -type f -name 'initrd-*.efi' -printf '%T@ %p\n' | sort -n)
|
mapfile -t initrd_files < <(find /boot/EFI/nixos -type f -name '*initrd*.efi' -printf '%T@ %p\n' | sort -n)
|
||||||
|
|
||||||
# List the kernel files in /boot/EFI/nixos sorted by modification time (oldest first)
|
# List the kernel files in /boot/EFI/nixos sorted by modification time (oldest first)
|
||||||
mapfile -t kernel_files < <(find /boot/EFI/nixos -type f -name 'kernel-*.efi' -printf '%T@ %p\n' | sort -n)
|
mapfile -t kernel_files < <(find /boot/EFI/nixos -type f -name '*kernel*.efi' -printf '%T@ %p\n' | sort -n)
|
||||||
|
|
||||||
# Count the number of initrd and kernel files
|
# Count the number of initrd and kernel files
|
||||||
initrd_count=${#initrd_files[@]}
|
initrd_count=${#initrd_files[@]}
|
||||||
@@ -46,44 +46,44 @@ delete_kernel_files=()
|
|||||||
|
|
||||||
# If there are fewer than KEEP_GENERATIONS initrd files, don't delete any
|
# If there are fewer than KEEP_GENERATIONS initrd files, don't delete any
|
||||||
if [ "$initrd_count" -le "$KEEP_GENERATIONS" ]; then
|
if [ "$initrd_count" -le "$KEEP_GENERATIONS" ]; then
|
||||||
log "Fewer than $KEEP_GENERATIONS initrd files found. No initrd files will be deleted."
|
log "Fewer than $KEEP_GENERATIONS initrd files found. No initrd files will be deleted."
|
||||||
else
|
else
|
||||||
# Get the initrd files to delete
|
# Get the initrd files to delete
|
||||||
delete_initrd_files=("${initrd_files[@]:0:initrd_count-KEEP_GENERATIONS}")
|
delete_initrd_files=("${initrd_files[@]:0:initrd_count-KEEP_GENERATIONS}")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If there are fewer than KEEP_GENERATIONS kernel files, don't delete any
|
# If there are fewer than KEEP_GENERATIONS kernel files, don't delete any
|
||||||
if [ "$kernel_count" -le "$KEEP_GENERATIONS" ]; then
|
if [ "$kernel_count" -le "$KEEP_GENERATIONS" ]; then
|
||||||
log "Fewer than $KEEP_GENERATIONS kernel files found. No kernel files will be deleted."
|
log "Fewer than $KEEP_GENERATIONS kernel files found. No kernel files will be deleted."
|
||||||
else
|
else
|
||||||
# Get the kernel files to delete
|
# Get the kernel files to delete
|
||||||
delete_kernel_files=("${kernel_files[@]:0:kernel_count-KEEP_GENERATIONS}")
|
delete_kernel_files=("${kernel_files[@]:0:kernel_count-KEEP_GENERATIONS}")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Log the files identified for deletion
|
# Log the files identified for deletion
|
||||||
log "Files identified for deletion:"
|
log "Files identified for deletion:"
|
||||||
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
||||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||||
log "$file"
|
log "$file"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Confirm dry run mode
|
# Confirm dry run mode
|
||||||
if [ "$DRY_RUN" = true ]; then
|
if [ "$DRY_RUN" = true ]; then
|
||||||
log "Dry run mode enabled. No files will be deleted."
|
log "Dry run mode enabled. No files will be deleted."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove old files
|
# Remove old files
|
||||||
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
||||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||||
if [ "$DRY_RUN" = false ]; then
|
if [ "$DRY_RUN" = false ]; then
|
||||||
if rm -f "$file"; then
|
if rm -f "$file"; then
|
||||||
log "Deleted: $file"
|
log "Deleted: $file"
|
||||||
else
|
else
|
||||||
log "Failed to delete: $file"
|
log "Failed to delete: $file"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
log "Dry run - would delete: $file"
|
log "Dry run - would delete: $file"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
log "Cleanup script completed."
|
log "Cleanup script completed."
|
||||||
|
|||||||
Reference in New Issue
Block a user