创建虚拟机
安装虚拟机
__win_install() {
_name="win-template"
virsh destroy "$_name" >/dev/null 2>&1
virsh undefine "$_name" >/dev/null 2>&1
_iso_win="/data/kvm/iso/zh-cn_windows_server_2022_updated_june_2022_x64_dvd_5a360af4.iso"
_system_img="/data/kvm/vm/win-template/system.qcow2"
mkdir -p ${_system_img%/*}
rm -rf "$_system_img"
qemu-img create -f qcow2 "$_system_img" 99G -o preallocation=off
virt-install --name="$_name" \
--memory=8192 \
--cpu host \
--vcpus "$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)" \
--os-type=windows \
--os-variant=win2k19 \
--noautoconsole \
--autostart \
--disk $_system_img,cache=none,bus=sata \
--disk $_iso_win,device=cdrom,bus=ide \
--graphics vnc \
--network network=default,model=virtio
}
__win_install
如果你 shell 工具支持 X11 显示, 此时会调起X11, 我的X11 显示的是中文乱码, 要解决这个问题应该是要Linux上安装中文字库
不过还是推荐使用 VNC 连接工具 连接完成安装, 如果VNC 无法连接, 那先关掉 X11 的显示窗口
安装过程这里就不贴了 详情 https://www.yuque.com/uuu/centos/install_course
由于安装命令中没有设置自动重启, 所以安装完成后点击重启, 虚拟机是不会重启的, 而是关闭状态, 这个不用理会, 应为我们马上就会删掉,重新导入它
可以使用以下命令查看虚拟机运行状态
virsh list --all
在安装完成后将虚拟机删除, 等下重新以导入虚拟机的方式重新启动
_name="centos7-template"
virsh destroy "$_name" >/dev/null 2>&1
virsh undefine "$_name" >/dev/null 2>&1
导入虚拟机
导入刚刚安装的虚拟机
__centos_import() {
_name="centos7-template"
virsh destroy "$_name" >/dev/null 2>&1
virsh undefine "$_name" >/dev/null 2>&1
_system_img="/data/kvm/vm/centos7-template/system.qcow2"
virt-install --name="$_name" \
--memory=4096 \
--cpu host \
--vcpus $(grep 'processor' /proc/cpuinfo | sort -u | wc -l) \
--os-type=linux \
--os-variant=rhel7.9 \
--autostart \
--noautoconsole \
--import \
--disk "$_system_img",cache=none,bus=sata \
--graphics vnc,listen=0.0.0.0,port=5900,password=p \
--network network=default,model=virtio
}
__centos_import