Sunday, September 22, 2013

CentOS 6.4 on Hyper-V

Sivaprasad Padisetty (Sept 2013)

Hyper-V now has excellent support to run Linux as a guest. In this short blog, I will walk you through setup of CentOS 6.4 with Hyper-V (Windows 8.1, Windows Server 2012 R2).
 

Install Hyer-V role 

On Windows Server 2012 R2, use Server Manager to install the Hyper-V Role.
On Windows 8.1, goto control panel, “Turn Windows features on or off”, install Hyper-V (including management tools)
Now you have the Hyper-V manager to create VM and manage VMs.
 

Minimal CentOS 6.4 

Download the appropriate ISO image from http://wiki.centos.org/Download. Use CentOS-6.4-x86_64-minimal.iso to build the minimal CentOS server.
Create a network switch in the Hyper-V, mark it external and select the network card that is connected to the internet. Call this switch name as “ExternalSwitch”. Internet connectivity is required to download packages from the internet.
To create a VM (Virual Machine) from the Hyper-V manager, start the VM create Wizard. Chose following values.
  • Specify Name and Location: “CentOS6.4”
  • Specify Generation: Generation 1 which is the default
  • Assign Memory : 1024 MB
  • Configure Networking: Connection =  “ExternalSwitch”, this gives internet connection to the VM
  • Connect Virtual Hard Drive = Chose “Create a virtual hard drive” and select a VHDX file name such as C:\VHDBase\CentOS64.vhdx
  • Installation Options: Chose “Install an operating system from a bootable CD/DVD-ROM”, the point to the minimal ISO image downloaded earlier.
  • Finish, Connect to this VM from Hyper-V manager and start it.

This starts the CentOS 6.4 installation, follow on screen installation instructions. It will ask you to choose language, timezone, root password, hostname etc. Leave the default network setting for eth0 as DHCP. Make the following changes, so that the interface is started on boot.
 
vi /etc/sysconfig/network-scripts/ifcfg-eth0 # change  ONBOOT from "no" to "yes"

reboot

now you should have network connection. For troubleshooting, the following command can be handy. This displays the kernel log messages.
 
dmesg | grep -i eth


Prepare VHD so that it is clonable

CentOS associates MAC address with eth0, when the VM is cloned it will get a new MAC address (this is the default behavior in Hyper-V). The cloned VM sees the new MAC address, which is different from the one assigned to eth0 originally, so it creates a new interface eth1. Since this is not the first interface, we only have one connection, there won’t be any network connection. Make the following changes to remove MAC association.
 
vi /etc/sysconfig/network-scripts/ifcfg-eth0 # delete the line "HWADDR=…". This removes the association with specific MAC address

vi /etc/udev/rules.d/70-persistent-net.rules # delete all the lines. This is where CentOS (udev) keeps track of new devices it sees. Deleting eth0 from here, means next time udev sees new MAC address it will be assigned to eth0

Apply updates for the installed packages
 
yum list updates
yum update

This constitutes the base VHD with minimal setup. Either clone this VHD to a new one or use that to do further work, or create a differencing disk off of this one. Differencing disk has the delta from the base image. To shutdown and turn the power down
 
shutdown -h 0

Installing Linux Integration Services

Download the latest Linux Integration Services for Hyper-V. The one I used is 3.4, however this does not officially support CentOS 6.4 (only supports 6.3). Hopefully a new update will be available soon. Download the ISO, edit the VM from Hyper-V manager, point the CD Drive to this ISO. After the VM boots, login as root, run the following commands.
 
 mkdir -p /mnt/cdrom
 mount /dev/cdrom /mnt/cdrom
 cd /mnt/cdrom/RHEL63
 ./install.sh
 umount /mnt/cdrom

Installing desktop

Some yum commands, to explore the list of packages and to install desktop. Using group based view is better, as packages are logically grouped, it will bring in all the dependencies
 
yum -v grouplist # list of groups, -v option is for version, displays short name.

#install destop/browswer
yum groupinstall basic-desktop general-desktop internet-browser fonts

Once the desktop is installed, the default run level should be changed so that desktop is launched after reboot.
 
vi /etc/inittab  #      change 'id:3:initdefault' to 'id:5:initdefault'
reboot

 

Development

Install the development environment.
 
yum groupinstall development

Create a file hello.c
 
#include

int main()
{
printf (“Hellow World\n”);
      return 0;
}

Compile and run
 
gcc hello.c
./a.out # run the executable, a.out is the default executable name

Create a file hello.cpp
 
#include
using namespace std;

int main()
{
cout << "Hello world!" << endl;
      return 0;
}

Compile and run
 
g++ hello.cpp -o hello  # executable is hello instead of a.out
./hello # run the executable

To use gdb, compile with “-g” option. “gdb executable”, will run the program in debug mode. You set the breakpoint, display variables, etc. “gdb –tui executable” will display the source in a primitive window.
Installing Eclipse, which is a nice IDE for Java, C, C++ etc
 
yum groupinstall Eclipse
eclipse # launch eclipse

Explore & Enjoy!
/Siva

No comments: