跳转至

虚拟机管理

安装虚拟机

手动安装虚拟机

qemu-img 命令用于手动创建磁盘,常见的格式有 raw 和 qcow2。在磁盘使用方式上,raw 格式磁盘文件的大小是创建时指定的大小,qcow2 格式磁盘文件的大小是实际使用的大小。所以建议使用 qcow2 格式。

> qemu-img create -f qcow2 /var/lib/libvirt/images/rocky-kvm01.qcow2 20G

Formatting '/var/lib/libvirt/images/rocky-kvm01.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=21474836480 lazy_refcounts=off refcount_bits=16

> ls -lh /var/lib/libvirt/images/

total 196K
-rw-r--r-- 1 root root 193K Feb 24 17:33 rocky-kvm01.qcow2

执行以下命令安装虚拟机:

virt-install \
    --virt-type kvm \
    --name rocky-kvm01 \
    --vcpus 1 \
    --memory 2048 \
    --disk /var/lib/libvirt/images/rocky-kvm01.qcow2 \
    --os-type linux \
    --os-variant rocky9 \
    --network network=default \
    --graphics vnc,password=f,listen=0.0.0.0,port=5903 \
    --cdrom /mnt/beegfs/iso-images/rocky/Rocky-9.1-x86_64-minimal.iso

--os-variant 参数支持的 guest OS 类型可以使用 osinfo-query os 命令查看:

> osinfo-query os | grep rocky 
 rocky-unknown        | Rocky Linux Unknown                                | unknown  | http://rockylinux.org/rocky/unknown
 rocky8               | Rocky Linux 8                                      | 8        | http://rockylinux.org/rocky/8
 rocky8-unknown       | Rocky Linux 8 Unknown                              | 8-unknown | http://rockylinux.org/rocky/8-unknown
 rocky8.4             | Rocky Linux 8.4                                    | 8.4      | http://rockylinux.org/rocky/8.4
 rocky8.5             | Rocky Linux 8.5                                    | 8.5      | http://rockylinux.org/rocky/8.5
 rocky8.6             | Rocky Linux 8.6                                    | 8.6      | http://rockylinux.org/rocky/8.6
 rocky9               | Rocky Linux 9                                      | 9        | http://rockylinux.org/rocky/9
 rocky9-unknown       | Rocky Linux 9 Unknown                              | 9-unknown | http://rockylinux.org/rocky/9-unknown
 rocky9.0             | Rocky Linux 9.0                                    | 9.0      | http://rockylinux.org/rocky/9.0
 
 # 或者
> virt-install --osinfo list  |grep rocky
rocky9
rocky9-unknown
rocky9.0
rocky8
rocky8-unknown
rocky8.6
rocky8.5
rocky8.4
rocky-unknow

查看 vnc 端口

> virsh vncdisplay rocky-kvm01

:3

> netstat -lntup|grep 5903

tcp        0      0 0.0.0.0:5903            0.0.0.0:*               LISTEN      1101856/qemu-kvm

通过 VNC 连接控制台,在 VNC 客户端中输入宿主机的 IP,默认起始端口为 TCP 5900,每启动一个 kvm 虚拟机,对应用的 VNC 端口会 +1

由于上面的命令中指定了,因此通过 VNC 地址 vnc://10.244.244.211:5903 和密码 f 即可进入 VNC 界面

自动化安装虚拟机

可以通过 kickstart 文件,以更自动、简单的方式安装虚拟机。Kickstart 文件是一个文本配置文件。可以使用刚刚安装完成的虚拟机中的 kickstart 文件。

例如从刚刚安装的虚拟机拷贝这个文件,并更改与机器相关的信息

# Generated by Anaconda 34.25.1.14
# Generated by pykickstart v3.32
#version=RHEL9
# Use graphical install
graphical
repo --name="minimal" --baseurl=file:///run/install/sources/mount-0000-cdrom/minimal

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

# Keyboard layouts
keyboard --xlayouts='cn','us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=enp1s0 --ipv6=auto --activate
network  --hostname=rocky-kvm02

# Use CDROM installation media
cdrom

%packages
@^minimal-environment

%end

# Run the Setup Agent on first boot
firstboot --enable

# Generated using Blivet version 3.4.0
ignoredisk --only-use=vda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=vda --size=512
part pv.551 --fstype="lvmpv" --ondisk=vda --size=19967
volgroup rl_rocky --pesize=4096 pv.551
logvol / --fstype="xfs" --size=19964 --name=root --vgname=rl_rocky

# System timezone
timezone Asia/Shanghai --utc

