Friday, December 28, 2012

How to Install Virtualbox 4.1 on CentOS 6.3


Oracle VirtualBox
 is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. VirtualBox is a general-purpose full virtualizer for x86 hardware. Targeted at server, desktop and embedded use, it is now the only professional-quality virtualization solution that is also Open Source Software.

VirtualBox supports a large number of guest operating systems:
  • Windows 3.x
  • Windows NT 4.0
  • Windows 2000
  • Windows XP
  • Windows Server 2003
  • Windows Vista
  • Windows 7
  • DOS
  • Linux (2.4, 2.6, 3.0, 3.1)
  • Solaris
  • OpenSolaris
  • OpenBSD
This howto uses Virtual Box yum repositories.
The following steps will need to be done with the root account.

Step 1: First let’s add Virtualbox repository key

Change to the /etc/yum.repos.d/ directory and download the VirtualBox repo file into your yum repository. With this you’ll be able to install VirtualBox through yum and updates for VirtualBox will also be included whenever you run the yum update command:
# cd /etc/yum.repos.d/
# wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
The following steps are optional: Verify the fingerprint of the gpg key in order confirm that the key downloaded belongs to virtualbox.org.
# wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc
# rpm --import sun_vbox.asc

Step 2: Install following dependency packages

# yum update
# yum groupinstall 'Development Tools'
# yum install SDL kernel-devel kernel-headers dkms
With the last command we have installed the kernel headers of our currently used kernel. The headers are located in the /usr/src/kernels/ directory, but it is likely that its directory is not named <kernel_version>-<architecture>, but has a different name so that the Virtualbox kernel module cannot be built later on because the expected kernel headers directory cannot be found. We are going to correct that now:
# uname -r
2.6.32-220.el6.x86_64
This means that there should be a directory called 2.6.32-220.el6.x86_64 in the /usr/src/kernels/ directory. We can check this now:
# cd /usr/src/kernels/
# ls –l
total 4
drwxr-xr-x 22 root root 4096 Jun 25 17:30 2.6.32-220.23.1.el6.x86_64
As you see, I have the directory 2.6.32-220.23.1.el6.x86_64, but not 2.6.32-220.el6.x86_64. Therefore we create a symlink called 2.6.18-238.el5-x86_64 that points to 2.6.18-238.9.1.el5-x86_64:
# ln -s 2.6.32-220.23.1.el6.x86_64 `uname -r`
Note: If you got kernel update or run older kernel than newest installed then reboot:
# reboot

Step 3: Install VirtualBox Latest Version 4.1 (currently 4.1.18)

Another optional step is to review the information of the VirtualBox package before proceeding with the installation (this will confirm the minor version):
# yum info VirtualBox-4.1
Proceed with installing VirtualBox (the command below is case sensitive). When prompted about the GPG key enter Y as long as the key ID matches the one above:
# yum install VirtualBox-4.1
Note:
This command create automatically vboxusers group and VirtualBox user must be member of that group.
This command also build needed kernel modules.
Rebuild kernel modules with following command:
# /etc/init.d/vboxdrv setup
## OR ##
# service vboxdrv setup
Finally don’t forget to add your user account to the newly created vboxusers group, where “username” is the Linux user that you are adding to this group:
# /usr/sbin/usermod -a -G vboxusers username
VirtualBox is now installed and ready to be used.

Step 4: Install VirtualBox 4.1.18 Oracle VM VirtualBox Extension Pack

VirtualBox Extension Pack Support for USB 2.0 devices, VirtualBox RDP and PXE boot for Intel cards
Note:There’s actually an open source (OSE) version, of virtualbox, witch uses VNC instead of RDP
# wget http://download.virtualbox.org/virtualbox/4.1.18/Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack
# VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack
(Make sure you grab the latest version from the VirtualBox web site.)
Now we need to allow RDP port thought iptables, this will enable us to connect to the vm console.
# vi /etc/sysconfig/iptables
 
#add before the last reject lines:
 
-A INPUT -p tcp --dport 3389 -j ACCEPT

Step5: Using VirtualBox On The Command Line

