improve configure_swap

This commit is contained in:
buildplan
2025-11-27 14:26:57 +00:00
parent dfae3a2fb9
commit c5a1621ae6

View File

@@ -4885,21 +4885,16 @@ configure_swap() {
fi fi
print_section "Swap Configuration" print_section "Swap Configuration"
# Check for existing swap partition entries in fstab for awareness
if lsblk -r | grep -q '\[SWAP\]'; then if lsblk -r | grep -q '\[SWAP\]'; then
print_warning "Existing swap partition found on disk." print_warning "Existing swap partition found on disk."
fi fi
# Detect active swap # Detect active swap
local existing_swap swap_type local existing_swap swap_type
# --show=NAME,TYPE,SIZE ensures consistent column order
existing_swap=$(swapon --show=NAME,TYPE,SIZE --noheadings --bytes 2>/dev/null | head -n 1 | awk '{print $1}' || true) existing_swap=$(swapon --show=NAME,TYPE,SIZE --noheadings --bytes 2>/dev/null | head -n 1 | awk '{print $1}' || true)
if [[ -n "$existing_swap" ]]; then if [[ -n "$existing_swap" ]]; then
swap_type=$(swapon --show=NAME,TYPE,SIZE --noheadings | head -n 1 | awk '{print $2}') swap_type=$(swapon --show=NAME,TYPE,SIZE --noheadings | head -n 1 | awk '{print $2}')
# Get human readable size for display using du (disk usage)
local display_size local display_size
display_size=$(du -h "$existing_swap" 2>/dev/null | awk '{print $1}' || echo "Unknown") display_size=$(du -h "$existing_swap" 2>/dev/null | awk '{print $1}' || echo "Unknown")
@@ -4916,12 +4911,16 @@ configure_swap() {
return 0 return 0
fi fi
# Comment out the partition in fstab to prevent it mounting on reboot sed -i "s|^${existing_swap}[[:space:]]|#&|" /etc/fstab
sed -i "s|^$existing_swap|#$existing_swap|" /etc/fstab
sed -i "s|^UUID=.*swap|#&|" /etc/fstab local swap_uuid
swap_uuid=$(blkid -s UUID -o value "$existing_swap" 2>/dev/null || true)
if [[ -n "$swap_uuid" ]]; then
sed -i "s|^UUID=${swap_uuid}[[:space:]]|#&|" /etc/fstab
fi
print_success "Swap partition disabled and removed from fstab." print_success "Swap partition disabled and removed from fstab."
# Clear variable to trigger new file creation below
existing_swap="" existing_swap=""
else else
print_info "Keeping existing swap partition." print_info "Keeping existing swap partition."
@@ -4935,16 +4934,19 @@ configure_swap() {
while true; do while true; do
read -rp "$(printf '%s' "${CYAN}Enter new swap size (e.g., 2G, 512M) [current: $display_size]: ${NC}")" SWAP_SIZE read -rp "$(printf '%s' "${CYAN}Enter new swap size (e.g., 2G, 512M) [current: $display_size]: ${NC}")" SWAP_SIZE
SWAP_SIZE=${SWAP_SIZE:-$display_size} SWAP_SIZE=${SWAP_SIZE:-$display_size}
if validate_swap_size "$SWAP_SIZE"; then break; else print_error "Invalid size. Use format like '2G' or '512M'."; fi if convert_to_mb "$SWAP_SIZE" >/dev/null; then
break
fi
done done
# Disk space check local REQUIRED_MB AVAILABLE_KB
local REQUIRED_SPACE AVAILABLE_SPACE if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then return 1; fi
REQUIRED_SPACE=$(convert_to_bytes "$SWAP_SIZE")
AVAILABLE_SPACE=$(df -k / | tail -n 1 | awk '{print $4}') AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
# Convert available to bytes for comparison (df output is 1K blocks)
if (( (AVAILABLE_SPACE * 1024) < REQUIRED_SPACE )); then # Compare (Convert MB to KB for comparison)
print_error "Insufficient disk space. Required: $SWAP_SIZE, Available: $(numfmt --to=iec $((AVAILABLE_SPACE * 1024)))" if (( (AVAILABLE_KB / 1024) < REQUIRED_MB )); then
print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: $((AVAILABLE_KB / 1024))MB"
exit 1 exit 1
fi fi
@@ -4952,12 +4954,17 @@ configure_swap() {
swapoff "$existing_swap" || { print_error "Failed to disable swap file."; exit 1; } swapoff "$existing_swap" || { print_error "Failed to disable swap file."; exit 1; }
print_info "Resizing swap file to $SWAP_SIZE..." print_info "Resizing swap file to $SWAP_SIZE..."
# Try fallocate first (fast), fallback to dd (compatible) # Try fallocate first (fast), fallback to dd (compatible)
if ! fallocate -l "$SWAP_SIZE" "$existing_swap" 2>/dev/null; then if ! fallocate -l "$SWAP_SIZE" "$existing_swap" 2>/dev/null; then
print_warning "fallocate failed (filesystem might not support it). Using dd (slower)..." print_warning "fallocate failed. Using dd (slower)..."
rm -f "$existing_swap" rm -f "$existing_swap"
# Quoted command substitution for ShellCheck compliance
if ! dd if=/dev/zero of="$existing_swap" bs=1M count="$(convert_to_mb "$SWAP_SIZE")" status=progress; then # Check if dd supports progress
local dd_status=""
if dd --version 2>&1 | grep -q "progress"; then dd_status="status=progress"; fi
if ! dd if=/dev/zero of="$existing_swap" bs=1M count="$REQUIRED_MB" $dd_status; then
print_error "Failed to create swap file with dd." print_error "Failed to create swap file with dd."
exit 1 exit 1
fi fi
@@ -4976,7 +4983,7 @@ configure_swap() {
fi fi
fi fi
# --- Case 3: Create New Swap (No swap exists, or partition was disabled above) --- # --- Case 3: Create New Swap ---
if [[ -z "$existing_swap" ]]; then if [[ -z "$existing_swap" ]]; then
if ! confirm "Configure a swap file (recommended for < 4GB RAM)?"; then if ! confirm "Configure a swap file (recommended for < 4GB RAM)?"; then
print_info "Skipping swap configuration." print_info "Skipping swap configuration."
@@ -4987,23 +4994,27 @@ configure_swap() {
while true; do while true; do
read -rp "$(printf '%s' "${CYAN}Enter swap file size (e.g., 2G, 512M) [2G]: ${NC}")" SWAP_SIZE read -rp "$(printf '%s' "${CYAN}Enter swap file size (e.g., 2G, 512M) [2G]: ${NC}")" SWAP_SIZE
SWAP_SIZE=${SWAP_SIZE:-2G} SWAP_SIZE=${SWAP_SIZE:-2G}
if validate_swap_size "$SWAP_SIZE"; then break; else print_error "Invalid size. Use format like '2G' or '512M'."; fi if convert_to_mb "$SWAP_SIZE" >/dev/null; then break; fi
done done
local REQUIRED_SPACE AVAILABLE_SPACE local REQUIRED_MB AVAILABLE_KB
REQUIRED_SPACE=$(convert_to_bytes "$SWAP_SIZE") if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then return 1; fi
AVAILABLE_SPACE=$(df -k / | tail -n 1 | awk '{print $4}') AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
if (( (AVAILABLE_SPACE * 1024) < REQUIRED_SPACE )); then if (( (AVAILABLE_KB / 1024) < REQUIRED_MB )); then
print_error "Insufficient disk space. Required: $SWAP_SIZE, Available: $(numfmt --to=iec $((AVAILABLE_SPACE * 1024)))" print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: $((AVAILABLE_KB / 1024))MB"
exit 1 exit 1
fi fi
print_info "Creating $SWAP_SIZE swap file at /swapfile..." print_info "Creating $SWAP_SIZE swap file at /swapfile..."
if ! fallocate -l "$SWAP_SIZE" /swapfile 2>/dev/null; then if ! fallocate -l "$SWAP_SIZE" /swapfile 2>/dev/null; then
print_warning "fallocate failed. Using dd (slower)..." print_warning "fallocate failed. Using dd (slower)..."
# Quoted command substitution for ShellCheck compliance
if ! dd if=/dev/zero of=/swapfile bs=1M count="$(convert_to_mb "$SWAP_SIZE")" status=progress; then # Check dd status support
local dd_status=""
if dd --version 2>&1 | grep -q "progress"; then dd_status="status=progress"; fi
if ! dd if=/dev/zero of=/swapfile bs=1M count="$REQUIRED_MB" $dd_status; then
print_error "Failed to create swap file." print_error "Failed to create swap file."
rm -f /swapfile || true rm -f /swapfile || true
exit 1 exit 1
@@ -5083,13 +5094,20 @@ EOF
# Helper: Convert size (e.g., 2G) to MB for dd count # Helper: Convert size (e.g., 2G) to MB for dd count
convert_to_mb() { convert_to_mb() {
local size="$1" local input="${1:-}"
local num="${size%[MGmg]}" local size="${input^^}"
size="${size// /}"
local num="${size%[MG]}"
local unit="${size: -1}" local unit="${size: -1}"
case "${unit^^}" in if ! [[ "$num" =~ ^[0-9]+$ ]]; then
print_error "Invalid swap size format: '$input'. Expected format: 2G, 512M." >&2
return 1
fi
case "$unit" in
G) echo "$((num * 1024))" ;; G) echo "$((num * 1024))" ;;
M) echo "$num" ;; M) echo "$num" ;;
*) echo "1024" ;; # Default to 1GB if parsing fails *) print_error "Unknown or missing unit in swap size: '$input'. Use 'M' or 'G' (e.g., 2G)." >&2
return 1 ;;
esac esac
} }