Thursday, June 23, 2016

[Quick Install - VirtualBox on CentOS]: Install Oracle VirtualBox And Manage It Using phpVirtualBox On CentOS 7 Headless Server

Install Oracle VirtualBox And Manage It Using phpVirtualBox On CentOS 7 Headless Server

We, all, knew how to install Oracle VirtualBox on CentOS 6.x and CentOS 7.x GUI based systems. But, what about on CentOS minimal server which is not having any GUI desktop environment? No worries, VirtualBox has a tool called “VBoxHeadless”. The VBoxHeadless can help you to create and manage Virtual machines without the need of VirtualBox GUI.
In this tutorial, let us see how to install and configure Oracle VirtualBox on CentOS 6.x and CentOS 7.x minimal servers.
Here is my test box details:
OS: CentOS 7.1 Server Minimal Installation (No GUI)
IP Address: 192.168.1.150/24

Preparing CentOS server

Before installing VirtualBox, we have to do a couple of tasks.
First of all, update your server.
yum update -y
Create a separate normal user (Ex. sk_vuser) for installing oracle virtualbox.
useradd sk_vuser
passwd sk_vuser
Log out, and log in back to that user.
Change to root user privileges, by using the following command:
su
Now, install the following prerequisites that are needed by oracle VirtualBox.
yum groupinstall 'Development Tools'
yum install SDL kernel-devel kernel-headers dkms
Reboot your server once to take effect all the changes.

Installing VirtualBox

Go to the /etc/yum.repos.d/ directory:
cd /etc/yum.repos.d/
Add the latest oracle virtualbox repository to your CentOS server. You can download the oracle virtualbox latest repository from here.
wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo
Add the virtualbox public key:
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
rpm --import oracle_vbox.asc
Now, update the repository and install latest oracle virtualbox version.
yum update && yum install VirtualBox-4.3
After installing virtualbox, add the virtualbox user sk_vuser to the vboxusers group.
usermod -G vboxusers sk_vuser
Now, run the following command to check if virtualbox kernel modules are loaded or not.
In CentOS 7.x:
systemctl status vboxdrv
In CentOS 6.x:
/etc/init.d/vboxdrv status
Sample output:
vboxdrv.service - LSB: VirtualBox Linux kernel module
   Loaded: loaded (/etc/rc.d/init.d/vboxdrv)
   Active: active (exited) since Thu 2015-06-11 19:20:09 IST; 19min ago
  Process: 561 ExecStart=/etc/rc.d/init.d/vboxdrv start (code=exited, status=0/SUCCESS)
Jun 11 19:20:04 server1.unixmen.local systemd[1]: Starting LSB: VirtualBox Linux kernel module...
Jun 11 19:20:09 server1.unixmen.local vboxdrv[561]: Starting VirtualBox kernel modules [  OK  ]
Jun 11 19:20:09 server1.unixmen.local systemd[1]: Started LSB: VirtualBox Linux kernel module.
If it, not loaded, run the following command to load them:
/etc/init.d/vboxdrv setup

Install Oracle VirtualBox Extension pack

Starting with VirtualBox 4.0, we should install Extension pack that will help us to manage and connect our remote guest consoles via web interface. This Extension pack provides the following functionalities to the VirtualBox guests.
  • The virtual USB 2.0 (EHCI) device
  • VirtualBox Remote Desktop Protocol (VRDP) support
  • Host webcam passthrough
  • Intel PXE boot ROM
  • Experimental support for PCI passthrough on Linux hosts
VirtualBox extension pack should be same version as VirtualBox base package. Download Extension pack for VirtualBox 4.3 from here.
wget http://download.virtualbox.org/virtualbox/4.3.28/Oracle_VM_VirtualBox_Extension_Pack-4.3.28-100309.vbox-extpack
Then, Install Extension pack as shown below.
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.3.28-100309.vbox-extpack
Sample output:
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully installed "Oracle VM VirtualBox Extension Pack".
That’s it Oracle VirtualBox and Extension pack have been installed successfully on our CentOS 6.5 server. Start using Oracle VirtualBox on your CentOS headless server. For creating and managing virtual machines from the command line, refer the official manual.
Instead, you can use Oracle VirtualBox GUI client called “phpvirtualbox”. to easily create and manage virtual machines via web browser of any remote desktop system.

Create and manage VMs from command line

