Friday, May 17, 2013

Setting up a KVM virtual machine in LVM (Logical Volume)



1. Install Ubuntu Server 12.04 64bit (Total Hard Disk Size = 200GB and 4GB RAM)
Hostname: U-HOST
Partitioning method: Manual
Create #1 primary 30.0GB f ext4 /
Create #2 primary 8.0GB f swap swap
Create #3 primary 176.7GB k lvm
Choose software to install: [*] OpenSSH server
Install the GRUB boot loader to the master boot record? <Yes>



2. After the base server installed, do the update
root@U-HOST:/iso#apt-get update
root@U-HOST:/iso#apt-get dist-upgrade



3. Install packages
root@U-HOST:/iso#apt-get install ubuntu-virt-server python-vm-builder kvm-ipxe



4. Add the current user to the group libvirtd
root@U-HOST:/iso#adduser 'root' libvirtd
root@U-HOST:/iso#adduser 'root' kvm

To check if KVM has successfully been installed, run
root@U-HOST:/iso#virsh -c qemu:///system list

it should display something like this:

root@U-HOST:~# virsh -c qemu:///system list
Id Name State
----------------------------------



5. Install bridge-utils... (In Ubuntu 12.04, this is package is already installed)
root@U-HOST:/iso#apt-get install bridge-utils




6. configure a bridge,
root@U-HOST:/iso#nano /etc/network/interfaces
Before the modification, it file is looks as follows:

# This file describes the network interfaces available on your system
 # and how to activate them. For more information, see interfaces(5).

 # The loopback network interface
 auto lo
 iface lo inet loopback

 # The primary network interface
 auto eth0
 iface eth0 inet dhcp



After modification, it will looks as follows,

# This file describes the network interfaces available on your system
 # and how to activate them. For more information, see interfaces(5).

 # The loopback network interface
 auto lo
 iface lo inet loopback

 # The primary network interface
 auto eth0
 iface eth0 inet manual


 auto br0
 iface br0 inet static
        address 192.168.122.21
        network 192.168.122.0
        netmask 255.255.255.0
        broadcast 192.168.122.255
        gateway 192.168.122.1
        dns-nameservers 8.8.8.8 8.8.4.4
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off


After the modification of 'interface' file, restart networking service,
root@U-HOST:/iso#/etc/init.d/networking restart


It should now show the network bridge (br0):
root@U-HOST:/iso#ifconfig

it will show following,

br0       Link encap:Ethernet HWaddr 52:54:00:14:81:b7
          inet addr:192.168.122.21 Bcast:192.168.122.255 Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe14:81b7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          RX packets:152 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:12428 (12.4 KB) TX bytes:10608 (10.6 KB)

eth0     Link encap:Ethernet HWaddr 52:54:00:14:81:b7
          inet6 addr: fe80::5054:ff:fe14:81b7/64 Scope:Link
            UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
         RX packets:157 errors:0 dropped:0 overruns:0 frame:0
         TX packets:83 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
         RX bytes:14796 (14.7 KB) TX bytes:10962 (10.9 KB)

lo        Link encap:Local Loopback
         inet addr:127.0.0.1 Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING MTU:16436 Metric:1
         RX packets:16 errors:0 dropped:0 overruns:0 frame:0
         TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:1184 (1.1 KB) TX bytes:1184 (1.1 KB)


Reboot the server now.



7. Create the Physical Volume
#apt-get install lvm2


Check the partitions by run,

#cat /proc/partitions

It should looks like this,

root@U-HOST:~# cat /proc/partitions
major minor #blocks name

253 0 209715200 vda
253 1 29295616 vda1
253 2 7812096 vda2
253 3 172605440 vda3
11 0 1048575 sr0

The 'vda3' is the lvm partition, you can check it by this,
root@U-HOST:/iso#fdisk -l

Disk /dev/vda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders, total 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006e7b2

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 58593279 29295616 83 Linux
/dev/vda2 58593280 74217471 7812096 82 Linux swap / Solaris
/dev/vda3 74217472 419428351 172605440 8e Linux LVM

Create physical volume
root@U-HOST:~# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created 
 
Create volume group
root@U-HOST:~# vgcreate vgpool /dev/vda3
Volume group "vgpool" successfully created 
 
”vgpool” can be replace with any name you want


Create logical volume
root@U-HOST:~# lvcreate -L 30G -n win7 vgpool
Logical volume "win7" created 
 
30G is the partition size, win7 is the logical volume name, vgpool is the volume group that created from previous step




8. Now you can install virtual machine from command line

Install virtinst by run,
root@U-HOST:/iso#apt-get install virtinst


I am going to use a Windows 7 ISO file where is located at
/iso/Windows.7.SP1.ENG.x86-x64.MAFIAA.iso


Install the Windows 7 Virtual Machine by running following command,


root@U-HOST:/iso#virt-install --connect qemu:///system --name=win7vm --ram=2048 --disk path=/dev/vgpool/win7 --cdrom=/iso/Windows.7.SP1.ENG.x86-x64.MAFIAA.iso --network bridge=br0,model=virtio --os-type=windows --noautoconsole --graphics vnc,listen=0.0.0.0,port=5901 –hvm

Starting install...
Creating domain... | 0 B 00:01
Domain installation still in progress. Waiting for installation to complete.



9. Install an virtual machine via “Virtual Machine Manager”
You can install “Virtual Machine Manager” from Ubuntu Software Centre on a Ubuntu Desktop Then, connect to

In “Virtual Machine Manager”, go to File -> Add Connection


























Type “yes” in following windows
















 

In Nex Window, type in the user's password


















After enter the password, you will see the “win7vm” in “Virtual Machine Manager”
  


 

Double click the virtual machine “win7vm”, it will show you the virtual machine status,
You can finish the Windows 7 installation by using Virtual Machine Manager
  
















No comments:

Post a Comment