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.
- Create a new virtual machine and install the bare minimum CentOS. I used the CentOS NetInstall Option. This takes about 20 minutes or so.
- Now, create a “backup” directory on the root level (“/”) of your disk and copy the following directories into this backup directory;
- Then on your physical server create tarball with the following directories included;
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)
- Copy the file physicalserver.tgz to your new virtual machine.
- 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.
- Once it has finished extracting do not attempt to reboot the server. Doing so will most probably render your image unbootable!
- Copy all of the directories within the backup directory to their original position (the ones from step 2) and confirm to overwrite existing files.
- Once done, issue the command “grub”. You will then be in the grub editor. Type the following commands to create a new MBR;
- root (hd0,0) (this is hd<zero>,<zero>)
- setup (hd0) (this is hd>zero)
You should see some confirmation messages. Type “quit” to exit the editor.
- 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;
- 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.
- Install VMWare Tools. Since your physical server has no VMWare Tools installed this is a obvious step.
- 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.
- 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.