Popular Posts

Friday, 23 May 2014

Kernel Virtual Machine

        
Virtual Machine
1.  Creating environment for virtualization:­
a) Install the RHEL FOR 64 Bit.
b) Now check the virtualization flag by running the command
[root@example ~]# egrep '(vmx|svm)' ­­color=always /proc/cpuinfo
c) To install KVM and virtinst (a tool to create virtual machines), run the command
[root@example ~]# yum install  qemu­kvm qemu* libvirt* python­virtinst*
d) Then start the libvirt daemon:
[root@example ~]# /etc/init.d/libvirtd start
e) To check if KVM has successfully been installed, run
[root@example ~]# virsh ­c qemu:///system list
( It should display something like this )
Id Name
State
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
2. For graphical installation of vm :­
[root@example ~]# virt­manager
( It is a interface for managing virtual machines. It presents a summary view of running domains,
their live performance & resource utilization statistics. The detailed view graphs performance &
utilization over time.)
Then create new machine by clicking on create a new virtual machine and then entering the
following details :­
a) Name
b) iso path
c) Choosing the Ram and CPU settings
d) Size of disk image on Hard Drive
New Vm will be created.
Error :­  If virt­manager command is not working then run the command
[root@example ~]# dbus­launch ­­exit­with­session virt­manager
3. Textual installation of vm:­
For installing virtual machine by command type:­
For centos/rhel :­
virt­install ­ncentos7 ­r 1024  ­­vcpus=1 ­­disk path=/var/lib/libvirt/images/centos7.img,size=20
­­bridge=virbr1 ­l ftp://192.168.122.1/pub/centos6.3/ ­x 'console=tty0 console=ttyS0,115200n8'
Here,
a) virt­install ­   Command for installing virtual machine
b) ­ncentos7  ­  n is parameter for name and centos7 is name of the vm to be created
c) ­r 1024    ­     Ram of size 1 Gb is allocated to vm
d) ­­disk      ­      Specifies media to use as storage for the guest, with various options:­
  i) path=/var/lib/libvirt/images/centos7.img ­    It is the path on disk where image of this vm is
stored
 ii) size=20   ­    Size of 20 Gb is allocated to this vm
e) ­­bridge=virbr1     ­    Connect to a bridge device in the host
f) ­l ftp://192.168.122.1/pub/centos6.3/    ­   It specifes the location where iso is mounted. Here
centos6.3 iso is mounted on /var/ftp/pub/centos6.3/ and it is ftp path. We can also specify :­
  i) nfs path
 ii) http path
iii) Path to the local directory where iso is mounted
g) ­x 'console=tty0 console=ttyS0,115200n8'   ­   x specifies the extra argument. It is used only
with ­l option. It specifies the serial console kernel parameter which is responsible for textual
installation
For ubuntu:­
/usr/sbin/virt­install ­ncentos7 ­r 1024  ­­vcpus=1  ­­disk
path=/var/lib/libvirt/images/centos7.img,size=20 ­­bridge=virbr1 ­l
http://ca.archive.ubuntu.com/ubuntu/dists/precise/main/installer­amd64/ ­x 'console=tty0
console=ttyS0,115200n8'
We use http location for ubuntu because when we mount the iso virt­inst doesn’t find installer
and give the error:
valueerror could not find an installable distribution at location
4. Assigning IPtables rules to access vm from remote machine using different ssh port which
redirect the query to the particular VM which is define in iptables rule.
To write iptables rule we must know the  mac address. For that 1st time we have to take console
login by typing command
[root@example Downloads]#  Virsh console vmname
Where, vmname is the name given at installation time. Here it is centos7.
Give the username and password entered at installation time
After login note the mac address  and then write init0 as we can exit from console by shutting it
off
Error:­ if after login it doesn’t take ip and mac then check ethtool settings
Now we are on base machine
a) Create a directory /etc/libvirt/hooks
Create qemu file in it /etc/libvirt/hooks/qemu
( This file is executed when a QEMU guest is started, stopped )

b) For vm named centos7 file /etc/libvirt/hooks/centos7  is created which will contain  :­
********************************************************************************************************
Guest_name=centos7
Host_port=
Guest_ipaddr=192.168.122.7
Guest_port=22
if [ "\$1" = "\$Guest_name" ]; then
if [ "\$2" = "stopped" ­o "\$2" = "reconnect" ]; then








