这里写目录标题
脚本功能
创建一个总目录列出列出脚本功能并通过source来其它对脚本进行调取
vim kvm_vm.sh
while :
do
cat <<-EOF
--------------------------------
1.查看虚拟机
2.安装虚拟机
3.为虚拟机添加设备
4.虚拟机快照管理
5.任意键退出
-------------------------------
EOF
read -p "input a num :" num
case $num in
1)
#调取查看虚拟机的脚本
source ./vm_info.sh
;;
2)
source ./create-vm.sh
;;
3)
source ./add_facility.sh
;;
4)
source ./snap.sh
;;
*)
break
esac
done
1 查看虚拟机
vim vm_info.sh
name(){
read -p "输入虚拟机名称 :" name
}
while :
do
cat <<-EOF
------------------------
1.列出所有的虚拟机
2.查看指定虚拟机的元数据信息
3.查看指定虚拟机的ip地址
4.启动虚拟机
5.关闭虚拟机
6.强制关机
7.重启虚拟机
8.任意键返回上级目录
------------------------
EOF
read -p "input a num :" num
case $num in
1)
virsh list --all
;;
2)
name
virsh dominfo $name
;;
3)
name
virsh doinfaddr $name
;;
4)
name
virsh start $name
;;
5)
name
virsh shutdown $name
;;
6)
name
virsh destroy $name
;;
7)
name
virsh reboot $name
;;
*)
break
;;
esac
done
1.1 列出所有虚拟机
virsh dominfo $name
1.2 查看指定虚拟机的元数据信息
virsh dominfo $name
1.3 查看指定虚拟机的ip地址
virsh doinfaddr $name
1.4 启动虚拟机
virsh start $name
1.5 关闭虚拟机
virsh shutdown $name
1.6 强制关机
virsh destroy $name
1.7 重启虚拟机
virsh reboot $name
2 安装虚拟机
vim create-vm.sh
while :
do
cat <<-EOF
----------------------------
1.全新安装
2.模板安装
3.任意键返回上级菜单
----------------------------
EOF
read -p "input a num :" num
case $num in
1)
source ./fresh-create.sh
;;
2)
source ./template-create.sh
;;
*)
break
;;
esac
done
2.1 全新安装
vim fresh-create.sh
set -ue
set -o pipefail
#创建相关目录
#定义相关变量
kvm_home=/home/kvm
kvm_iso=$kvm_home/iso/CentOS-7-x86_64-Minimal-1708.iso
kvm_ks_file=$kvm_home/ks/ks.cfg
kvm_disk=$kvm_home/virtualdisk
#设置默认值
def_vm_name="centos$(date +%Y-%m-%d" "%H:%M:%S)"
def_mem_size=1024
def_vcpus=1
def_disk_size=10G
#用户交互获取变量值
echo "输入KVM虚拟机名称"
echo "输入想要创建的服务器主机名 "
read -p "默认值为localhost :" host_name
echo "输入虚拟机cpu核心数"
read -p "默认值为1 :" vcpus
echo "输入虚拟机内存大小 默认1024M"
read -p "不需要输入单位 :" mem_size
while :
do
echo "输入虚拟机使用的磁盘容量"
read -p "默认 10G,[示例写法:15G]:" disk_size
echo "格式不正确"
continue
else
break
fi
done
vm_name=${vm_name:-$def_vm_name}
host_name=${host_name:-localhost}
vcpus=${vcpus:-$def_vcpus}
mem_size=${mem_size:-$def_mem_size}
disk_size=${disk_size:-$def_disk_size}
new_disk=${kvm_disk}/${vm_name}.qcow2
#用户交互获取变量值
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 linux \
--location ${kvm_iso} \
--network default \
--graphics none \
--os-variant rhel7 \
--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@bogon ~]# cat /home/kvm/ks/ks.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$KXeyOPUO$5JlUe3pPpi.hplHB71JdX/
# 系统语言
#lang zh_CN.UTF-8
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
# Use text mode install
# 使用字符界面安装操作系统
text
# Do not configure the X Window System
# 不配置图形界面系统
skipx
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# 设置动态获取 IP
network --bootproto=dhcp --device=eth0 --onboot=on
network --hostname=HOSNAME
# 设置静态 IP
# network --bootproto=static --device=eth0 --gateway=192.168.122.1 --ip=192.168.122.100 --nameserver=192.168.122.1 --netmask=255.255.255.0 --activate
# 系统安装完成后 重启系统
#halt
reboot
# System timezone 系统时区
timezone Asia/Shanghai
# System services 系统启动时候自动启动的服务
services --enable="chronyd"
# System bootloader configuration
#系统启动引导方式 vda 是kvm 虚拟机时候的第块磁盘名称
bootloader --location=mbr --boot-drive=vda
# 清除主引导记录
zerombr
# 删除原来的分区和磁盘标签
clearpart --all --initlabel
# 磁盘分区信息,这里采用的是 lvm 自动分区
autopart --type=lvm
# 下面是自定义分区
#part /boot --fstype="xfs" --ondisk=vda --size=200
#part / --fstype="xfs" --ondisk=vda --size=10040
#part /var --fstype="xfs" --ondisk=vda --size=2048
#part /home --fstype="xfs" --ondisk=vda --size=2048
# 安装软件
%packages
@^minimal
@core
chrony
%end
# 禁用 kdump
%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
2.2 模板安装
拷贝原来虚拟机的配置文件 centos7.xml 到当前用户的家目录下,并命名为 centos7-mod.xml
cp /etc/libvirt/qemu/centos7.xml centos7-mod.xml
vim template-create.sh
read -p "输入vm-name :" name
read -p "输入vm-mem :" mem
vm_name=$name
vm_uuid=`uuidgen`
vm_mem=$mem
vm_disk_path=/home/$name.qcow2
vm_mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
#传参数传入模板当中并生成新的配置文件
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%$vm_disk_path% -e s%vm-mac%$vm_mac% ./centos7-mod.xml >> $name.xml
#创建新虚拟机的虚拟磁盘
cp ./centos7-mod.qcow2 /home/$name.qcow2
#判断本机是否安装virsh
yum -y install libvirt-client &>/dev/null
if [ $? != 0 ];then
yum -y install libvirt-client
fi
#定义虚拟机
virsh define $vm_name.xml
#启动虚拟机
virsh start $vm_name
以下为centos7-mod.xml内容
[root@bogon ~]# cat centos7-mod.xml
<!--
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.0
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>vm-name</name>
<uuid>vm-uuid</uuid>
<memory unit='KiB'>vm-mem</memory>
<currentMemory unit='KiB'>vm-mem</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='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</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='vda' 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>
3 为虚拟机添加设备
vim add_facility.sh
while :
do
cat <<-EOF
-------------------------------
1.添加硬盘
2.添加内存
3.添加网卡
4.任意键返回上级目录
-------------------------------
EOF
read -p "input a num :" num
case $num in
1)
source ./add-disk.sh
;;
2)
source ./add-mem.sh
;;
3)
source ./add-mac.sh
;;
4)
break
;;
esac
done
3.1 添加硬盘
vim add-disk.sh
read -p "输入想添加硬盘的虚拟机名称 :" add_disk_vm_name
read -p "输入添加磁盘的名称为 :" add_disk_name
read -p "添加硬盘的大小 :" add_disk_size
echo "格式不正确,正确格式:10G"
continue
else
break
fi
done
disk_dir=/home/kvm/virtualdisk/centos7
ls ${disk_dir} || mkdir -p ${disk_dir}
if [ ! -f "${add_disk_vm_name}" ];then
fi
for i in {a..z}
do
virsh domblklist ${add_disk_vm_name} | grep "^vd$i" &>/dev/null
if [ $? -ne 0 ];then
break
fi
done
dev=vd$i
cat > ${disk_dir}/disk.xml <<-EOF
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
<source file='dir'/>
<target dev='vdi' bus='virtio'/>
</disk>
EOF
sed -ri s%vdi%$dev% ${disk_dir}/disk.xml
sed -ri s%dir%${disk_dir}/${add_disk_name}.qcow2% ${disk_dir}/disk.xml
virsh attach-device ${add_disk_vm_name} ${disk_dir}/disk.xml --persistent
3.2 添加内存
vim add-mem.sh
read -p "输入需要添加内存的虚拟机名称 :" vm_name
read -p "设置最大可分配内存(只需填数字) : " vm_max_mem
virsh shutdown $vm_name &>/dev/null
virsh setmaxmem centos7-1 ${vm_max_mem}G &>/dev/null
echo "内存添加成功"
3.3 添加网卡
read -p "输入想添加网卡的虚拟机名称 :" vm_name
mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
dir=/home/kvm/virtualdisk/centos7
cat > $dir/mac.xml <<-EOF
<interface type='network'>
<mac address='52:54:00:$mac'/>
<source network='default'/>
<model type='virtio'/>
</interface>
EOF
virsh attach-device $vm_name $dir/mac.xml --persistent
4 虚拟机快照管理
vim snap.sh
read_fun(){
read -p "输入虚拟机名称 :" vm_name
read -p "输入快照名称 :" snap_name
}
judge_fun(){
if [ $? -ne 0 ];then
echo "请重新输入"
continue
else
break
fi
}
while :
do
cat <<-EOF
------------------------------
1.查看虚拟机的快照
2.虚拟机创建快照
3.虚拟机恢复快照
4.删除快照
5.任意键返回上级目录
------------------------------
EOF
read -p "input a num :" num
case $num in
1)
read -p "输入虚拟机名称 :" vm_name
virsh snapshot-list $vm_name
;;
2)
while :
do
read_fun
virsh snapshot-create-as $vm_name $snap_name
judge_fun
done
;;
3)
while :
do
read_fun
virsh snapshot-revert $vm_name --snapshotname $snap_name
judge_fun
done
;;
4)
while :
do
read_fun
virsh snapshot-delete $vm_name --snapshotname $snap_name
judge_fun
done
;;
*)
break
;;
esac
done
4.1 查看虚拟机的快照
virsh snapshot-list $vm_name
4.2 虚拟机创建快照
virsh snapshot-create-as $vm_name $snap_name
4.3 虚拟机恢复快照
virsh snapshot-revert $vm_name --snapshotname $snap_name
4.4 删除快照
virsh snapshot-delete $vm_name --snapshotname $snap_name