任务1 虚拟化介绍

1. 简介

虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机。在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率
虚拟化使用软件的方法重新定义划分IT资源,可以实现IT资源的动态分配、灵活调度、跨域共享,提高IT资源利用率,使IT资源能够真正成为社会基础设施,服务于各行各业中灵活多变的应用需求。

2. 虚拟化分类

虚拟化技术可分为:全虚拟化和半虚拟化。
★全虚拟化:最流行的虚拟化方法使用名为Hypervisor的一种软件,在虚拟服务器和底层硬件之间建立一个抽象层。VMware和微软的VirtualPC是代表该方法的两个商用产品,而基于核心的虚拟机(KVM)是面向Linux系统的开源产品。Hypervisor可以捕获CPU指令,为指令访问硬件控制器和外设充当中介。因而,完全虚拟化技术几乎能让任何一款操作系统不用改动就能安装到虚拟服务器上,而它们不知道自己运行在虚拟化环境下。主要缺点是,Hypervisor 给处理器带来的负荷会很大。
★半虚拟化:完全虚拟化是处理器密集型技术,因为它要求Hypervisor管理各个虚拟服务器,并让它们彼此独立。减轻这种负担的一种方法就是,改动客户端操作系统,让它以为自己运行在虚拟环境下,能够与Hypervisor 协同工作。这种方法就叫准虚拟化(para-virtualization)Xen。它是开源准虚拟化技术的一个例子。操作系统作为虚拟服务器在Xen hypervisor上运行之前,它必须在核心层面进行某些改变。因此,Xen适用于BSD、Linux、Solaris及其他开源操作系统,但不适合像Windows这些专有的操作系统进行虚拟化处理,因为它们无法改动。准虚拟化技术的优点是性能高,经过准虚拟化处理的服务器可与Hypervisor协同工作,其响应能力几乎不亚于未经过虚拟化处理的服务器。准虚拟化与完全虚拟化相比优点明显,以至于微软和VMware都在开发这项技术,以完善各自的产品。

3. 虚拟化架构

image.png
Hypervisor直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的Linux系统。Xen和VMWare的ESXi 都属于这个类型。
image.png
物理机上首先安装常规的操作系统,比如 RedHat、Ubuntu和Windows。Hypervisor作为OS上的一个程序模块运行,并对管理虚拟机进行管理。KVM、VirtualBox和 VMWare Workstation 都属于这个类型。

任务2 KVM介绍

1. 简介

KVM(Kernel-Based Virtual Machines)是一个基于Linux内核的虚拟化技术, 可以直接将Linux内核转换为Hypervisor(系统管理程序)从而使得Linux内核能够直接管理虚拟机, 直接调用Linux内核中的内存管理、进程管理子系统来管理虚拟机。
KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术)。是基于硬件的完全虚拟化。而Xen早期则是基于软件模拟的Para-Virtualization,新版本则是基于硬件支持的完全虚拟化。但Xen本身有自己的进程调度器、存储管理模块等,所以代码较为庞大。广为流传的商业系统虚拟化软件VMware ESX系列是基于软件模拟的Full-Virtualization。

2. 架构

image.png

任务3 安装KVM

1. 调整虚拟机

修改虚拟机内存,至少设置成2 GB,双击“编辑虚拟机设置”按钮,如下图所示:
image.png
将虚拟机的内存修改为“2048”,如图所示:
image.png
在“硬件”选项卡中单击“处理器”,在虚拟化引擎中勾选如红框内所示选项,如下图所示:
image.png
接下来,我们添加硬盘,单击“添加”,如下图所示:
image.png
image.png
image.png
image.png
image.png
image.png
单击“确定”按钮,如下图所示:
image.png
至此,虚拟机设置完成。如图所示:
image.png
配置完成,我们启动虚拟机。

2. 检查CPU参数

启动虚拟机,我们查看以下,内存是否更改成功,命令如下:

  1. # 修改主机名
  2. [root@localhost ~]# hostnamectl set-hostname kvm
  3. [root@localhost ~]# su
  4. [root@kvm ~]#
  5. [root@kvm ~]# free
  6. total used free shared buff/cache available
  7. Mem: 1868688 99352 1620776 8784 148560 1622204
  8. Swap: 2097148 0 2097148
  9. [root@kvm ~]#

检查CPU是否开启虚拟化支持,命令如下:

  1. [root@kvm ~]# grep -Ei 'vmx|svm' /proc/cpuinfo

image.png
如果显示为空,就要检查你虚拟机设置,是否打“√”,如图:
image.png

3. 挂载新磁盘