iptables ­t nat ­D PREROUTING ­p tcp ­­dport \$Host_port ­j DNAT \
­­to \$Guest_ipaddr:\$Guest_port
iptables ­D FORWARD ­d \$Guest_ipaddr/32 ­p tcp ­m state ­­state NEW \
­m tcp ­­dport \$Guest_port ­j ACCEPT
fi
if [ "\$2" = "start" ­o "\$2" = "reconnect" ]; then
iptables ­t nat ­A PREROUTING ­p tcp ­­dport \$Host_port ­j DNAT \
­­to \$Guest_ipaddr:\$Guest_port
iptables ­I FORWARD ­d \$Guest_ipaddr/32 ­p tcp ­m state ­­state NEW \
­m tcp ­­dport \$Guest_port ­j ACCEPT
fi
fi

********************************************************************************************************
where,
i) Guest_name=centos7    ­­­   Name of the vm
ii)Host_port=2207    ­­­  the port by which it will be accessed from remote machine
iii)Guest_ipaddr=192.168.122.7  ­­ vm’s ip address
Every vm will have separate file including the above data with the name vmname  in
/etc/libvirt/hooks/ which will have executable permission.
Note:­ But for the first vm we have to do entry in qemu file then after this new file will be created
with vmname in /etc/libvirt/hooks/vmname and its source will be defined in qemu file like this:­
­­­> At the end in file  /etc/libvirt/hooks/qemu add this line when any new vm is created
 source  /etc/libvirt/hooks/centos7  ( This is setting for vm named centos7 )
c) Virsh settings
[root@example Downloads]#  virsh net­destroy default
[root@example Downloads]#  virsh net­edit default
Insert this line :­
<host mac='52:54:00:34:11:54' name='centos7’' ip='192.168.122.7' />
Where, mac address is the address being noted and name is hostname and ip we can give by
our choice.
[root@example ~]# virsh net­start default
[root@example ~]# /etc/init.d/libvirtd restart
To see all the vm on base machine type
[root@example ~]# virsh list ­­all
To forcefully shutdown vm type
[root@example ~]# virsh destroy centos7
To start vm type
[root@example ~]# virsh start centos7
Now take the login from remote machine by entering command
[root@example ~]# ssh baseipaddress ­p2207 ­l username
By typing this command from remote machine this will launch us to vm by not connecting to
base machine
( Here base ipaddress is the base machine’s ip address and 2207 is port specified in vm file in
hooks )
5. Cloning of VM
Clone of any vm will make the cloned machine with the same hardware and software
configuration of the original.
→ Command for making clone machine vm2 of the original machine vm1:­
virt­clone ­­original vm1 ­­name vm2 ­­file /var/lib/libvirt/images/vm2.img
­­> We can also define the mac address which matches the range being assigned to machine
by typing command
virt­clone ­­original vm1 ­­name vm2 ­­file /var/lib/libvirt/images/vm2.img ­­mac 52:54:00:34:11:54
6. To change ram and cpu
virsh destroy test
virsh define /etc/libvirt/qemu/test.xml
virsh edit test
virsh start test
7. Script for creating vm clone of the 4 already created machines:­
#!/bin/bash
usage=" Usage: $(basename $0)  [­d <HostName>] [­t <template>]

[­r <ram>] [­c <vcpu>]







