一、环境
系统 CentOS6.4x64最小化安装
二、安装
#安装epel [[email protected] ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [[email protected] ~]# sed -i ‘[email protected]#[email protected]@g‘ /etc/yum.repos.d/epel.repo [[email protected] ~]# sed -i ‘[email protected]@#[email protected]‘ /etc/yum.repos.d/epel.repo
查看cpu是否支持kvm虚拟化
#查询有结果表明支持kvm虚拟化 [[email protected] ~]# egrep "vmx|svm" /proc/cpuinfo flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts tpr_shadow vnmi ept vpid #安装kvm模块 [[email protected] ~]# yum install qemu-kvm -y [[email protected] ~]# lsmod |grep kvm kvm_intel 53484 0 kvm 316506 1 kvm_intel [[email protected] ~]# ln -s /usr/libexec/qemu-kvm /usr/sbin/ #配置软连接 #安装管理工具 [[email protected] ~]# yum install virt-manager python-virtinst libvirt libvirt-python qemu-kvm-tools -y #启动libvirtd [[email protected] ~]# service libvirtd start Starting libvirtd daemon: [ OK ]
三、创建虚拟机
#创建一个大小10G的硬盘镜像文件 [[email protected] ~]# mkdir -p /data/kvm/{os,hosts} [[email protected] ~]# qemu-img create -f raw /data/kvm/hosts/vm1.raw 10G Formatting ‘/data/kvm/hosts/vm1.raw‘, fmt=raw size=10737418240 [[email protected] ~]# ll /data/kvm/hosts/vm1.raw -rw-r--r-- 1 root root 10737418240 Jul 28 17:50 /data/kvm/hosts/vm1.raw #查看磁盘信息 [[email protected] ~]# qemu-img info /data/kvm/hosts/vm1.raw image: /data/kvm/hosts/vm1.raw file format: raw virtual size: 10G (10737418240 bytes) disk size: 0 #从光盘复制安装文件到系统中 [[email protected] ~]# dd if=/dev/cdrom of=/data/kvm/os/centos64.iso 8502692+0 records in 8502692+0 records out 4353378304 bytes (4.4 GB) copied, 127.289 s, 34.2 MB/s [[email protected] ~]# ll -h /data/kvm/os/centos64.iso -rw-r--r-- 1 root root 4.1G Jul 28 18:00 /data/kvm/os/centos64.iso #创建网桥br0 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 BOOTPROTO=static ONBOOT=yes TYPE=Bridge IPADDR=192.168.1.16 NETMASK=255.255.255.0 [[email protected] ~]# ifup br0
使用virsh创建一台虚拟机
[[email protected] ~]# virt-install --virt-type kvm --name vm1 --ram 512 --cdrom=/data/kvm/os/centos64.iso --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel6 --disk path=/data/kvm/hosts/vm1.raw --network bridge=br0 Starting install... Creating domain... | 0 B 00:00 Domain installation still in progress. You can reconnect to the console to complete the installation process. [[email protected] ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 2459/qemu-kvm tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1340/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master tcp 0 0 :::22 :::* LISTEN 1340/sshd tcp 0 0 ::1:25 :::* LISTEN 1416/master #虚拟机的配置信息默认写在一个文件中 [[email protected] ~]# ll /etc/libvirt/qemu total 8 drwx------ 3 root root 4096 Jul 28 18:08 networks -rw------- 1 root root 3024 Jul 28 18:22 vm1.xml #对应的虚拟机
通过vnc连接进行虚拟机安装,过程略
安装完成后查看虚拟机
[[email protected] ~]# virsh list --all Id Name State ---------------------------------------------------- - vm1 shut off #关闭状态 #启动虚拟机vm1 [[email protected] ~]# virsh start vm1 Domain vm1 started [[email protected] ~]# virsh list #只能查看启动了的虚拟机 Id Name State ---------------------------------------------------- 2 vm1 running #虚拟机启动后vm1还处于监听状态 [[email protected] ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 24174/qemu-kvm tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1340/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master tcp 0 0 :::22 :::* LISTEN 1340/sshd tcp 0 0 ::1:25 :::* LISTEN 1416/master
使用vnc登陆到vm1,如图
kvm虚拟机默认是使用NAT方式来上网的,我们需要配置成桥接模式,配置过程请参考
http://www.chenyudong.com/archives/libvirt-kvm-bridge-network.html
下面给出修改后的网卡配置文件信息
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO="static" IPADDR=192.168.1.15 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ONBOOT="yes" TYPE="Ethernet" BRIDGE=br0 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 BOOTPROTO=static ONBOOT=yes TYPE=Bridge IPADDR=192.168.1.16 NETMASK=255.255.255.0 #调整内核参数,使桥接生效 [[email protected] ~]# sysctl -p net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 #主要就是这3条 #查看结果,能看到br0已经桥接到eth0上了 [[email protected] ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c294ebc48 no eth0 vnet0
在kvm虚拟机中查看先关信息如下,且能ping通外网
kvm虚拟机默认不支持virsh shutdown domain-name的方式关闭虚拟机,需要安装acpid,并启动服务
在kvm虚拟机中执行如下2条命令
#kvm虚拟机中的操作 yum install acpid -y service acpid start #宿主机中的操作 [[email protected] ~]# virsh list Id Name State ---------------------------------------------------- 2 vm1 running [[email protected] ~]# virsh shutdown vm1 Domain vm1 is being shutdown [[email protected] ~]# virsh list Id Name State ---------------------------------------------------- 2 vm1 running [[email protected] ~]# virsh list Id Name State #结果显示虚拟机已经被关系了 ---------------------------------------------------- #再重新启动虚拟机 [[email protected] ~]# virsh list --all Id Name State ---------------------------------------------------- - vm1 shut off [[email protected] ~]# virsh start vm1 Domain vm1 started [[email protected] ~]# virsh list Id Name State ---------------------------------------------------- 3 vm1 running
四、为kvm虚拟机添加console功能
1、添加ttyS0的安全许可,允许root登录:
# echo "ttyS0" >> /etc/securetty
2、在/etc/grub.conf文件中为内核添加参数:
console=ttyS0
这步要注意:console=ttyS0一定要放在kernel这行中(大约在第16行),不能单独一行,即console=ttyS0是kernel的一个参数,不是单独的,如下(往右拉进度条,在最后):
cat -n /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/vda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0 initrd /initramfs-2.6.32-431.el6.x86_64.img
3、在/etc/inittab中添加agetty:
S0:12345:respawn:/sbin/agetty ttyS0 115200
4、重启客户机:
# reboot
最后切换到kvm虚拟机
virsh console vm1
五、管理虚拟机
A.拷贝一个虚拟机
#首先导出一个xml [[email protected] ~]# virsh dumpxml vm1 >q.xml [[email protected] ~]# ll total 60 -rw-------. 1 root root 2783 Jul 28 16:49 anaconda-ks.cfg -rw-r--r--. 1 root root 3298 Jul 28 16:50 cobbler.ks -rw-r--r--. 1 root root 20504 Jul 28 16:49 install.log -rw-r--r--. 1 root root 5882 Jul 28 16:48 install.log.syslog -rw-r--r--. 1 root root 2228 Jul 28 16:50 ks-post.log -rw-r--r--. 1 root root 1 Jul 28 16:50 ks-post-nochroot.log -rw-r--r--. 1 root root 1162 Jul 28 16:42 ks-pre.log -rw-r--r-- 1 root root 2803 Jul 28 20:51 q.xml #cp一个镜像 [[email protected] ~]# cp /data/kvm/hosts/vm1.raw /data/kvm/hosts/q.raw #编辑q.xml文件 [[email protected] ~]# cp q.xml /etc/libvirt/qemu/ [[email protected] ~]# vim /etc/libvirt/qemu/q.xml <name>vm2</name> #将name修改成vm2 <source file=‘/data/kvm/hosts/q.raw‘/> #指定我们新复制的镜像文件 <uuid>d0071932-59fd-4579-9521-104e7b0808f1</uuid> #修改uuid <mac address=‘52:54:00:25:be:42‘/> #修改mac地址 #定义新的虚拟机vm2 [[email protected] ~]# virsh define /etc/libvirt/qemu/q.xml Domain vm2 defined from /etc/libvirt/qemu/q.xml [[email protected] ~]# virsh list --all Id Name State ---------------------------------------------------- - vm1 shut off - vm2 shut off #启动虚拟机 [[email protected] ~]# virsh start vm2 Domain vm2 started [[email protected] ~]# virsh list --all Id Name State ---------------------------------------------------- 4 vm2 running - vm1 shut off
时间: 2024-10-10 17:11:33