使用lsblk命令,检查虚拟机是否新增一块50 GB的磁盘,命令如下:

  1. [root@kvm ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 20G 0 disk
  4. ├─sda1 8:1 0 500M 0 part /boot
  5. └─sda2 8:2 0 19.5G 0 part
  6. ├─centos-root 253:0 0 17.5G 0 lvm /
  7. └─centos-swap 253:1 0 2G 0 lvm [SWAP]
  8. sdb 8:16 0 50G 0 disk
  9. sr0 11:0 1 4.5G 0 rom /mnt/centos

我们可以看到确实新增了一块磁盘名为sdb。
接下来格式化这块磁盘,格式化成ext4格式,命令如下:

  1. [root@kvm ~]# mkfs.ext4 /dev/sdb
  2. mke2fs 1.42.9 (28-Dec-2013)
  3. /dev/sdb is entire device, not just one partition!
  4. 无论如何也要继续? (y,n) y
  5. 文件系统标签=
  6. OS type: Linux
  7. 块大小=4096 (log=2)
  8. 分块大小=4096 (log=2)
  9. Stride=0 blocks, Stripe width=0 blocks
  10. 3276800 inodes, 13107200 blocks
  11. 655360 blocks (5.00%) reserved for the super user
  12. 第一个数据块=0
  13. Maximum filesystem blocks=2162163712
  14. 400 block groups
  15. 32768 blocks per group, 32768 fragments per group
  16. 8192 inodes per group
  17. Superblock backups stored on blocks:
  18. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  19. 4096000, 7962624, 11239424
  20. Allocating group tables: 完成
  21. 正在写入inode表: 完成
  22. Creating journal (32768 blocks): 完成
  23. Writing superblocks and filesystem accounting information: 完成
  24. [root@kvm ~]# blkid /dev/sdb
  25. //blkid+磁盘:查看磁盘信息
  26. /dev/sdb: UUID="1cd04c8c-0d3a-49f4-9429-0c3e83b96ccf" TYPE="ext4"

格式化完成后,我们挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载,命令如下:

  1. [root@kvm ~]# mkdir /kvm_data
  2. [root@kvm ~]# mount /dev/sdb /kvm_data/

这种方式挂载,重启虚拟机后,需要再次重新挂载,我们为了方便开机后可以自动挂载,编写/etc/fstab文件,添加文件最末一行,命令如下:

  1. [root@kvm ~]# vim /etc/fstab
  2. [root@kvm ~]# cat /etc/fstab
  3. #
  4. # /etc/fstab
  5. # Created by anaconda on Sun Aug 22 18:00:27 2021
  6. #
  7. # Accessible filesystems, by reference, are maintained under '/dev/disk'
  8. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  9. #
  10. /dev/mapper/centos-root / xfs defaults 0 0
  11. UUID=8b3d2b54-a493-49f4-8494-91a676edb7e7 /boot xfs defaults 0 0
  12. /dev/mapper/centos-swap swap swap defaults 0 0
  13. /dev/cdrom /mnt/centos iso9660 defaults 0 0
  14. /dev/sdb /kvm_data ext4 defaults 0 0

image.png

4. 关闭防火墙和selinux

关闭防火墙并设置开机不自启,命令如下:

  1. [root@kvm ~]# systemctl stop firewalld
  2. [root@kvm ~]# systemctl disable firewalld

关闭SELinux,编辑/etc/selinux/config,将enforcing修改为disabled,命令如下:

  1. [root@kvm ~]# vim /etc/selinux/config
  2. [root@kvm ~]# cat /etc/selinux/config

image.png
修改完成后,重启虚拟机,使其生效,使用getenforce查看,命令如下:

  1. [root@kvm ~]# getenforce
  2. Disabled

5. 安装KVM

上述操作完成后,我们使用Yum进行安装KVM,命令如下:

  1. [root@kvm ~]# yum install -y virt-* libvirt bridge-utils qemu-img
  2. # 升级系统内核
  3. [root@kvm ~]# yum upgrade -y

任务4 启动KVM

1. 配置网卡

增加桥接网卡ifcfg-br0,命令如下:

  1. [root@kvm ~]# cd /etc/sysconfig/network-scripts/
  2. [root@kvm network-scripts]# cp ifcfg-
  3. ifcfg-eno16777736 ifcfg-lo
  4. [root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0
  5. //ifcfg-eno16777736为配置IP的网卡,你的网卡可能与我的不同

修改桥接网卡ifcfg-br0的内容,命令如下:

  1. [root@kvm network-scripts]# vim ifcfg-br0
  2. [root@kvm network-scripts]# cat ifcfg-br0
  3. TYPE=Bridge
  4. BOOTPROTO=none
  5. NAME=br0
  6. DEVICE=br0
  7. ONBOOT=yes
  8. IPADDR=192.168.200.50
  9. NETMASK=255.255.255.0
  10. GATEWAY=192.168.200.2
  11. DNS1=114.114.114.114
  12. DNS2=8.8.8.8

修改NAT网卡ifcfg-eno16777736,内容如下:

  1. [root@kvm network-scripts]# vim ifcfg-eno16777736
  2. [root@kvm network-scripts]# cat ifcfg-eno16777736
  3. TYPE=Ethernet
  4. BOOTPROTO=none
  5. NAME=eno16777736
  6. DEVICE=eno16777736
  7. ONBOOT=yes
  8. BRIDGE=br0

修改完成网卡内容后,重新启动网卡服务并查看网卡信息,命令如下:

  1. [root@kvm network-scripts]# service network restart
  2. Restarting network (via systemctl): [ 确定 ]
  3. [root@kvm network-scripts]# ifconfig
  4. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  5. inet 192.168.200.50 netmask 255.255.255.0 broadcast 192.168.200.255
  6. inet6 fe80::20c:29ff:fe2c:c8ee prefixlen 64 scopeid 0x20<link>
  7. ether 00:0c:29:2c:c8:ee txqueuelen 1000 (Ethernet)
  8. RX packets 24 bytes 1288 (1.2 KiB)
  9. RX errors 0 dropped 0 overruns 0 frame 0
  10. TX packets 19 bytes 1598 (1.5 KiB)
  11. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  12. eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  13. ether 00:0c:29:2c:c8:ee txqueuelen 1000 (Ethernet)
  14. RX packets 92 bytes 8212 (8.0 KiB)
  15. RX errors 0 dropped 0 overruns 0 frame 0
  16. TX packets 70 bytes 9137 (8.9 KiB)
  17. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  18. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  19. inet 127.0.0.1 netmask 255.0.0.0
  20. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  21. loop txqueuelen 1000 (Local Loopback)
  22. RX packets 0 bytes 0 (0.0 B)
  23. RX errors 0 dropped 0 overruns 0 frame 0
  24. TX packets 0 bytes 0 (0.0 B)
  25. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  26. virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  27. inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
  28. ether 52:54:00:c3:eb:c3 txqueuelen 1000 (Ethernet)
  29. RX packets 0 bytes 0 (0.0 B)
  30. RX errors 0 dropped 0 overruns 0 frame 0
  31. TX packets 0 bytes 0 (0.0 B)
  32. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

可以清楚的看到br0网卡出现并且带有IP,而以前的eno16777736网卡则没有IP。网卡配置完成

2. 启动libvirtd服务

首先检查KVM模块是否加载,命令如下:

  1. [root@kvm network-scripts]# lsmod|grep kvm
  2. kvm_intel 188740 0
  3. kvm 637515 1 kvm_intel
  4. irqbypass 13503 1 kvm

启动libvirtd并检查是否成功启动,命令如下:

  1. [root@kvm network-scripts]# systemctl start libvirtd
  2. [root@kvm network-scripts]# ps -ef |grep libvirt
  3. root 1004 1 0 03:22 ? 00:00:00 /usr/sbin/libvirtd
  4. nobody 1669 1 0 03:22 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  5. root 1670 1669 0 03:22 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  6. root 8837 8324 0 03:26 pts/0 00:00:00 grep --color=auto libvirt

启动成功后,使用brctl命令可以看到两个网卡,如下代码所示:

  1. [root@kvm network-scripts]# cd
  2. [root@kvm ~]# brctl show
  3. bridge name bridge id STP enabled interfaces
  4. br0 8000.000c292cc8ee no eno16777736
  5. virbr0 8000.525400c3ebc3 yes virbr0-nic

3. 命令行安装CentOS 7

CentOS7镜像下载地址:
http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2003.iso
首先,通过SCRT上传CentOS 7镜像到/tmp目录下,如果没有镜像的可以自行下载。上传后,使用ll命令查看/tmp目录下是否存在CentOS 7镜像文件,命令如下:

  1. [root@kvm ~]# cd /opt/
  2. 用量 4228096
  3. -rw-r--r-- 1 root root 4329570304 8 22 09:54 CentOS-7-x86_64-DVD-1511.iso

镜像存在,接下来使用virt-install进行CentOS 7的安装,命令如下:

  1. [root@kvm opt]# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/opt/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"

命令解释:
l —name:指定虚拟机的名称。
l —memory:指定分配给虚拟机的内存资源大小。
l maxmemory:指定可调节的最大内存资源大小,因为KVM支持热调整虚拟机的资源。
l —vcpus:指定分配给虚拟机的CPU核心数量。
l maxvcpus:指定可调节的最大CPU核心数量。
l —os-type:指定虚拟机安装的操作系统类型。
l —os-variant:指定系统的发行版本。
l —location:指定ISO镜像文件所在的路径,支持使用网络资源路径,也就是说可以使用URL。
l —disk path:指定虚拟硬盘所存放的路径及名称,size则是指定该硬盘的可用大小,单位是G。
l —bridge:指定使用哪一个桥接网卡,也就是说使用桥接的网络模式。
l —graphics:指定是否开启图形。
l —console:定义终端的属性,target_type 则是定义终端的类型。
l —extra-args:定义终端额外的参数。
打完上面那条命令后,等一会,就可以看见下面这段命令,这是系统基础设置,带[!]基本都是需要配置的,接下来我们开始配置“Timezone settings”,输入“2”,按Enter键,命令如下:

  1. Starting installer, one moment...
  2. anaconda 21.48.22.56-1 for CentOS 7 started.
  3. * installation log files are stored in /tmp during the installation
  4. * shell is available on TTY2
  5. * when reporting a bug add logs from /tmp as separate text/plain attachments
  6. 17:01:51 Not asking for VNC because we don't have a network
  7. ================================================================================
  8. ================================================================================
  9. Installation
  10. 1) [x] Language settings 2) [!] Timezone settings
  11. (English (United States)) (Timezone is not set.)
  12. 3) [!] Installation source 4) [!] Software selection
  13. (Processing...) (Processing...)
  14. 5) [!] Installation Destination 6) [x] Kdump
  15. (No disks selected) (Kdump is enabled)
  16. 7) [ ] Network configuration 8) [!] Root password
  17. (Not connected) (Password is not set.)
  18. 9) [!] User creation
  19. (No user will be created)
  20. Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 2
  21. =====================================================================

