Skip to content

Instantly share code, notes, and snippets.

@centminmod centminmod/autoswap.sh Secret
Last active Aug 29, 2015

Embed
What would you like to do?
kvm swap file
#!/bin/bash
######################################
# auto swap creator for centminmod.com
######################################
# calculation based on largest free disk
# space based partition size
LARGESTPART=$(df -k -x tmpfs -x devtmpfs -l -P | awk 'NR>=2 {print $4,$6}' | sort -k1 -nr | head -n1)
LPARTSIZE=$(echo $LARGESTPART | awk '{print $1}')
LPARTNAME=$(echo $LARGESTPART | awk '{print $2}')
SWAPBASEDIR="$LPARTNAME"
#####################################
# check to see if partition name ends with / otherwise
# added ending forward slash /
if [[ -z "$(echo $SWAPBASEDIR | grep '\/$')" ]]; then
SWAPBASEDIR="${LPARTNAME}/"
fi
#####################################
echo $LARGESTPART
echo $LPARTSIZE
echo $LPARTNAME
#####################################
if [[ "$(echo $LPARTSIZE | bc)" -ge '13631488' ]]; then
# 2GB Swap when greater than 13631488 bytes free disk space
SWAPSIZE='2048k'
elif [[ "$(echo $LPARTSIZE | bc)" -ge '10485760' ]] && [[ "$(echo $LPARTSIZE | bc)" -lt '13631488' ]]; then
# 1GB Swap when between >10485760 and <13631488 bytes free disk space
SWAPSIZE='1024k'
elif [[ "$(echo $LPARTSIZE | bc)" -ge '9961000' ]] && [[ "$(echo $LPARTSIZE | bc)" -lt '10485760' ]]; then
# 512MB Swap when between >9961000 and <10485760 bytes free disk space
SWAPSIZE='512k'
elif [[ "$(echo $LPARTSIZE | bc)" -ge '9437184' ]] && [[ "$(echo $LPARTSIZE | bc)" -lt '9961000' ]]; then
# 256MB Swap when between >9437184 and <9961000 bytes free disk space
SWAPSIZE='256k'
fi
# create swap file if one doesn't exist and if system is a
# non-OpenVZ system
if [[ -z "$(swapon -s)" ]] && [[ ! -f /proc/user_beancounters ]]; then
echo "create $SWAPSIZE swap file"
dd if=/dev/zero of=${SWAPBASEDIR}swapfile bs=1024 count=$SWAPSIZE
ls -lah ${SWAPBASEDIR}swapfile
mkswap ${SWAPBASEDIR}swapfile
swapon ${SWAPBASEDIR}swapfile
chown root:root ${SWAPBASEDIR}swapfile
chmod 0600 ${SWAPBASEDIR}swapfile
swapon -s
echo "${SWAPBASEDIR}swapfile swap swap defaults 0 0" >> /etc/fstab
mount -a
fi
df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/ploop63399p1 25798972 3761296 20727164 16% /
devtmpfs 524288 0 524288 0% /dev
tmpfs 524288 0 524288 0% /dev/shm
tmpfs 524288 164 524124 1% /run
tmpfs 524288 0 524288 0% /sys/fs/cgroup
find non-tmpfs or non-devtmpfs based block device/partitions and print from row 2 or higher
df -k -x tmpfs -x devtmpfs -l | awk 'NR>=2 {print $4,$6}'
20727176 /
sorting by column 1
df -k -l | awk 'NR>=2 {print $4,$6}' | sort -k1
20727176 /
524124 /run
524288 /dev
524288 /dev/shm
524288 /sys/fs/cgroup
#!/bin/bash
echo "create 2GB swap file"
dd if=/dev/zero of=/swapfile bs=1024 count=2048k
ls -lah /swapfile
mkswap /swapfile
swapon /swapfile
chown root:root /swapfile
chmod 0600 /swapfile
swapon -s
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
mount -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.