Tag Archives: kvm

Quick copy & paste how-to: Install KVM on CentOS 6


CentOS 6.7 64bit
ISO: ftp://ftp.hosteurope.de/mirror/centos.org/6.7/isos/x86_64/CentOS-6.7-x86_64-netinstall.iso

URL for netinstall: ftp://ftp.hosteurope.de/mirror/centos.org/6.7/os/x86_64

LVM vg: 100%

Host: lv_root / 200 GB

vi /etc/sysconfig/selinux
SELINUX=disabled

yum install wget screen lynx vim kvm libvirt python-virtinst qemu-kvm ncftp tigervnc tigervnc-server system-config-firewall-tui xterm twm bridge-utils virt-manager openssh-askpass openssh-clients dbus-python dbus

yum groupinstall Desktop

yum update

/etc/init.d/messagebus start

system-config-firewall-tui
-> disable firewall

vi /etc/sysconfig/vncservers
VNCSERVERS="10:root"
VNCSERVERARGS[10]="-geometry 1024x768"

vi /etc/ssh/sshd_config
Port 450
PermitRootLogin without-password

adduser admin
passwd admin

chkconfig rpcidmapd off
chkconfig rpcgssd off
chkconfig nfslock off
chkconfig netfs off
chkconfig rpcbind off
chkconfig dnsmasq off
chkconfig NetworkManager off
chkconfig messagebus off

vncpasswd

vi /root/.vnc/xstartup

#!/bin/sh
[ -r /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n
export LANG
export SYSFONT
vncconfig -iconic &
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
OS=`uname -s`
if [ $OS = 'Linux' ]; then
case "$WINDOWMANAGER" in
*gnome*)
if [ -e /etc/SuSE-release ]; then
PATH=$PATH:/opt/gnome/bin
export PATH
fi
;;
esac
fi
if [ -x /etc/X11/xinit/xinitrc ]; then
exec /etc/X11/xinit/xinitrc
fi
if [ -f /etc/X11/xinit/xinitrc ]; then
exec sh /etc/X11/xinit/xinitrc
fi
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &

chmod 755 /root/.vnc/xstartup

vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
# BROADCAST=x.x.x.x
# DNS1=x.x.x.x
# GATEWAY=x.x.x.x
HWADDR=00:25:90:38:66:c6
# IPADDR=x.x.x.x
# NETMASK=x.x.x.x
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
# DNS2=x.x.x.x
IPV6INIT=no
USERCTL=no
BRIDGE=br0

vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=none
ONBOOT=yes
DELAY=0
NM_CONTROLLED=no
BROADCAST=x.x.x.x
DNS1=x.x.x.x
GATEWAY=x.x.x.x
IPADDR=x.x.x.x
NETMASK=x.x.x.x
DNS2=x.x.x.x
IPV6INIT=no
USERCTL=no

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

In /etc/postfix/main.cf:
inet_protocols = ipv4

reboot

Add hostname to /etc/hosts

/etc/init.d/vncserver start

VNC connect to host:10

virt-manager:

localhost ->

1. Virtual Networks -> default -> Basic details: device: (or virbr0), Autostart: unchecked -> Apply

2. Storage -> + (add) -> logical, Name “vm-lvm” -> Build Pool: unchecked -> Source Name: name of existing LVM volume group -> Finish

Done!

KVM: How to resize (grow) an ext4 LVM VM

We use LVM both on the guest and the CentOS KVM host. Let’s upgrade from 5 GB to 15 GB on the guest.

1. Shutdown the VM.

2. On the KVM host, take a backup of the VM LV in case something goes wrong.

[root@host ~]# dd if=/dev/vg_host01/lv_guest01-vm of=./lv_guest01-vm-5gb

3. On the KVM host, resize the VM LV.

Add 1 GB:

[root@host ~]# lvresize -L+1G /dev/vg_host01/lv_guest01-vm

Or add all available space from the VG:

[root@host ~]# lvresize -l+100%FREE /dev/vg_host01/lv_guest01-vm

4. Boot up the VM again.

5. Delete and recreate the LVM partition.

root@guest:~$ parted /dev/vda
(parted) unit s
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 30720000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 2048s 1026047s 1024000s primary ext4 boot
2 1026048s 10239999s 9213952s primary lvm

(parted) rm 2
Warning: WARNING: the kernel failed to re-read the partition table on /dev/vda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.

Exit parted. You can also use fdisk to delete the partition, but when recreating the partition, fdisk doesn’t know about the right first and last cylinders. Maybe exiting and running fdisk again solves that (forgot to check that, we’ll try that next time) – UPDATE: on another try with fdisk it worked, no need for parted. You can do everything with fdisk in one run, just don’t exit it and use the start cylinder from the deleted partition. It will know the end cylinder automatically. Ok, so we deleted the partition using parted and now we start fdisk again to recreate the partition. Also, for some reason, the first partition (1) is now just half in size, 512 MB instead of 1024 MB. Weird. parted messed something up there?

root@guest:~$ fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/vda: 15.7 GB, 15728640000 bytes
222 heads, 30 sectors/track, 4612 cylinders
Units = cylinders of 6660 * 512 = 3409920 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000831eb

Device Boot Start End Blocks Id System
/dev/vda1 * 1 155 512000 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (1-4) p
Partition number (1-4): 2
First cylinder (155-4612, default 155):
Using default value 155
Last cylinder, +cylinders or +size{K,M,G} (155-4612, default 4612):
Using default value 4612

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/vda: 15.7 GB, 15728640000 bytes
222 heads, 30 sectors/track, 4612 cylinders
Units = cylinders of 6660 * 512 = 3409920 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000831eb

Device Boot Start End Blocks Id System
/dev/vda1 * 1 155 512000 83 Linux
/dev/vda2 155 4612 14844936 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

6. Reboot the VM.

7. Resize the PV.

Let’s look at the PV first:

root@guest:~$ pvdisplay
— Physical volume —
PV Name /dev/vda2
VG Name vg_guest01
PV Size 4.39 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 1124
Free PE 0
Allocated PE 1124
PV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R

Still 5 GB. Let’s resize it:

root@guest:~$ pvresize /dev/vda2
Failed to read physical volume “/dev/vda2”
0 physical volume(s) resized / 0 physical volume(s) not resized

Don’t worry about the “not resized”. It worked:

root@guest:~$ pvdisplay
— Physical volume —
PV Name /dev/vda2
VG Name vg_guest01
PV Size 14.16 GiB / not usable 1016.00 KiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3624
Free PE 2500
Allocated PE 1124
PV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R

8. Resize the LV.

root@guest:~$ lvresize -l+100%FREE /dev/vg_guest01/lv_root

Check it:

root@guest:~$ lvdisplay /dev/vg_guest01/lv_root
— Logical volume —
LV Path /dev/vg_guest01/lv_root
LV Name lv_root
VG Name vg_guest01
LV UUID 3Qfe2E-5j5H-H8el-iwxt-oN81-DvYq-AzENqH
LV Write Access read/write
LV Creation host, time guest01, 2013-03-02 00:41:16 +0100
LV Status available
# open 1
LV Size 14.03 GiB
Current LE 3592
Segments 2
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0

9. Resize the ext4 FS.

root@guest:~$ resize2fs /dev/vg_guest01/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_guest01/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg_guest01/lv_root to 3678208 (4k) blocks.
The filesystem on /dev/vg_guest01/lv_root is now 3678208 blocks long.

10. Reboot the VM.

11. Done.

root@guest:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_guest01-lv_root
14G 1.2G 12G 9% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 485M 53M 408M 12% /boot