“Timezone settings”时区设置选择5) Asia亚洲,再选择城市 62) Shanghai上海,命令如下:

  1. Timezone settings
  2. Available regions
  3. 1) Africa 6) Atlantic 10) Pacific
  4. 2) America 7) Australia 11) US
  5. 3) Antarctica 8) Europe 12) Etc
  6. 4) Arctic 9) Indian
  7. 5) Asia
  8. Please select the timezone.
  9. Use numbers or type names directly [b to region list, q to quit]: 5
  10. ================================================================================
  11. ================================================================================
  12. Timezone settings
  13. Available timezones in region Asia
  14. 1) Aden 28) Irkutsk 54) Pyongyang
  15. 2) Almaty 29) Jakarta 55) Qatar
  16. 3) Amman 30) Jayapura 56) Qyzylorda
  17. 4) Anadyr 31) Jerusalem 57) Rangoon
  18. 5) Aqtau 32) Kabul 58) Riyadh
  19. 6) Aqtobe 33) Kamchatka 59) Sakhalin
  20. 7) Ashgabat 34) Karachi 60) Samarkand
  21. 8) Baghdad 35) Kathmandu 61) Seoul
  22. 9) Bahrain 36) Khandyga 62) Shanghai
  23. 10) Baku 37) Kolkata 63) Singapore
  24. 11) Bangkok 38)Krasnoyarsk 64) Srednekolymsk
  25. 12) Beirut 39) Kuala_Lumpur 65) Taipei
  26. 13) Bishkek 40) Kuching 66) Tashkent
  27. 14) Brunei 41) Kuwait 67) Tbilisi
  28. 15) Chita 42) Macau 68) Tehran
  29. 16) Choibalsan 43) Magadan 69) Thimphu
  30. 17) Colombo 44) Makassar 70) Tokyo
  31. 18) Damascus 45) Manila 71) Ulaanbaatar
  32. 19) Dhaka 46) Muscat 72) Urumqi
  33. 20) Dili 47) Nicosia 73) Ust-Nera
  34. 21) Dubai 48) Novokuznetsk 74) Vientiane
  35. 22) Dushanbe 49) Novosibirsk 75) Vladivostok
  36. Press ENTER to continue
  37. 23) Gaza 50) Omsk 76) Yakutsk
  38. 24) Hebron 51) Oral 77) Yekaterinburg
  39. 25) Ho_Chi_Minh 52) Phnom_Penh 78) Yerevan
  40. 26) Hong_Kong 53) Pontianak
  41. 27) Hovd
  42. Please select the timezone.
  43. Use numbers or type names directly [b to region list, q to quit]: 62
  44. ================================================================================

