KVM一
一、KVM软件安装
1、 环境准备
2、 查看CPU是否支持VT技术
[root@server ~]# cat /proc/cpuinfo | grep -E 'vmx|svm'
3、清理环境:卸载KVM
[root@server ~]# yum remove `rpm -qa | egrep 'qemu|virt|KVM'` -y
[root@server ~]# rm -rf /var/lib/libvirt /etc/libvirt/
4、安装软件
Centos6
[root@server ~]# yum groupinstall "Virtualization" "Virtualization Client" "Virtualization Platform" "Virtualization Tools" -y
Centos7:
[root@server ~]# yum install *qemu* *virt* librbd1-devel -y
qemu-KVM: 主包
libvirt:API接口
virt-manager:图形管理程序
Centos8:
yum 组安装已经可以使用了
问题:在进行组安装的时候会出现关于rpm版本的错误问题
解决:#yum upgrade rpm -y
组安装成功之后还是要单独安装virt-manager工具
5、启动服务
[root@server ~]# systemctl start libvirtd
6、查看KVM模块加载
[root@server ~]# lsmod | grep KVM
KVM_intel 53484 3
KVM 316506 1 KVM_intel
二、GuestOS安装
- 图形方式(非常重要 非常简单)
- 完全文本模式(现场配置虚拟机的规格)
- 命令行模式(重中之重 最常用 模板镜像+配置文件 方式配置规格)
- 通过飞机驾驶舱安装和管理虚拟机
[root@server ~]# yum install *cockpit* -y
[root@server ~]# systemctl start cockpit
访问:
浏览器访问
服务器地址:9090
1、图形模式安装Guest OS
[root@server ~]# virt-manager
2、完全文本方式安装
注意:了解即可
极端情况-服务器没有图形 客户端也没有图形
#virt-install --connect qemu:///system -n vm6 -r 512 --disk path=/virhost/vmware/vm6.img,size=7 --os-type=linux --os-variant=rhel6 --vcpus=1 --network bridge=br0 --location=http://127.0.0.1/rhel6u4 -x console=ttyS0 --nographics
#virt-install --connect qemu:///system -n vm9 -r 2048 --disk path=/var/lib/libvirt/images/vm9.img,size=7 --os-type=linux --os-variant=centos7.0 --vcpus=1 --location=ftp://192.168.100.230/centos7u3 -x console=ttyS0 --nographics
注意:
-
用这种方式安装的操作系统,大小写会胡乱变化,不影响远程操作
-
内存必须2G以上
查看KVM支持的OS版本:
[root@server ~]# man virt-install
[root@server ~]# osinfo-query os | grep centos
排错:
安装过程中:
手动配置IP地址
到url位置找不到路径,要返回去手动选择url,重新配置url为ftp://192.168.100.230/rhel6u4,这里的ip不要写127.0.0.1而是br0的ip
给虚拟机指定的内存必须大于2048M,不然报错如下: dracut-initqueue[552]: /sbin/dmsquash-live-root: line 273: printf: write error: No space left on device
逃脱符:
Escape character is ^]
3、命令行模式安装
虚拟机的组成部分
- 虚拟机配置文件
[root@server ~]# ls /etc/libvirt/qemu
networks vm1.xml
- 储存虚拟机的介质
[root@server ~]# ls /var/lib/libvirt/images/
vm1.img
根据配置文件创建虚拟机
1.需要有磁盘镜像文件:
[root@server ~]# cp vm1.img vm2.img
2.需要有配置文件
[root@server ~]# cp vm1.xml vm2.xml
3.配置文件需要修改必要的内容
4.创建虚拟机:
[root@server ~]# virsh define /etc/libvirt/qemu/vm2.xml
注: allocate 英[ˈæləkeɪt] 美[ˈæləkeɪt]
拨…(给); 划…(归); 分配…(给); [例句]Tickets are limited and will be allocated to those who apply first 票数有限,先申请者先得。
4、实例
模板镜像+配置文件 方式创建虚拟机
1.拷贝模板镜像和配置文件
[root@KVM ~][root@server ~]# cp /var/lib/libvirt/images/vm2.img /var/lib/libvirt/images/vm3.img
[root@KVM ~][root@server ~]# cp /etc/libvirt/qemu/vm2.xml /etc/libvirt/qemu/vm3.xml
2.修改配置文件
[root@server ~]# vim /etc/libvirt/qemu/vm3.xml
<domain type='KVM'>
<name>vm3</name>
<uuid>a2f62549-c6b7-4b8f-a8e2-c14edda35a78</uuid>
<memory unit='KiB'>2099200</memory>
<currentMemory unit='KiB'>2099200</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Haswell-noTSX</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-KVM</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm3.im'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm3-1.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x16' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:f2:28:6f'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</domain>