Tuesday, April 12, 2011

Hooks in Genati

At my current job, we have a few servers that are running virtualization software. We were originally using Xen, but with the latest version of Debian Squeeze, we were force to upgrade and in doing so, found that KVM seem to work a bit better in our system. I did not set up our cluster, so I'm not going to go into detail about our setup, but I can go into how to set up hooks.

Hooks allow you to run a custom script right before a new instance is setup. You can do things like set up your network interfaces, firewalls and apt-get new programs. First thing is first, where the hell do you put the script? Here:
 $ /etc/ganeti/instance-debootstrap/hooks/
In this directory, I have a file called "clear-root-password". I uses this as the basis for my files. The things that I needed to do when I setup a new instance:
  • set up my network interface
  • download some standard programs
  • set up my firewall
Now that I have my goals, how the hell do I get it working? So the first thing that I learned, was the Ganeti documentation is ... ok. I can't say that the things that I did will work for you, but it worked for me. To run the bash scripts, you have to make Ganeti run your script, make the file executable. When the hook script is running, there is no output to standard output. If everything works correctly, it just tells you everything is ok. If there is an error, the tail of the log file for that operation is printed to the screen.

You can add an network interface by adding the following line to your hook file:
echo "iface eth0 inet static" >> "$TARGET"/etc/network/interface
echo "address $NIC_0_IP" >> "$TARGET"/etc/network/interface
echo "netmask 255.255.255.0" >> "$TARGET"/etc/network/interface
echo "gateway 192.168.1.254" >> "$TARGET"/etc/network/interface
The idea behind this is that the $TARGET variable is the temporary path that is the file system of your new instance. You can copy files from the node's filesystem to the new instance too. It's just like a normal file. Pretty sweet if you ask me. Also, the $NIC_0_IP is the ip address that I passed into --net parameter when running gnt-instance add.

Next, what about apt-get? I want to run a command on this new instance and install vim. You can do this by issuing the following command:
 chroot "$TARGET" apt-get install vim
If you don't know about chroot, now you do. It's freaking sweet. You can now run a command as if "$TARGET" was your root directory ( / ), so apt-get will install vim on your new instance.

Finally, when setting up a firewall file, you want to make sure that it can only be read by root. To do this on your new instance, you can issue the following command:
 cp /etc/firewall.sh "$TARGET"/root/
chmod 600 "$TARGET"/root/firewall.sh
When changing the permissions on the instance, just issue the chmod command as normal.

Using "printenv" bash command allowed me to see the variables that were given to me by Ganeti in the bash script, as the documentation didn't seem to match up exactly with what I was getting in my environment.

Hope this helps someone, leave a comment if you have a question or some point to make.

1 comment:

SMF said...

Fantastic. Will visit again. A+