Creating A VM
# VBoxManage createvm --name yourvirtualname --register
Now to find the correct OS type your going to install run (there are x86 and x64 versions):
# VBoxManage list ostypes
Now let’s add some hardware to the newly creted vm
# VBoxManage modifyvm myvmname --memory 1024 --cpus 1 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0 --ostype Linux
NOTE: If you’re running a host cpu with hardware virtualization support you can ignore this notice. Well if you’re not then you should know that you won’t be able to run x64 guests (understandable), but you won’t be able to run guests with more than one vcpu, because of VirtualBox, Virtual SMP.
Ok, moving on, let’s create and atach an hdd
# VBoxManage createhd --filename myvmname_hdd0.vdi --size 20480
# VBoxManage storagectl myvmname --name "storage-ctrl" --add sata
# VBoxManage storageattach myvmname --storagectl "storage-ctrl" --port 0 --device 0 --type hdd --medium myvmname_hdd0.vdi
And an iso image in order to install the so into the vm (previously uploaded to the /tmp folder)
# VBoxManage storageattach myvmname --storagectl "storage-ctrl" --port 1 --device 0 --type dvddrive --medium /tmp/CentOS-6.2-i386-netinstall.iso
To “eject” the iso image use:
# VBoxManage storageattach myvmname--storagectl "storage-ctrl" --port 1 --device 0 --type dvddrive --medium none
You can set RDP with VBoxManage, while creating the VM with the command bellow.
However, you won’t need rdp for anything else that to install it so it’s preferred set RDP while running the VM’s.
# VBoxManage modifyvm myvmname --vrde on --vrdeport 3389
If/When you need to delete the vm, just run this:
# VBoxManage unregistervm myvmname --delete
Starting and Managing VM’s
Control the VM
# VBoxManage controlvm myvmname poweroff
# VBoxManage controlvm myvmname pause
# VBoxManage controlvm myvmname reset
To list all vm’s
# VBoxManage list vms
Or just the ones running
# VBoxManage list runningvms
Show all info about one vm
# VBoxManage showvminfo myvmname
Start the vm with:
# VBoxHeadless --startvm myvmname -vrde on -vrdeproperty TCP/Ports=3389 &
In order to start any vm at boot time just run them from “/etc/rc.local”
# su yourvboxusername-c "VBoxHeadless -startvm myvmname -vrde on -vrdeproperty TCP/Ports=3389 >/path-to-your-log-file/logfile.log 2>&1 &"
Note: When you register a VM, it will be “attached” to the user who created/registered it so when run it from rc.local you will need to use the correct user.
To install VirtualBox Guest Additions on the guest, check here: VirtualBox Headless – Install VirtualBox Guest Additions

Step 6: Connecting To A VM From A Remote Desktop

Connecting from Windows XP/Vista/7
Click Start > All Programs > Accessories > Remote Desktop Connection.
In the window, enter the IP address of the computer you are connecting to the IP address text field, as shown below
Once the IP address has been entered, click Connect.
When prompted, enter your username and password to connect to the remote computer.
To disconnect from the session and leave it running, click Start > Disconnect.
To logoff the session, click Start > Log Off.
Connecting from linux
On Linux desktops, you can use the rdesktop command to connect to the VM. I’m assuming you’re using a Fedora desktop here.
On Fedora, you must install rdesktop first. Open a terminal (Applications > System Tools > Terminal)…
… and become root:
$ su
Then install rdesktop
# yum install rdesktop
Then type in the following command:
# rdesktop -a 16 10.10.10.100
(10.10.10.100 is the host IP address, not the one of the guest – replace it with your own IP address or hostname; -a 16 means 16 bit colour depth.)

If The Remote Desktop Connection Doesn’t Work…

If the remote desktop connection doesn’t work, you’ve probably missed the step where I install theVirtualBox extension pack in chapter 2. Please install the extension pack now, stop your virtual machine (see chapter 3.3), and modify your virtual machine to accept remote desktop connections:
# VBoxManage modifyvm "Ubuntu 12.04 Server" --vrde on
(If the extension pack is already installed when you create your virtual machine, remote desktop connections are automatically enabled.)
Then start your virtual machine again.

No comments:

Post a Comment