第七章:KVM虚拟机克隆
1.简介
虚拟机的克隆⽅式分为完整克隆与链接克隆
2.KVM完整克隆
格式: virt-clone --auto-clone -o 虚拟机名称 -n 克隆名称
[root@wjl ~]# virt-clone --auto-clone -o web66 -n web77
Allocating 'web01-vda-clone.raw' | 10 GB 00:00:21
Clone 'web77' created successfully.
[root@wjl ~]# virsh list --all
Id Name State
----------------------------------------------------
- web24 shut off
- web66 shut off
- web77 shut off
- web88 shut off
virt-clone这个命令是基于全克隆的,也就是拷⻉虚拟磁盘⽂件和虚拟配置⽂件来实现的完整克隆,速度慢,占⽤空间多。
3.KVM链接克隆
KVM的链接克隆是通过创建⼀个链接磁盘⽂件来实现的链接克隆,⽽kvm软件包中并没有实现全⾃动链接克隆的命令或⼯具,只 能⼿动实现.
格式: qemu-img create -f qcow2 -b 源磁盘⽂件 链接磁盘⽂件
-f 指定磁盘⽂件格式类型
-b 执⾏链接磁盘⽂件路径
[root@wjl ~]# qemu-img create -f qcow2 -F qcow2 -b /opt/web88-vda.qcow2 /opt/web99-vda.qcow2
Formatting '/opt/web99-vda.qcow2', fmt=qcow2 size=10737418240 backing_file='/opt/web88-vda.qcow2' backing_fmt='qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
#查看链接磁盘⽂件信息
[root@wjl ~]# qemu-img info /opt/web99-vda.qcow2
image: /opt/web99-vda.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: /opt/web88-vda.qcow2
backing file format: qcow2
Format specific information:
compat: 1.1
lazy refcounts:
#导出原始机的配置⽂件,并修改磁盘⽂件路径
[root@wjl ~]# virsh dumpxml web88 > web99-vda.xml
#需要修改的内容如下:
#修改虚拟机名称 <name > web99</name >
#删除UUID <uuid > 727a1566-ff36-4ed0-a096-f844f6bf3262</uuid >
#修改磁盘路径 <source file ='/opt/web99.qcow2' />
#删除MAC地址 <mac address ='52:54:00:bd:b6:44' />
[root@wjl opt]# vi web99.xml
<domain type ='kvm' >
<name > web99</name >
<memory unit ='KiB' > 1048576</memory >
<currentMemory unit ='KiB' > 1048576</currentMemory >
<vcpu placement ='static' > 1</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 ='md-clear' />
<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 ='/opt/web99-vda.qcow2' />
<target dev ='vda' bus ='virtio' />
<address type ='pci' domain ='0x0000' bus ='0x00' slot ='0x06' 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 ='0x04' function ='0x7' />
</controller >
<controller type ='usb' index ='0' model ='ich9-uhci1' >
<master startport ='0' />
<address type ='pci' domain ='0x0000' bus ='0x00' slot ='0x04' function ='0x0' multifunction ='on' />
</controller >
<controller type ='usb' index ='0' model ='ich9-uhci2' >
<master startport ='2' />
<address type ='pci' domain ='0x0000' bus ='0x00' slot ='0x04' function ='0x1' />
</controller >
<controller type ='usb' index ='0' model ='ich9-uhci3' >
<master startport ='4' />
<address type ='pci' domain ='0x0000' bus ='0x00' slot ='0x04' function ='0x2' />
</controller >
<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 ='0x05' function ='0x0' />
</controller >
<controller type ='pci' index ='0' model ='pci-root' />
<interface type ='network' >
<source network ='default' />
<model type ='virtio' />
#导入虚拟机
[root@wjl opt]# virsh define web99.xml
Domain web99 defined from web99.xml
#开启虚拟机
[root@wjl opt]# virsh start web99
Domain web99 started
第⼋章:KVM虚拟机⽹络配置
1.KVM 虚拟机⽹络介绍
libvirt服务安装后,默认会安装⼀块 virbr0 的虚拟⽹卡,libvirt在创建KVM虚拟机后,默认使⽤了⼀个名为default的nat⽹络,地 址段为 192.168.122.0/24,这个⽹络默认使⽤virbr0(虚拟交换机)作为桥接接⼝,为虚拟机提供⽹络转发服务,默认⽹段 192.168.122.0/24,使⽤dnsmasq来为使⽤nat⽹络的虚拟机提供dns及dhcp服务。
dhcp分配到虚拟机的ip列表在以下⽂件查看:/var/lib/libvirt/dnsmasq/default.conf
[root@wjl opt]# cat /var /lib/libvirt/dnsmasq/default .conf
##WARNING: THIS IS AN AUTO-GENERATED FILE . CHANGES TO IT ARE LIKELY TO BE
##OVERWRITTEN AND LOST. Changes to this configuration should be made using:
## virsh net-edit default
## or other application using the libvirt API.
##
## dnsmasq conf file created by libvirt
strict -order
pid-file =/var /run/libvirt/network/default .pid
except -interface =lo
bind-dynamic
interface =virbr0
dhcp-range=192.168 .122.2 ,192.168 .122.254
dhcp-no-override
dhcp-authoritative
dhcp-lease-max=253
dhcp-hostsfile=/var /lib/libvirt/dnsmasq/default .hostsfile
addn-hosts=/var /lib/libvirt/dnsmasq/default .addnhosts
[root@wjl opt]#
2.修改virbr0地址段
可以修改⽹段,也可以⽤默认⽹段,如需修改通过如下⽅式:
[root@kvm01 ~]# virsh net-edit default
#修改virbiro地址
<ip address ='192.168.10.1' netmask ='255.255.255.0' >
#定义dncp范围
<dhcp >
<range start ='192.168.10.2' end ='192.168.10.254' />
<network >
<name > default</name >
<uuid > 9a99c3e6-e4b3-4350-a6e1-93c8f4c149ed</uuid >
<forward mode ='nat' />
<bridge name ='virbr0' stp ='on' delay ='0' />
<mac address ='52:54:00:dd:04:d7' />
<ip address ='192.168.10.1' netmask ='255.255.255.0' >
<dhcp >
<range start ='192.168.10.2' end ='192.168.10.254' />
</dhcp >
</ip >
</network >
重新定义⽹络,default⽹络的配置⽂件:/etc/libvirt/qemu/networks/default.xml
[root@wjl opt]# virsh net-define /etc/libvirt/qemu/networks/default.xml
Network default defined from /etc/libvirt/qemu/networks/default.xml
[root@wjl opt]# cat /etc/libvirt/qemu/networks/default.xml
<network >
<name > default</name >
<uuid > 9a99c3e6-e4b3-4350-a6e1-93c8f4c149ed</uuid >
<forward mode ='nat' />
<bridge name ='virbr0' stp ='on' delay ='0' />
<mac address ='52:54:00:dd:04:d7' />
<ip address ='192.168.10.1' netmask ='255.255.255.0' >
<dhcp >
<range start ='192.168.10.2' end ='192.168.10.254' />
</dhcp >
</ip >
</network >
停⽌⽹卡(关闭运⾏的虚拟机)
[root@wjl opt ]# virsh net-destroy default
Network default destroyed
启动⽹卡
[root@wjl opt]# virsh net- start default
Network default started
重启libvirtd
[root@wjl opt ]# systemctl restart libvirtd
查看virbr0⽹络
[root@wjl opt]# ip a s virbr0
7 : virbr0: <BROADCAST,MULTICAST,UP ,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52 :54 :00 :dd :04 :d7 brd ff:ff:ff:ff:ff:ff
inet 192.168 .10 .1 /24 brd 192.168 .10 .255 scope global virbr0
valid_lft forever preferred_lft forever
3.KVM虚拟机配置固定IP
启动⼀个KVM并登录并配置固定IP地址
localhost login: root
Password:
Last login: Mon Feb 27 17 :19 :02 on tty1
[root@localhost ~ ]# ip a s
1 : lo: < LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/ loopback 00 :00 :00 :00 :00 :00 brd 00 :00 :00 :00 :00 :00
inet 127.0 .0 .1 / 8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1 / 128 scope host
valid_lft forever preferred_lft forever
2 : eth0: < BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ ether 52 :54 :00 :bb:01 :b8 brd ff:ff:ff:ff:ff:ff
inet 192.168 .10 .227 / 24 brd 192.168 .10 .255 scope global noprefixroute dynamic eth0
valid_lft 3521 sec preferred_lft 3521 sec
inet6 fe80::fdb9:25 d7:ad3e:bb64/ 64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~ ]#
KVM虚拟机配置固定IP
[root@localhost network-scripts]
TYPE ="Ethernet"
PROXY_METHOD ="none"
BROWSER_ONLY ="no"
BOOTPROTO ="none"
DEFROUTE ="yes"
IPV4_FAILURE_FATAL ="no"
IPV6INIT ="yes"
IPV6_AUTOCONF ="yes"
IPV6_DEFROUTE ="yes"
IPV6_FAILURE_FATAL ="no"
IPV6_ADDR_GEN_MODE ="stable-privacy"
NAME ="eth0"
UUID ="fd99a289-31a4-469c-a2fb-9a78873400f6"
DEVICE ="eth0"
ONBOOT ="yes"
IPADDR =192.168 .10.10
NETMASK =255.255 .255.0
GATEWAY =192.168 .10.2
DNS1 =8.8 .8.8
[root@localhost network-scripts]
[root@localhost network-scripts]
提示:KVM虚拟机访问外部⽹络记住iptables SNAT模式进⾏了源地址转换,如果外部⽹络需要访问到KVM虚拟机中,需要在宿主 机通过iptables添加端⼝映射来实现,如果不希望通过端⼝映射来实现,可以使⽤KVM的桥接⽹络更加⽅便。
4.KVM虚拟机桥接⽹络
在KVM虚拟机中,Bridge(⽹桥)模式可以让客户机和宿主机共享⼀个物理⽹络设备来连接⽹络,客户机有⾃⼰独⽴的IP地址,可 以直接连接与宿主机⼀模⼀样的⽹络,客户机可以访问外部⽹络,外部⽹络也可以直接访问客户机,Bridge模式使⽤⾮常⽅便,应 ⽤也⾮常⼴泛。
5.创建桥接⽹卡
将宿主机⼯作的⽹卡绑定到 br0 桥接⽹卡上
格式: virsh iface-bridge ens32 br0
ens32 为宿主机⽹卡
br0 为桥接⽹卡
[root@kvm01 ~ ]# virsh iface-bridge ens32 br0
使⽤附加设备 br0 ⽣成桥接 ens32 失败
重启系统后登录查看⽹卡
[root@wjl ~ ]# ip a s ens32
[root@wjl ~ ]# ip a s br0
...
inet 192.168 .0 .17 /24 brd 192.168 .0 .255 scope global br0
安装系统并配置固定IP地址
6. 创建KVM虚拟机并使⽤桥接⽹卡
#此时,物理⽹⼝ enp32⽹卡没有⾃⼰的 IP 地址, ⽹桥寄⽣在它身上,⽹桥与物理⽹⼝ MAC 地址相同。
测试访问外部⽹络
[root@wjl ~ ]# ping www.baidu.com
ping: www.baidu.com: 未知的名称或服务
添加DNS服务(修改br0⽹卡即可)
[root@wjl ~ ]# vim /etc/sysconfig/network-scripts/ifcfg-br0
上述内容省略...
DNS1="223.5.5.5"
重启⽹络
[root@wjl ~ ]# systemctl restart network
[root@wjl ~ ]# ping www.baidu.com
6.创建KVM虚拟机并使⽤桥接⽹卡
# virt- install
#此时,物理⽹⼝ enp32⽹卡没有⾃⼰的 IP 地址, ⽹桥寄⽣在它身上,⽹桥与物理⽹⼝ MAC 地址相同
测试访问外部⽹络
ping: www.baidu.conm: Name or service not known
[root@localhost ~ ]# ping www.baidu.com
PING www.a.shifen.com (112.80 .248 .76 ) 56 (84 ) bytes of data.
64 bytes from 112.80 .248 .76 (112.80 .248 .76 ): icmp_seq= 1 ttl= 128 time = 38.6 ms
64 bytes from 112.80 .248 .76 (112.80 .248 .76 ): icmp_seq= 2 ttl= 128 time = 40.5 ms
64 bytes from 112.80 .248 .76 (112.80 .248 .76 ): icmp_seq= 3 ttl= 128 time = 32.8 ms
^ C
添加DNS服务(修改br0⽹卡即可)
[root@wjl ~ ]# vim / etc/ sysconfig/ network- scripts/ ifcfg- br0
重启⽹络
[root@wjl ~ ]# systemctl restart network
[root@wjl ~ ]# ping www.baidu.com
6.创建KVM虚拟机并使⽤桥接⽹卡
virt-install
7.修改虚拟机使⽤桥接⽹卡
格式: virsh domiflist 虚拟机名称
[root@localhost ~]# virsh domiflist web111
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge br0 virtio 52:54:00:7b:7d:db
关机状态下修改⽹络模式为bridge
[root@localhost ~ ]# virsh edit web111
Domain web111 XML configuration edited.
[root@wjl ~ ]# virsh edit web111
#/network 搜索关键字
<interface type ='bridge' > 修改类型
<source bridge='br0' /> ⽹卡名称
启动虚拟机验证
[root@localhost ~ ]# virsh start web111
Domain web111 started
通过VNC登录配置固定IP
[root@localhost ~ ]# virsh vncdisplay web111
:0
[root@localhost ~]
<target dev='vnet0' />
[root@localhost ~]
bridge name bridge id STP enabled interfaces
br0 8000.000c2900bb00 yes ens33
vnet0
virbr0 8000.525400c69e8e yes virbr0-nic
第九章:KVM虚拟机硬件的热添加
1.热添加硬盘
案例:为web02虚拟机添加⼀块新qcow2硬盘,并对硬盘空间进⾏扩容(KVM虚拟机默认磁盘名称:vda、vdb、vdc依次类 推,可进⼊KVM内部查看)
创建⼀块qcow2格式的虚拟硬盘 ,容量为10G
[root@wjl opt ]
Formatting '/opt/web01-vdb.qcow2' , fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off
[root@wjl opt ]
CentOS-7-x86_64-DVD-1804.iso web01-vda.raw web01-vdb.qcow2
[root@wjl opt ]
image: /opt/web01-vdb.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
永久添加硬盘
--live 在线添加
--config 永久保存配置(不加临时⽣效)
[root@wjl opt] # virsh attach-disk web01 /opt/web01-vdb.qcow2 vdb --subdriver qcow2 --live --config
Disk attached successfully
进⼊KVM中验证磁盘,并使⽤磁盘
# 查看磁盘信息
#格式化硬盘为xfs⽂件系统(⽆需分区,可以直接格式化⽂件系统)
#查看文件系统类型
#挂载到目录存储数据
#查看分区使用情况
命令⾏执⾏umount临时卸载命令
永久剥离(如需剥离磁盘,需要提前卸载)
[root@wjl opt] # virsh detach-disk web01 vdb --live --config
Disk detached successfully
对刚才剥离的qcow2磁盘空间扩容(⽆法在线扩容,必须先剥离)
[root@wjl opt ]
Image resized.
[root@wjl opt ]
image: /opt/web01-vdb.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 27M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
添加硬盘到KVM虚拟机(宿主机进⾏)
[root@wjl opt] # # virsh attach-disk web01 /opt/web01-vdb.qcow2 vdd --subdriver qcow2 --live --config
Disk attached successfully
进⼊KVM虚拟机验证磁盘容量
挂载并刷新⽂件系统(在kvm虚拟机中进⾏)
刷新文件系统(查看容量)
2.热添加⽹卡
案例:为KVM虚拟机web03添加⼀块⽹卡
格式: virsh attach-interface 虚拟机名称 --type bridge --source ⽹桥名 --model virtio
--type ⽤于指定⽹卡类型
--source 宿主机⽹桥名
--model virtio 驱动模式(⽤于指定⽹卡类型以eth开头)
--live 在线添加
--config 永久保存配置(不加临时⽣效)
[root@localhost ~] # virsh attach-interface web02 --type bridge --source br0 --model virtio --live --config
Interface attached successfully
进⼊KVM虚拟机验证⽹卡 (添加完⽹卡需要⼿动配置新添加得⽹卡配置⽂件)
[root@localhost ~ ]# ip a s
3 : eth1: < BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ ether 52 :54 :00 :6 b:84 :83 brd ff:ff:ff:ff:ff:ff
inet 192.168 .88 .131 / 24 brd 192.168 .88 .255 scope global noprefixroute dynamic eth1
valid_lft 1769 sec preferred_lft 1769 sec
inet6 fe80::3886 :d27c:648 c:b985/ 64 scope link noprefixroute
valid_lft forever preferred_lft forever
热删除⽹卡
--mac 通过mac地址删除
[root@localhost /] # virsh detach-interface web02 --type bridge --mac 52 :54 :00 :6 b:84 :83 --live --config
Interface detached successfully
3.热添加内存
热添加内存与CPU前提是创建KVM虚拟机时设置maxmemory与maxvcpus参数
maxmemory 定义虚机⼤可以分配内存的⼤⼩
maxvcpus 定义虚拟机⼤可以使⽤的CPU核数
创建KVM虚拟机,并设置maxmemory与maxvcpus的值
[root@wjl opt]# virt-install --virt-type kvm --os-type linux --os-variant rhel7 --name web04 --memory 1024 ,maxmemory=2048 --vcpus 1 ,maxvcpus=4 --disk /opt/web04-vda.qcow2,format=qcow2,size=30 --cdrom /opt/CentOS-7 -x86_64-DVD-1804.i so --network network=default --graphics vnc,listen=0.0 .0 .0 --noautoconsole
Starting install...
Allocating 'web04-vda.qcow2' | 30 GB 00 :00 :00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
安装系统后进⼊到KVM虚拟机查看内存
[root@localhost ~ ]# free -h
虚拟机热添加2048M内存(总空间)
格式: virsh setmem 虚拟机名称 内存⼤⼩
--live 在线添加
--config 永久保存配置(不加临时⽣效)
[root@wjl opt] # virsh setmem web04 2048 M --live --config
KVM虚拟机验证内存
提示:也可以缩减内存,但缩减时必须要保证虚拟机运⾏所需的空间,否则虚拟机会出现异常。
4.热添加CPU
KVM虚机设置maxvcpus后在线添加CPU核数
格式: virsh setvcpus web04 2 --live --config
--live 在线添加
--config 永久保存配置(不加临时⽣效)
[root@wjl opt] # virsh setvcpus web04 2 --live --config
提示:如果需要缩减cpu核数,不⽀持在线 --live 参数,缩减后需要重新启动⽣效。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步