Shell script creates a virtual machine and configures the network card IP address (two scripts)

Source: Internet
Author: User

###################### #脚本1: Creating a virtual machine kvm#########################
#!/bin/bash
Qcow2_dir=/var/lib/libvirt/images
Read-p "Please enter the virtual machine number:" Num #主机号
If [$num-lt 9];then
Num=0$num # #主机号
Fi
If [-Z ' $num '];then
echo "Error Output"
Exit 1
Fi
Kvm_name=host$num #主机名字
echo "Creating image ...."
Qemu-img create-f qcow2-b ${qcow2_dir}/rhel7.4.qcow2 ${qcow2_dir}/${kvm_name}.qcow2 &>/dev/null # # #镜像文件
Echo-e "\e[32;1m[ok]\e[0m"
Cat/etc/libvirt/qemu/rhel7.4.xml >/etc/libvirt/qemu/${kvm_name}.xml # #描述文件
Sed-i "/<name>rhel7.4/s/rhel7.4/${kvm_name}/"/etc/libvirt/qemu/${kvm_name}.xml
Sed-i "/&LT;UUID&GT;/D"/etc/libvirt/qemu/${kvm_name}.xml
Sed-i "/<source/s/rhel7.4.qcow2/${kvm_name}.qcow2/"/etc/libvirt/qemu/${kvm_name}.xml
Sed-i "/<mac address=/d"/etc/libvirt/qemu/${kvm_name}.xml #删除mac地址, motorized automatic generation
Virsh Define/etc/libvirt/qemu/${kvm_name}.xml &>/dev/null
echo "Creating Virtual machine ..."
Echo-e "\e[32;1m[ok]\e[0m"
################### #脚本2: eth0 nic with IP address #####################
#!/bin/bash
Clear
Mountpoint= "/media/virimage"
#判断不存在挂载目录
[!-D $mountpoint] && mkdir $mountpoint
#如果有设备挂载到该挂载点, the first umount uninstall
If Mount |grep-q "$mountpoint"; then
Umount $mountpoint
Fi
Virsh List--all
Read-p "Enter virtual machine name:"
echo "Please later ..."
#将虚拟机的磁盘文件挂载到/media/virtimage Directory
guestmount-d $name-I $mountpoint
echo "---------------------------------------"
Read-p "Please enter the IP address to configure:" IP
Read-p "Please enter the number of subnet mask bits to configure:" num
#静态地址
Sed-i "s; (bootproto=). ; \1none; "${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
#开机自起
Sed-i "s; (onboot=).
; \1yes; "${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
#如果ip地址不存在, add an IP address
Grep-q "ipaddr" ${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0 | | echo "Ipaddr= $ip" >> ${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
#如果ip地址存在
Sed-i "s; (ipaddr=). ; \1$ip; "${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
#如果子网掩码不存在, add a subnet mask
Grep-q "PREFIX" ${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0 | | echo "prefix= $num" >> ${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
#如果子网掩码存在
Sed-i "s; (prefix=).
; \1$num; "${mountpoint}/etc/sysconfig/network-scripts/ifcfg-eth0
echo "Ifup eth0" >> ${mountpoint}/etc/rc.d/rc.local
chmod +x ${mountpoint}/etc/rc.d/rc.local
Umount ${mountpoint}
Sleep 0.5
Virsh start $name

Shell script creates a virtual machine and configures the network card IP address (two scripts)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.