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".
|
||||
|
||||
# Number of generations to keep
|
||||
KEEP_GENERATIONS=5
|
||||
KEEP_GENERATIONS=4
|
||||
|
||||
# Log file for cleanup actions
|
||||
LOG_FILE="/var/log/cleanup-boot.log"
|
||||
@@ -15,12 +15,12 @@ DRY_RUN=false
|
||||
|
||||
# Check for dry run argument
|
||||
if [ "$1" = "--dry-run" ]; then
|
||||
DRY_RUN=true
|
||||
DRY_RUN=true
|
||||
fi
|
||||
|
||||
# Function to log messages
|
||||
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
|
||||
@@ -29,10 +29,10 @@ set -e
|
||||
log "Starting cleanup script. Keeping the latest $KEEP_GENERATIONS generations."
|
||||
|
||||
# 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)
|
||||
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
|
||||
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 [ "$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
|
||||
# Get the initrd files to delete
|
||||
delete_initrd_files=("${initrd_files[@]:0:initrd_count-KEEP_GENERATIONS}")
|
||||
# Get the initrd files to delete
|
||||
delete_initrd_files=("${initrd_files[@]:0:initrd_count-KEEP_GENERATIONS}")
|
||||
fi
|
||||
|
||||
# If there are fewer than KEEP_GENERATIONS kernel files, don't delete any
|
||||
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
|
||||
# Get the kernel files to delete
|
||||
delete_kernel_files=("${kernel_files[@]:0:kernel_count-KEEP_GENERATIONS}")
|
||||
# Get the kernel files to delete
|
||||
delete_kernel_files=("${kernel_files[@]:0:kernel_count-KEEP_GENERATIONS}")
|
||||
fi
|
||||
|
||||
# Log the files identified for deletion
|
||||
log "Files identified for deletion:"
|
||||
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||
log "$file"
|
||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||
log "$file"
|
||||
done
|
||||
|
||||
# Confirm dry run mode
|
||||
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
|
||||
|
||||
# Remove old files
|
||||
for file_entry in "${delete_initrd_files[@]}" "${delete_kernel_files[@]}"; do
|
||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||
if [ "$DRY_RUN" = false ]; then
|
||||
if rm -f "$file"; then
|
||||
log "Deleted: $file"
|
||||
else
|
||||
log "Failed to delete: $file"
|
||||
fi
|
||||
else
|
||||
log "Dry run - would delete: $file"
|
||||
fi
|
||||
file=$(echo "$file_entry" | cut -d' ' -f2-)
|
||||
if [ "$DRY_RUN" = false ]; then
|
||||
if rm -f "$file"; then
|
||||
log "Deleted: $file"
|
||||
else
|
||||
log "Failed to delete: $file"
|
||||
fi
|
||||
else
|
||||
log "Dry run - would delete: $file"
|
||||
fi
|
||||
done
|
||||
|
||||
log "Cleanup script completed."
|
||||
|
||||
Reference in New Issue
Block a user