OpenVZ Forum


Home » General » Support » Hooks at start/stop of VE (A question about hooks)
Hooks at start/stop of VE [message #40853] Wed, 13 October 2010 16:29 Go to next message
oscarah is currently offline  oscarah
Messages: 2
Registered: October 2010
Junior Member
Hello,

I've been searching for a while, and I didn't find a method to execute a script when a VE starts or stops. Is it possible? Is it there a (official) way to execute a hook on every container?

Thanks to all
Smile
Re: Hooks at start/stop of VE [message #40878 is a reply to message #40853] Tue, 19 October 2010 16:54 Go to previous messageGo to next message
oscarah is currently offline  oscarah
Messages: 2
Registered: October 2010
Junior Member
Well, searching a bit more, I've found something. If you put an script called CTID.mount, on /etv/vz/conf, it will be called at mounting the specified CTID. There are a CTID.umount too.

The problem with this is that .umount is called AFTER umounting root filesystem. I want a script called on post-start and pre-stop of each container.

So, to solve this, I've writen a small script which wrapps the tool vzctl, and launchs the hooks. The script is the following:

#!/bin/bash
# -*- mode: sh; coding: utf-8 -*-

# Copyright (c) 2010 Oscar AceƱa.
# This is Free Software, under terms of GPL v3.0 (or later)

# NOTE: this will not check the correctness of parameters, so
# call it only correctly

# Execution synopsis
#  $ vzctl [--help | --version]
#  $ vzctl [--quiet | --verbose] <start | stop | restart> CTID

if [ -z "$VZCTL" ]; then VZCTL=/usr/sbin/vzctl-bin; fi
if [ -z "$HOOKSDIR" ]; then HOOKSDIR=/etc/vz/hooks; fi

if [ ! -d "$HOOKSDIR" ]; then
    $VZCTL "$@"
    echo "No hooks defined"
    exit $?
fi

if [ $# -gt 1 ] && [ "$1" != "--help" ] && [ "$1" != "--version" ]; then

    # --skiplock is not documented, but is used on /etc/init.d/vz
    if [ "$1" == "--verbose" ] || [ "$1" == "--quiet" ] || [ "$1" == "--skiplock" ] ; then
	CMD="$2"
	CTID="$3"
    else
	CMD="$1"
	CTID="$2"
    fi

    if ! [ "$CTID" -eq "$CTID" 2> /dev/null ]; then
	nCTID=$(vzlist "$CTID" 2> /dev/null | grep "$CTID" | awk '{print $1}')
	if [ -z $nCTID ]; then
	    echo "CT ID $CTID is invalid."
	    exit 1
	fi
    else
	nCTID=$CTID
    fi

    if vzlist $nCTID | grep -qs 'running'; then

	if [ $CMD == "stop" ] || [ $CMD == "restart" ]; then
	    hook="$HOOKSDIR/$nCTID.pre.stop"
	    if [ -f "$hook" ]; then $hook; fi
	fi

	$VZCTL "$@"

	if [ $CMD == "restart" ]; then
	    hook="$HOOKSDIR/$nCTID.post.start"
	    if [ -f "$hook" ]; then $hook; fi
	fi
	exit 0

    else

	$VZCTL "$@"

	if [ $CMD == "start" ] || [ $CMD == "restart" ]; then
	    hook="$HOOKSDIR/$nCTID.post.start"
	    if [ -f "$hook" ]; then $hook; fi
	fi
	exit 0
    fi
fi

# In order to identify the wrapper
if [ -z "$@" ]; then echo -n "w-"; fi

$VZCTL "$@"
exit $?


It will call hooks on /etc/vz/hooks directory. The names of each hook is CTID.pre.stop and CTID.post.start. This files must have execution permision.

I'm using this script as an alternative (/etc/alternatives) for vzctl. You can install it using this makefile:

# -*- mode: make-gnumakefile; coding: utf-8 -*-

all:

install:
	install -d $(DESTDIR)/usr/local/bin
	install -d $(DESTDIR)/etc/alternatives
	install -d $(DESTDIR)/usr/sbin

	install -m 755 vzctl-wrapp.sh $(DESTDIR)/usr/local/bin
	mv $(DESTDIR)/usr/sbin/vzctl $(DESTDIR)/usr/sbin/vzctl-bin
	ln -fs $(DESTDIR)/usr/local/bin/vzctl-wrapp.sh $(DESTDIR)/etc/alternatives/vzctl
	ln -fs $(DESTDIR)/etc/alternatives/vzctl $(DESTDIR)/usr/sbin

remove:
	rm $(DESTDIR)/etc/alternatives/vzctl
	rm $(DESTDIR)/usr/local/bin/vzctl-wrapp.sh
	mv $(DESTDIR)/usr/sbin/vzctl-bin $(DESTDIR)/usr/sbin/vzctl


The makefile is for standard Debian systems. Change locations of files to match your system. To install, just "make install" as root. To remove, "make remove".

Hope it help you.

Cheers,
  • Attachment: vzctl-wrapp.sh
    (Size: 1.63KB, Downloaded 211 times)
  • Attachment: Makefile
    (Size: 0.57KB, Downloaded 219 times)
Re: Hooks at start/stop of VE [message #40883 is a reply to message #40853] Wed, 20 October 2010 08:23 Go to previous message
kir is currently offline  kir
Messages: 1645
Registered: August 2005
Location: Moscow, Russia
Senior Member

There are also $CTID.start and $CTID.stop script.

There are also premount and postumount scripts, see this git commit:
http://git.openvz.org/?p=vzctl;a=commit;h=eeceb721472483f6e8 d90cbfe72967c3336b5e77


Kir Kolyshkin
http://static.openvz.org/userbars/openvz-developer.png

[Updated on: Wed, 20 October 2010 08:23]

Report message to a moderator

Previous Topic: ovzkernel-2.6.18-194.17.1.el5.028stab070.7 kernel panic in megaraid_sas
Next Topic: hplip can't find hp network printer within container
Goto Forum:
  


Current Time: Sun May 11 08:22:57 GMT 2025

Total time taken to generate the page: 0.03368 seconds