输入完毕后,可以发现2的[!]变成了[x],证明配置完毕。
接下来我们配置“Software selection”,选择“4”,在选择“c”,因为默认就是minimalinstall,按Enter键,命令如下:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [x] Installation source 4) [!] Software selection
  5. (Local media) (Minimal Install)
  6. 5) [!] Installation Destination 6) [x] Kdump
  7. (No disks selected) (Kdump is enabled)
  8. 7) [ ] Network configuration 8) [!] Root password
  9. (Not connected) (Password is not set.)
  10. 9) [!] User creation
  11. (No user will be created)
  12. Please make your choice from above ['q' to quit | 'b' to begin installation |
  13. 'r' to refresh]: 4
  14. ================================================================================
  15. ================================================================================
  16. Base environment
  17. Software selection
  18. Base environment
  19. 1) [x] Minimal Install 7) [ ] Server with GUI
  20. 2) [ ] Compute Node 8) [ ] GNOME Desktop
  21. 3) [ ] Infrastructure Server 9) [ ] KDE Plasma Workspaces
  22. 4) [ ] File and Print Server 10) [ ] Development and Creative Work
  23. 5) [ ] Basic Web Server station
  24. 6) [ ] Virtualization Host
  25. Please make your choice from above ['q' to quit | 'c' to continue |
  26. 'r' to refresh]: c

接下来我们配置“Installation Destination”,选择“5”,其余的依次选择“c”,按Enter键,命令如下:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [!] Installation source 4) [!] Software selection
  5. (Processing...) (Processing...)
  6. 5) [!] Installation Destination 6) [x] Kdump
  7. (No disks selected) (Kdump is enabled)
  8. 7) [ ] Network configuration 8) [!] Root password
  9. (Not connected) (Password is not set.)
  10. 9) [!] User creation
  11. (No user will be created)
  12. Please make your choice from above ['q' to quit | 'b' to begin installation |
  13. 'r' to refresh]: 5
  14. ================================================================================
  15. ================================================================================
  16. Probing storage...
  17. Installation Destination
  18. [x] 1) : 10 GiB (vda)
  19. 1 disk selected; 10 GiB capacity; 10 GiB free ...
  20. Please make your choice from above ['q' to quit | 'c' to continue |
  21. 'r' to refresh]: c
  22. ================================================================================
  23. ================================================================================
  24. Autopartitioning Options
  25. [ ] 1) Replace Existing Linux system(s)
  26. [x] 2) Use All Space
  27. [ ] 3) Use Free Space
  28. Installation requires partitioning of your hard drive. Select what space to use
  29. for the install target.
  30. Please make your choice from above ['q' to quit | 'c' to continue |
  31. 'r' to refresh]: c
  32. ================================================================================
  33. ================================================================================
  34. Partition Scheme Options
  35. [ ] 1) Standard Partition
  36. [ ] 2) Btrfs
  37. [x] 3) LVM
  38. [ ] 4) LVM Thin Provisioning
  39. Select a partition scheme configuration.
  40. Please make your choice from above ['q' to quit | 'c' to continue |
  41. 'r' to refresh]: c
  42. Generating updated storage configuration
  43. Checking storage configuration...
  44. ================================================================================
  45. ================================================================================

接下来配置“Root password”,选择8,按Enter键,命令如下:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [x] Installation source 4) [x] Software selection
  5. (Local media) (Minimal Install)
  6. 5) [x] Installation Destination 6) [x] Kdump
  7. (Automatic partitioning selecte (Kdump is enabled)
  8. d) 8) [!] Root password
  9. 7) [ ] Network configuration (Password is not set.)
  10. (Not connected)
  11. 9) [!] User creation
  12. (No user will be created)
  13. Please make your choice from above ['q' to quit | 'b' to begin installation |
  14. 'r' to refresh]: 8
  15. ================================================================================
  16. ================================================================================
  17. Please select new root password. You will have to type it twice.

