Script to double/halve OpenVZ resources

The following script allows you to easily double or halve resources in an OpenVZ container.

You would install this script as "/usr/local/bin/vz-double-resources" and also create a symlink named "vz-half-resources" to it (probably also in /u/l/b).

This then allows you to just call "vz-double-resources 123 shmpages" in case you've been notified that there have been shmpages beancounter failures (resource limit has been hit).
This outputs the command to double the bean counter limit, and allows for easy execution by just forwarding the output to "sh", as in "vz-double-resources 123 shmpages | sh -".

Here's the script (available and maintained as/at Gist):


# cat =vz-double-resources
if [ -z $VZ ] || [ -z $RESOURCE ]; then
    echo "Usage: $0 VZ RESOURCE"
    exit 1
case $0 in
  *-double-*) OPERATION="*2" ;;
  *-half-*)   OPERATION="/2" ;;
  *) echo "Invalid: $0" ; exit 1;;
echo "# Operation: $OPERATION"
# get failures:
# awk '$NF ~ /[0-9]*[1-9]$/' /proc/bc/*/resources
if [ -f /proc/bc/$VZ/resources ] ; then
# uid  resource                     held              maxheld              barrier                limit              failcnt
  resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]')
  awk /$resource/ /proc/bc/$VZ/resources | {
    read resource held maxheld barrier limit failcnt
    if [ -n "$resource" ]; then
      cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save"
      echo $cmd
[[ -n "$cmd" ]] && exit
if [ -f /etc/vz/conf/$VZ.conf ] ; then
  value=$(grep -i "^$RESOURCE=" /etc/vz/conf/$VZ.conf)
  eval $value
  RESOURCE=$(echo $RESOURCE | tr '[:lower:]' '[:upper:]')
  resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]')
  value=$(eval echo \$${RESOURCE})
  echo $value | {
    IFS=: read barrier limit
    cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save"
    echo $cmd
  echo "ERROR: container $VZ not found."
  exit 1

This is a script I've been using since quite a while (otherwise it would use zsh instead of bash), and have not looked into after creating it.
I just noticed that it is quite useful in general and therefore wanted to make it public.

No feedback yet

Leave a comment

Your email address will not be revealed on this site.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
You can just use your OpenID to provide your name, e-mail and url.