Thursday, August 1, 2013

Starting vmtoolsd as a service on Red Hat / CentOS

If you're like me;  you may manage virtual servers within vSphere.. Linux ones.  Red Hat ones, in particular, but this applies to CentOS as well.

A long, long time ago, in a galaxy far away, the vmware-tools setup procedure installed the necessary init script for you.  Lately though, for new images that I've been building - those init scripts aren't getting installed by the vmware tools installation package.  So they don't start up on reboot.  VMware based backups failed; clock were going askew, you name it.   I need that daemon started on reboot.

Without a SysV init script handy, I had to roll my own.. and this is the result;  despite having worked with Linux for well over 15 years, setting up SysV init scripts remain somewhat of a black art.  The ones on our older system were more complicated than we needed.  I was aiming for something simpler and portable.

With RHEL 7, the rumor mills are abuzz with systemd so that may change. But, I'm a practical system administrator, and it isn't here yet...  Yet.

Anyways, here it is;  place this script @ /etc/rc.d/init.d as 'vmtoolsd' then run `chkconfig vmtoolsd on` and `service vmtoolsd start` (if you change the script name, then change chkconfig and service commands accordingly):

#!/bin/bash
#
#   vmtoolsd          Start/stop the vmware tools daemon
#
# chkconfig:  2345 90 60
# description: vmtoolsd is a daemon that starts up.  for some reason, it
#              doesn't include a sysv init startup file in the latest release.
#              so i have to write this
#

### BEGIN INIT INFO
# Provides: vmtoolsd
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2345
# Default-Stop: 90
# Short-Description: Run vmware tools daemon
# Description:  Yadda yadda.
### END INIT INFO

RETVAL=0
prog="vmtoolsd"
exec="/usr/sbin/vmtoolsd"
lockfile="/var/lock/subsys/vmtoolsd"

# Source function library
. /etc/rc.d/init.d/functions

start() {
    # Ensure no one has access
    if [ $UID -ne 0 ]; then
        echo "User has insufficient privileges."
        exit 4
    fi

    [ -x $exec ] || exit 5

    echo -n $"Starting $prog: "
    daemon $prog --background=/var/run/vmtoolsd.pid
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile

}

stop() {
    if [ $UID -ne 0 ]; then
        echo "User has insufficient privileges."
        exit 4
    fi

    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec
        RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec -HUP
    else
        failure $"Reloading $prog"
    fi
    retval=$?
    echo
}

rh_status() {
    status -p /var/run/vmtoolsd.pid $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}



case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
esac
exit $?

8 comments:

  1. Thank you very much for this post. The script is very userfull for me.
    I have centos 6.4 64bits and i installed : vmware-tools-repo-RHEL6-9.0.5-1.el6.x86_64.rpm
    After the installation i received this error :
    [ warning] [vmsvc] Hostinfo_Daemonize: Lock held on PID path [/var/run/vmtoolsd.pid], error 11, not daemonizing.
    and using ps there is´nt any process called: vmtools.
    Using your scripts it start really well.

    ReplyDelete
  2. After I put the script in place on my test RHEL 6.5 test server - Vmtools starts after boot, but only stays up momentarily and goes down again. Still trying to troubleshoot that.

    ReplyDelete
  3. Appears Upstart was taking it right back down - although the upstart process would not start the vmtools like it was intended. I moved the /etc/init/vmware-tools.conf file out of /etc/init and vmtools stayed up after boot. Thank you for the script and workaround this will be very helpful until either vmware or Red Hat resolve the issue.

    ReplyDelete
  4. I set all this up on a CentOS 6.5 x64 VM. chkconfig adds the entry ok, but when I try 'service vmtoolsd start' or any parameter actually, I get this error;

    env: /etc/init.d/vmtoolsd: No such file or directiory

    Yes, vmtoolsd is in /usr/sbin, and I can start it manually, just not via the script

    ReplyDelete
  5. The vmtoolsd script needs to be placed in /etc/init.d then run the chkconfig again. It will create the necessary symlinks in /etc/rc.x (for the corresponding runlevel).

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. The Init scripts are not there because:
    "the /sbin/init program (init) from the sysvinit package has been replaced with Upstart in recent Linux distributions"

    See:
    VMware Tools init script is missing from the /etc/init.d directory on Linux virtual machines (2015685)
    http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2015685

    ReplyDelete