依次输入两次密码,密码相同,我设置的为123456,命令如下:

  1. Password: 123456
  2. //密码为密文,不显示。
  3. Password (confirm): 123456
  4. ================================================================================
  5. ================================================================================
  6. Question
  7. You have provided a weak password: The password fails the dictionary check - it
  8. is too simplistic/systematic
  9. Would you like to use it anyway?
  10. //这句话的意思:你的密码太过简单,是否使用它,输入yes即可。
  11. Please respond 'yes' or 'no': yes
  12. =========================================================
  13. =========================================================

配置完成,选择“b”,按Enter键后,开始安装,命令如下:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [x] Installation source 4) [x] Software selection
  5. (Local media) (Minimal Install)
  6. 5) [x] Installation Destination 6) [x] Kdump
  7. (Automatic partitioning selecte (Kdump is enabled)
  8. d) 8) [x] Root password
  9. 7) [ ] Network configuration (Password is set.)
  10. (Not connected)
  11. 9) [ ] User creation
  12. (No user will be created)
  13. Please make your choice from above ['q' to quit | 'b' to begin installation |
  14. 'r' to refresh]: b
  15. ================================================================================
  16. ================================================================================
  17. Progress
  18. Setting up the installation environment
  19. .
  20. Creating disklabel on /dev/vda
  21. .
  22. Creating xfs on /dev/vda1
  23. .
  24. Creating lvmpv on /dev/vda2
  25. .
  26. Creating swap on /dev/mapper/centos-swap
  27. .
  28. ……………………
  29. Installing readline (34/297)
  30. Installing gawk (35/297)
  31. Installing elfutils-libelf (36/297)
  32. Installing libgpg-error (37/297)
  33. Installing libffi (38/297)
  34. Installing libattr (39/297)
  35. Installing libacl (40/297)
  36. Installing libcap (41/297)
  37. Installing libgcrypt (42/297)
  38. Installing cpio (43/297)
  39. Installing libxml2 (44/297)
  40. Installing libnl3 (45/297)
  41. Installing expat (46/297)
  42. Installing p11-kit (47/297)
  43. ……………………
  44. .
  45. Performing post-installation setup tasks
  46. .
  47. Configuring installed system
  48. .
  49. Writing network configuration
  50. .
  51. Creating users
  52. .
  53. Configuring addons
  54. .
  55. Generating initramfs
  56. .
  57. Running post-installation scripts
  58. .
  59. Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
  60. Installation complete. Press return to quit
  61. //按回车
  62. ……………………
  63. CentOS Linux 7 (Core)
  64. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  65. localhost login: root
  66. Password: 123456
  67. [root@localhost ~]#
  68. //自动获取IP给新的虚拟机
  69. [root@localhost ~]# dhclient eth0
  70. //按“Ctrl+]”键,退出终端,回到宿主机。
  71. [root@kvm ~]#

CentOS 7安装完成。

任务5 虚拟机管理

1. KVM基本管理

完成虚拟机安装,已经退回到宿主机,接下来我们来通过宿主机virsh命令进行管理刚才安装的CentOS 7虚拟机。
查看虚拟机列表,命令如下:

  1. [root@kvm ~]# virsh list
  2. //查看虚拟机列表,只能看到正在运行的虚拟机
  3. Id 名称 状态
  4. ----------------------------------------------------
  5. 2 test running
  6. [root@kvm ~]# virsh list --all
  7. //查看虚拟机列表,包括未运行的虚拟机
  8. Id 名称 状态
  9. ----------------------------------------------------
  10. 2 test running

进入指定的虚拟,命令如下:

  1. [root@kvm ~]# virsh console test
  2. Connected to domain test
  3. Escape character is ^]
  4. //如果迟迟未动敲下回车就ok,下面就是正常登录时需要输入的用户名密码
  5. CentOS Linux 7 (Core)
  6. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  7. localhost login:

若发生以下报错信息,命令如下:

  1. [root@kvm ~]# virsh console test
  2. Connected to domain test
  3. Escape character is ^]
  4. error: operation failed: Active console session exists for this domain

解决方式,命令如下:

  1. [root@kvm ~]# ps -ef |grep console
  2. root 11167 10987 0 01:55 tty1 00:00:00 virsh console test
  3. root 16017 15750 0 10:01 pts/1 00:00:00 grep --color=auto console
  4. [root@kvm ~]# kill -9 11167

vrish常用命令,命令如下:

  1. virsh shutdown test
  2. //关闭虚拟机
  3. virsh start test
  4. //开启虚拟机
  5. virsh destroy test
  6. //类似stop,这个是强制停止
  7. virsh undefine test
  8. //彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到
  9. virsh autostart test
  10. //宿主机开机该虚拟机也开机
  11. virsh autostart --disable test
  12. //解除开机启动
  13. virsh suspend test
  14. //挂起
  15. virsh resume test
  16. //恢复

2. 克隆虚拟机

克隆虚拟机之前,要先关闭虚拟机,不然会提示这种错误,命令如下:

  1. [root@kvm ~]# virt-clone --original test --name test02 --file /kvm_data/test02.img
  2. ERROR Domain with devices to clone must be paused or shutoff.

