Script 09 · Storage I/O
9. Increase IOPS Server Performance
Adds noatime and memory writeback tuning to reduce unnecessary disk writes and improve perceived I/O behavior.
Category: Storage I/O
Risk: Medium
Lines: calculating
Language: Bash / Linux
What this script does
- Reduce metadata writes.
- Tune dirty page behavior.
- Show current scheduler state for SSD/NVMe review.
Prerequisites
- Root access
- Valid /etc/fstab
- Console access recommended before fstab edits
Execution flow
- Backs up /etc/fstab
- Adds noatime to root mount
- Attempts remount
- Appends sysctl I/O settings
- Displays disk scheduler
Validation checklist
- findmnt /
- mount | grep noatime
- sysctl vm.swappiness
- cat /sys/block/*/queue/scheduler
Operational cautions
- Bad fstab edits can prevent boot.
- noatime may affect software that depends on access time metadata.
Original script notes
ℹ️ Script Info: Optimizes disk read/write performance by adding the noatime flag to /etc/fstab (reducing unnecessary disk writes) and tweaking kernel I/O scheduler parameters.
cat << 'EOF' > optimize_iops.sh && chmod +x optimize_iops.sh && ./optimize_iops.sh
#!/bin/bash
# Output Colors
GREEN='\033[0;32m'; RED='\033[0;31m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
echo -e "${BLUE}=====================================================${NC}"
echo -e "${BLUE} SERVER IOPS & PERFORMANCE OPTIMIZER ${NC}"
echo -e "${BLUE}=====================================================${NC}"
# Check Root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}[!] Please run as root (sudo).${NC}"
exit 1
fi
# --- PART 1: FSTAB Optimization (noatime) ---
echo -e "${YELLOW}[1/3] Configuring /etc/fstab (noatime)...${NC}"
FSTAB="/etc/fstab"
BACKUP="/etc/fstab.bak.$(date +%F_%H-%M)"
# 1. Backup Fstab
cp "$FSTAB" "$BACKUP"
echo -e "${GREEN}[+] Backup fstab created at: $BACKUP${NC}"
# 2. Check if noatime is already on root
if grep -q " / " "$FSTAB" | grep -q "noatime"; then
echo -e "${YELLOW}[i] 'noatime' is already active on root partition. Skipping this step.${NC}"
else
# Replacement Logic using SED
# Finding line containing space / space (root partition)
# Replacing 'errors=remount-ro' with 'noatime,errors=remount-ro'
# OR replacing 'defaults' with 'defaults,noatime' if errors=... is missing
echo -e "${YELLOW}[*] Adding 'noatime' flag to root partition...${NC}"
# Target: errors=remount-ro
sed -i '/[[:space:]]\/[[:space:]]/s/errors=remount-ro/noatime,errors=remount-ro/' "$FSTAB"
# Target: defaults (if errors=remount-ro not used, usually defaults)
# Only replace if "noatime" was not added by previous command
if ! grep -q " / " "$FSTAB" | grep -q "noatime"; then
sed -i '/[[:space:]]\/[[:space:]]/s/defaults/defaults,noatime/' "$FSTAB"
fi
echo -e "${GREEN}[OK] fstab configuration updated.${NC}"
fi
# Remount so effect is immediate without reboot (if possible)
echo -e "${YELLOW}[*] Attempting to remount root partition...${NC}"
mount -o remount / 2>/dev/null && echo -e "${GREEN}[OK] Remount success. noatime effect is active.${NC}" || echo -e "${RED}[!] Remount failed (normal if busy), effect active after reboot.${NC}"
# --- PART 2: Kernel Optimization (Sysctl) for IOPS ---
echo -e "\n${YELLOW}[2/3] Adding Kernel Tuning (Sysctl) for I/O...${NC}"
SYSCTL_CONF="/etc/sysctl.conf"
ADDED_TWEAK=0
add_sysctl() {
local KEY="$1"
local VAL="$2"
if grep -q "^$KEY" "$SYSCTL_CONF"; then
echo -e " - $KEY already exists, skipping..."
else
echo "$KEY = $VAL" >> "$SYSCTL_CONF"
echo -e "${GREEN} + Adding: $KEY = $VAL${NC}"
ADDED_TWEAK=1
fi
}
# Tweak Explanation:
# vm.swappiness=10 -> Reduce Swap usage (since swap I/O is slow). Prioritize RAM.
# vm.vfs_cache_pressure=50 -> Keep inode/dentry cache in RAM longer (faster file access).
# vm.dirty_ratio=20 -> Start writing data to disk before RAM gets too full (prevents sudden I/O freeze).
# vm.dirty_background_ratio=5 -> Start background write process earlier.
echo "# --- Custom IOPS Optimization ---" >> "$SYSCTL_CONF"
add_sysctl "vm.swappiness" "10"
add_sysctl "vm.vfs_cache_pressure" "50"
add_sysctl "vm.dirty_ratio" "20"
add_sysctl "vm.dirty_background_ratio" "5"
if [ $ADDED_TWEAK -eq 1 ]; then
echo -e "${YELLOW}[*] Applying sysctl changes...${NC}"
sysctl -p > /dev/null
else
echo -e "${YELLOW}[i] No new changes to sysctl.${NC}"
fi
# --- PART 3: I/O Scheduler (Optional Check) ---
echo -e "\n${YELLOW}[3/3] Current I/O Scheduler Info:${NC}"
# Check scheduler for main disk (sda or nvme)
for disk in /sys/block/sd* /sys/block/nvme*; do
if [ -d "$disk" ]; then
NAME=$(basename "$disk")
SCHED=$(cat "$disk/queue/scheduler")
echo -e "Disk $NAME: $SCHED"
fi
done
echo -e "${YELLOW}[i] Tip: For SSD/NVMe, ensure using 'none', 'mq-deadline', or 'kyber'.${NC}"
echo -e "\n${BLUE}=====================================================${NC}"
echo -e "${BLUE} OPTIMIZATION COMPLETE ${NC}"
echo -e "${BLUE}=====================================================${NC}"
echo -e "Script saved at: $(pwd)/optimize_iops.sh"
echo -e "Please REBOOT your server to ensure all fstab configurations run perfectly."
EOF