OpenVZ container scripts

Update: Added vzmem-all.sh script just created today.  Tiny but effective.

After using OpenVZ for about 5 days (learning about it and making it work for me), I slapped together some scripts to make life easier to see the environment.  As we all know, it’s nice to organize things 🙂

Now, these are by no means perfect scripts and I have had no time to make them pretty, however if you put these scripts into your path on the host, you will be able to use them to pull information up, or easily create a container in OpenvVZ.

vzbeancounters.sh – A script that  spits out the /proc/user_beancounters from a specific container.
Parameter: container id

vzcreate.sh – A script that creates a mostly automated installation of an OpenVZ container.
Parameters:
Mandatory – container id, hostname, ip address
Optional – nameserver, template, configuration.

vzmemory.sh – A concise organized memory display of a specific container.
Parameter: container id

vztemplates.sh – displays available templates

vzmem-all.sh – Displays a full rundown of vzmemory.sh for every container running

All of these scripts are below.  You can copy-and-paste them into a file and chmod +x that file.
If it helps you, I’m glad.

vzbeancounters.sh

#!/bin/sh
#
# Script written by Norman Lund
# from the host, this script gets the beancounters file from a container
#

[[ $# -eq 0 ]] && { echo “Parameter: ${0} <Container ID>”; exit 1; }

CTID=${1}
echo “CTID: ${CTID}”
vzctl exec ${CTID} cat /proc/user_beancounters

vzcreate.sh

#!/bin/bash
#Vps Creator.Script created by Subin Hutton#
# Edited by Norman Lund
#
# Creates an OpenVZ container.
#

if [ -z ${3} ]; then
echo “${0} <CTID> <Hostname> <IP Address> [nameserver – OPTIONAL] [Template – OPTIONAL] [VPS-Config – OPTIONAL]”
exit;
fi

CTID=${1}
HOSTNAME=${2}
IPADDR=${3}
NAMESERVER=”10.1.1.2″
TEMPLATE=”${5}”
CONFIGURATION=”${6}”

if [ -z ${4} ]; then
echo “”
echo “* Default nameserver being used (${NAMESERVER})”
else
NAMESERVER=${4}
fi

if [ ! -z ${5} ]; then
TEMPLATE=${5}
else
echo “”
ls -a /vz/template/cache/ | grep tar.gz | awk “{print $9}” | sed “s/.tar.gz//g”
echo -n “”
echo -e -n “\nEnter the os template you want:”
read TEMPLATE
fi

if [ ! -z ${6} ]; then

CONFIGURATION=${6}
else
CONFIGS=`ls -a /etc/sysconfig/vz-scripts/*.conf-sample | sed ‘s/ve-//g’ | sed ‘s/.conf-sample//g’`
echo “”
echo “Configs available: ”
for CONFIG in ${CONFIGS}
do
basename ${CONFIG}
done
echo “”
echo “Which Configuration do you desire?: ”
read CONFIGURATION
fi

echo -n “Enter root password (will not display):”
read -s pass

vzctl create ${CTID} –ostemplate $template –config ${CONFIGURATION}
vzctl set ${CTID} –onboot yes –save
vzctl set ${CTID} –hostname ${HOSTNAME} –save
vzctl set ${CTID} –ipadd ${IPADDR} –save
vzctl set ${CTID} –nameserver ${NAMESERVER} –save
vzctl set ${CTID} –userpasswd root:$pass –save
vzctl start ${CTID}
echo -n “Installation Completed”
echo “”

vzmemory.sh

#!/bin/bash
#
# Container memory display
# call this with the CTID of the container will display the memory info
#

if [ $# -eq 0 ]; then
echo “${0} CTID”
echo “”
exit;
fi
CTID=${1}

STATUS=`vzlist -o status ${CTID} | grep -v STATUS 2>&1`

if [ “${STATUS}” == “stopped” ]; then
echo “${CTID}: Container stopped.”
exit
fi

# Hostname
HOSTNAME=`vzlist -o hostname ${CTID} | grep -v HOSTNAME`

# kmemsize’s limit in bytes
KMEM_L=`vzlist -o kmemsize.l ${CTID} | grep -v KMEMSIZE`
# kmemsize’s barrier in bytes
KMEM_B=`vzlist -o kmemsize.b ${CTID} | grep -v KMEMSIZE`
KMEM_HELD=`vzbeancounters.sh ${CTID} | grep kmemsize  | awk ‘{ print $3 }’`

# lockedpages’s limit in pages (4kb)
LOCKEDPAGES_L=`vzbeancounters.sh ${CTID} | grep lockedpages | awk ‘{ print $4 }’`
# lockedpages’s barrier in pages (4kb)
LOCKEDPAGES_B=`vzbeancounters.sh ${CTID} | grep lockedpages | awk ‘{ print $5 }’`
# lockedpage’s “held” memory in pages (4kb)
LOCKEDPAGES_HELD=`vzbeancounters.sh ${CTID} | grep lockedpages  | awk ‘{ print $2 }’`
LOCKEDPAGES_FAILCNT=`vzlist -o lockedpages.f ${CTID} | grep -v LOCKEDP`

# lockedpages’s limit in pages (4kb)
SHMPAGES_L=`vzbeancounters.sh ${CTID} | grep shmpages | awk ‘{ print $4 }’`
# lockedpages’s barrier in pages (4kb)
SHMPAGES_B=`vzbeancounters.sh ${CTID} | grep shmpages | awk ‘{ print $5 }’`
# lockedpage’s “held” memory in pages (4kb)
SHMPAGES_HELD=`vzbeancounters.sh ${CTID} | grep shmpages  | awk ‘{ print $2 }’`
SHMPAGES_FAILCNT=`vzlist -o shmpages.f ${CTID} | grep -v SHMP.F`

# physpages’s limit in pages (4kb)
PHYS_L=`vzlist -o physpages.l ${CTID} | grep -v PHYS`
# physpages’s barrier in pages (4kb)
PHYS_B=`vzlist -o physpages.b ${CTID} | grep -v PHYS`
PHYS_HELD=`vzbeancounters.sh ${CTID} | grep physpages  | awk ‘{ print $2 }’`
PHYS_FAILCNT=`vzlist -o physpages.f ${CTID} | grep -v PHYSP`

# privvmpages’s limit in pages (4kb)
PRIVVM_L=`vzlist -o privvmpages.l ${CTID} | grep -v PRIVVMP`
# physpages’s barrier in pages (4kb)
PRIVVM_B=`vzlist -o privvmpages.b ${CTID} | grep -v PRIVVMP`
PRIVVM_HELD=`vzbeancounters.sh ${CTID} | grep privvmpages  | awk ‘{ print $2 }’`
PRIVVM_FAILCNT=`vzlist -o privvmpages.f ${CTID} | grep -v PRIVVMP`

# vmguarpages limit in pages (4kb)
VMGUAR_L=`vzlist -o vmguarpages.l ${CTID} | grep -v VMGUARP`
# physpages’s barrier in pages (4kb)
VMGUAR_B=`vzlist -o vmguarpages.b ${CTID} | grep -v VMGUARP`
VMGUAR_HELD=`vzbeancounters.sh ${CTID} | grep vmguarpages  | awk ‘{ print $2 }’`
VMGUAR_FAILCNT=`vzlist -o vmguarpages.f ${CTID} | grep -v VMGUARP`

# vmguarpages limit in pages (4kb)
OOMGUAR_L=`vzlist -o oomguarpages.l ${CTID} | grep -v OOMGUARP`
# physpages’s barrier in pages (4kb)
OOMGUAR_B=`vzlist -o oomguarpages.b ${CTID} | grep -v OOMGUARP`
OOMGUAR_HELD=`vzbeancounters.sh ${CTID} | grep oomguarpages  | awk ‘{ print $2 }’`
OOMGUAR_FAILCNT=`vzlist -o oomguarpages.f ${CTID} | grep -v OOM `

# kmemsize limit converted to megabytes
KMEM_L_MB=$(((($KMEM_L/1024)/1024)))
# kmemsize barrier converted to megabytes
KMEM_B_MB=$(((($KMEM_B/1024)/1024)))
# kmemsize held converted to megabytes
KMEM_HELD_MB=$(((($KMEM_HELD/1024)/1024)))

# physpages limit converted to megabytes
PHYS_L_MB=$(((($PHYS_L/1024)*4)))
# physpages barrier converted to megabytes
PHYS_B_MB=$(((($PHYS_B/1024)*4)))
PHYS_HELD_MB=$(((($PHYS_HELD/1024)*4)))

# physpages limit converted to megabytes
SHMPAGES_L_MB=$(((($SHMPAGES_L/1024)*4)))
# physpages barrier converted to megabytes
SHMPAGES_B_MB=$(((($SHMPAGES_B/1024)*4)))
SHMPAGES_HELD_MB=$(((($SHMPAGES_HELD/1024)*4)))

# privmpages limit converted to megabytes
PRIVVM_L_MB=$(((($PRIVVM_L/1024)*4)))
# privmpages barrier converted to megabytes
PRIVVM_B_MB=$(((($PRIVVM_B/1024)*4)))
# privmpages “held” converted to megabytes
PRIVVM_HELD_MB=$(((($PRIVVM_HELD/1024)*4)))

# lockedpages limit converted to megabytes
LOCKEDPAGES_L_MB=$(((($LOCKEDPAGES_L/1024)*4)))
# lockedpages barrier converted to megabytes
LOCKEDPAGES_B_MB=$(((($LOCKEDPAGES_B/1024)*4)))
# lockedpages “held” converted to megabytes
LOCKEDPAGES_HELD_MB=$(((($LOCKEDPAGES_HELD/1024)*4)))

# lockedpages limit converted to megabytes
VMGUAR_L_MB=$(((($VMGUAR_L/1024)*4)))
# lockedpages barrier converted to megabytes
VMGUAR_B_MB=$(((($VMGUAR_B/1024)*4)))
# lockedpages “held” converted to megabytes
VMGUAR_HELD_MB=$(((($VMGUAR_HELD/1024)*4)))

# lockedpages limit converted to megabytes
OOMGUAR_L_MB=$(((($OOMGUAR_L/1024)*4)))
# lockedpages barrier converted to megabytes
OOMGUAR_B_MB=$(((($OOMGUAR_B/1024)*4)))
# lockedpages “held” converted to megabytes
OOMGUAR_HELD_MB=$(((($OOMGUAR_HELD/1024)*4)))

# lockedpages limit converted to megabytes
SHMPAGES_L_MB=$(((($SHMPAGES_L/1024)*4)))
# lockedpages barrier converted to megabytes
SHMPAGES_B_MB=$(((($SHMPAGES_B/1024)*4)))
# lockedpages “held” converted to megabytes
SHMPAGES_HELD_MB=$(((($SHMPAGES_HELD/1024)*4)))

# spit out display
echo “”
echo “CTID: ${CTID}     Hostname: ${HOSTNAME}”
echo “B = Barrier (soft limit)  L = Limit (hard limit)  Failed = Fail Count”
echo “Kernel:   HELD: ${KMEM_HELD_MB} Mb        B: ${KMEM_B_MB} Mb      L: ${KMEM_L_MB} Mb”
echo “Phys:     HELD: ${PHYS_HELD_MB} Mb        B: ${PHYS_B_MB} Mb      L: ${PHYS_L_MB} MbFailed: ${PHYS_FAILCNT}”
echo “PrivVM:   HELD: ${PRIVVM_HELD_MB} Mb      B: ${PRIVVM_B_MB} Mb    L: ${PRIVVM_L_MB} Mb      Failed: ${PRIVVM_FAILCNT}”
echo “LckdPgs:  HELD: ${LOCKEDPAGES_HELD_MB} Mb B: ${LOCKEDPAGES_B_MB} Mb       L: ${LOCKEDPAGES_L_MB} Mb Failed: ${LOCKEDPAGES_FAILCNT}”
echo “ShmPgs:   HELD: ${SHMPAGES_HELD_MB} Mb    B: ${SHMPAGES_B_MB} Mb  L: ${SHMPAGES_L_MB} Mb    Failed: ${SHMPAGES_FAILEDCNT}”
echo “VmGrPgs:  HELD: ${VMGUAR_HELD_MB} Mb      B: ${VMGUAR_B_MB} Mb    L: ${VMGUAR_L_MB} Mb      Failed: ${VMGUAR_FAILCNT}”
echo “OomGrPgs: HELD: ${OOMGUAR_HELD_MB} Mb     B: ${OOMGUAR_B_MB} Mb   L: ${OOMGUAR_L_MB} Mb     Failed: ${OOMGUAR_FAILCNT}”

vztemplates.sh

#!/bin/sh
#
# just displays existing templates (sans .tar.gz)
#

echo “”
echo “Templates available: ”
ls -la /vz/template/cache/ | grep tar.gz | awk “{print $9}” | sed “s/.tar.gz//g” | awk ‘{ print $9 }’
echo “”

vzmem-all.sh

#!/bin/sh
#
VZLIST=`vzlist | grep -v CTID | awk ‘{ print $1 }’`
for I in ${VZLIST}; do vzmemory.sh ${I}; echo “————“; done

3 thoughts on “OpenVZ container scripts

  1. Thanks for the scripts Norman. Will give me a nice head start. If you’d like copies of any improvements I make to them, just drop me a line. – k

  2. It is possible, however so can you 🙂

    I merely copy-and-pasted these files into the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.