linux · tech

Install vmware-tools from ESXi/vSphere 5 on Ubuntu 12.04

Let me clarify this: if it wasn’t required by our client, I would not touch VMware with a 3 meters pole.

Since VMware is a bloated and obsolete product, by default it won’t support your Ubuntu 12.04 virtual machines. VMware tools won’t compile there: too old and outdated to support kernel 3.x that exists since last year. Yep, it’s shit like this that makes our life miserable.

Luckly, some fellow sysadmin from elsewhere patched the old, outdated, crappy VMware tools and another one ported the patches so they could compile on modern systems.

Of course, none of this has been addressed by the vendor (VMware? I’m looking at you), so you need to get your patched kernel modules from a stranger in the forums. That’s exactly why you pay thousands of euros in VMware licenses: zero support and a tgz with (third-party) patched kernel modules on a forum.

Enough with the rant, how do we proceed? First thing first, you need to (try to) install the VMware Tools in the usual manner: select your VM, right click, select Guest -> Install VMware Tools, mount the CD-Rom, run the installer.

After the installer fails miserably to compile the kernel modules (did you install build-essential and linux-kernel-headers?), you need to download this file (it’s the tgz linked in the previous article: don’t worry, you’re only downloading kernel modules from a web forum, it’s safe!), then:

$ sudo -i
# cd /usr/lib/vmware-tools/lib/modules/sources/
# tar zxvf ~/patched-esxi*.tgz 

At this point you should be set, try to run again and see if the modules are compiled properly. If they are, rejoice! If not, blame VMware for they lazyness.

linux · tech · tips

How to use a Bash function in an if statement

Used to proper programming languages, it took me a bit to get this straight. Since Bash functions can only return numeric values, you have to use them like this:

function foo {
	if [ (your conditions) ]; then
		return 0
		return 1

if [ "$?" -eq 0 ]; then
	echo "Conditions verified"
	echo "Conditions NOT verified"

What’s happening here? You use the function return value ($?) as an argument for the if statement. If you want to work directly with something returned by the function or with strings, you are out of luck (strings are usually addressed using global variables).

But it turns out, you can execute the function using the $() formula, so if you want to return a string (or a number that you can use in the if statement without using $?) you actually have to echo it in the function, like this:

function cpuname {
	echo $(grep 'model name' /proc/cpuinfo | cut -d: -f2 | tail -n1)

if [ "x$(cpuname)" == "xAMD Athlon" ]; then
	echo "We are running on AMD";
	echo "We are NOT running on AMD";

Why the “x” in the string comparison? Because Bash is broken and in some versions could fail this test with empty strings.

Another example, with numbers:

function usercount {
	echo $(wc -l /etc/passwd | cut -d' ' -f1)

if [ "$(usercount)" -gt 100 ]; then
	echo "This system is quite popular"
	echo "Ok, it's only the two of us and a few system accounts"

Remember to comment your code, so the people not knowing this particular construct will be able to catch up!