mirror of
https://github.com/buildplan/du_setup.git
synced 2025-12-29 16:14:59 +00:00
Improved swap configuration with retries
This commit is contained in:
97
du_setup.sh
97
du_setup.sh
@@ -4885,6 +4885,7 @@ configure_swap() {
|
|||||||
fi
|
fi
|
||||||
print_section "Swap Configuration"
|
print_section "Swap Configuration"
|
||||||
|
|
||||||
|
# Check for existing swap partition entries in fstab
|
||||||
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
|
||||||
@@ -4912,7 +4913,6 @@ configure_swap() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
sed -i "s|^${existing_swap}[[:space:]]|#&|" /etc/fstab
|
sed -i "s|^${existing_swap}[[:space:]]|#&|" /etc/fstab
|
||||||
|
|
||||||
local swap_uuid
|
local swap_uuid
|
||||||
swap_uuid=$(blkid -s UUID -o value "$existing_swap" 2>/dev/null || true)
|
swap_uuid=$(blkid -s UUID -o value "$existing_swap" 2>/dev/null || true)
|
||||||
if [[ -n "$swap_uuid" ]]; then
|
if [[ -n "$swap_uuid" ]]; then
|
||||||
@@ -4920,7 +4920,6 @@ configure_swap() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
print_success "Swap partition disabled and removed from fstab."
|
print_success "Swap partition disabled and removed from fstab."
|
||||||
|
|
||||||
existing_swap=""
|
existing_swap=""
|
||||||
else
|
else
|
||||||
print_info "Keeping existing swap partition."
|
print_info "Keeping existing swap partition."
|
||||||
@@ -4928,39 +4927,47 @@ configure_swap() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# --- Case 2: Swap File detected ---
|
# --- Case 2: Resize Existing Swap File ---
|
||||||
if confirm "Modify existing swap file size?"; then
|
if confirm "Modify existing swap file size?"; then
|
||||||
local SWAP_SIZE
|
local SWAP_SIZE REQUIRED_MB
|
||||||
|
|
||||||
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 convert_to_mb "$SWAP_SIZE" >/dev/null; then
|
# 1. Validate Format
|
||||||
break
|
if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then
|
||||||
|
continue
|
||||||
fi
|
fi
|
||||||
|
# 2. Min Size
|
||||||
|
if (( REQUIRED_MB < 128 )); then
|
||||||
|
print_error "Swap size too small (minimum: 128M)."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# 3. Disk Space Check
|
||||||
|
local AVAILABLE_KB AVAILABLE_MB
|
||||||
|
AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
|
||||||
|
AVAILABLE_MB=$((AVAILABLE_KB / 1024))
|
||||||
|
if (( AVAILABLE_MB < REQUIRED_MB )); then
|
||||||
|
print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: ${AVAILABLE_MB}MB"
|
||||||
|
# Suggest Max Safe Size (80% of free space)
|
||||||
|
local MAX_SAFE_MB=$(( AVAILABLE_MB * 80 / 100 ))
|
||||||
|
if (( MAX_SAFE_MB >= 128 )); then
|
||||||
|
print_info "Suggested maximum: ${MAX_SAFE_MB}M (leaves 20% free space)"
|
||||||
|
fi
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
break
|
||||||
done
|
done
|
||||||
|
|
||||||
local REQUIRED_MB AVAILABLE_KB
|
|
||||||
if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then return 1; fi
|
|
||||||
|
|
||||||
AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
|
|
||||||
|
|
||||||
# Compare (Convert MB to KB for comparison)
|
|
||||||
if (( (AVAILABLE_KB / 1024) < REQUIRED_MB )); then
|
|
||||||
print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: $((AVAILABLE_KB / 1024))MB"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
print_info "Disabling existing swap file..."
|
print_info "Disabling existing swap file..."
|
||||||
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, fallback to dd
|
||||||
# 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. Using dd (slower)..."
|
print_warning "fallocate failed. Using dd (slower)..."
|
||||||
rm -f "$existing_swap"
|
rm -f "$existing_swap"
|
||||||
|
|
||||||
# Check if dd supports progress
|
|
||||||
local dd_status=""
|
local dd_status=""
|
||||||
if dd --version 2>&1 | grep -q "progress"; then dd_status="status=progress"; fi
|
if dd --version 2>&1 | grep -q "progress"; then dd_status="status=progress"; fi
|
||||||
|
|
||||||
@@ -4990,44 +4997,52 @@ configure_swap() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local SWAP_SIZE
|
local SWAP_SIZE REQUIRED_MB
|
||||||
|
|
||||||
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 convert_to_mb "$SWAP_SIZE" >/dev/null; then break; fi
|
# 1. Validate Format
|
||||||
|
if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# 2. Min Size
|
||||||
|
if (( REQUIRED_MB < 128 )); then
|
||||||
|
print_error "Swap size too small (minimum: 128M)."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# 3. Disk Space Check
|
||||||
|
local AVAILABLE_KB AVAILABLE_MB
|
||||||
|
AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
|
||||||
|
AVAILABLE_MB=$((AVAILABLE_KB / 1024))
|
||||||
|
if (( AVAILABLE_MB < REQUIRED_MB )); then
|
||||||
|
print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: ${AVAILABLE_MB}MB"
|
||||||
|
# Suggest Max Safe Size
|
||||||
|
local MAX_SAFE_MB=$(( AVAILABLE_MB * 80 / 100 ))
|
||||||
|
if (( MAX_SAFE_MB >= 128 )); then
|
||||||
|
print_info "Suggested maximum: ${MAX_SAFE_MB}M (leaves 20% free space)"
|
||||||
|
fi
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
break
|
||||||
done
|
done
|
||||||
|
|
||||||
local REQUIRED_MB AVAILABLE_KB
|
|
||||||
if ! REQUIRED_MB=$(convert_to_mb "$SWAP_SIZE"); then return 1; fi
|
|
||||||
AVAILABLE_KB=$(df -k / | tail -n 1 | awk '{print $4}')
|
|
||||||
|
|
||||||
if (( (AVAILABLE_KB / 1024) < REQUIRED_MB )); then
|
|
||||||
print_error "Insufficient disk space. Required: ${REQUIRED_MB}MB, Available: $((AVAILABLE_KB / 1024))MB"
|
|
||||||
exit 1
|
|
||||||
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)..."
|
||||||
|
|
||||||
# Check dd status support
|
|
||||||
local dd_status=""
|
local dd_status=""
|
||||||
if dd --version 2>&1 | grep -q "progress"; then dd_status="status=progress"; fi
|
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
|
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
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! chmod 600 /swapfile || ! mkswap /swapfile >/dev/null || ! swapon /swapfile; then
|
if ! chmod 600 /swapfile || ! mkswap /swapfile >/dev/null || ! swapon /swapfile; then
|
||||||
print_error "Failed to enable swap file."
|
print_error "Failed to enable swap file."
|
||||||
rm -f /swapfile || true
|
rm -f /swapfile || true
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add to fstab if missing
|
|
||||||
if ! grep -q '^/swapfile ' /etc/fstab; then
|
if ! grep -q '^/swapfile ' /etc/fstab; then
|
||||||
echo '/swapfile none swap sw 0 0' >> /etc/fstab
|
echo '/swapfile none swap sw 0 0' >> /etc/fstab
|
||||||
print_success "Swap entry added to /etc/fstab."
|
print_success "Swap entry added to /etc/fstab."
|
||||||
@@ -5092,7 +5107,7 @@ EOF
|
|||||||
log "Swap configuration completed."
|
log "Swap configuration completed."
|
||||||
}
|
}
|
||||||
|
|
||||||
# Helper: Convert size (e.g., 2G) to MB for dd count
|
# Helper: Convert size (e.g., 2G) to MB for validation/dd
|
||||||
convert_to_mb() {
|
convert_to_mb() {
|
||||||
local input="${1:-}"
|
local input="${1:-}"
|
||||||
local size="${input^^}"
|
local size="${input^^}"
|
||||||
@@ -5106,8 +5121,10 @@ convert_to_mb() {
|
|||||||
case "$unit" in
|
case "$unit" in
|
||||||
G) echo "$((num * 1024))" ;;
|
G) echo "$((num * 1024))" ;;
|
||||||
M) echo "$num" ;;
|
M) echo "$num" ;;
|
||||||
*) print_error "Unknown or missing unit in swap size: '$input'. Use 'M' or 'G' (e.g., 2G)." >&2
|
*)
|
||||||
return 1 ;;
|
print_error "Unknown or missing unit in swap size: '$input'. Use 'M' or 'G'." >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user