KVM操作手册
snow chuai汇总、整理、撰写---2020/2/4
最后更新日期---2025/03/05
1. 安装KVM及桥接配置
1) 确认本地设备已经开启虚拟化支持
[root@node1 ~]# grep -E '(vmx|svm)' /proc/cpuinfo
2) 安装KVM
[root@node1 ~]# yum install qemu-kvm libvirt virt-install bridge-utils -y
3) 确认KVM被成功加载
[root@node1 ~]# lsmod | grep kvm
kvm_intel 188644 0
kvm 621480 1 kvm_intel
irqbypass 13503 1 kvm
4) 启动KVM
[root@node1 ~]# systemctl enable --now libvirtd
5) 设置br0桥接设备
# 创建并设置br0
[root@node1 ~]# nmcli connection add type bridge autoconnect yes con-name br0 ifname br0
Connection 'br0' (491dd4da-1641-4ff2-bf5b-17a928d700d1) successfully added.
[root@node1 ~]# nmcli connection modify br0 ipv4.addresses 192.168.10.111/24 ipv4.method manual
[root@node1 ~]# nmcli connection modify br0 ipv4.gateway 192.168.10.1
[root@node1 ~]# nmcli connection modify br0 ipv4.dns 192.168.10.11
# 将eth0加入至br0中
[root@node1 ~]# nmcli device disconnect eth0
[root@node1 ~]# nmcli connection delete eth0
Connection 'eth0' (43e81ba8-5171-4ca6-ae34-58c64d73d9a9) successfully deleted.
[root@node1 ~]# nmcli connection add type bridge-slave autoconnect yes con-name eth0 ifname eth0 master br0
Connection 'eth0' (b3eea871-56c1-4cfb-a57b-d7df454ee8a8) successfully added.
[root@node1 ~]# nmcli device disconnect br0
Device 'br0' successfully disconnected.
[root@node1 ~]# nmcli device connect eth0
Device 'eth0' successfully activated with 'b3eea871-56c1-4cfb-a57b-d7df454ee8a8'.
[root@node1 ~]# ip a s br0
9: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:13:3c:d4 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.111/24 brd 192.168.10.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
inet6 fe80::f855:966f:1de9:eed6/64 scope link noprefixroute
valid_lft forever preferred_lft forever
|
2. 创建虚拟机-CLI模式
1) 创建虚拟机
(1) 具备远程服务器的安装方法
[root@node1 ~]# virt-install \
--name centos77 \
--ram 2048 \
--disk path=/var/lib/libvirt/images/c77.img,size=10,format=qcow2 \
--vcpus 2 \
--os-type linux \
--os-variant rhel7 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'ftp://csrv.1000cc.net/pub/CentOS/7/os/x86_64' \
--extra-args 'console=ttyS0,115200n8 serial'
(2) ISO安装方法
[root@node1 ~]# virt-install \
--name centos77 \
--ram 2048 \
--disk path=/var/lib/libvirt/images/c77.img,size=8,format=qcow2 \
--vcpus 2 \
--os-type linux \
--os-variant rhel7 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial --location '/mnt/centos7.iso' \
--extra-args 'console=ttyS0,115200n8 serial'
(3) 使用VNC方法
[root@node1 ~]# virt-install \
> --name centos79 \
> --ram 4096 \
> --disk path=/var/lib/libvirt/images/c79.img,size=8,format=qcow2 \
> --vcpus 4 \
> --os-type linux \
> --os-variant rhel7 \
> --network bridge=br0 \
> --noautoconsole --graphics vnc,listen=0.0.0.0,port=6222 \
> --location '/mnt/CentOS-7-x86_64-Minimal-2207-02.iso'
# 开始安装
Starting install...
Retrieving file vmlinuz... | 6.4 MB 00:00:00
Retrieving file initrd.img... | 53 MB 00:00:00
Allocating 'c77.img' | 8.0 GB 00:00:02
......
......
安装界面
......
......
1) Start VNC
2) Use text mode
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: 2
================================================================================
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(ftp://192.168.10.10/pub/CentOS (Minimal Install)
/7/os/x86_64) 6) [x] Kdump
5) [x] Installation Destination (Kdump is enabled)
(Automatic partitioning 8) [x] Root password
selected) (Password is set.)
7) [x] Network configuration
(Wired (eth0) connected)
9) [ ] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: b
================================================================================
================================================================================
Progress
Setting up the installation environment
......
......
# 安装完成
CentOS Linux 7 (Core)
Kernel 3.10.0-1062.el7.x86_64 on an x86_64
kvm1 login:
2) 离开Guest主机(非关机)
CentOS Linux 7 (Core)
Kernel 3.10.0-1062.el7.x86_64 on an x86_64
kvm1 login: ^] # 按住"Ctrl"+"]"
[root@kvm1 ~]#
3) 连接Guest主机
[root@node1 ~]# virsh console centos77
Connected to domain centos77
Escape character is ^] # 回车
CentOS Linux 7 (Core)
Kernel 3.10.0-1062.el7.x86_64 on an x86_64
kvm1 login:
# 如果以后使用virsh console的时候,卡在"Escape character is ^] "不动.
****解决办法****
1) 确认ttyS0存在,且未被注释(如果注释或不存在请手动修改或增加)
[root@node1 ~]# cat /etc/securetty | grep ttyS0
2) 开启虚机允许ttyS0登录(向内核加入一个参数)
[root@node1 ~]# grubby --update-kernel=ALL --args="console=ttyS0"
[root@node1 ~]# reboot
# 如果想确认虚拟机磁盘类型,可执行以下命令操作.
[root@node1 ~]# qemu-img info /var/lib/libvirt/images/c77.img
image: c77.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 136K
cluster_size: 65536
# 如果想转换虚拟机磁盘类型,可执行以下命令操作.
-p : 显示镜像的转换进度
-f : 指定被转换的镜像是什么格式
-O: 指定转换后的镜像格式
# 例将raw镜像格式转换为qcow2
[root@node1 ~]# qemu-img convert -p -f raw -O qcow2 c77.img c77.qcow2
|
3. Libvirt基本操作
1) 列出正在运行实例(虚拟机)
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
4 centos77 running
2) 列出所有的实例(包含未运行的实例)
[root@node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
4 centos77 running
- centos81 shut off
3) 关闭实例
[root@node1 ~]# virsh shutdown centos77
Domain centos77 is being shutdown
4) 强制关闭电源
[root@node1 ~]# virsh destroy centos77
Domain centos77 destroyed
5) 运行实例
[root@node1 ~]# virsh start centos77
Domain centos77 started
6) 开机即自动启动实例
[root@node1 ~]# virsh autostart centos77
Domain centos77 marked as autostarted
7) 关闭自启动实例
[root@node1 ~]# virsh autostart --disable centos77
Domain centos77 unmarked as autostarted
8) 暂停(挂起)实例
[root@node1 ~]# virsh suspend centos77
Domain centos77 suspended
9) 恢复挂起的实例
[root@node1 ~]# virsh resume centos77
Domain centos77 resumed
(10) 对本地KVM服务器进行克隆
[root@node1 ~]# virsh shutdown centos77
Domain centos77 is being shutdown
# 说明
-o:指定要克隆的实例名称
-n:命名新克隆的实例
-f:指定克隆出来的磁盘镜像存放的路径
--connect:如果本地克隆,可忽略此参数。远程克隆时需要调用
[root@node1 ~]# virt-clone --connect=qemu:///system -o centos77 -n node1 -f /var/lib/libvirt/images/node1.img
Allocating 'node1.img' | 8.0 GB 00:00:04
Clone 'node1' created successfully.
# 验证克隆结果
[root@node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos77 shut off
- node1 shut off
[root@node1 ~]# ls -l /var/lib/libvirt/images/node1.img # 磁盘所在位置
-rw------- 1 root root 1595867136 Feb 4 21:06 /var/lib/libvirt/images/node1.img
[root@node1 ~]# ls -l /etc/libvirt/qemu/node1.xml # 配置文件所在位置
-rw------- 1 root root 3300 Feb 4 21:06 /etc/libvirt/qemu/node1.xml
(11) 连接远程KVM服务器进行克隆
[root@node1 ~]# virt-clone --connect=qemu+ssh://root@csrv.1000cc.net/system -o c77 -n node1 -f /var/lib/libvirt/images/node1.img
root@csrv.1000cc.net's password:
Allocating 'node1.img' | 20 GB 00:00:44
Clone 'node1' created successfully.
[root@node1 ~]# virsh -c qemu+ssh://root@192.168.10.10/system list --all
......
- node11 shut off
......
(12) 删除虚拟机
[root@node1 ~]# virsh undefine node1
Domain node1 has been undefined
(13) 创建实例快照
[root@node1 ~]# virsh snapshot-create-as centos77 base
Domain snapshot base created
(14) 查看实例快照
[root@node1 ~]# virsh snapshot-list centos77
Name Creation Time State
------------------------------------------------------------
base 2020-02-04 21:17:23 +0800 shutoff
(15) 恢复实例快照
[root@node1 ~]# virsh snapshot-revert centos77 base
(16) 删除实例快照
[root@node1 ~]# virsh snapshot-delete centos77 base
Domain snapshot base deleted
(17) 进入virsh SHELL环境并退出
[root@node1 ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh # quit
[root@node1 ~]#
|
4. 创建虚拟机-GUI模式
1) 本地需要有GUI环境或服务器安装好GUI管理工具后,开启SSH X11转发
2) 安装GUI管理程序
[root@node1 ~]# yum install virt-manager -y
3) 在菜单栏上可以找到virt-manager软件图标
4) 基于GUI创建实例
# 点击图标创建实例
# 更改显示协议为VNC
|
5. 安装及使用virt-tools
1) 安装virt-tools
[root@node1 ~]# yum install libguestfs-tools libguestfs-xfs virt-top -y
2) 使用virt-tools
(1) 设定环境变量
[root@node1 ~]# export LIBGUESTFS_BACKEND=direct
(2) 显示自带的OS模板
[root@node1 ~]# virt-builder -l
(3) 创建基于一个centos7.6的模板镜像
[root@node1 ~]# virt-builder centos-7.6 --format qcow2 --size 20G -o centos76.qcow2 --root-password password
(4) 安装CentOS7.6
[root@node1 ~]# virt-install \
--name centos-76 \
--ram 4096 \
--disk path=/var/kvm/images/centos76.qcow2 \
--vcpus 2 \
--os-type linux \
--os-variant rhel7.6 \
--network bridge=br0 \
--graphics none \
--serial pty \
--console pty \
--boot hd \
--import
(5) 显示一个虚拟机中的文件
[root@node1 ~]# virt-ls -l -d centos76 /etc
(6) 查看虚拟机中的文件
[root@node1 ~]# virt-cat -d centos76 /etc/passwd
(7) 编辑虚拟中的文件
# 虚拟机必须关机的情况下使用
[root@node1 ~]# virt-edit -d centos76 /etc/profile
(8) 显示虚拟机中的磁盘使用率
[root@node1 ~]# virt-df -d centos76
(9) 挂载虚拟机磁盘
# 虚拟机必须关机的情况下使用
[root@node1 ~]# guestmount -d centos76 -i /mnt
(10) 将虚拟机的文件复制到当前目录
[root@node1 ~]# virt-copy-out -d centos76 /etc/passwd ./
(11) 将当前主机的文件复制到虚拟机指定目录
# 虚拟机必须关机的情况下使用
[root@node1 ~]# virt-copy-in -d centos77 test.txt /root/
|
6. SPICE安装及使用
1) 安装SPICE服务器端软件
[root@node1 ~]# yum install spice-server spice-protocol -y
2) 配置实例的SPICE调用
(1) 对已经存在的实例进行配置
[root@node1 ~]# virsh edit centos7
......
......
......
......
......
......
# 找到如下字段,在其下添加如下内容
<input type='keyboard' bus='ps2'/>
# 设定使用协议为spice,监听端口为5900,监听地址为0.0.0.0,接入密码为password
<graphics type='spice' port='5900' autoport='no' listen='0.0.0.0' passwd='password'>
<listen type='address' address='0.0.0.0'/>
</graphics>
# 设定声卡类型为ac97
<sound model='ac97'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</sound>
# 设定显卡
<video>
<model type='qxl' ram='65536' vram='32768' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
[root@node1 ~]# systemctl restart libvirtd
[root@node1 ~]# virsh start centos7
(2) 创建新实例时直接使用SPICE
[root@node1 ~]# virt-install \
--name c77 \
--ram 3000 \
--disk path=/var/lib/libvirt/images/c77.img,size=10 \
--vcpus=4 \
--os-type linux \
--os-variant=rhel7 \
--network bridge=br0 \
--graphics spice,listen=0.0.0.0,password=password,keymap=en \
--video qxl \
--cdrom /tmp/c77.ISO
(3) 防火墙设定
[root@node1 ~]# firewall-cmd --add-port=5900/tcp --permanent
[root@node1 ~]# firewall-cmd --reload
2) 客户端连接
(1) 方式1
选择”远程桌面查看器” → 主界面 → 连接(Connet) → 选择”SPICE → Host → 实例IP:5900 →连接 Connet → 输入密码 → 授权(Authenticate) → 打开
(2) 方式2
[root@node1 ~]# yum install virt-viewer
(3) 方式3(Ubuntu/Linuxmint需要安装支持的组件
[snow@linuxmint ~]$ sudo apt install gir1.2-spiceclientgtk-3.0 sice-vdagent -y
|
7. CPU透传(让虚拟实例安装KVM服务器并建立虚拟机)
1) 启动透传
[root@node1 ~]# vim /etc/modprobe.d/kvm-nested.conf
options kvm_intel nested=1
[root@node1 ~]# modprobe -r kvm_intel # 卸载内核模块kvm_intel
[root@node1 ~]# modprobe kvm_intel #重新加载内核模块kvm_intel
[root@node1 ~]# cat /sys/module/kvm_intel/parameters/nested
Y
2) 设定实例
[root@node1 ~]# virsh edit centos77
......
......
......
......
......
......
# 将CPU模式改为以下内容----开启'透传模式'
<cpu mode='host-passthrough' check='partial'>
<model fallback='allow'/>
</cpu>
......
......
......
......
......
......
|
8. 基于本地存储的热迁移
1) 更新QEMU
[root@node1 ~]# /usr/libexec/qemu-kvm -version
QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-167.el7_7.4), Copyright (c) 2003-2008 Fabrice Bellard
[root@node1 ~]# yum install centos-release-qemu-ev -y
[root@node1 ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-QEMU-EV.repo
[root@node1 ~]# yum --enablerepo=centos-qemu-ev install qemu-kvm-ev -y
[root@node1 ~]# systemctl restart libvirtd
[root@node1 ~]# /usr/libexec/qemu-kvm -version
QEMU emulator version 2.12.0 (qemu-kvm-ev-2.12.0-33.1.el7_7.4)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers
2) 配置防火墙
[root@node1 ~]# firewall-cmd --add-service={libvirt,libvirt-tls} --permanent
[root@node1 ~]# firewall-cmd --add-port=49152-49215/tcp --permanent
[root@node1 ~]# firewall-cmd --reload
[root@node2 ~]# firewall-cmd --add-service={libvirt,libvirt-tls} --permanent
[root@node2 ~]# firewall-cmd --add-port=49152-49215/tcp --permanent
[root@node2 ~]# firewall-cmd --reload
4) 于node2节点创建一个用来迁移的存储
(1) 确认node1上需要迁移的实例的硬盘大小
[root@node1 ~]# ll /var/lib/libvirt/images/c77.img
-rw------- 1 root root 8591573053 Feb 4 22:23 /var/lib/libvirt/images/c77.img
(2) 于node2上创建一样名称、大小的磁盘
[root@node2 ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/c77.img 8591573053
Formatting '/var/lib/libvirt/images/c77.img', fmt=qcow2 size=8591573053 cluster_size=65536 lazy_refcounts=off refcount_bits=16
(3) 将node1的实例迁移至node2上
[root@node01 ~]# virsh list
Id Name State
----------------------------------------------------
1 centos77 running
[root@node1 ~]# virsh migrate --live --copy-storage-all centos77 qemu+ssh://node2.1000cc.net/system
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
[root@node02 ~]# virsh list
Id Name State
----------------------------------------------------
1 centos77 running
(4) 将node2的实例回迁至node1上
[root@node2 ~]# virsh migrate --live --copy-storage-all centos77 qemu+ssh://node1.1000cc.net/system
[root@node2 ~]# virsh list
Id Name State
----------------------------------------------------
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
1 centos77 running
|
9. 基于共享存储的热迁移
1) 建立一个共享存储服务器(本实验以NFS作为共享存储)
[root@node3 ~]# yum install nfs-utils -y
[root@node3 ~]# vim /etc/idmapd.conf
# 修改第5行
Domain = 1000cc.net
[root@node3 ~]# vim /etc/exports
/kvmdisk 192.168.10.0/24(rw,no_root_squash)
[root@node3 ~]# systemctl enable --now rpcbind nfs-server
2) KVM Srv保存磁盘镜像的目录作为挂载点挂载NFS的共享目录
3) 防火墙设定
[root@node1 ~]# firewall-cmd --add-service={libvirt,libvirt-tls} --permanent
[root@node1 ~]# firewall-cmd --add-port=49152-49215/tcp --permanent
[root@node1 ~]# firewall-cmd --reload
[root@node2 ~]# firewall-cmd --add-service={libvirt,libvirt-tls} --permanent
[root@node2 ~]# firewall-cmd --add-port=49152-49215/tcp --permanent
[root@node2 ~]# firewall-cmd --reload
4) 设定实例
[root@node1 ~]# virsh shutdown centos77
Domain centos77 is being shutdown
[root@node1 ~]# virsh edit centos77
# 将cache模式改为none
......
......
<driver name='qemu' type='qcow2' cache='none'/>
......
......
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
14 centos77 running
5) 进行热迁移
[root@node1 ~]# virsh migrate --live centos77 qemu+ssh://node2.1000cc.net/system
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
[root@node2 ~]# virsh list
Id Name State
----------------------------------------------------
2 centos77 running
[root@node2 ~]# virsh migrate --live centos77 qemu+ssh://node1.1000cc.net/system
[root@node2 ~]# virsh list
Id Name State
----------------------------------------------------
[root@node1 ~]# virsh list
Id Name State
----------------------------------------------------
15 centos77 running
|
10. UEFI设定
1) 安装OVMF(开源虚拟机固件)
[root@node1 ~]# vim /etc/yum.repos.d/kraxel.repo
[qemu-firmware-jenkins]
name=firmware for qemu, built by jenkins, fresh from git repos
baseurl=https://www.kraxel.org/repos/jenkins/
enabled=0
gpgcheck=0
[root@node1 ~]# yum --enablerepo=qemu-firmware-jenkins install OVMF -y
[root@node1 ~]# vim /etc/libvirt/qemu.conf
# 于757行,添加如下内容
nvram = [
"/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd:/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd",
]
[root@node1 ~]# systemctl restart libvirtd
2) 更新KVM
[root@node1 ~]# /usr/libexec/qemu-kvm -version
QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-167.el7_7.4), Copyright (c) 2003-2008 Fabrice Bellard
[root@node1 ~]# yum install centos-release-qemu-ev -y
[root@node1 ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-QEMU-EV.repo
[root@node1 ~]# yum --enablerepo=centos-qemu-ev install qemu-kvm-ev -y
[root@node1 ~]# systemctl restart libvirtd
[root@node1 ~]# /usr/libexec/qemu-kvm -version
QEMU emulator version 2.9.0(qemu-kvm-ev-2.9.0-16.el7_4.13.1)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers
3) 创建UEFI虚拟机
[root@node1 ~]# virt-install \
--name Win2016 \
--ram 8000 \
--disk path=/var/kvm/images/Win2016.img,size=60 \
--vcpus=4 \
--os-type windows \
--os-variant=win10 \
--network bridge=br0 \
--graphics spice,listen=0.0.0.0,password=password \
--video qxl \
--cdrom /tmp/Win2016.ISO \
--boot uefi
4) 启动后出现UEFI Shell输入[exit]退出
5) 进入UEFI设定,如果没有更改,直接按[Continue]继续进入安装
|
11. 快照备份与迁移
1) 备份快照
[root@node2 ~]# virsh snapshot-create-as c77 base
[root@node2 ~]# cd /var/lib/libvirt/qemu/
[root@node2 ~]# tar cfpz snapshot.tgz snapshot
[root@node2 ~]# virsh snapshot-delete c77 base
[root@node2 ~]# virsh migrate --live c77 qemu+ssh://srv1.1000cc.net/system
[root@node2 ~]# scp snapshot.tgz root@node1.1000cc.net:~
2) 恢复快照
[root@node1 ~]# ls
anaconda-ks.cfg snapshot.tgz
[root@node1 ~]# tar xfpz snapshot.tgz -C /var/lib/libvirt/qemu/
[root@node1 ~]# ll /var/lib/libvirt/qemu/snapshot/c77/base.xml
-rw------- 1 root root 4712 Mar 27 18:45 /var/lib/libvirt/qemu/snapshot/c77/base.xml
3) 确认快照恢复
[root@node1 ~]# virsh snapshot-list c77
Name Creation Time State
------------------------------------------------------------
[root@node1 ~]# systemctl restart libvirtd
[root@node1 ~]# virsh snapshot-list c77
Name Creation Time State
------------------------------------------------------------
base 2020-02-04 18:43:07 +0800 running
|
12. 生成虚拟机xml配置文件,并启动虚拟机
1) 生成虚拟机xml配置文件
# 找一个已经安装好系统的磁盘镜像文件
[root@node1 ~]# cp c7-cloud.img /var/lib/libvirt/images/c7.img
[root@node1 ~]# virt-install --name=c7 \
--disk path=/var/lib/libvirt/images/c7.img,device=disk,bus=virtio,format=qcow2 \
--noautoconsole --graphics vnc,listen=0.0.0.0,port=6222 \
--ram=2048 \
--vcpus=2 \
--network bridge=br0 \
--dry-run --print-xml > /var/lib/libvirt/qemu/c7.xml
# 参数说明
--noautoconsole:禁止自动连接至虚拟机的控制台;
--graphics: 指定使用vnc协议,连接kvm server的6222端口---vnc端口范围必须大于等于5900端口;
--dry-run:执行创建虚拟机的整个过程,但不真正创建虚拟机、改变主机上的设备配置信息及将其创建的需求通知给libvirt;
--print-xml:如果虚拟机不需要安装过程(--import、--boot),则显示生成的XML而不是创建此虚拟机;默认情况下,此选项仍会创建磁盘映像;
本脚本同时采用--dry-run及--print-xml时将不会自动创建磁盘文件
2) 启动虚拟机
[root@node1 ~]# virsh define /var/lib/libvirt/qemu/c7.xml
Domain c7 defined from /var/lib/libvirt/qemu/c7.xml
[root@node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
- c7 shut off
[root@node1 ~]# virsh start c7
virsh start c7
[root@node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
1 c7 running
3) 使用vnc工具连接虚拟机
vnc工具---->连接---->kvm-server-ip-address:6222
|
13. 为KVM实例[实例启动状态下]添加网卡及硬盘
1) 添加网卡
(1) 确认实例当前所连接的网络信息
[root@srv1 ~]# virsh domiflist centos79
Interface Type Source Model MAC
-------------------------------------------------------
- bridge br0 virtio 52:54:00:a2:c6:2a
(2) 为实例添加网卡
[root@srv1 ~]# virsh attach-interface centos79 --type bridge --source br0
Interface attached successfully
(3) 确认实例已存在新网卡
[root@srv1 ~]# virsh domiflist centos79
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge br0 virtio 52:54:00:a2:c6:2a
vnet1 bridge br0 rtl8139 52:54:00:57:e5:74
(4) 重新生成实例的配置文件
[root@srv1 ~]# virsh dumpxml centos79 > /etc/libvirt/qemu/centos79.xml
(5) 为实例应用新的xml配置文件
[root@srv1 ~]# virsh define /etc/libvirt/qemu/centos79.xml
Domain centos79 defined from /etc/libvirt/qemu/centos79.xml
(6) 修改实例中新网卡的model type
# 可实现将ensxxx改为ethx的网卡编号顺序
[root@srv1 ~]# vim /etc/libvirt/qemu/centos79.xml
# 搜索关键字interface,找到第二块网卡的配置区段,将rtl8139,修改为virtio
......
......
......
......
......
......
<interface type='bridge'>
<mac address='52:54:00:57:e5:74'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</interface>
......
......
......
......
......
......
# 修改完成后,将虚拟实例关闭.在确认新网卡的总线类型
[root@srv1 ~]# virsh domiflist centos79
Interface Type Source Model MAC
-------------------------------------------------------
- bridge br0 virtio 52:54:00:a2:c6:2a
- bridge br0 virtio 52:54:00:57:e5:74
2) 添加硬盘
(1) 创建一个新硬盘
[root@srv1 ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/c7.img 5G
(2) 修改实例的XML配置文件
[root@srv1 ~]# virsh edit centos79
# 找到"<disk type='file' device='disk'>"字段,于</disk>字段下添加如下内容
......
......
......
......
......
......
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/c79.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
# 设定新的硬盘所在路径及文件名
<source file='/var/lib/libvirt/images/c7.img'/>
# 设定新盘在虚拟实例中所在设备文件名称
<target dev='vdb' bus='virtio'/>
</disk>
......
......
......
......
......
......
(3) 重新启动虚拟实例后,进入虚拟实例,查看是否存在具有vdb且大小为5G的硬盘
[root@localhost ~]# lsblk | grep vdb
vdb 252:16 0 5G 0 disk
|
如对您有帮助,请随缘打个赏。^-^