首页 > 百科知识 正文

超详细KVM虚拟化(kvm虚拟化使用教程)

时间:2023-11-20 17:08:58 阅读:891 作者:初念如常

一、知识点

熟练掌握虚拟机的基本管理、克隆、快照、磁盘管理、配置调整、虚拟机迁移。

二、环境配置1、基础环境

主机名

IP地址

CPU、内存

硬盘

网卡

kvm

192.168.200.206

2vCPU开启虚拟化、4G

20G+50G

net

三、实操1、基础环境

关闭防火墙、selinux、iptables、查看系统是否支持虚拟化

[root@kvm ~]# systemctl stop firewalld [root@kvm ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. [root@kvm ~]# setenforce 0 [root@kvm ~]# vi /etc/selinux/config SELINUX=disabled [root@kvm ~]# iptables -F [root@kvm ~]# iptables -X [root@kvm ~]# iptables -Z [root@kvm ~]# iptables-save # Generated by iptables-save v1.4.21 on Wed Mar 10 18:10:10 2021 *filter :INPUT ACCEPT [23:1560] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [13:1272] COMMIT # Completed on Wed Mar 10 18:10:10 2021 [root@kvm ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination # 查看内存大小4G [root@kvm ~]# free total used free shared buff/cache available Mem: 3866948 110560 3614428 8744 141960 3583752 Swap: 2097148 0 2097148 # 查看CPU是否支持虚拟化 vmx:intel\svm:AMD [root@kvm ~]# cat /proc/cpuinfo |grep vmx flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xsaves flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xsaves [root@kvm ~]# cat /proc/cpuinfo |grep svm # 这里因为不是AMD的CPU,所以没有输出显示。

2、格式化硬盘

# 格式化硬盘 [root@kvm ~]# mkfs.ext4 /dev/sdb mke2fs 1.42.9 (28-Dec-2013) /dev/sdb is entire device, not just one partition! Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 3276800 inodes, 13107200 blocks 655360 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2162163712 400 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done # 检查是否格式化成功 [root@kvm ~]# blkid /dev/sdb /dev/sdb: UUID="4f8488d6-1095-4dd8-af07-c7f58ef9a246" TYPE="ext4" # 挂载硬盘 [root@kvm ~]# mkdir /kvm_data [root@kvm ~]# mount /dev/sdb /kvm_data/ # 设置开机自动挂载 [root@kvm ~]# vi /etc/fstab /dev/sdb /kvm_data ext4 defaults 0 0

3、安装KVM

[root@kvm ~]# yum install -y virt-* libvirt bridge-utils qemu-img

4、配置网卡

复制一块网卡,命名为br0

分别修改两块网卡内容,重启网卡