First, let us create a Ubuntu 14.04 server VM with 512MB RAM and 8GB virtual hdd.
To do that, run:
VBoxManage createvm --name "Ubuntu 14.04" --register
Sample output:
Virtual machine 'Ubuntu 14.04' is created and registered.
UUID: 6ac096ec-01a8-4f5b-9a84-46f874eb7cfb
Settings file: '/root/VirtualBox VMs/Ubuntu 14.04/Ubuntu 14.04.vbox'
Next, assign the RAM size and network card mode(Bridge or NAT).
VBoxManage modifyvm "Ubuntu 14.04" --memory 512 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0
Assign virtual hard drive size.
VBoxManage createhd --filename Ubuntu_14_04.vdi --size 8000
Sample Output:
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. UUID: 8d48aef4-eb92-4181-aed1-63ccbae0f2b1
Add Storage controller:
VBoxManage storagectl "Ubuntu 14.04" --name "IDE Controller" --add ide
Attach the Virtual hard drive file(vdi) to the storage controller using command:
VBoxManage storageattach "Ubuntu 14.04" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /home/sk/Ubuntu_14_04.vdi
Now, Assign the Ubuntu ISO media using command:
VBoxManage storageattach "Ubuntu 14.04 Server" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/sk/ubuntu-14.04-server-i386.iso
That’s it. The new VM has been created.
To know about VBoxManage command, run:
VBoxManage --help
Or, refer the Official guide.
Now, start the VM using command:
VBoxHeadless --startvm "Ubuntu 14.04"
To stop a running VM, enter the following command:
VBoxManage controlvm "Ubuntu 14.04" poweroff
Similarly we can pause a VM, using command:
VBoxManage controlvm "Ubuntu 14.04" pause
To reset a VM, enter:
VBoxManage controlvm "Ubuntu 14.04" reset
To learn more about VBoxHeadless options, run:
VBoxHeadless --help
Sample output:
Oracle VM VirtualBox Headless Interface 4.3.28
(C) 2008-2015 Oracle Corporation
All rights reserved.
Usage:
   -s, -startvm, --startvm <name|uuid>   Start given VM (required argument)
   -v, -vrde, --vrde on|off|config       Enable (default) or disable the VRDE
                                         server or don't change the setting
   -e, -vrdeproperty, --vrdeproperty <name=[value]> Set a VRDE property:
                                         "TCP/Ports" - comma-separated list of ports
                                         the VRDE server can bind to. Use a dash between
                                         two port numbers to specify a range
                                         "TCP/Address" - interface IP the VRDE server
                                         will bind to
   --settingspw <pw>                     Specify the settings password
   --settingspwfile <file>               Specify a file containing the settings password
   -c, -capture, --capture               Record the VM screen output to a file
   -w, --width                           Frame width when recording
   -h, --height                          Frame height when recording
   -r, --bitrate                         Recording bit rate when recording
   -f, --filename                        File name when recording. The codec used
                                         will be chosen based on the file extension
For more details, refer the official manual.
Creating and managing might be bit difficult and time consuming method for some lazy administrators. If you are one of them, there is another way to create and manage VMs in the headless from a Remote system web browser using a tool called “phpvirtualbox”.
You can use Oracle VirtualBox GUI client called “phpvirtualbox” to easily create and manage virtual machines via a web browser from any remote desktop system.

What is phpVirtualBox?

phpVirtualBox is an open source, web-based client written using PHP. It can be used to manage and administer Oracle VirtualBox headless instances.

Install phpVirtualBox

Before installing phpVirtualBox, we have to install Apache web server and PHP. To install Apache, PHP refer the following links.
Additionally, install php-soap extension which is required by phpVirtualBox to work well.
yum install php-soap
Restart the web server to take effect the changes.
In CentOS 7.x:
systemctl restart httpd
In CentOS 6.x:
service httpd restart
Now, Download the latest version from the phpVirtualBox official site.
wget http://sourceforge.net/projects/phpvirtualbox/files/phpvirtualbox-4.3-3.zip
Extract the zip file using the following command:
unzip phpvirtualbox-4.3-3.zip
Move the extracted folder to your apache root folder.
mv phpvirtualbox-4.3-3 /var/www/html/phpvirtualbox

Configure phpVirtualBox

Edit phpVirtualBox config.php file:
cp /var/www/html/phpvirtualbox/config.php-example /var/www/html/phpvirtualbox/config.php
vi /var/www/html/phpvirtualbox/config.php
Change the username/password for system user that runs VirtualBox. In our case, the username/password is sk_vuser/centos.
[...]
var $username = 'sk_vuser';
var $password = 'centos';
[...]
Save and close the file.
Create a file called /etc/default/virtualbox:
vi /etc/default/virtualbox
Append the following line:
VBOXWEB_USER=sk_vuser
Now, start vboxweb-service, and make it to start automatically on every reboot.
/etc/init.d/vboxweb-service start
chkconfig vboxweb-service on
I don’t want to messing around with iptables and SElinux. So, I simply disabled them both.
In CentOS 7.x:
systemctl disable firewalld
systemctl stop firewalld
In CentOS 6.x:
service iptables stop
chkconfig iptables off
Disable SELinux:
vi /etc/sysconfig/selinux
Set SELINUX=disabled.
[...]
SELINUX=disabled
[...]
Reboot your system once to take effect all the changes.

Access phpVirtualBox Web console

Now, go to any client systems that has installed with GUI desktop environments, and try to access the URL http://ip-address-of-virtualbox-headless-server/phpvirtualbox from the web browser.
Now, try again the same URL mentioned above. The following screen should appear. Enter the username/password.
The default username/password is admin/admin.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_001
Now, you’ll be redirected to the phpVirtualBox main dashboard.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_002
From here, you can create, modify, delete, and manage virtual machines.

Creating sample Virtual Machine

Let us create a sample virtual machine to install Ubuntu 14.04.
Click on the New button on the top left corner. Enter the Virtual machine name.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_003
Set the Memory size for the virtual machine.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_004
Select “Create a virtual hard drive now” option.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_005
Select hard drive file type.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_006
Choose whether the new virtual hard drive file should grow as it is used (dynamically allocated) or if it should be created at its maximum size (fixed size).
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_007
Enter the size of the virtual hdd.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_008
That’s it. The virtual machine has been created. Start installing the OS, and use the virtual machine.
phpVirtualBox - VirtualBox Web Console - Mozilla Firefox_009
phpVirtualBox tool will definitely help the system administrators who have only headless CentOS server, and also the installation is not that difficult. So, You can easily run virtual machines as many as your hardware supports from your headless CentOS server.
References:
Cheers!

No comments: