[root@localhost kvm]
/kvm
├── disk
│ ├── wc1.qcow2
│ ├── wc2.qcow2
│ └── wc.qcow2
├── kvm-manager.sh
├── lib
│ ├── 1
│ ├── function.func
│ └── install-new.sh
├── module
│ ├── centos7-mod.xml
│ ├── disk-mod.xml
│ ├── ks.cfg
│ └── network-mod.xml
└── tmp
├── centos7_wc1.xml
├── centos7_wc2.xml
├── centos7_.xml
├── disk_centos20201210T210142.xml
├── network_centos20201210T210142.xml
└── network.xml
4 directories, 18 files
[root@localhost kvm]
source ./lib/function.func
show_meu(){
cat <<EOF
1 查看当前所有的虚拟机 2 全新自动化安装虚拟机
3 利用模板快速创建虚拟机 4 给指定的虚拟机添加磁盘
5 给指定的虚拟机添加网卡 * 输入其他退出
EOF
}
while true
do
show_meu
read -p "开始你的选择>>:" choice
case $choice in
1)
show_all
;;
2)
gen_new
;;
3)
fast_new
;;
4)
add_disk
;;
5)
add_net
;;
*)
echo "退出!!!"
break
;;
esac
done
[root@localhost lib]
show_all (){
virsh list --all
}
gen_new(){
source ./lib/install-new.sh
}
fast_new(){
echo "利用模板创建一个新的虚拟机"
read -p "name:>>" vm_name
vm_uuid=$(uuidgen)
vm_mac=$(openssl rand -hex 3 | sed -r 's/..\B/&:/g')
read -p "磁盘路径(例如:/kvm/disk/xxx.qcow2)>>" vm_disk_path
read -p "磁盘名字:>>" vm_disk_name
source ./module/centos7-mod.xml > ./tmp/centos7_${vm_name}.xml
cp /home/kvm/virtual-img/centos20201210T210142.qcow2 ./disk/${vm_name}.qcow2
virsh define ./tmp/centos7_${vm_name}.xml
virsh start ${vm_name}
virsh domifaddr ${vm_name}
}
add_disk(){
echo "给指定的虚拟机添加硬盘"
look=`virsh list --all | awk 'NR !=1 && NR !=2 {printf "%s,%s",$2," "}'`
echo "现有的虚拟机有${look}"
read -p "请输入创建磁盘的容量(例如:2)(G)>>:" disk_size
read -p "请输入虚拟机配置文件的路径(例如:/kvm/disk/xxx.qcow2)>>:" config_file_path
read -p "输入添加磁盘的虚拟机名字>>:" vm_name
vir_shell=$(virsh domblklist ${vm_name} | grep ^vd | awk '{print$1}' | tail -1)
letter=${vir_shell:2:1}
num=$(printf "%d" "'${letter}")
if [[ "$num" == 122 ]];then
echo "输入错误。。不能继续创建。。。"
exit
fi
next_num=$(($num + 1))
next_letter=$(printf \\x`printf %x ${next_num}`)
new_disk_name="vd${next_letter}"
qemu-img create -f qcow2 ${config_file_path} ${disk_size}G
source ./module/disk-mod.xml > ./tmp/disk_${vm_name}.xml
virsh attach-device ${vm_name} ./tmp/disk_${vm_name}.xml --persistent
virsh domblklist ${vm_name}
}
add_net(){
look=`virsh list --all | awk 'NR !=1 && NR !=2 {printf "%s,%s",$2," "}'`
echo "现有的虚拟机有${look}"
read -p "输入需要添加网卡的虚拟机名称>>:" vm_name
mac=$(openssl rand -hex 3 | sed -r 's/..\B/&:/g')
echo "已经给虚拟机[${vm_name}] 创建了 MAC:${mac}"
source ./module/network-mod.xml > ./tmp/network_${vm_name}.xml
echo "已经创建了 XML 配置文件"
virsh attach-device ${vm_name} ./tmp/network_${vm_name}.xml --persistent
echo "已经添加了网卡"
virsh domiflist ${vm_name}
}
[root@localhost lib]
set -ue
set -o pipefail
KVM_HOME=/kvm
ls ${KVM_HOME}/virtual-img 1>/dev/null 2>&1 || mkdir -p ${KVM_HOME}/virtual-img
KVM_ISO=${KVM_HOME}/CentOS-7-x86_64-Minimal.iso
KVM_KS_FILE=./module/ks.cfg
KVM_IMG_DIR=${KVM_HOME}/virtual-img
OS_TYPE="linux"
DEF_OS_VARIANT="rhel7"
DEF_VM_NAME="centos$(date +%Y%m%dT%H%M%S)"
DEF_MEM_SIZE=1024
DEF_VCPUS=1
DEF_DISK_SIZE=10G
echo "输入将要创建的KVM虚拟机名称,不是主机名哦"
read -p "默认值${DEF_VM_NAME}>>:" VM_NAME
echo "输入将要创建的服务器主机名"
read -p "默认 localhost>>:" HOST_NAME
read -p "输入虚拟机 CPU 核心数默认 1 示例:2>>:" VCPUS
echo "输入虚拟机内存大小,默认1024M,不支持其他单位"
read -p "只需要添数字即可>>:" MEM_SIZE
echo "输入虚拟机使用的磁盘容量"
read -p "默认 10G,[示例写法:15G]>>:" DISK_SIZE
VM_NAME=${VM_NAME:-$DEF_VM_NAME}
HOST_NAME=${HOST_NAME:-localhost.localdomain}
VCPUS=${VCPUS:-$DEF_VCPUS}
MEM_SIZE=${MEM_SIZE:-$DEF_MEM_SIZE}
DISK_SIZE=${DISK_SIZE:-$DEF_DISK_SIZE}
OS_VARIANT=${OS_VARIANT:-$DEF_OS_VARIANT}
new_disk=${KVM_IMG_DIR}/${VM_NAME}.qcow2
if [[ ! "${DISK_SIZE}" =~ G$ ]] || [[ ! "${DISK_SIZE::-1}" =~ ^[0-9]+$ ]]
then
echo "格式不正确,正确示例:20G"
exit
fi
if [ ! -f "${new_disk}" ];then
qemu-img create -f qcow2 ${new_disk} ${DISK_SIZE}
fi
virt-install -v \
--arch x86_64 --virt-type kvm \
--name ${VM_NAME} \
--memory ${MEM_SIZE} \
--vcpus ${VCPUS} \
--os-type ${OS_TYPE} \
--location ${KVM_ISO} \
--network default \
--graphics none \
--os-variant ${OS_VARIANT} \
--initrd-inject "${KVM_KS_FILE}" \
--extra-args "ks=file:/ks.cfg \
console=tty0 console=ttyS0,115200n8 \
hostname=${HOST_NAME}" \
--disk ${new_disk},cache=writeback,io=threads,bus=virtio
[root@localhost module]
echo "<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit centos7-1
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>${vm_name}</name>
<uuid>${vm_uuid}</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</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='full'>
<model fallback='forbid'>SandyBridge</model>
<feature policy='require' name='hypervisor'/>
<feature policy='require' name='xsaveopt'/>
</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='${vm_disk_path}'/>
<target dev='${vm_disk_name}' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:${vm_mac}'/>
<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>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
</devices>
</domain>"
[root@localhost module]
echo "<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
<source file='${config_file_path}'/>
<target dev='${new_disk_name}' bus='virtio'/>
</disk>"
[root@localhost module]
install
keyboard 'us'
rootpw --iscrypted $1$KXeyOPUO$5JlUe3pPpi.hplHB71JdX/
lang en_US
auth --useshadow --passalgo=sha512
text
skipx
selinux --disabled
firewall --disabled
network --bootproto=dhcp --device=eth0 --onboot=on
network --hostname=HOSNAME
reboot
timezone Asia/Shanghai
services --enable="chronyd"
bootloader --location=mbr --boot-drive=vda
zerombr
clearpart --all --initlabel
autopart --type=lvm
%packages
@^minimal
@core
chrony
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%post --interpreter=/bin/bash
yum install -y epel-release bash-completion vim-enhanced wget
yum group install "Development Tools" -y
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@localhost module]
echo "<interface type='network'>
<mac address='52:54:00:${mac}'/>
<source network='default'/>
<model type='virtio'/>
</interface>"