Shell script-KVM to create a new virtual machine

KVM creates a new virtual machine

#!/usr/bin/bash
##MOD
##ShanHai

#定义变量
read -p "请输入创建的新虚拟机名称:" vm_name
vm_uuid=`uuidgen`
vm_mem=1048576
vm_mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
#源镜像路径
image=/home/kvm/virtual-img/centos20210326T193020.qcow2
#源模板路径
mod=/etc/libvirt/qemu/centos7-mod.xml
#新镜像路径
new_image=/home/kvm/virtual-img/${
    
    vm_name}.qcow2
#新模板路径
new_mod=/etc/libvirt/qemu/${
    
    vm_name}.xml

#创建新虚拟机
cp -f $image $new_image
cp -f $mod $new_mod
sed -r \
-e  s%vm-name%$vm_name% \
-e  s%vm-uuid%$vm_uuid% \
-e  s%vm-mem%$vm_mem% \
-e  s%vm-disk-path%$new_image% \
-e  s%vm-mac%$vm_mac% \
$mod > $new_mod

#启动新虚拟机
systemctl restart libvirtd
virsh start $vm_name
virsh list --all | grep $vm_name

Results of the

Insert picture description here

Guess you like

Origin blog.csdn.net/AnNan1997/article/details/115255682