kvm(未完成)
前言
kvm是什么?干什么用的? 直白一点,它就是在linux中
部署前准备工作
具体部署
图形化部署
模板快速部署
virsh管理虚拟机
添加硬件
存储池
快照
网络连接配置
脚本一键部署
脚本的方便之处就不多说了,但这个脚本又该用什么样的思路写呢?
脚本的灵魂就是人机交互,我选择创建几个,叫什么名字,占用多大内存,使用者设定,脚本进行配置.
比如,比希望随便什么名字,我不想交互.给我一个马上可以使用的虚拟机就可以了.亦或者,需要自定义设置名字,mac,
再或者批量创建多个虚拟机.
你知道的,virsh启动虚拟机的是xxxx.xml这个文件中的xxxx来启动的.那你就需要去先修改这个文件中的,名称,uuid,mac等这些东西,但是我们既然使用了脚本,我们是不是选择人机交互这个样子.
需要在/etc/libvirt/qemu目录下创建一份模板,模板如下.
[root@bogon ~]# vim /etc/libvirt/qemu/vmmodel.xml
<!配置文件模板>
<domain type='kvm'>
<name>kvmname</name> <!1调用脚本中获取到的用户编辑的kvmname>
<uuid>kvmuuid</uuid><!2调用脚本中用户获取的uuid>
<memory unit='KiB'>kvmmem</memory> <!3调用脚本中获取到的用户编辑的kvmmen>
<currentMemory unit='KiB'>kvmmem</currentMemory><!3调用脚本中获取到的用户编辑kvmmem>
<vcpu placement='static'>kvmcpu</vcpu><!4调用脚本中获取到的用户编辑kvmcpu>
<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='kvmimg'/><!5磁盘的文件路径kvming>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' 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:kvmmac'/><!6调用脚本中获取到的用户编辑的kvmmac>
<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>
获取用户信息配置kvm(shell)
#!/bin/bash
##
##
##创建自定义配置虚拟机函数
batch_self_define(){
reap -p "请输入虚拟机名称:" new_name
reap -p "请输入虚拟机内存大小:" new_mem
reap -p "请输入虚拟机的个数:" new_cpu
sourceimage=/var/lib/libvirt/images/centos7.0.qcow2 #镜像盘的路径
sourcexml=/etc/libvirt/qemu/vmmodel.xml
new_img=/var/lib/libvirt/images/$(kvmname).qcow2
new_xml=/var/libvirt/qemu/$(kvmname).xml
cp $sourceimage $new_img
cp $sourcxml $new_xml
kvmname=$new_na
kvmmem=$new_men
kvmcpu=$new_cpu
kvmuuid=`uuidgen` ##bash里面用uuidgen会随机生成uuid,且不重复
kvmimg=$new_img
kvmmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'` ##生成16进制的6个字符,
sed -i "s/$(kvmname)//g"
}