Fixing CentOS high cpu usage when running as a virtual machine under VirtualBox
Sunday, 21st April 2013, 20:28
Wow I've been insanely busy since the beginning of this year, and have only just managed to force myself into a blog entry for the first time since March. For that I feel very bad, poor site being neglected, the RSPCW's might take it away from me if I'm not careful.
Maybe a quick entry will keep them at bay for a short while!
To the point...
I recently moved a website from VMware Player to Sun... I mean Oracle VirtualBox. The process was a bit annoying for two reasons, the first of which involved the VM just refusing to get very far with booting.
As a general rule, I'm a big fan of CentOS for servers, because it is a very server oriented flavour of Linux and most importantly each release has a very long support cycle. Desktop users don't tend to care about support cycles very much, but when it comes to servers their importance is critical.
The last thing you want to do is a complete reinstall on a server box, when there is no real reason not to other than the fact security and stability updates are no longer being made available for it.
But there is one thing about it that irritates me, and that is the boot up process. Remember how Windows XP would blue screen if you did something drastic like change the motherboard? Well CentOS does something similar, it refuses to boot very far.
Migrating CentOS from VMWare to Virtualbox
So the first thing you need to do, is solve this issue. One of the first parts of the whole CentOS boot process is the loading of a RAM disk image which is used to initialise parts of the hardware before it loads the rest. This isn't a big generic monster which handles every piece of hardware you can think of, oh no. It's a custom created machine specific RAM disk that was made for your specific machine during installation.
Only we just changed our virtual hardware didn't we, so it doesn't work does it.
But it's not the hardest thing in the world to fix thankfully, and you can do it using this helpful webpage on the CentOS website.
You will need the first install CD image to boot to, and you'll also have to work out what the right .img file in the /mnt/sysimage directory applies to your current system, since after a year or two of yum updates, you'll have a fair few out of date ones in there.
Done that? Now to fix the high CPU!
In theory, at this stage you are now booting into a working CentOS installation, though you may have to tweak things like network set-up. The easiest thing to do there though is to make sure your VirtualBox machine has the same MAC address for any network cards you were using in VMware Player.
But you'll probably be hitting a snag about now, whilst the idle CPU usage for your host machine when running this VM under VMware was... well pretty idle, suddenly you are probably finding VirtualBox is going a bit nuts even when the VM itself is showing it's CPU to be doing nothing.
Don't panic! The fix for this is easy.
Boot up your CentOS VM, log in and type edit the file /etc/grub.conf with the text editor of your choice. VI if you like, or Joe if you are like me and prefer an editor without cryptic (or at least less cryptic) keyboard shortcuts.
Now, find the line which starts with kernel and lists that long filename which you probably already worked out when re-making the RAM disk above. It will most likely be the top entry. And then add the argument divider=10 on the end.
Here is mine for reference:
kernel /vmlinuz-2.6.18-348.3.1.el5 ro root=/dev/VolGroup00/LogVol00 vga=791 divider=10
With that done, all you need to do now is reboot and your CPU should be lower once more!
Want to know what you just did? Well the Kernel was trying to create interrupts at 1,000 per second, at least I think that is the rate it does it by default, feel free to correct me if I'm wrong.
The divider line changes that to 100 times a second, which is a lot less, and a lot less hassle for VirtualBox. :)