关闭虚拟机后,再次进行克隆,命令如下:

  1. [root@kvm ~]# virt-clone --original test --name test02 --file /kvm_data/test02.img
  2. 正在分配 'test02.img' | 10 GB 00:00:07
  3. 成功克隆 'test02'

命令解释:
l —original:指定克隆源虚拟机。
l —name:指定克隆后的虚拟机名字。
l —file:指定目标虚拟机的虚拟磁盘文件。
查看虚拟机配置文件/etc/libvirt/qemu/,看是否增加test02.xml文件,命令如下:

  1. [root@kvm ~]# ls /etc/libvirt/qemu/
  2. networks test02.xml test.xml

接下来,我们启动刚刚克隆的虚拟机test02,首先查看虚拟机列表,然后启动克隆的虚拟机test02,命令如下:

  1. [root@kvm ~]# virsh list --all
  2. Id 名称 状态
  3. ----------------------------------------------------
  4. - test 关闭
  5. - test02 关闭

3. 快照管理

快照是在我们使用Vmware的时候,很常用的一个功能。它可以回到之前的某一状态。在KVM中RAW格式的虚拟磁盘不支持做快照,qcow2支持。
创建快照,命令如下:

  1. [root@kvm ~]# virsh snapshot-create test
  2. 已生成域快照 1633907626

查看test.img信息,同时会查看到快照列表,命令如下:

  1. [root@kvm ~]# qemu-img info /kvm_data/test.img
  2. image: /kvm_data/test.img
  3. file format: qcow2
  4. virtual size: 10G (10737418240 bytes)
  5. disk size: 1.1G
  6. cluster_size: 65536
  7. Snapshot list:
  8. ID TAG VM SIZE DATE VM CLOCK
  9. 1 1633907626 0 2021-10-11 07:13:46 00:00:00.000
  10. Format specific information:
  11. compat: 1.1
  12. lazy refcounts: true

列出所有快照,命令如下:

  1. [root@kvm ~]# virsh snapshot-list test
  2. 名称 生成时间 状态
  3. ------------------------------------------------------------
  4. 1633907626 2021-10-11 07:13:46 +0800 shutoff

查看当前快照版本,命令如下:

  1. [root@kvm ~]# virsh snapshot-current test
  2. <domainsnapshot>
  3. <name>1633907626</name>
  4. <state>shutoff</state>
  5. <creationTime>1633907626</creationTime>
  6. <memory snapshot='no'/>
  7. <disks>
  8. <disk name='vda' snapshot='internal'/>
  9. <disk name='hda' snapshot='no'/>
  10. </disks>
  11. <domain type='kvm'>
  12. <name>test</name>
  13. <uuid>e7654728-bed3-4920-a448-678da4346b54</uuid>
  14. <memory unit='KiB'>1048576</memory>
  15. <currentMemory unit='KiB'>524288</currentMemory>
  16. <vcpu placement='static' current='1'>2</vcpu>
  17. <os>
  18. <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
  19. <boot dev='hd'/>
  20. </os>
  21. <features>
  22. <acpi/>
  23. <apic/>
  24. </features>
  25. <cpu mode='custom' match='exact' check='partial'>
  26. <model fallback='allow'>Broadwell-noTSX-IBRS</model>
  27. <feature policy='require' name='md-clear'/>
  28. <feature policy='require' name='spec-ctrl'/>
  29. <feature policy='require' name='ssbd'/>
  30. </cpu>
  31. <clock offset='utc'>
  32. <timer name='rtc' tickpolicy='catchup'/>
  33. <timer name='pit' tickpolicy='delay'/>
  34. <timer name='hpet' present='no'/>
  35. </clock>
  36. <on_poweroff>destroy</on_poweroff>
  37. <on_reboot>restart</on_reboot>
  38. <on_crash>destroy</on_crash>
  39. <pm>
  40. <suspend-to-mem enabled='no'/>
  41. <suspend-to-disk enabled='no'/>
  42. </pm>
  43. <devices>
  44. <emulator>/usr/libexec/qemu-kvm</emulator>
  45. <disk type='file' device='disk'>
  46. <driver name='qemu' type='qcow2'/>
  47. <source file='/kvm_data/test.img'/>
  48. <target dev='vda' bus='virtio'/>
  49. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  50. </disk>
  51. <disk type='file' device='cdrom'>
  52. <driver name='qemu' type='raw'/>
  53. <target dev='hda' bus='ide'/>
  54. <readonly/>
  55. <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  56. </disk>
  57. <controller type='usb' index='0' model='ich9-ehci1'>
  58. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
  59. </controller>
  60. <controller type='usb' index='0' model='ich9-uhci1'>
  61. <master startport='0'/>
  62. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
  63. </controller>
  64. <controller type='usb' index='0' model='ich9-uhci2'>
  65. <master startport='2'/>
  66. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
  67. </controller>
  68. <controller type='usb' index='0' model='ich9-uhci3'>
  69. <master startport='4'/>
  70. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
  71. </controller>
  72. <controller type='ide' index='0'>
  73. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  74. </controller>
  75. <controller type='virtio-serial' index='0'>
  76. <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  77. </controller>
  78. <controller type='pci' index='0' model='pci-root'/>
  79. <interface type='bridge'>
  80. <mac address='52:54:00:53:d4:0f'/>
  81. <source bridge='br0'/>
  82. <model type='virtio'/>
  83. <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  84. </interface>
  85. <serial type='pty'>
  86. <target type='isa-serial' port='0'>
  87. <model name='isa-serial'/>
  88. </target>
  89. </serial>
  90. <console type='pty'>
  91. <target type='serial' port='0'/>
  92. </console>
  93. <channel type='unix'>
  94. <target type='virtio' name='org.qemu.guest_agent.0'/>
  95. <address type='virtio-serial' controller='0' bus='0' port='1'/>
  96. </channel>
  97. <input type='mouse' bus='ps2'/>
  98. <input type='keyboard' bus='ps2'/>
  99. <memballoon model='virtio'>
  100. <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
  101. </memballoon>
  102. <rng model='virtio'>
  103. <backend model='random'>/dev/urandom</backend>
  104. <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
  105. </rng>
  106. </devices>
  107. </domain>
  108. </domainsnapshot>

