attilagyorffy.com

Memory efficient Ubuntu VMs

Introduction

Attila Györffy

Attila Györffy

I'm a Ruby/JS dev/trainer with a focus on quality. An ex-Londoner, @terracycle, @ubxd, @lastfm. Follow me at http://twitter.com/attilagyorffy


ubuntu vm memory resources efficiency virtualisation vmware

Memory efficient Ubuntu VMs

Posted by Attila Györffy on .
Featured

ubuntu vm memory resources efficiency virtualisation vmware

Memory efficient Ubuntu VMs

Posted by Attila Györffy on .

My primary desktop environment is based on Mac OS X (currently 10.9 Beta) but I use Ubuntu as my production server OS and occasionally I use it for Linux development as well. Having a memory efficient VM can be a huge win for multiple reasons:

  • When running through virtualisation, more memory can be kept for the host machine
  • VM providers often provide limited RAM resources to your VMs based on your account type. Having memory efficient machines could save you a significant amount of money on a monthly basis.

Alternative mode to install Ubuntu

So, up until lately I was not aware of alternative ways of installing Ubuntu, I just assumed that the installer installs only the base system and you can install additional packages afterwards. Turns out, I was wrong. Upon firing up the Ubuntu installer it gives a handy (but not too prominent) option to install a minimum virtual machine. This can be enabled by pressing F4 in the initial installer screen:

Picture

So what are the main differences between the basic and the minimum virtual machine installs? Guess what: VM installs are optimised for usage within a virtual machine, running XEN, KVM, VMWare Fusion, or Virtualbox for example.

A default server installs the server kernel as well as the "Basic Ubuntu server" task packages, whereas the VM version installs a virtual kernel with no additional packages. A virtual kernel is a more lightweight version of the basic kernel with several modules like audio and SATA left out.

You can do the math:

  • The VM option currently uses about half of the disk space a normal install would use up. (680MB vs 1.1GB). The VM option prevents installation of multiple drivers to support varying types of hardware, as that would be useless on the standardized VM architecture. (On a side note, this also means it probably won't work as a physical install)

  • The VM loption loads fewer of the Linux Kernel modules and fewer processes into memory by default. The changes in memory consumption are significant:

The real memory footprint of a basic install is 561M - 487M (cached) - 23M (buffers) = 51M

The VM version uses 50M - 21M (cached) - 6M (buffers) = 23M

That means the VM uses half the physical memory of the regular server.

If you are running Ubuntu via VMWare Fusion, make sure to uncheck the 'Easy install' option in the beginning, otherwise it will fall back to the normal installer.

The install took about 5 minutes on my MacBook Air and the system boots up in less than 7 seconds, running an OpenSSH server and an Avahi daemon (for convenience). Not bad.

Disable additional unnecessary Linux Kernel modules

Even when you install Ubuntu Server with this specific mode there are still a couple of Linux Kernel modules that you are very likely not going to be using on a Server VM. Since when do you need joystick or sound support on a server anyway? Let's disable extra stuff from loading at startup.

You can use lsmod to get the status of modules in the Linux Kernel:

$ lsmod

This will show you the contents of the /proc/modules in a nicely formatted way, showing what kernel modules are currently loaded. Note that the floppy and snd or ac97 modules are most likely going to be listed.

Now we can start blacklisting modules that we are not going to be using by adding them to the /etc/modprobe.d/blacklist.conf file.

NOTE: Make sure to create a backup of that file before you'd start adding modifications to the linux kernel module blacklist:

$ sudo -i
# cd /etc/modprobe.d/
# cp blacklist.conf blacklist.conf.original

I've added the following modules to my blacklist (these may be different on your setup, make sure to blacklist your modules based on the output of lsmod):

# cat >> blacklist.conf
# Stop kernel modules from loading that are unnecessary on a virtual machine:

blacklist ac97_bus
blacklist bluetooth
blacklist btusb
blacklist floppy
blacklist gameport
blacklist joydev
blacklist psmouse
blacklist snd
blacklist snd_ac97_codec
blacklist snd_ens1371
blacklist snd_page_alloc
blacklist snd_pcm
blacklist snd_rawmidi
blacklist snd_seq_device
blacklist snd_timer
blacklist sound core
^D

Now you're ready to reboot your system:

$ reboot -h now

When the system rebooted, you can verify that your changes were successful by running lsmod to see that the blacklisted Linux Kernel modules have not been loaded anymore.

Attila Györffy

Attila Györffy

https://attilagyorffy.com

I'm a Ruby/JS dev/trainer with a focus on quality. An ex-Londoner, @terracycle, @ubxd, @lastfm. Follow me at http://twitter.com/attilagyorffy

View Comments...