First prep the system for the auto expand script
# Create directory for the script to live in
sudo mkdir -p /etc/expand-disk
Add the Script to crontab to run every 15 minutes
echo "*/15 * * * * /etc/expand-disk/auto-expand-disk.sh" | sudo tee -a /etc/crontab
Save the script in /etc/expand-disk/auto-expand-disk.sh (Full copy-paste command is present below this codeblock
#!/bin/bash
# Define log file
LOG_FILE="/var/log/expand_disk.log"
touch "$LOG_FILE"
chmod 666 "$LOG_FILE"
# Disk and partition
DISK="/dev/sdb"
PARTITION="/dev/sdb1"
# Logging function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" >/dev/null 2>&1
}
log "Starting disk expansion check for $PARTITION"
# Ensure required tools are installed
for cmd in lsblk growpart resize2fs partprobe; do
if ! command -v "$cmd" &>/dev/null; then
log "Error: $cmd not found. Install the necessary package."
exit 1
fi
done
# Trigger a disk rescan
log "Rescanning disk $DISK for changes..."
echo 1 > /sys/class/block/${DISK##*/}/device/rescan 2>/dev/null
partprobe "$DISK" 2>/dev/null
# Get disk and partition size
AVAILABLE_SPACE=$(lsblk -b -n -o NAME,SIZE | grep -w "$(basename "$DISK")" | awk '{print $2}')
PARTITION_END=$(lsblk -b -n -o NAME,SIZE | grep -w "$(basename "$PARTITION")" | awk '{print $2}')
# Debugging output
log "Disk total size: ${AVAILABLE_SPACE:-'UNKNOWN'} bytes"
log "Partition size: ${PARTITION_END:-'UNKNOWN'} bytes"
# Ensure values are retrieved correctly
if [ -z "$AVAILABLE_SPACE" ] || [ -z "$PARTITION_END" ]; then
log "Error: Could not determine disk or partition size."
exit 1
fi
# Check if there is unallocated space
if (( PARTITION_END >= AVAILABLE_SPACE )); then
log "No unallocated space available on $DISK."
exit 0
fi
# Expand partition
log "Expanding partition $PARTITION"
growpart "$DISK" 1
if [ $? -ne 0 ]; then
log "Error: Failed to expand partition."
exit 1
fi
# Trigger another rescan after expansion
log "Triggering another rescan after partition expansion..."
partprobe "$DISK"
sleep 2
# Resize filesystem
log "Resizing filesystem on $PARTITION"
resize2fs "$PARTITION"
if [ $? -ne 0 ]; then
log "Error: Failed to resize filesystem."
exit 1
fi
log "Successfully expanded $PARTITION"
exit 0
For creating the file directly via a copy/paste use this
sudo tee /etc/expand-disk/auto-expand-disk.sh <<EOF
#!/bin/bash
# Define log file
LOG_FILE="/var/log/expand_disk.log"
touch "$LOG_FILE"
chmod 666 "$LOG_FILE"
# Disk and partition
DISK="/dev/sdb"
PARTITION="/dev/sdb1"
# Logging function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" >/dev/null 2>&1
}
log "Starting disk expansion check for $PARTITION"
# Ensure required tools are installed
for cmd in lsblk growpart resize2fs partprobe; do
if ! command -v "$cmd" &>/dev/null; then
log "Error: $cmd not found. Install the necessary package."
exit 1
fi
done
# Trigger a disk rescan
log "Rescanning disk $DISK for changes..."
echo 1 > /sys/class/block/${DISK##*/}/device/rescan 2>/dev/null
partprobe "$DISK" 2>/dev/null
# Get disk and partition size
AVAILABLE_SPACE=$(lsblk -b -n -o NAME,SIZE | grep -w "$(basename "$DISK")" | awk '{print $2}')
PARTITION_END=$(lsblk -b -n -o NAME,SIZE | grep -w "$(basename "$PARTITION")" | awk '{print $2}')
# Debugging output
log "Disk total size: ${AVAILABLE_SPACE:-'UNKNOWN'} bytes"
log "Partition size: ${PARTITION_END:-'UNKNOWN'} bytes"
# Ensure values are retrieved correctly
if [ -z "$AVAILABLE_SPACE" ] || [ -z "$PARTITION_END" ]; then
log "Error: Could not determine disk or partition size."
exit 1
fi
# Check if there is unallocated space
if (( PARTITION_END >= AVAILABLE_SPACE )); then
log "No unallocated space available on $DISK."
exit 0
fi
# Expand partition
log "Expanding partition $PARTITION"
growpart "$DISK" 1
if [ $? -ne 0 ]; then
log "Error: Failed to expand partition."
exit 1
fi
# Trigger another rescan after expansion
log "Triggering another rescan after partition expansion..."
partprobe "$DISK"
sleep 2
# Resize filesystem
log "Resizing filesystem on $PARTITION"
resize2fs "$PARTITION"
if [ $? -ne 0 ]; then
log "Error: Failed to resize filesystem."
exit 1
fi
log "Successfully expanded $PARTITION"
exit 0
EOF