[root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0 [root@kvm network-scripts]# vi ifcfg-br0 TYPE=Bridge BOOTPROTO=none NAME=br0 DEVICE=br0 ONBOOT=yes IPADDR=192.168.200.206 NETMASK=255.255.255.0 GATEWAY=192.168.200.2 DNS1=114.114.114.114 [root@kvm network-scripts]# vi ifcfg-eno16777736 TYPE=Ethernet BOOTPROTO=static NAME=eno16777736 DEVICE=eno16777736 ONBOOT=yes BRIDGE=br0 [root@kvm network-scripts]# systemctl restart network [root@kvm network-scripts]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000 link/ether 00:0c:29:87:40:d7 brd ff:ff:ff:ff:ff:ff 3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:0c:29:87:40:d7 brd ff:ff:ff:ff:ff:ff inet 192.168.200.206/24 brd 192.168.200.255 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::60e0:29ff:fe15:bfec/64 scope link valid_lft forever preferred_lft forever # 启动服务 [root@kvm network-scripts]# systemctl start libvirtd [root@kvm network-scripts]# ps -ef |grep libvirt root 14300 1 0 04:54 ? 00:00:00 /usr/sbin/libvirtd nobody 14406 1 0 04:55 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper root 14407 14406 0 04:55 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper root 14438 2673 0 04:55 pts/0 00:00:00 grep --color=auto libvirt # 查看网卡 [root@kvm network-scripts]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c298740d7 no eno16777736 virbr0 8000.000000000000 yes

5、安装虚拟机

[root@kvm network-scripts]# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0" ERROR Host does not support any virtualization options

安装虚拟机遇到一个报错:通过yum install -y openssl 解决

# 再次执行安装命令,执行成功,进入设置界面 [root@kvm network-scripts]# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"

超详细KVM虚拟化(kvm虚拟化使用教程)-第1张

第一个设置是设置语言、第二个设置是选择时区、第三个是设置安装源、第四个是软件的选择、第五个是安装得目标、第七个是network不需要设置、第八个是root密码、第九个是否要创建普通用户。

这里我们从1开始设置

输入1回车进入语言界面

超详细KVM虚拟化(kvm虚拟化使用教程)-第2张

回车显示全部、按68回车选择中文、按1显示简体中文

回车继续2选择亚洲、上海

回车继续选择5、安装目标

超详细KVM虚拟化(kvm虚拟化使用教程)-第3张

回车继续 使用所有的空间c

超详细KVM虚拟化(kvm虚拟化使用教程)-第4张

继续 标准分区选1

超详细KVM虚拟化(kvm虚拟化使用教程)-第5张

超详细KVM虚拟化(kvm虚拟化使用教程)-第6张

c继续

选8设置root密码

超详细KVM虚拟化(kvm虚拟化使用教程)-第7张

密码设置太简单,yes

按b开始安装

超详细KVM虚拟化(kvm虚拟化使用教程)-第8张

安装完成,回车退出,重启

超详细KVM虚拟化(kvm虚拟化使用教程)-第9张

CTRL 】 返回宿主机

6、虚拟机基本管理

检查虚拟机是否被挂载

[root@kvm ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 5.4G 13G 31% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.8M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sdb 50G 1.2G 46G 3% /kvm_data /dev/sda1 497M 127M 370M 26% /boot tmpfs 378M 0 378M 0% /run/user/0 查看虚拟机占用的磁盘 [root@kvm ~]# ll -h /kvm_data/ total 1.1G drwx------ 2 root root 16K Mar 11 04:13 lost found -rw------- 1 qemu qemu 11G Mar 11 17:44 test.img

①、查看虚拟机列表

不加 –-all 只列举出开机状态的虚拟机,

[root@kvm ~]# virsh list Id Name State ---------------------------------------------------- 2 test running

②、启动虚拟机

virsh start 虚拟机名字

③、进入虚拟机

[root@kvm ~]# virsh console test Connected to domain test Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-327.el7.x86_64 on an x86_64 # 输入用户名和密码即可 localhost login:

④、获取IP地址

dhclient eth0 [root@localhost ~]# dhclient eth0 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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 qlen 1000 link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ff inet 192.168.200.128/24 brd 192.168.200.255 scope global dynamic eth0 valid_lft 1801sec preferred_lft 1801sec inet6 fe80::5054:ff:fe50:b0bc/64 scope link valid_lft forever preferred_lft forever

⑤、开启\关闭虚拟机

virsh shutdown test //关闭虚拟机 virsh start test //开启虚拟机

⑥可以查看虚拟机配置文件

ls /etc/libvirt/qemu/

⑦、类似stop,这个是强制停止

virsh destroy test/

⑧、彻底销毁虚拟机

virsh undefine test//彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到了

⑨、宿主机开机自启

virsh autostart test//宿主机开机该虚拟机也开机 virsh autostart --disable test//解除开机启动

⑩、挂起恢复

virsh suspend test//挂起 virsh resume test//恢复

7、克隆虚拟机

先关闭虚拟机

[root@kvm ~]# virsh shutdown test Domain test is being shutdown # 关闭后,就会发现console进不去虚拟机了 [root@kvm ~]# virsh console test error: The domain is not running # 克隆速度很快 -- original 源目标 --name 被克隆的名字 ---file 被克隆存放路径 [root@kvm ~]# virt-clone --original test --name test02 --file /kvm_data/test02.img Allocating 'test02.img' | 10 GB 00:15 Clone 'test02' created successfully. # 被克隆的虚拟机同样可以使用dhclient eth0获取IP地址。

8、快照

raw格式的虚拟磁盘不支持做快照,qcow2支持

①、创建快照

[root@kvm ~]# virsh snapshot-create test Domain snapshot 1615482601 created

②、查看快照信息

[root@kvm ~]# qemu-img info /kvm_data/test.img image: /kvm_data/test.img file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.1G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 1615482601 0 2021-03-12 01:10:01 00:00:00.000 Format specific information: compat: 1.1 lazy refcounts: true

③、查看快照

列出快照

[root@kvm ~]# virsh snapshot-list test Name Creation Time State ------------------------------------------------------------ 1615482601 2021-03-12 01:10:01 0800 shutoff 查看快照信息 [root@kvm ~]# qemu-img info /kvm_data/test.img image: /kvm_data/test.img file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.1G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 1615482601 0 2021-03-12 01:10:01 00:00:00.000 Format specific information: compat: 1.1 lazy refcounts: true

④、查看当前所在快照版本

[root@kvm ~]# virsh snapshot-create test Domain snapshot 1615484184 created [root@kvm ~]# virsh snapshot-current test <domainsnapshot> <name>1615484184</name> <state>shutoff</state> <creationTime>1615484184</creationTime> <memory snapshot='no'/> <disks> <disk name='vda' snapshot='internal'/> <disk name='hda' snapshot='no'/> </disks> <domain type='kvm'> <name>test</name> <uuid>61e88908-c0c4-4aed-86a7-503f524f1b4b</uuid> <memory unit='KiB'>1048576</memory> <currentMemory unit='KiB'>524288</currentMemory> <vcpu placement='static' current='1'>2</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='/kvm_data/test.img'/> <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='bridge'> <mac address='52:54:00:50:b0:bc'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <target type='isa-serial' port='0'> <model name='isa-serial'/> </target> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <channel type='unix'> <target type='virtio' name='org.qemu.guest_agent.0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='mouse' bus='ps2'/> <input type='keyboard' bus='ps2'/> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </memballoon> <rng model='virtio'> <backend model='random'>/dev/urandom</backend> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> </rng> </devices> </domain> </domainsnapshot>

⑤、查看所有快照配置文件

[root@kvm ~]# ls /var/lib/libvirt/qemu/snapshot/test 1615484184.xml

⑥、恢复指定快照

[root@kvm ~]# virsh snapshot-revert test 1615482601

⑦、删除快照

[root@kvm ~]# virsh snapshot-delete test 1615482601 Domain snapshot 1615482601 deleted

9、磁盘格式

查看虚拟磁盘格式,命令如下:

[root@kvm ~]# qemu-img info /kvm_data/test.img image: /kvm_data/test.img file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.1G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true

创建2 GB的RAW格式磁盘,命令如下:

[root@kvm ~]# qemu-img create -f raw /kvm_data/test_1.img 2G Formatting '/kvm_data/test_1.img', fmt=raw size=2147483648 RAW格式的磁盘转换为qcow2格式,命令如下: [root@kvm ~]# qemu-img convert -O qcow2 /kvm_data/test_1.img /kvm_data/test_1.qcow2

查看test1.img大小,命令如下:

[root@kvm ~]# ls -lh /kvm_data/test_1.img -rw-r--r-- 1 root root 2.0G May 3 15:11 /kvm_data/test_1.img [root@kvm ~]# ls -lh /kvm_data/test_1.qcow2 -rw-r--r-- 1 root root 193K May 3 15:38 /kvm_data/test_1.qcow2 //可以看到qcow2文件比较小,raw文件大小和我们指定空间大小一样是2G //raw格式的磁盘性能比qcow2要好,但是raw格式的磁盘无法做快照

将test02转成raw格式,命令如下:

[root@kvm ~]# qemu-img convert -O raw /kvm_data/test02.img /kvm_data/test02_2.raw

更改磁盘格式和文件路径,然后启动test02虚拟机,命令如下:

[root@kvm ~]# virsh edit test02 ………… <disk type='file' device='disk'> <driver name='qemu' type='qcow'/> <source file='/kvm_data/test02.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> //找到上面这部分将qcow修改为raw,/kvm_data/test02.img改为/kvm_data/test02_2.raw <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/kvm_data/test02_2.raw'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> ………………

查看磁盘所属用户和组,命令如下:

[root@kvm ~]# ls -l /kvm_data/ total 3310628 drwx------ 2 root root 16384 Apr 29 00:20 lost found -rw-r--r-- 1 root root 10737418240 May 3 16:14 test02_2.raw -rw------- 1 root root 1133772800 May 3 16:02 test02.img -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2 -rw------- 1 root root 10739384832 May 3 14:14 test.img

启动虚拟机test02,然后再次查看磁盘所属用户和组,命令如下:

[root@kvm ~]# virsh start test02 Domain test02 started [root@kvm ~]# ls -l /kvm_data/ total 3310628 drwx------ 2 root root 16384 Apr 29 00:20 lost found -rw-r--r-- 1 qemu qemu 10737418240 May 3 16:14 test02_2.raw -rw------- 1 root root 1133772800 May 3 16:02 test02.img -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2 -rw------- 1 root root 10739384832 May 3 14:14 test.img //启动后所属组变化成qemu的是test02_2.raw,证明这个磁盘正在被使用

10、磁盘扩容

我们知道,在这里磁盘一共有两种格式,一种是RAW格式,一种是qcow2格式,接下来我们分别给这两种磁盘格式进行扩容。

首先扩容RAW格式,命令如下:

[root@kvm ~]# qemu-img resize /kvm_data/test02_2.raw 2G Image resized.

查看test02_2.raw信息,命令如下:

[root@kvm ~]# qemu-img info /kvm_data/test02_2.raw image: /kvm_data/test02_2.raw file format: raw virtual size: 12G (12884901888 bytes) disk size: 1.0G

进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:

[root@kvm ~]# virsh console test02 [root@localhost ~]# fdisk -l Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes //可以看到磁盘还是10 GB

退出终端,关闭test02虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:

[root@kvm ~]# virsh destroy test02 Domain test02 destroyed [root@kvm ~]# virsh start test02 Domain test02 started [root@kvm ~]# virsh console test02 [root@localhost ~]# fdisk -l Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到已经变成12 GB了。

接下来,我们怎么使用新增加的这2 GB磁盘,那就是分区,命令如下:

[root@localhost ~]# fdisk /dev/vda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n //新建分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): //按Enter键 First sector (20971520-25165823, default 20971520): //按Enter键 Using default value 20971520 Last sector, sectors or size{K,M,G} (20971520-25165823, default 25165823): //按Enter键 Using default value 25165823 Partition 3 of type Linux and of size 2 GiB is set Command (m for help): p //查看分区列表 Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM /dev/vda3 20971520 25165823 2097152 83 Linux Command (m for help): w //保存退出并退出终端 除了对已有磁盘扩容外,还可以额外增加磁盘,命令如下: [root@kvm ~]# qemu-img create -f raw /kvm_data/test02_3.raw 5G Formatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120

使用virsh edit编辑test02虚拟机,将新磁盘增加到test02虚拟机,命令如下

[root@kvm ~]# virsh edit test02 ……………… <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/kvm_data/test02_2.raw'/> <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='raw'/> <source file='/kvm_data/test02_3.raw'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/> </disk> ………………

关闭test02虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:

[root@kvm ~]# virsh destroy test02 Domain test02 destroyed [root@kvm ~]# virsh start test02 Domain test02 started [root@kvm ~]# virsh console test02 [root@localhost ~]# fdisk -l Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM /dev/vda3 20971520 25165823 2097152 83 Linux Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到多了一块5 GB的磁盘,退出终端。

接下来,我们扩容qcow2格式的磁盘,命令如下:

[root@kvm ~]# qemu-img resize /kvm_data/test.img 2G Image resized. //若提示qemu-img: Can't resize an image which has snapshots,需要删除快照

查看test.img信息,命令如下:

[root@kvm ~]# qemu-img info /kvm_data/test.img image: /kvm_data/test.img file format: qcow2 virtual size: 12G (12884901888 bytes) disk size: 1.1G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true

关闭test虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:

[root@kvm ~]# virsh destroy test Domain test destroyed [root@kvm ~]# virsh start test Domain test started [root@kvm ~]# virsh console test [root@localhost ~]# fdisk -l Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

分区新增加的2 GB磁盘,命令如下:

[root@localhost ~]# fdisk /dev/vda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): First sector (20971520-25165823, default 20971520): Using default value 20971520 Last sector, sectors or size{K,M,G} (20971520-25165823, default 25165823): Using default value 25165823 Partition 3 of type Linux and of size 2 GiB is set Command (m for help): w /保存并退出终端

新增一块qcow2格式的磁盘,命令如下:

[root@kvm ~]# qemu-img create -f qcow2 /kvm_data/test_2.img 5G Formatting '/kvm_data/test_2.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off

使用virsh edit编辑test虚拟机,将新磁盘增加到test虚拟机,命令如下:

[root@kvm ~]# virsh edit test ……………… <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/kvm_data/test.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='/kvm_data/test_2.img'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> </disk> ………………

关闭test虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:

[root@kvm ~]# virsh destroy test Domain test destroyed [root@kvm ~]# virsh start test Domain test started [root@kvm ~]# virsh console test [root@localhost ~]# fdisk -l Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0005b865 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 20971519 9972736 8e Linux LVM /dev/vda3 20971520 25165823 2097152 83 Linux Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

退出终端

11、调整CPU内存、网卡

查看虚拟机配置信息,命令如下:

[root@kvm ~]# virsh dominfo test Id: 8 Name: test UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5 OS Type: hvm State: running CPU(s): 1 CPU time: 20.8s Max memory: 1048576 KiB Used memory: 524288 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0

编辑虚拟机内存,命令如下:

[root@kvm ~]# virsh edit test ………… <memory unit='KiB'>1048576</memory> //最大内存 <currentMemory unit='KiB'>524288</currentMemory> //可用内存 <vcpu placement='static' current='1'>2</vcpu> //最大cpu ……………… //将以上内容修改为如下内容 ………… <memory unit='KiB'>1048576</memory> <currentMemory unit='KiB'>624288</currentMemory> <vcpu placement='static' current='1'>2</vcpu> …………

关闭虚拟机,重启动虚拟机,命令如下:

[root@kvm ~]# virsh destroy test Domain test destroyed [root@kvm ~]# virsh start test Domain test started

查看虚拟机配置信息,看是否修改成功,命令如下:

[root@kvm ~]# virsh dominfo test Id: 10 Name: test UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5 OS Type: hvm State: running CPU(s): 1 CPU time: 21.4s Max memory: 1048576 KiB Used memory: 624288 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0

除了这种修改方式之外,还有一种动态修改,命令如下:

[root@kvm ~]# virsh setmem test 800m //动态修改内存 [root@kvm ~]# virsh setvcpus test 2 //动态修改cpu,只可以增加不可以减少

查看配置信息,看是否修改成功,命令如下:

[root@kvm ~]# virsh dominfo test Id: 10 Name: test UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5 OS Type: hvm State: running CPU(s): 2 CPU time: 22.8s Max memory: 1048576 KiB Used memory: 819200 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0 [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml //需要把配置写入到配置文件里

查看网卡列表,命令如下:

[root@kvm ~]# virsh domiflist test Interface Type Source Model MAC ------------------------------------------------------- vnet1 bridge br0 virtio 52:54:00:93:bf:07

增加一块新的网卡,并设置为NAT网络模式(virbr0类似VMware的VMnet8),这里如果写--source br0,则网络模式为桥接,命令如下:

[root@kvm ~]# virsh attach-interface test --type bridge --source virbr0 Interface attached successfully [root@kvm ~]# virsh domiflist test Interface Type Source Model MAC ------------------------------------------------------- vnet1 bridge br0 virtio 52:54:00:93:bf:07 vnet2 bridge virbr0 rtl8139 52:54:00:29:ed:8b [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml

进入test虚拟机,查看网卡信息,命令如下:

[root@kvm ~]# virsh console test [root@localhost ~]# ifconfig ens10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.122.8 netmask 255.255.255.0 broadcast 192.168.122.255 inet6 fe80::5054:ff:fe29:ed8b prefixlen 64 scopeid 0x20<link> ether 52:54:00:29:ed:8b txqueuelen 1000 (Ethernet) RX packets 5186 bytes 7413267 (7.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1480 bytes 84889 (82.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.16.129 netmask 255.255.255.0 broadcast 192.168.16.255 inet6 fe80::5054:ff:fe15:abbd prefixlen 64 scopeid 0x20<link> ether 52:54:00:15:ab:bd txqueuelen 1000 (Ethernet) RX packets 13 bytes 1328 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 16 bytes 1668 (1.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 52:54:00:93:bf:07 txqueuelen 1000 (Ethernet) RX packets 55 bytes 8520 (8.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

12、迁移虚拟机

首先关闭虚拟机,命令如下:

[root@kvm ~]# virsh shutdown test Domain test is being shutdown

查看虚拟机磁盘所在目录,命令如下:

[root@kvm ~]# virsh domblklist test Target Source ------------------------------------------------ vda /kvm_data/test.img vdb /kvm_data/test_2.img hda - [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test03.xml //如果是远程机器,需要把该配置文件拷贝到远程机器上 [root@kvm ~]# rsync -av /kvm_data/test.img /kvm_data/test03.img //-bash: rsync: command not found则yum install -y rsync //如果是迁移到远程,则需要把该磁盘文件拷贝到远程机器上 sending incremental file list test.img sent 10,742,006,844 bytes received 35 bytes 60,179,310.25 bytes/sec total size is 10,739,384,832 speedup is 1.00

因为是迁移到本机,配置文件用的是test子机的配置,不改会有冲突,所以需要修改该文件。如果是远程机器不用修改,命令如下:

[root@kvm ~]# vi /etc/libvirt/qemu/test03.xml <name>test03</name> //修改domname: //修改uuid(随便改一下数字,位数不要变) <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/kvm_data/test03.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> //修改磁盘路径 定义新虚拟机,命令如下: [root@kvm ~]# virsh define /etc/libvirt/qemu/test03.xml Domain test03 defined from /etc/libvirt/qemu/test03.xml

查看虚拟机列表,会发现新迁移的虚拟机test03,命令如下:

[root@kvm ~]# virsh list --all Id Name State ---------------------------------------------------- 6 test02 running - test shut off - test03 shut off

遇到的问题:1、处理器开启虚拟化,遇见这个问题

超详细KVM虚拟化(kvm虚拟化使用教程)-第10张

解决方法:

管理员运行cmd

执行:bcdedit /set hypervisorlaunchtype off

重启就可以开启了,但是过一段时间,再重启打开虚拟机还是会遇见这个问题,只能再重启解决。

尝试把VMware升级到16也无法解决这个问题。

2、dhclient eh0获取不到IP地址

[root@localhost ~]# dhclient eth0 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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 qlen 1000 link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ff

排查问题发现我习惯关闭VMware本地DHCP服务,所以没有获取到IP地址,开启后就获取到IP地址了。

超详细KVM虚拟化(kvm虚拟化使用教程)-第11张

[root@localhost ~]# dhclient eth0 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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 qlen 1000 link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ff inet 192.168.200.128/24 brd 192.168.200.255 scope global dynamic eth0 valid_lft 1801sec preferred_lft 1801sec inet6 fe80::5054:ff:fe50:b0bc/64 scope link valid_lft forever preferred_lft forever

KVM虚拟化的安装和基本管理就讲到这里了,如有疑问欢迎多多评论。

超详细KVM虚拟化(kvm虚拟化使用教程)-第12张

,

版权声明:该问答观点仅代表作者本人。如有侵犯您版权权利请告知 cpumjj@hotmail.com,我们将尽快删除相关内容。