Ploop auto-compact script for OpenVZ nodes: compact.sh

Section 1. Problem:

Ploop has a minor issue arising from dynamic resizing involving file deletion and no space on the hardware node.

  • When a file is deleted inside a container, the host node does not automatically reclaim the space. (Bug 3008)
  • When a host node is out of space, files inside the container can become corrupted.

Section 2. Solution

This is where the compact.sh script comes in handy.  I have written this script to resolve the issues by running in the cron every 15 minutes on a host node.

*/15 * * * *  /path/to/compact.sh

How the script works

  1. Checks the /vz partition for the available disk space.
  2. If the disk space is under the minimum allowed or a certain amount of time has passed (onxruns), the scripts run vzctl compact on every VPS on the node.
  3. Compact reclaims the space for the host node.
  4. If compact is unable to reclaim enough space, an email is sent notifying you of the disk usage.

Settings

/tmp can be changed to any directory by changing the CFG=”/tmp” line at the top of the script.

Set compact to automatically run every X times.  By default, compact will run every 96 times (~24 hours) even if there is enough free space. 0 will disable this feature.

echo 96 > /tmp/compact.onxruns

Set the minimum level of disk space (in MB).  If the available disk space falls below this, it will compact the containers.  This defaults to 5% of your /vz partition size.  To set 10GB:

echo 10000 > /tmp/compact.minfree

Set the email address used by the script (requires mailx to be installed to send an email)

echo me@email.com > /tmp/compact.email

Set the vz partition (if varies from /vz)

echo /vz2 > /tmp/compact.vzdir

Turn logging off/on (default: 1).  Will log to /tmp/compact.log

echo 0/1 > /tmp/compact.logging

 

Download the script

wget http://www.byteonsite.com/scripts/compact.sh
chmod +x compact.sh

 

Debugging

If you want to make sure all the settings are correct. Run with the debug option to dump the settings.

./compact.sh debug

Script source code

#!/bin/bash
# compact.sh by Devon of ByteOnSite for OpenVZ
# Distribute freely with link to http://blog.byteonsite.com/?p=87

# SETTINGS

# Config Directory
CFG="/tmp"

# VZ Partition
VZ=`cat $CFG/compact.vzdir 2>/dev/null`
# Run Compact Automatically Every X Runs. Default: 96 runs, if run every 15 minutes this is every 24 hours.
ONXRUNS=`cat $CFG/compact.onxruns 2>/dev/null`
# Minimum Free Space in MB.  Will attempt to compact if free space drops below this.  Default: automatically generated, 5% free space.
MINFREE=`cat $CFG/compact.minfree 2>/dev/null`
# Email Address
EMAIL=`cat $CFG/compact.email 2>/dev/null`
# Log enabled
LOG=`cat $CFG/compact.logging 2>/dev/null`

# END SETTINGS

# DEFAULT SETTINGS

# Default VZ Partition
if [ ! $VZ ]; then
	VZ="/vz"
	echo $VZ > $CFG/compact.vzdir
fi
# Default ONXRUNS
if [ ! $ONXRUNS ]; then
	ONXRUNS=96
	echo $ONXRUNS > $CFG/compact.onxruns
fi
# Default MINFREE (5% of Total Partition)
if [ ! $MINFREE ]; then
	MINFREE=$[`df -PBM $VZ | awk 'NR==2 {print $2}' | cut -f1 -dM`/20]
	echo $MINFREE > $CFG/compact.minfree
fi
if [ ! $LOG ]; then
	LOG=1
	echo $LOG > $CFG/compact.logging
fi

# END DEFAULT SETTINGS

# LOGIC

FREESPACE=`df -PBM $VZ | awk 'NR==2 {print $4}' | cut -f1 -dM`

# DEBUG
if [ "$1" = "debug" ]; then
	echo "CFG: $CFG"
	echo "VZ: $VZ"
	echo "FREESPACE: ${FREESPACE}MB"
	echo "MINFREE: ${MINFREE}MB"
	echo "ONXRUNS: $ONXRUNS"
	echo "LOG: $LOG"
	echo "EMAIL: $EMAIL"
	if [ ! -f "/bin/mail" ]; then
		echo "MAILX: NOT FOUND. EMAILS WON'T BE SENT ON WARNING"
	fi
	exit 0
fi
# END DEBUG

i=`cat $CFG/compact.count`
if [ ! "$i" ] || [ "$i" -eq $ONXRUNS ]; then
	i=1
fi
if [ $i -eq 1 ] || [ $FREESPACE -lt $MINFREE ]; then
	# Run compact for every VPS
	stdout="/dev/null"
    if [ $LOG -eq 1 ]; then
    	stdout="$CFG/compact.log"
    fi
	for veid in `vzlist -H -o veid`
	do
		date=`date`
		echo "Starting compact on VEID $veid at $date.." >$stdout 2>&1
		vzctl compact $veid >$stdout 2>&1
	done
	# Check disk space again
	FREESPACE=`df -PBM $VZ | awk 'NR==2 {print $4}' | cut -f1 -dM`
	if [ $FREESPACE -lt $MINFREE ]; then
		message="Unable to compact containers enough to reduce free space below MINFREE levels. Free Space: $FREESPACE MB."
		echo $message | wall
		hostname=`hostname`
		if [ $EMAIL ]; then
			echo $message | mail -s "Compact failure on $hostname" $EMAIL
		fi
	fi
fi

# Increase count
i=$[$i+1]
echo $i > $CFG/compact.count

# END LOGIC
exit 0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.