查看所有快照配置文件,命令如下:

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

恢复指定快照,命令如下:

  1. [root@kvm ~]# virsh snapshot-revert test 1633907626

删除指定快照,命令如下:

  1. [root@kvm ~]# virsh snapshot-delete test 1633907626
  2. 已删除域快照 1633907626

4. 磁盘格式

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

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

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

  1. [root@kvm ~]# qemu-img create -f raw /kvm_data/test_1.img 2G
  2. Formatting '/kvm_data/test_1.img', fmt=raw size=2147483648

RAW格式的磁盘转换为qcow2格式,命令如下:

  1. [root@kvm ~]# qemu-img convert -O qcow2 /kvm_data/test_1.img /kvm_data/test_1.qcow2

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

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

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

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

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

  1. [root@kvm ~]# virsh edit test02
  2. …………
  3. <disk type='file' device='disk'>
  4. <driver name='qemu' type='qcow'/>
  5. <source file='/kvm_data/test02.img'/>
  6. <target dev='vda' bus='virtio'/>
  7. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  8. </disk>
  9. //找到上面这部分将qcow修改为raw,/kvm_data/test02.img改为/kvm_data/test02_2.raw
  10. <disk type='file' device='disk'>
  11. <driver name='qemu' type='raw'/>
  12. <source file='/kvm_data/test02_2.raw'/>
  13. <target dev='vda' bus='virtio'/>
  14. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  15. </disk>
  16. ………………

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

  1. [root@kvm ~]# ls -l /kvm_data/
  2. 总用量 3426900
  3. drwx------ 2 root root 16384 10 11 05:05 lost+found
  4. -rw-r--r-- 1 root root 10737418240 10 11 07:26 test02_2.raw
  5. -rw------- 1 root root 1173749760 10 11 07:10 test02.img
  6. -rw-r--r-- 1 root root 2147483648 10 11 07:19 test_1.img
  7. -rw-r--r-- 1 root root 197120 10 11 07:20 test_1.qcow2
  8. -rw------- 1 root root 10739384832 10 11 07:17 test.img

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

  1. [root@kvm ~]# virsh start test02
  2. test02 已开始
  3. [root@kvm ~]# ls -l /kvm_data/
  4. 总用量 3426900
  5. drwx------ 2 root root 16384 10 11 05:05 lost+found
  6. -rw-r--r-- 1 qemu qemu 10737418240 10 11 07:26 test02_2.raw
  7. -rw------- 1 root root 1173749760 10 11 07:10 test02.img
  8. -rw-r--r-- 1 root root 2147483648 10 11 07:19 test_1.img
  9. -rw-r--r-- 1 root root 197120 10 11 07:20 test_1.qcow2
  10. -rw------- 1 root root 10739384832 10 11 07:17 test.img

5. 磁盘扩容

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

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

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

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

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

  1. [root@kvm ~]# virsh console test02
  2. 连接到域 test02
  3. 换码符为 ^]
  4. //回车
  5. CentOS Linux 7 (Core)
  6. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  7. localhost login: root
  8. Password:
  9. Last login: Mon Oct 11 06:58:54 on ttyS0

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

  1. [root@localhost ~]# fdisk -l
  2. Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
  3. Units = sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk label type: dos
  7. Disk identifier: 0x00028cab
  8. Device Boot Start End Blocks Id System
  9. /dev/vda1 * 2048 1026047 512000 83 Linux
  10. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  11. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  12. Units = sectors of 1 * 512 = 512 bytes
  13. Sector size (logical/physical): 512 bytes / 512 bytes
  14. I/O size (minimum/optimal): 512 bytes / 512 bytes
  15. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  16. Units = sectors of 1 * 512 = 512 bytes
  17. Sector size (logical/physical): 512 bytes / 512 bytes
  18. I/O size (minimum/optimal): 512 bytes / 512 bytes
  19. //可以看到磁盘还是10 GB

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

  1. [root@kvm ~]# virsh destroy test02
  2. test02 被删除
  3. [root@kvm ~]# virsh start test02
  4. test02 已开始
  5. [root@kvm ~]# virsh console test02
  6. 连接到域 test02
  7. 换码符为 ^]
  8. [root@localhost ~]# fdisk -l
  9. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  10. Units = sectors of 1 * 512 = 512 bytes
  11. Sector size (logical/physical): 512 bytes / 512 bytes
  12. I/O size (minimum/optimal): 512 bytes / 512 bytes
  13. Disk label type: dos
  14. Disk identifier: 0x00028cab
  15. Device Boot Start End Blocks Id System
  16. /dev/vda1 * 2048 1026047 512000 83 Linux
  17. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  18. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  19. Units = sectors of 1 * 512 = 512 bytes
  20. Sector size (logical/physical): 512 bytes / 512 bytes
  21. I/O size (minimum/optimal): 512 bytes / 512 bytes
  22. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  23. Units = sectors of 1 * 512 = 512 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到已经变成12 GB了。
