当前位置:网站首页>KVM script management - the road to dream
KVM script management - the road to dream
2022-06-25 13:03:00 【The road to dream】
# Template quick create virtual machine
# The template directory /kvmtemplete/
# Template file /kvmtemplete/centos7-mod.xml
Be careful :xml In the template file vm_name,vm_uuid,vm_mem,vm_disk_path,vm_mac Modification of
# Script modkvm_install.sh
#!/bin/bash
## Use templates to quickly create virtual machines
modkvm_install(){
def_name=`openssl rand -hex 5`
read -p " Please enter the name of the virtual machine [ Default name :$def_name]:" vm_name
read -p " Please enter the memory size [ Default 1G], Do not enter units :" vm_mem
vm_name=${vm_name:-$def_name} # Define default name
vm_uuid=`uuidgen`
def_mem=1
vm_mem=${vm_mem:-$def_mem} # Define the default memory
vm_mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
vm_mem=${vm_mem}00000
# Source mirror location
sourceimage=/kvmtemplete/centos7-disk01.qcow2
# Source template location
sourcexml=/kvmtemplete/centos7-mod.xml
# New mirror location
newimg=/kvm/img/${vm_name}.qcow2
# New template location
newxml=/etc/libvirt/qemu/${vm_name}.xml
echo " Copying templates and images "
cp $sourceimage $newimg
cp $sourcexml $newxml
echo " Modifying template "
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%$newimg% \
-e s%vm_mac%$vm_mac% $sourcexml > $newxml
echo " Register virtual machine ${vm_name}"
#systemctl restart libvirtd
virsh define /etc/libvirt/qemu/${vm_name}.xml
echo " Starting virtual machine "
virsh start $vm_name
if [ $? -eq 0 ];then
echo " Start successfully, automatically list all virtual machine information "
#virsh list --all | grep $vm_name | awk '{print $2"——"$3}'
virsh list --all | awk '{print $2"——"$3$4}'
else
echo " Boot failure !!!!!!!!"
fi
}
$1
# test
bash modkvm_install.sh modkvm_install
# Create a brand new kvm virtual machine
# Script kvm_install.sh
#!/usr/bin/env bash
kvm_install(){
set -ue
set -o pipefail
# Create related directories
#ls /home/kvm/{ks,virtualhost,virtual-img} 1>/dev/null 2>&1 || mkdir -p /home/kvm/{virtualhost,virtual-img}
# The variables of this program
KVM_HOME=/kvm
KVM_ISO=${KVM_HOME}/iso/CentOS-7-x86_64-DVD-2009.iso
KVM_KS_FILE=${KVM_HOME}/ks/ks.cfg
KVM_IMG_DIR=${KVM_HOME}/img
OS_TYPE="linux"
DEF_OS_VARIANT="rhel7"
DEF_VM_NAME="centos$(date +%Y%m%dT%H%M%S)"
# Set the default memory size 1024 M, This unit is fixed M, Other companies are not supported
DEF_MEM_SIZE=1024
DEF_VCPUS=1
DEF_DISK_SIZE=10G
# Interactive installation
echo " Enter the... To be created KVM Virtual machine name , Not the host name "
read -p " The default value is ${DEF_VM_NAME}>>:" VM_NAME
echo " Enter the host name of the server to be created "
read -p " Default localhost>>:" HOST_NAME
read -p " Input virtual machine CPU Default number of cores 1 Example :2>>:" VCPUS
echo " Enter the virtual machine memory size , Default 1024M, Other companies are not supported "
read -p " Just add numbers >>:" MEM_SIZE
echo " Enter the disk capacity used by the virtual machine "
read -p " Default 10G,[ Example writing :15G]>>:" DISK_SIZE
# Variable substitution , If no value is entered , Just use the default values
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 " Incorrect format , The correct sample :20G"
exit
fi
if [ ! -f "${new_disk}" ];then
qemu-img create -f qcow2 ${new_disk} ${DISK_SIZE}
fi
# Start to create
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 bridge=br0 \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--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
# Bridging mode , The premise is that you have built the bridge
# --network bridge=br0 \
# end
}
$1
Be careful : modify iso Image directory ,ks.cfg File directory ,img Disk image directory
# test
bash kvm_install.sh kvm_install
# Add hard disk
# Hard disk configuration template
cat disk.mod
echo "<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
<source file='${disk_path}'/>
<target dev='${disk}' bus='virtio'/>
</disk>"
# Script
disk_install.sh
#!/bin/bash
disk_install(){
virsh list --all
read -p " Please enter the virtual machine to add the hard disk :" vm_name
# Get unused drive letter
for i in {a..z}
do
virsh domblklist $vm_name | grep "^vd${i}" > /dev/null
if [ $? -ne 0 ];then
break
fi
done
echo "1"
disk=vd${i}
# Make disk template
# Create an empty disk
# mkdir /centos7 &>/dev/null
disk_home=/kvm/img/${vm_name}-${disk}.qcow2
echo $disk_home
disk_path=${disk_home}
source /mnt/disk.mod > disk.xml
echo "2"
read -p " How much to add G The hard disk of [1G]:" size
# Create an empty disk
qemu-img create -f qcow2 ${disk_home} ${size}
echo "3"
# Add hard disk
virsh attach-device ${vm_name} disk.xml --persistent
# Check the disk
virsh domblklist ${vm_name}
}
disk_del(){
virsh list --all
read -p " Please enter the virtual machine you want to delete the hard disk : " vm_name
virsh domblklist $vm_name
read -p " Please enter the name of the hard disk to delete [ for example :vdb]" disk_v
virsh detach-disk $vm_name $disk_v --persistent
}
$1
# test Add hard disk
bash disk_install.sh disk_install
# test Delete the hard disk
bash disk_install.sh disk_del
# Add network card
# Network card template
cat net.mod
echo " <interface type='network'>
<mac address='52:54:00:${s}'/>
<source network='default'/>
<model type='virtio'/>
</interface>"
# Script net_install.sh
cat net_install.sh
#!/bin/bash
## Add virtual network card
# Add virtual network card
net_install(){
virsh list --all
read -p " Please enter the virtual machine to add :" vm_name
echo " Add virtual network card "
s=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
source /mnt/net.mod > net.xml
virsh attach-device ${vm_name} net.xml --persistent
echo " View virtual network card "
virsh domiflist ${vm_name}
}
net_del(){
virsh list --all
read -p " Please enter the virtual machine name :" vm_name
virsh domiflist $vm_name
read -p " Please enter the interface name :" net
mac_in=`virsh domiflist $vm_name | grep $net | awk '{print $5}'`
virsh detach-interface $vm_name network $mac_in --persistent
}
$1
# test add to Delete
bash net_install.sh net_install
bash net_install.sh net_del
# Create a snapshot
# Script
#!/bin/bash
snap_create() {
disk_home=/kvm/img/
virsh list --all
read -p " Please enter the virtual machine you want to create a snapshot : " vm_name
virsh list --all | grep $vm_name &>/dev/null
if [ $? -eq 0 ];then
stat=`virsh list --all | grep $vm_name | awk '{print $3}'`
echo " The current virtual machine state is $stat"
read -p " Continue to create snapshot [y/n]" option
case $option in
y|Y)
read -p " Please enter the name of the snapshot : " snap
echo " Creating snapshot "
disk_type=`qemu-img info ${disk_home}${vm_name}.qcow2 | awk -F ":[ ]" 'NR==2 {print $2}'`
if [ "$disk_type" = "qcow2" ];then
virsh snapshot-create-as $vm_name $snap
virsh snapshot-list $vm_name
if [ $? -eq 0 ];then
echo " Create success "
else
echo " Create failure "
break
fi
else
echo " Type does not support , Exiting !!!"
break
fi
;;
n|N)
break
;;
*)
break
;;
esac
else
echo " The virtual machine does not exist , Please re-enter !"
continue
fi
}
snap_delete() {
virsh list --all
read -p " Please enter the virtual machine to delete the snapshot : " vm_name
virsh snapshot-list $vm_name
read -p " Please enter the delete snapshot name : " vm_snap
virsh snapshot-delete $vm_name --snapshotname $vm_snap
virsh snapshot-list $vm_name
}
$1
# test add to Delete
bash snap.sh snap_create
bash snap.sh snap_delete
Integrated script kvmControl.sh
#!/bin/bash
## kvm Manage scripts
# Load module
source /mnt/disk_install.sh # Load the hard disk module
source /mnt/kvm_install.sh # Load the newly installed module
source /mnt/modkvm_install.sh # Load the template and install the module
source /mnt/net_install.sh # Load the network card module
source /mnt/snap.sh # Load the snapshot module
while :
do
cat <<-EOF
————————————————————————————————————————————————————————————————————————————————————————————————
1. View all current virtual machines
2. Quickly create virtual machines
3. Create a new virtual machine
4. Add hardware
5. Create a snapshot
6. Delete snapshot
7. Delete the hard disk
8. Delete network card
q/Q. Enter exit
———————————————————————————————————————————————————————————————————————————————————————————————
EOF
read -p " Please enter your choice :" option
case "$option" in
1)
virsh list --all
;;
2)
modkvm_install
;;
3)
kvm_install
;;
4)
read -p "[1] Add network card ,[2] Add hard disk ,[q/Q] sign out : " HW
case "$HW" in
2)
disk_install
;;
1)
net_install
;;
q|Q)
exit
esac
;;
5)
snap_create
;;
6)
snap_delete
;;
7)
disk_del
;;
8)
net_del
;;
q|Q)
break
;;
esac
done
边栏推荐
- mysql导入导出数据到excel表日期出现问题
- 美创入选“2022 CCIA中国网络安全竞争力50强”榜单
- KVM 脚本管理 —— 筑梦之路
- 1251- Client does not support authentication protocol MySql错误解决方案
- Using CMD (command prompt) to install MySQL & configure the environment
- Jenkins Pipeline使用
- Wechat full-text search technology optimization
- Baidu search stability analysis story
- Summer Ending
- Sword finger offer II 032 Effective anagrams
猜你喜欢
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
Introduction to mongodb chapter 01 introduction to mongodb
Koa frame
Geospatial search: implementation principle of KD tree
J2EE从入门到入土01.MySQL安装
It is extraordinary to make a move, which is very Oracle!
Update PIP & Download jupyter Lab
Sword finger offer day 1 stack and queue (simple)
Talk about 11 key techniques of high availability
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
随机推荐
The editor is used every day. What is the working principle of language service protocol?
使用Visio画立方体
量化交易之回测篇 - 期货CTA策略实例(TQZFutureRenkoScalpingStrategy)
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
Geospatial search - > R tree index
Command line garbled
leetcode - 384. Scramble array
Sword finger offer day 3 string (simple)
利用cmd(命令提示符)安装mysql&&配置环境
WIN10环境下配置pytorch
画图常用配色
剑指Offer 第 2 天链表(简单)
[AI helps scientific research] fool drawing of loss curve
深圳民太安智能二面_秋招第一份offer
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
torch. Tensor splicing and list (tensors)
Used in time filter (EL table)
Jenkins pipeline uses
Resolution of PPT paper drawing
Update PIP & Download jupyter Lab