How to create a virtual machine server image from a physical CentOS server

I run a couple of servers over at a server farm. All of those machines run VMWare ESX, so today I wanted to move my last physical server to a VMWare image. If you want to convert a physical Windows machine then your task is quite easy. Start up the freely available VMWare Converter and start converting your physical machine to a virtual one. But what about when you need to convert a Linux server?

Since I really did not want to setup a new CentOS machine with all the packages I need and copying stuff from the physical to the virtual I did the following steps successfully. Keep in mind that this is on CentOS 5.2. I am sure this works for other Linux distributions as well.

  1. Create a new virtual machine and install the bare minimum CentOS. I used the CentOS NetInstall Option. This takes about 20 minutes or so.
  2. Now, create a “backup” directory on the root level (“/”) of your disk and copy the following directories into this backup directory;
    1. boot
    2. etc/fstab
    3. lib/modules
  3. Then on your physical server create tarball with the following directories included;
    1. boot
    2. bin
    3. etc
    4. home
    5. lib
    6. sbin
    7. usr
    8. root
    9. var
    10. opt

    Include any other directories that you need or have created. You should not need to include the “tmp” or the “lost & found” one. It is important to keep the permissions settings and this use the following command to create the tar;
    tar czvfp physicalserver.tgz <directoriesabove>
    (be prepared to make yourself some coffee in the meantime)

  4. Copy the file physicalserver.tgz to your new virtual machine.
  5. On the virtual machine move the physicalserver.tgz to the root (“/”) folder and extract it. This will probably take some time and you should get to your second cup of coffee.
  6. Once it has finished extracting do not attempt to reboot the server. Doing so will most probably render your image unbootable!
  7. Copy all of the directories within the backup directory to their original position (the ones from step 2) and confirm to overwrite existing files.
  8. Once done, issue the command “grub”. You will then be in the grub editor. Type the following commands to create a new MBR;
    1. root (hd0,0) (this is hd<zero>,<zero>)
    2. setup (hd0) (this is hd>zero)
      You should see some confirmation messages. Type “quit” to exit the editor.
  9. Now reboot the server and pray 🙂

I have done the above steps with 3 servers and it has always worked without problems. But there are some post-reboot steps that you should do as well. They are;

  1. Network Adapter. During the move above you will loose your network adapter. Thought CentOS still got everything setup and all working, you wont be able to reach outside of the virtual machine. Funny thing is that the Network Adapter in the Virtual Machines Configuration Settings also shows no problem. To make it work again you have to remove the Network Adapter in the Virtual Machine Settings and add one again.
  2. Install VMWare Tools. Since your physical server has no VMWare Tools installed this is a obvious step.
  3. Run security configuration. In my case, after step 1 and 2 above were done, I still could not get outside of my network. This was due to a mess up with the security settings of CentOS and most probably SELinux is running and prohibiting any network traffic. Simply run “system-config-security” in the shell and adjust your settings.
  4. Reboot your server.

With the above steps you should have a full copy of your physical server on your virtual machine environment. Let me know how it goes for you in the comments.