接下来,我们怎么使用新增加的这2 GB磁盘,那就是分区,命令如下:

  1. [root@localhost ~]# fdisk /dev/vda
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Command (m for help): n
  6. //新建分区
  7. Partition type:
  8. p primary (2 primary, 0 extended, 2 free)
  9. e extended
  10. Select (default p): p
  11. Partition number (3,4, default 3):
  12. //按Enter键
  13. First sector (20971520-25165823, default 20971520):
  14. //按Enter键
  15. Using default value 20971520
  16. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  17. //按Enter键
  18. Using default value 25165823
  19. Partition 3 of type Linux and of size 2 GiB is set
  20. Command (m for help): p
  21. //查看分区列表
  22. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  23. Units = sectors of 1 * 512 = 512 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/O size (minimum/optimal): 512 bytes / 512 bytes
  26. Disk label type: dos
  27. Disk identifier: 0x00028cab
  28. Device Boot Start End Blocks Id System
  29. /dev/vda1 * 2048 1026047 512000 83 Linux
  30. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  31. /dev/vda3 20971520 25165823 2097152 83 Linux
  32. Command (m for help): w
  33. //保存退出并退出终端
  34. The partition table has been altered!
  35. Calling ioctl() to re-read partition table.
  36. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  37. The kernel still uses the old table. The new table will be used at
  38. the next reboot or after you run partprobe(8) or kpartx(8)
  39. Syncing disks.

除了对已有磁盘扩容外,还可以额外增加磁盘,命令如下:

  1. [root@kvm ~]# qemu-img create -f raw /kvm_data/test02_3.raw 5G
  2. Formatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120

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

  1. [root@kvm ~]# virsh edit test02
  2. ………………
  3. <disk type='file' device='disk'>
  4. <driver name='qemu' type='raw'/>
  5. <source file='/kvm_data/test02_2.raw'/>
  6. <target dev='vda' bus='virtio'/>
  7. <address type='pci' domain='0x0000' bus='0x00' slot='0x06'
  8. function='0x0'/>
  9. </disk>
  10. //找到这一部分内容,在下面增加以下内容
  11. <disk type='file' device='disk'>
  12. <driver name='qemu' type='raw'/>
  13. <source file='/kvm_data/test02_3.raw'/>
  14. <target dev='vdb' bus='virtio'/>
  15. <address type='pci' domain='0x0000' bus='0x00' slot='0x09'
  16. function='0x0'/>
  17. </disk>
  18. ………………

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

  1. [root@kvm ~]# virsh destroy test02
  2. test02 被删除
  3. [root@kvm ~]# virsh start test02
  4. test02 已开始
  5. [root@kvm ~]# virsh console test02
  6. 连接到域 test02
  7. 换码符为 ^]
  8. [root@localhost ~]# fdisk -l
  9. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  10. Units = sectors of 1 * 512 = 512 bytes
  11. Sector size (logical/physical): 512 bytes / 512 bytes
  12. I/O size (minimum/optimal): 512 bytes / 512 bytes
  13. Disk label type: dos
  14. Disk identifier: 0x00028cab
  15. Device Boot Start End Blocks Id System
  16. /dev/vda1 * 2048 1026047 512000 83 Linux
  17. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  18. /dev/vda3 20971520 25165823 2097152 83 Linux
  19. Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
  20. Units = sectors of 1 * 512 = 512 bytes
  21. Sector size (logical/physical): 512 bytes / 512 bytes
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes
  23. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  24. Units = sectors of 1 * 512 = 512 bytes
  25. Sector size (logical/physical): 512 bytes / 512 bytes
  26. I/O size (minimum/optimal): 512 bytes / 512 bytes
  27. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  28. Units = sectors of 1 * 512 = 512 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到多了一块5 GB的磁盘,退出终端。
接下来,我们扩容qcow2格式的磁盘,命令如下:

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

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

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

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

  1. [root@kvm ~]# virsh destroy test
  2. Domain test destroyed
  3. [root@kvm ~]# virsh start test
  4. Domain test started
  5. [root@kvm ~]# virsh console test
  6. [root@localhost ~]# fdisk -l
  7. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  8. Units = sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk label type: dos
  12. Disk identifier: 0x00028cab
  13. Device Boot Start End Blocks Id System
  14. /dev/vda1 * 2048 1026047 512000 83 Linux
  15. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  16. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  17. Units = sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  21. Units = sectors of 1 * 512 = 512 bytes
  22. Sector size (logical/physical): 512 bytes / 512 bytes
  23. I/O size (minimum/optimal): 512 bytes / 512 bytes

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

  1. [root@localhost ~]# fdisk /dev/vda
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Command (m for help): n
  6. Partition type:
  7. p primary (2 primary, 0 extended, 2 free)
  8. e extended
  9. Select (default p): p
  10. Partition number (3,4, default 3):
  11. First sector (20971520-25165823, default 20971520):
  12. Using default value 20971520
  13. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  14. Using default value 25165823
  15. Partition 3 of type Linux and of size 2 GiB is set
  16. Command (m for help): w
  17. The partition table has been altered!
  18. Calling ioctl() to re-read partition table.
  19. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  20. The kernel still uses the old table. The new table will be used at
  21. the next reboot or after you run partprobe(8) or kpartx(8)
  22. Syncing disks.
  23. //保存并退出终端

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

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

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