Linux KVM virsh-install 安装、导入模板

工具

查看 MAC 地址 使用信息

1
2
3
4
5
6
7
8
9
10
11
root@debian:~/kvm# cat show_mac_address_status.sh
#!/bin/bash
grep 'mac address' /etc/libvirt/qemu/*.xml|awk -F ": " '{print $2, $1}'| sort
root@debian:~/kvm#
root@debian:~/kvm#
root@debian:~/kvm#
root@debian:~/kvm# ./show_mac_address_status.sh
<mac address='12:34:56:78:90:a1'/> /etc/libvirt/qemu/win10_21H2.xml
<mac address='12:34:56:78:90:a2'/> /etc/libvirt/qemu/blog.xml
<mac address='52:54:00:92:69:4d'/> /etc/libvirt/qemu/koolshare.xml
root@debian:~/kvm#

查看 OS与发行版信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@debian:~/kvm# cat  show_os_info.sh
#!/bin/bash
find /usr/share/osinfo/os/

root@debian:~/kvm# ./show_os_info.sh
/usr/share/osinfo/os/
/usr/share/osinfo/os/debian.org
/usr/share/osinfo/os/debian.org/debian-2.0.xml
/usr/share/osinfo/os/debian.org/debian-8.xml
/usr/share/osinfo/os/debian.org/debian-2.1.xml
/usr/share/osinfo/os/debian.org/debian-4.xml
/usr/share/osinfo/os/debian.org/debian-7.xml
/usr/share/osinfo/os/debian.org/debian-1.2.xml
/usr/share/osinfo/os/debian.org/debian-9.xml
/usr/share/osinfo/os/debian.org/debian-3.xml
/usr/share/osinfo/os/debian.org/debian-3.1.xml
/usr/share/osinfo/os/debian.org/debian-1.3.xml

查看 VNC 端口使用情况

1
2
3
4
5
6
7
8
9
10
11
12
root@debian:~/kvm# cat show_vnc_port_status.sh
#!/bin/bash
grep 'graphics.*vnc' /etc/libvirt/qemu/*.xml | awk -F ": " '{print $2, $1}'| sort

root@debian:~/kvm#
root@debian:~/kvm# ./show_vnc_port_status.sh
<graphics type='vnc' port='5920' autoport='no' listen='0.0.0.0'> /etc/libvirt/qemu/win10_21H2.xml
<graphics type='vnc' port='5921' autoport='no' listen='0.0.0.0'> /etc/libvirt/qemu/koolshare.xml
<graphics type='vnc' port='5922' autoport='no' listen='0.0.0.0'> /etc/libvirt/qemu/blog.xml
root@debian:~/kvm#
root@debian:~/kvm#
root@debian:~/kvm#

重新定义一个 XML文件

1
2
3
4
5
6
7
8
root@debian:~/kvm# cat redefine

NAME="win10_21H2"
virsh destroy ${NAME}
virsh define /etc/libvirt/qemu/${win10_21H2}.xml
virsh start ${win10_21H2}

root@debian:~/kvm#

全新安装

Debian 11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
root@debian:~/kvm# cat kvm_install_blog.sh
#!/bin/bash
OS_DOMAIN='blog'
OS_DISK="/home/OS_Disk_Image/${OS_DOMAIN}.disk"
OS_IMG_DIR='/mnt/data/08_各种镜像/OS_Linux/'

virsh destroy $OS_DOMAIN > /dev/null 2>&1
virsh undefine $OS_DOMAIN > /dev/null 2>&1

rm -rf $OS_DISK
rm -rf /var/log/libvirt/qemu/${OS_DOMAIN}.log
rm -rf /etc/libvirt/storage

virt-install \
--virt-type=kvm \
--os-type=linux \
--os-variant=debian10 \
--name ${OS_DOMAIN} \
--cpu=host-passthrough \
--accelerate \
--autostart \
--ram 2048 \
--noautoconsole \
--vcpus sockets=1,cores=1,threads=1 \
--network mac=12:34:56:78:90:A2,bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5922,password=chunli \
--disk path=${OS_DISK},size=40,format=raw,bus=virtio \
--cdrom ${OS_IMG_DIR}/debian/debian-11.2.0-amd64-DVD-1.iso \

root@debian:~/kvm#

Linux Console 控制台模板

1
2
3
4
5
6
7
8
9
10
11
12
virt-install                                                   \
--virt-type=kvm \
--name=centos6 \
--memory=1024 \
--vcpus=1 \
--bridge=br0 \
--os-type=linux \
--location=/var/lib/libvirt/boot/CentOS-6.5-i386-bin-DVD1.iso \
--disk path=/var/lib/libvirt/images/centos6.disk,size=10 \
--console=pty,target_type=serial \
--graphics=none \
--extra-args="console=tty0 console=ttyS0"

Windows 10

建议安装专业版,企业版的管理功能用不上反而是累赘,消耗CPU资源.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@debian:~/kvm# cat kvm_install_win10_21H2.sh
#!/bin/bash
OS_DOMAIN='win10_21H2'
OS_DISK="/home/OS_Disk_Image/${OS_DOMAIN}.disk"
OS_IMG_DIR='/mnt/data/08_各种镜像/OS_windows/'

virsh destroy $OS_DOMAIN > /dev/null 2>&1
virsh undefine $OS_DOMAIN > /dev/null 2>&1

rm -rf $OS_DISK
rm -rf /var/log/libvirt/qemu/${OS_DOMAIN}.log
rm -rf /etc/libvirt/storage

virt-install \
--virt-type=kvm \
--os-type=windows \
--os-variant=win10 \
--name ${OS_DOMAIN} \
--cpu=host-passthrough \
--accelerate \
--autostart \
--ram 4096 \
--noautoconsole \
--vcpus sockets=1,cores=2,threads=2 \
--network mac=12:34:56:78:90:A1,bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5920,password=chunli \
--disk path=${OS_DISK},size=80,format=raw,bus=virtio \
--disk path=${OS_IMG_DIR}/virtio-win/virtio-win-0.1.190_amd64.vfd,device=floppy \
--cdrom ${OS_IMG_DIR}/windows_10_2111/zh-cn_windows_10_business_editions_version_21h2_x64_dvd_93b4cb1a.iso \

root@debian:~/kvm#

windows_10 模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
virt-install                                               \
--virt-type=kvm \
--os-type=windows \
--name code \
--accelerate \
--autostart \
--hvm \
--ram 8192 \
--vcpus sockets=1,cores=2,threads=2 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5920,password=chunli \
--disk path=/home/OS_Disk_Image/virtio-win-0.1.126_amd64.vfd,device=floppy \
--disk path=/home/OS_Disk_Image/windows_10_1809.disk,size=200,format=qcow2,bus=virtio \
--cdrom=/home/OS_Disk_Image/cn_windows_10_consumer_edition_version_1809_updated_sept_2018_x64_dvd_051b7719.iso \
--noautoconsole

windows 模板
兼容 CentOS Debian

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
virt-install                                               \
--virt-type=kvm \
--os-type=windows \
--name win10 \
--accelerate \
--autostart \
--hvm \
--ram 4096 \
--vcpus sockets=1,cores=2,threads=2 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5920,password=chunli \
--disk path=/home/OS_Disk_Image/virtio-win-0.1.126_amd64.vfd,device=floppy \
--disk path=/home/OS_Disk_Image/win10.disk,size=80,format=raw,bus=virtio \
--cdrom=/mnt/data/08_各种镜像/OS_windows/windows_10_LTSC_2019/cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso \
--noautoconsole

win10 模板
经过测试对比, Windows10版本中 1709空闲时占用CPU最低, 1809占用CPU最高, 1903,LTSC 约30%

#VNC 也可以不设定密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
virt-install                            \
--name win10_1709 \
--ram 4096 \
--os-type=windows \
--os-variant=win10 \
--virt-type=kvm \
--accelerate \
--autostart \
--noautoconsole \
--cpu=host \
--vcpus sockets=1,cores=2,threads=2 \
--graphics vnc,listen=0.0.0.0,port=5920 \
--boot cdrom \
--network bridge=br0,model=virtio \
--disk path=/home/OS_Disk_Image/win10_1709.disk,size=80,format=raw,bus=virtio \
--disk path=/mnt/data/08_各种镜像/OS_windows/virtio-win-0.1.126_amd64.vfd,device=floppy \
--disk path=/mnt/data/08_各种镜像/OS_windows/windows_10_1709/cn_windows_10_multi-edition_version_1709_updated_dec_2017_x64_dvd_100406696.iso,device=cdrom \

镜像导入

koolshare

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@debian:~/kvm# cat kvm_install_koolshare.sh
virt-install \
--virt-type=kvm \
--os-type=linux \
--name koolshare \
--accelerate \
--autostart \
--ram 256 \
--vcpus sockets=1,cores=1,threads=1 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5921,password=chunli \
--import \
--disk path=/home/OS_Disk_Image/koolshare.disk,format=raw,bus=virtio \
--noautoconsole

root@debian:~/kvm#

软路由模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@dell ~]# cat install_kvm_mikrotik.sh
name=mikrotik
RAM=256
ostype=linux
osvariant=centos7.0

virsh destroy mikrotik
sleep 1
virsh undefine mikrotik
rm -rf /home/OS_Disk_Image/${name}.disk

virt-install \
--name $name \
--os-type=$ostype \
--os-variant=$osvariant \
--accelerate \
--autostart \
--hvm \
--ram $RAM \
--cpu=host \
--vcpus sockets=1,cores=2,threads=1 \
--network bridge=br0,model=virtio \
--network bridge=br1,model=virtio \
--graphics vnc,listen=0.0.0.0,port=5928,password=chunli \
--noautoconsol \
--cdrom=/home/OS_Disk_Image/mikrotik-6.44.6.iso \
--disk path=/home/OS_Disk_Image/${name}.disk,size=1,format=raw,bus=ide

[root@dell ~]#