# Root password
rootpw --iscrypted --allow-ssh $6$Q7X2OXQHwa/3oggF$BzDf2goyhSf6Tta.7Fg5GWvwLvsNLF27HymF8ul/ViDXNO7fbRU3.hS.DCbss0j7Iq0KTnG0eyvsREZaujwi8.

启动一个 python web server 作为服务端

> pwd
/mnt/beegfs/iso-images/rocky

> ls
Rocky-8.7-x86_64-dvd1.iso     Rocky-9.1-x86_64-dvd.iso      anaconda-ks.cfg
Rocky-8.7-x86_64-minimal.iso  Rocky-9.1-x86_64-minimal.iso

> python3 -m http.server 18080 --bind 0.0.0.0

挂载光盘

 mount Rocky-9.1-x86_64-minimal.iso rocky

Kickstart 环境准备完成后,执行以下命令,自动化安装虚拟机:

> qemu-img create -f qcow2 /var/lib/libvirt/images/rocky-kvm02.qcow2 20G

> virt-install \
    --virt-type kvm \
    --name rocky-kvm02 \
    --vcpus 1 \
    --memory 2048 \
    --disk /var/lib/libvirt/images/rocky-kvm02.qcow2 \
    --os-type linux \
    --os-variant rocky9 \
    --network network=default \
    --graphics vnc,password=f,listen=0.0.0.0,port=5904 \
    --location /mnt/beegfs/iso-images/rocky/Rocky-9.1-x86_64-minimal.iso \
    --extra-args="inst.ks=http://10.244.244.201:18080/anaconda-ks.cfg"

# note: rocky9 changing ks= -> inst.ks=
# refer: https://wiki.rockylinux.org/team/testing/QA/Testcase_Kickstart_Installation/?h=inst.ks#setup

# 需要终端调试,可以增加参数
# --console pty,target_type=serial
# --extra-args='console=ttyS0,115200n8 serial'

管理虚拟机

列出所有虚拟机

> virsh list --all
 Id   Name          State
-----------------------------
 7    rocky-kvm01   running
 25   rocky-kvm02   paused

挂起与恢复

使用 suspend 挂起虚拟机

> virsh suspend rocky-kvm02
Domain 'rocky-kvm02' suspended

> virsh list --all
 Id   Name          State
-----------------------------
 7    rocky-kvm01   running
 25   rocky-kvm02   paused

resume 恢复虚拟机

> virsh resume rocky-kvm02
Domain 'rocky-kvm02' resumed

> virsh list --all
 Id   Name          State
-----------------------------
 7    rocky-kvm01   running
 25   rocky-kvm02   running

console

纯文字版的控制台

virsh console rocky-kvm02

# 退出:ctrl + ']'

如果要使用 console,还需要更改虚拟机的内核参数

grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
grub2-mkconfig -o /boot/grub2/grub.cfg

vnc

快速查看 VNC 端口

> virsh vncdisplay rocky-kvm02
:4

重启

> virsh list --all
 Id   Name          State
-----------------------------
 7    rocky-kvm01   running
 25   rocky-kvm02   running


> virsh restart rocky-kvm02

自动开机

> virsh autostart rocky-kvm02

ll /etc/libvirt/qemu/autostart

重命名

domrename

删除虚拟机

要删除运行中的虚拟机,需要先关闭再删除;对于已经关闭的虚拟机,可以直接删除:

> virsh destroy kvm3
Domain 'kvm3' destroyed

> virsh list --all
 Id   Name          State
------------------------------
 1    rocky-kvm01   running
 3    rocky-kvm02   running
 4    kvm1          running
 5    kvm2          running
 -    kvm3          shut off

> virsh undefine kvm3
Domain 'kvm3' has been undefined

> virsh list --all
 Id   Name          State
-----------------------------
 1    rocky-kvm01   running
 3    rocky-kvm02   running
 4    kvm1          running
 5    kvm2          running

最后删除磁盘软件:

rm -f /var/lib/libvirt/images/kvm3.qcow2

https://www.cnblogs.com/wyzhou/p/9682238.html

https://www.jianshu.com/p/5d22e84f3328

https://mp.weixin.qq.com/s/1mmet00GTf2_PzeXBBmAbQ

https://mp.weixin.qq.com/s/l5oYNhDF4R9SLUoerYHJnw

https://weilu2.github.io/2018/10/14/%E5%9F%BA%E4%BA%8E-KickStart-%E6%97%A0%E4%BA%BA%E5%80%BC%E5%AE%88%E7%9A%84%E5%85%A8%E5%91%BD%E4%BB%A4%E8%A1%8C-KVM-%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B/