内部机器空闲,支持虚拟化,正好拿来划几台测试机使用。过程如下:
========================
磁盘划分:
一、采用lvm划分磁盘空间
[root@localhost ~]# fdisk -l
Disk /dev/sda: 292.0 GB, 291999055872 bytes
255 heads, 63 sectors/track, 35500 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4676a26d
[root@localhost ~]# fdisk /dev/sda
创建主分区:
n
p
更改分区格式为lvm类型:
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
w
[root@localhost ~]# partprobe
[root@localhost ~]# mkfs.ext4 /dev/sda 同步到磁盘完成并格式化
[root@localhost ~]# vgcreate volgroup00 /dev/sda4 创建vg名为volgroup00
[root@localhost ~]# vgdisplay 查看已创建的组
[root@localhost ~]# lvcreate -L 220G -n lvData volgroup00 从VG中划分一个LV
创建了一个名字为lvData,容量大小是220G的分区,其中:-L:指定LV的大小 -n:指定LV的名。volgroup00:表示从这个VG中划分LV;
[root@localhost ~]#lvdisplay
[root@localhost ~]# mkfs.ext4 /dev/volgroup00/lvData
[root@localhost ~]# mkdir /lvmdata
[root@localhost ~]# mount /dev/volgroup00/lvData /lvmdata
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon May 19 15:52:39 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=20d7244f-7ab6-4e62-9d90-f7b2833bff9c / ext4 defaults 1 1
UUID=217279dc-d2dd-48f2-b2ff-38f46c1fa067 /home ext4 defaults 1 2
UUID=4fa3df21-f1bd-433b-bb57-7ef802a18eda swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/volgroup00/lvData /lvmdata ext4 defaults 1 2
========================
KVM环境安装:
1. 环境准备
需要BIOS开启虚拟化支持cpu-->virtual.... enabled
egrep '(vmx|svm)' --color=always /proc/cpuinfo 验证查看物理机(主要是CPU)是否支持虚拟机技术
#more /etc/selinux/config
SELINUX=disabled
[root@localhost lvmdata]# vi /etc/sysctl.conf
[root@localhost lvmdata]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@localhost lvmdata]#yum install kvm virt-manager libvirt libvirt-python python-virtinst libvirt-client python-virtinst libvirt libvirt-client qemu-kvm
[root@localhost lvmdata]#yum install bridge-utils libvirt
bridge-utils网桥工具安装
libvirt 就是管理工具(服务需手动启动)
[root@localhost lvmdata]#yum install libguestfs-tools工具安装相关virsh管理工具
[root@localhost lvmdata]# chkconfig libvirtd on
[root@localhost lvmdata]# service libvirtd start
[root@localhost lvmdata]#vi /etc/libvirt/libvirtd.conf
listen_addr = "192.168.1.1"
[root@localhost lvmdata]#/etc/init.d/libvirtd restart
[root@qa228 lvmdata]# modprobe kvm
[root@qa228 lvmdata]# modprobe kvm-intel
[root@localhost lvmdata]# lsmod |grep kvm 检查kvm模块是否加载
kvm 316506 0
说明:
虚拟机镜像的默认位置在/var/lib/libvirt/images/文件夹下,下面我们这里存放在刚刚划分好的/lvmdata目录下。
虚拟机的xml配置文件默认在/etc/libvirt/qemu/目录下
桥接模式配置网络环境来安装虚拟机:
[root@localhost lvmdata]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@localhost qemu]# more /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:15:17:19:AF:BE
IPADDR=192.168.1.227
NETMASK=255.255.0.0
GATEWAY=192.168.1.1
TYPE=Ethernet
BRIDGE=br0
UUID=6c6e7237-97cc-41e4-b273-c21c0b2054c0
ONBOOT=yes
NM_CONTROLLED=yes
#BOOTPROTO=dhcp
[root@localhost qemu]# more /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
HWADDR=00:15:17:19:AF:BE
IPADDR=192.168.1.227
NETMASK=255.255.0.0
GATEWAY=192.168.1.1
TYPE=Bridge
UUID=6c6e7237-97cc-41e4-b273-c21c0b2054c0
ONBOOT=yes
NM_CONTROLLED=yes
#BOOTPROTO=dhcp
/etc/init.d/network restart
[root@qa228 lvmdata]# brctl show 查看桥接情况
bridge name bridge id STP enabled interfaces
br0 8000.0015172449bc no eth0
vnet0
virbr0 8000.5254007815c0 yes virbr0-nic
2. 开始准备安装虚拟机,修改xmanager连接的密码:
[root@localhost lvmdata]#vi /etc/libvirt/qemu.conf
vnc_listen = "0.0.0.0"
vnc_password = "123456"
[root@localhost lvmdata]#export DISPLAY=192.168.1.34:0.0 定义xmanager,用来远程安装的windows机器
[root@localhost lvmdata]#virt-install --name=test03 --ram 2048 --vcpus=2 --disk path=/lvmdata/test03.img,size=30 --vnc --accelerate --cdrom /lvmdata/CentOS-6.4-x86_64-bin-DVD1.iso --network bridge=br0,model=virtio --force --autostart --k en-us
-bash: virt-viewer: command not found
virt-viewer未安装好.
[root@localhost lvmdata]#yum install qemu-kvm-tools virt-v2v virt-viewer
[root@localhost lvmdata]#export DISPLAY=192.168.1.34:0.0
[root@localhost lvmdata]#virt-viewer -c qemu:///system test03 通过Xmanager远程连接进入图形化界面安装
第二种安装虚拟机方式采用qemu先创建qcow2格式的镜像文件:
qemu-img create -f qcow2 /lvmdata/test03.img 30G
qemu-img info test03.img
常用的格式为raw和qcow2,推荐使用qcow2格式。
raw格式:可以简单、容易地导出到其它模拟器中,但是立即分配占用空间大。
qcow2格式:是qcow格式的升级版本,是目前最万能的格式。使用它可获得较小映像,也是虚拟池一直在使用的镜像格式,支持镜像快照,方便的恢复管理。
export DISPLAY=192.168.1.34:0.0
virt-install --name test03 --ram=2048 --arch=x86_64 --vcpus=2 --check-cpu --os-type=linux --os-variant='rhel6' -c /lvmdata/CentOS-6.4-x86_64-bin-DVD1.iso --disk path=/lvmdata/test03.img,device=disk,bus=virtio,format=qcow2 --bridge=br0 --noautoconsole
再通过xmanager连接
================kvm虚拟机的克隆分为两种情况=========================
一、本机虚拟机直接克隆:
[root@localhost lvmdata]# virsh destroy dev01 先关闭被克隆虚拟机dev01
[root@localhost lvmdata]# virt-clone -o dev01 -n test01 -f test01.img 克隆出test01镜像文件
[root@localhost lvmdata]## vi /etc/libvirt/qemu/test01.xml 可更改虚拟机cpu,内存配置
[root@localhost lvmdata]# virsh start test01 启动新虚拟机test01
连接虚拟机两种方式:
1.通过xmanager连接修改主机名,IP,参照生成的虚拟机xml文件来更改UUID:
[root@localhost lvmdata]# export DISPLAY=192.168.1.34:0.0
[root@localhost lvmdata]# virt-viewer -c qemu:///system test01
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/hosts
/etc/sysconfig/network
网卡重启时提示找不到eth0网卡,报错如下:Device eth0 does not seem to be present,delaying initialization FAILED
vi /etc/udev/rules.d/70-persistent-net.rules
修改将eth0注释掉,eth1改为eth0
重启网络和服务器OK
2.使用console登录虚拟机
[root@centoskvm img]# virsh console test01
若出现无法从console登录的情况,会卡在“Escape character is ^]”处无法继续。登录linux虚拟机控制台需设置如下:
1)添加ttyS0的安全许可,允许root登录:
[root@1001vm img]# echo "ttyS0" >> /etc/securetty
2)在/etc/grub.conf文件中为内核添加参数:console=ttyS0
[root@localhost ~]# cat /etc/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-358.el6.x86_64)
root (hd0,0)
kernel /boot/vmlinuz-2.6.32-358.el6.x86_64 ro root=UUID=20d7244f-7ab6-4e62-9d90-f7b2833bff9c rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet console=ttyS0
initrd /boot/initramfs-2.6.32-358.el6.x86_64.img
3)在/etc/inittab中添加agetty:
S0:12345:respawn:/sbin/agetty ttyS0 115200
[root@localhost qemu]# tail /etc/inittab
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
S0:12345:respawn:/sbin/agetty ttyS0 115200
重启客户虚拟机。再次尝试console命令可以正常使用。
[root@localhost img]# virsh console test01
Connected to domain test01
Escape character is ^]
[ OK ]
Setting hostname ...........
修改主机名,IP,参照生成的虚拟机xml文件来更改UUID
二、异地物理机间虚拟机克隆:
1)原物理机上关闭被克隆虚拟机
2)导入kvm虚拟机配置文件
virsh dumpxml test01 > /etc/libvirt/qemu/test02.xml
3)复制kvm虚拟机磁盘文件
cp test01.img test02.img
4)将img与xml文件拷贝至新物理机上
直接编辑修改配置文件test02.xml
更改内存,cpu,img镜像文件路径.
<domain type='kvm'>
<name>test02</name>
<uuid>437ec2c9-d3e7-421e-0b9f-1e54f4f5b3cd</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='rhel6.4.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/lvmdata/test02.img'/>
[root@qa228 lvmdata]# virsh define /etc/libvirt/qemu/test02.xml 定义新虚拟机配置文件
Domain dev01 defined from /etc/libvirt/qemu/test02.xml
[root@qa228 lvmdata]# virsh list --all
Id Name State
----------------------------------------------------
- test02 shut off
[root@qa228 lvmdata]# virsh start test02
Domain test02 started
用xmanager连接登录进行主机名,IP等修改.
[root@qa228 lvmdata]# export DISPLAY=192.168.1.34:0.0
[root@qa228 lvmdata]#virt-viewer -c qemu:///system test02
虚拟机相关管理命令:
virsh destroy test02 关机
virsh undefine test02 删除
qemu-img info test02.img 查看镜像文件的信息