[­t ( 1. centos16 ( php version 5.2)
( 2. centos6 ( php version 5.3)
( 3. ubuntu12 ( php version 5.3)
( 4. ubuntu12­5.2 ( php version 5.2)
[­r (optional) need to pass this argument only if you need more ram than 1 gb]
[­c (optional) need to pass this argument only if you need more cpu than 1]
  Note:­ Two Options Are  Mandatory (­d && ­t )
  "
#echo $#
if [ $# ­lt 4 ];then
echo  "$usage"
exit
fi
while getopts hd:t:r:c: opts
do
case "$opts" in
h) echo "$usage"

exit

;;
d) hostname="$OPTARG"
;;
t) template="$OPTARG"

;;
r) ram="$OPTARG"

;;
c) vcpu="$OPTARG"
             ;;


esac
done
# For checking if vm with given name already exists
c=`virsh list ­­all | awk ­F" " '{print $2}'`
for v in $c
do
if [ $v = "$hostname" ];then
echo "vm with name $hostname already exists"
exit;
fi
done
# for checking if conf file already exists of the given hostname
a=`ls /etc/httpd/conf.d/ | awk ­F".conf" '{print $1}'`
for v in $a
do
if [ "$hostname" = "$v" ]; then
echo "site with name $hostname already exists"
exit;
fi
done
if [[ $template = "centos6" ||  $template = "centos16" || $template = "ubuntu12" || $template =
"ubuntu12­5.2" ]];then
mac=` awk 'NR == 1 {print $1}' /etc/vm/notused.txt`
# notused.txt file contains the unused mac address and ipaddress which are given to the cloned
machine
ipaddress=` awk 'NR == 1 {print $2}' /etc/vm/notused.txt `
# after using the addresses they are copied to used.txt
cat >> /etc/vm/used.txt << EOF
$mac, $ipaddress
EOF
port=`awk ­F"." 'NR == 1 {print $4}' /etc/vm/notused.txt`
# the used address is deleted from notused.txt
sed ­i '1d' /etc/vm/notused.txt
newport=22$port
/bin/touch
/bin/chmod
+x
/etc/libvirt/hooks/$hostname
/etc/libvirt/hooks/$hostname
cat >> /etc/libvirt/hooks/$hostname << EOF
Guest_name=$hostname
Host_port=$newport
Guest_ipaddr=$ipaddress
Guest_port=22
if [ "\$1" = "\$Guest_name" ]; then
if [ "\$2" = "stopped" ­o "\$2" = "reconnect" ]; then

iptables ­t nat ­D PREROUTING ­p tcp ­­dport \$Host_port ­j DNAT \

­­to \$Guest_ipaddr:\$Guest_port

iptables ­D FORWARD ­d \$Guest_ipaddr/32 ­p tcp ­m state ­­state NEW \

­m tcp ­­dport \$Guest_port ­j ACCEPT
fi
if [ "\$2" = "start" ­o "\$2" = "reconnect" ]; then

iptables ­t nat ­A PREROUTING ­p tcp ­­dport \$Host_port ­j DNAT \

­­to \$Guest_ipaddr:\$Guest_port

iptables ­I FORWARD ­d \$Guest_ipaddr/32 ­p tcp ­m state ­­state NEW \

­m tcp ­­dport \$Guest_port ­j ACCEPT
fi
fi
EOF
cat >> /etc/libvirt/hooks/qemu << EOF
source  /etc/libvirt/hooks/$hostname
EOF
/usr/bin/virsh destroy $template
/usr/bin/virt­clone ­­original $template ­­name $hostname ­­file
/var/lib/libvirt/images/$hostname.img ­­mac $mac
if [ ! ­z $vcpu ]; then
sed ­i "s/<vcpu>1<\/vcpu>/<vcpu>$vcpu<\/vcpu>/g" /etc/libvirt/qemu/$hostname.xml
fi
if [ ! ­z $ram  ]; then
sed ­i "s/<memory>1048576<\/memory>/<memory>$ram"048576"<\/memory>/g"
/etc/libvirt/qemu/$hostname.xml
sed ­i
"s/<currentMemory>1048576<\/currentMemory>/<currentMemory>$ram"048576"<\/currentMem
ory>/g" /etc/libvirt/qemu/$hostname.xml
fi
/usr/bin/virsh start $template
## creating virtual host file on base machine to transfer the request for $hostname to vm
/bin/touch /etc/httpd/conf.d/$hostname.conf
cat >> /etc/httpd/conf.d/$hostname.conf << EOF
<VirtualHost *:80>
ServerName $hostname.keenable.com
#ProxyRequests off
ProxyVia Full
ProxyPreserveHost On
ProxyRequests off
Proxypass /  http://$ipaddress:80/
ProxypassReverse  / http://$ipaddress:80/
</VirtualHost>
EOF
/etc/init.d/httpd reload
/usr/bin/virsh start $hostname
echo  "
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++"
echo   " NOTE:­ your machine has been created with portname ­ $newport and you can ssh
"
echo   " ssh 192.168.100.55 ­l vm ­p$newport  and passwd is vm
"
echo   " you can change password of vm sudo (user) after login
"
echo   "
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++"
else
echo "=============  ­t must have valid option ================"
echo
echo "$usage"
exit
fi
Running the script:­
sh script.sh ­d vmname ­t template
Here, vmname can be any name that you want to give and template is the already created vm to
be cloned.

No comments:

Post a Comment