任务1 虚拟化介绍

1. 简介

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

2. 虚拟化分类

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

3. 虚拟化架构

Type-I型
image.png
Hypervisor直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的Linux系统。Xen和VMWare的ESXi 都属于这个类型。
Type-II型
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
KVM架构图

任务3 安装KVM

1. 调整虚拟机

修改虚拟机内存,至少设置成2 GB,双击“编辑虚拟机设置”按钮,如下图所示:
image.png
将虚拟机的内存修改为“2048”,如图所示:
image.png
在“硬件”选项卡中单击“处理器”, 在虚拟化引擎中勾选如红框内所示选项,如下图所示:
image.png
接下来,我们添加硬盘,单击“添加”,如下图所示:
image.png
然后选择硬盘,单击“下一步”按钮,如下图所示:
image.png
选择“SCSI”,单击“下一步”按钮,如下图所示:
image.png
选中“创建新虚拟磁盘”选项,单击“下一步”按钮,如下图所示:
image.png
将最大磁盘大小修改为“60”,然后点击“下一步”按钮,如下图所示:
image.png
单击“完成”按钮,磁盘添加完成。如下图所示:
image.png
单击“确定”按钮,如下图所示:

image.png
至此,虚拟机设置完成。如图所示:
image.png
配置完成,我们启动虚拟机。

2. 检查CPU参数

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

  1. [root@localhost ~]# free
  2. total used free shared buff/cache available
  3. Mem: 1863040 138636 1579844 9744 144560 1573584
  4. Swap: 2097148 0 2097148
  5. [root@localhost ~]#

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

  1. [root@localhost ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
  2. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss 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 invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
  3. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss 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 invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
  4. [root@localhost ~]#

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

3. 挂载新磁盘

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

  1. [root@localhost ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 20G 0 disk
  4. ├─sda1 8:1 0 1G 0 part /boot
  5. └─sda2 8:2 0 19G 0 part
  6. ├─centos-root 253:0 0 17G 0 lvm /
  7. └─centos-swap 253:1 0 2G 0 lvm [SWAP]
  8. sdb 8:16 0 60G 0 disk
  9. sr0 11:0 1 1024M 0 rom
  10. [root@localhost ~]#

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

  1. [root@localhost ~]# 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. 3932160 inodes, 15728640 blocks
  11. 786432 blocks (5.00%) reserved for the super user
  12. 第一个数据块=0
  13. Maximum filesystem blocks=2164260864
  14. 480 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@localhost ~]#
  25. //blkid+磁盘:查看磁盘信息
  26. [root@localhost ~]# blkid /dev/sdb
  27. /dev/sdb: UUID="501c7dfb-805c-4383-bab6-723bd6a49040" TYPE="ext4"
  28. [root@localhost ~]#

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

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

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

  1. [root@localhost ~]# vim /etc/fstab
  2. [root@localhost ~]# cat /etc/fstab
  3. #
  4. # /etc/fstab
  5. # Created by anaconda on Tue Aug 10 23:37:47 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=12ba9b17-8596-439c-bd06-cca6d607513b /boot xfs defaults 0 0
  12. /dev/mapper/centos-swap swap swap defaults 0 0
  13. /dev/sdb /kvm_data ext4 defaults 0 0
  14. [root@localhost ~]#

4. 关闭防火墙和selinux

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

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

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

  1. [root@localhost ~]# vim /etc/selinux/config
  2. [root@localhost ~]# cat /etc/selinux/config
  3. # This file controls the state of SELinux on the system.
  4. # SELINUX= can take one of these three values:
  5. # enforcing - SELinux security policy is enforced.
  6. # permissive - SELinux prints warnings instead of enforcing.
  7. # disabled - No SELinux policy is loaded.
  8. SELINUX=disabled
  9. # SELINUXTYPE= can take one of three values:
  10. # targeted - Targeted processes are protected,
  11. # minimum - Modification of targeted policy. Only selected processes are protected.
  12. # mls - Multi Level Security protection.
  13. SELINUXTYPE=targeted
  14. [root@localhost ~]#

5. 安装KVM

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

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

任务4 启动KVM

1. 配置网卡

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

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

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

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

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

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

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

  1. [root@localhost network-scripts]# service network restart
  2. Restarting network (via systemctl): [ 确定 ]
  3. [root@localhost network-scripts]# ifconfig
  4. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  5. inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255
  6. inet6 fe80::3c93:7bff:fe6f:23b2 prefixlen 64 scopeid 0x20<link>
  7. ether 00:0c:29:58:6e:81 txqueuelen 1000 (Ethernet)
  8. RX packets 27 bytes 1842 (1.7 KiB)
  9. RX errors 0 dropped 0 overruns 0 frame 0
  10. TX packets 28 bytes 2380 (2.3 KiB)
  11. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  12. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  13. ether 00:0c:29:58:6e:81 txqueuelen 1000 (Ethernet)
  14. RX packets 96882 bytes 141115746 (134.5 MiB)
  15. RX errors 0 dropped 0 overruns 0 frame 0
  16. TX packets 11850 bytes 1452899 (1.3 MiB)
  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 70 bytes 6090 (5.9 KiB)
  23. RX errors 0 dropped 0 overruns 0 frame 0
  24. TX packets 70 bytes 6090 (5.9 KiB)
  25. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  26. [root@localhost network-scripts]#

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

2. 启动libvirtd服务

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

  1. [root@localhost network-scripts]# lsmod|grep kvm
  2. kvm_intel 188688 0
  3. kvm 636969 1 kvm_intel
  4. irqbypass 13503 1 kvm
  5. [root@localhost network-scripts]#

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

  1. [root@localhost network-scripts]# systemctl start libvirtd
  2. [root@localhost network-scripts]# ps -ef |grep libvirt
  3. root 3081 1 6 12:00 ? 00:00:00 /usr/sbin/libvirtd
  4. nobody 3197 1 0 12:00 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  5. root 3198 3197 0 12:00 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  6. root 3247 1508 0 12:00 pts/0 00:00:00 grep --color=auto libvirt
  7. [root@localhost network-scripts]#

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

  1. [root@localhost network-scripts]# cd
  2. [root@localhost ~]# brctl show
  3. bridge name bridge id STP enabled interfaces
  4. br0 8000.000c29586e81 no ens33
  5. virbr0 8000.5254003e01aa yes virbr0-nic
  6. [root@localhost ~]#

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镜像文件,命令如下:
这里我上传的是2003版的

  1. [root@localhost ~]# cd /tmp/
  2. [root@localhost tmp]# ls
  3. CentOS-7-x86_64-DVD-2003.iso
  4. systemd-private-af176190b89c468baa54a54ae13d96f8-chronyd.service-f7vrhy
  5. vmware-root_689-4021587913
  6. [root@localhost tmp]# ll
  7. 总用量 4669440
  8. -rw-r--r--. 1 root root 4781506560 10 13 2020 CentOS-7-x86_64-DVD-2003.iso
  9. drwx------. 3 root root 17 10 1 11:29 systemd-private-af176190b89c468baa54a54ae13d96f8-chronyd.service-f7vrhy
  10. drwx------. 2 root root 6 10 1 11:29 vmware-root_689-4021587913
  11. [root@localhost tmp]#

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

  1. [root@localhost tmp]# 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-2003.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"

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

  1. ================================================================================
  2. Installation
  3. 1) [x] Language settings 2) [!] Time settings
  4. (English (United States)) (Timezone is not set.)
  5. 3) [!] Installation source 4) [!] Software selection
  6. (Processing...) (Processing...)
  7. 5) [!] Installation Destination 6) [x] Kdump
  8. (No disks selected) (Kdump is enabled)
  9. 7) [ ] Network configuration 8) [!] Root password
  10. (Not connected) (Password is not set.)
  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]: 2
  15. ================================================================================
  16. Time settings
  17. Timezone: not set
  18. NTP servers:not configured
  19. 1) Set timezone
  20. 2) Configure NTP servers
  21. Please make your choice from above ['q' to quit | 'c' to continue |
  22. 'r' to refresh]: 1

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

  1. ================================================================================
  2. Timezone settings
  3. Available regions
  4. 1) Europe 6) Pacific 10) Arctic
  5. 2) Asia 7) Australia 11) US
  6. 3) America 8) Atlantic 12) Etc
  7. 4) Africa 9) Indian
  8. 5) Antarctica
  9. Please select the timezone.
  10. Use numbers or type names directly [b to region list, q to quit]: 2
  11. ================================================================================
  12. Timezone settings
  13. Available timezones in region Asia
  14. 1) Aden 29) Hong_Kong 56) Pontianak
  15. 2) Almaty 30) Hovd 57) Pyongyang
  16. 3) Amman 31) Irkutsk 58) Qatar
  17. 4) Anadyr 32) Jakarta 59) Qostanay
  18. 5) Aqtau 33) Jayapura 60) Qyzylorda
  19. 6) Aqtobe 34) Jerusalem 61) Riyadh
  20. 7) Ashgabat 35) Kabul 62) Sakhalin
  21. 8) Atyrau 36) Kamchatka 63) Samarkand
  22. 9) Baghdad 37) Karachi 64) Seoul
  23. 10) Bahrain 38) Kathmandu 65) Shanghai
  24. 11) Baku 39) Khandyga 66) Singapore
  25. 12) Bangkok 40) Kolkata 67) Srednekolymsk
  26. 13) Barnaul 41) Krasnoyarsk 68) Taipei
  27. 14) Beirut 42) Kuala_Lumpur 69) Tashkent
  28. 15) Bishkek 43) Kuching 70) Tbilisi
  29. 16) Brunei 44) Kuwait 71) Tehran
  30. 17) Chita 45) Macau 72) Thimphu
  31. 18) Choibalsan 46) Magadan 73) Tokyo
  32. 19) Colombo 47) Makassar 74) Tomsk
  33. 20) Damascus 48) Manila 75) Ulaanbaatar
  34. 21) Dhaka 49) Muscat 76) Urumqi
  35. 22) Dili 50) Nicosia 77) Ust-Nera
  36. Press ENTER to continue
  37. 23) Dubai 51) Novokuznetsk 78) Vientiane
  38. 24) Dushanbe 52) Novosibirsk 79) Vladivostok
  39. 25) Famagusta 53) Omsk 80) Yakutsk
  40. 26) Gaza 54) Oral 81) Yangon
  41. 27) Hebron 55) Phnom_Penh 82) Yekaterinburg
  42. 28) Ho_Chi_Minh 83) Yerevan
  43. Please select the timezone.
  44. Use numbers or type names directly [b to region list, q to quit]: 65
  45. ================================================================================
  46. Installation
  47. 1) [x] Language settings 2) [x] Time settings
  48. (English (United States)) (Asia/Shanghai timezone)
  49. 3) [x] Installation source 4) [x] Software selection
  50. (Local media) (Minimal Install)
  51. 5) [!] Installation Destination 6) [x] Kdump
  52. (No disks selected) (Kdump is enabled)
  53. 7) [ ] Network configuration 8) [!] Root password
  54. (Not connected) (Password is not set.)
  55. 9) [!] User creation
  56. (No user will be created)
  57. Please make your choice from above ['q' to quit | 'b' to begin installation |
  58. 'r' to refresh]:

输入完毕后,可以发现2的[!]变成了[x],证明配置完毕。
接下来我们配置“Installation Destination”,选择“5”,其余的依次选择“c”,按Enter键,命令如下:

  1. ================================================================================
  2. Installation
  3. 1) [x] Language settings 2) [x] Time settings
  4. (English (United States)) (Asia/Shanghai timezone)
  5. 3) [x] Installation source 4) [x] Software selection
  6. (Local media) (Minimal Install)
  7. 5) [!] Installation Destination 6) [x] Kdump
  8. (No disks selected) (Kdump is enabled)
  9. 7) [ ] Network configuration 8) [!] Root password
  10. (Not connected) (Password is not set.)
  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]: 5
  15. ================================================================================
  16. Probing storage...
  17. Installation Destination
  18. [ ] 3) Use Free Space
  19. Installation requires partitioning of your hard drive. Select what space to use
  20. for the install target.
  21. Please make your choice from above ['q' to quit | 'c' to continue |
  22. 'r' to refresh]: c
  23. ================================================================================
  24. Partition Scheme Options
  25. [ ] 1) Standard Partition
  26. [ ] 2) Btrfs
  27. [x] 3) LVM
  28. [ ] 4) LVM Thin Provisioning
  29. Select a partition scheme configuration.
  30. Please make your choice from above ['q' to quit | 'c' to continue |
  31. 'r' to refresh]: c
  32. Generating updated storage configuration
  33. Checking storage configuration...
  34. ================================================================================
  35. Installation
  36. 1) [x] Language settings 2) [x] Time settings
  37. (English (United States)) (Asia/Shanghai timezone)
  38. 3) [x] Installation source 4) [x] Software selection
  39. (Local media) (Minimal Install)
  40. 5) [x] Installation Destination 6) [x] Kdump
  41. (Automatic partitioning (Kdump is enabled)
  42. selected) 8) [!] Root password
  43. 7) [ ] Network configuration (Password is not set.)
  44. (Not connected)
  45. 9) [!] User creation
  46. (No user will be created)
  47. Please make your choice from above ['q' to quit | 'b' to begin installation |
  48. 'r' to refresh]:

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

  1. ================================================================================
  2. Installation
  3. 1) [x] Language settings 2) [x] Time settings
  4. (English (United States)) (Asia/Shanghai timezone)
  5. 3) [x] Installation source 4) [x] Software selection
  6. (Local media) (Minimal Install)
  7. 5) [x] Installation Destination 6) [x] Kdump
  8. (Automatic partitioning (Kdump is enabled)
  9. selected) 8) [!] Root password
  10. 7) [ ] Network configuration (Password is not set.)
  11. (Not connected)
  12. 9) [!] User creation
  13. (No user will be created)
  14. Please make your choice from above ['q' to quit | 'b' to begin installation |
  15. 'r' to refresh]: 8
  16. 依次输入两次密码,密码相同,我设置的为123456,命令如下:
  17. ================================================================================
  18. Please select new root password. You will have to type it twice.
  19. Password:
  20. Password (confirm):
  21. //这句话的意思:你的密码太过简单,是否使用它,输入yes即可。
  22. ================================================================================
  23. Question
  24. The password you have provided is weak: The password fails the dictionary check
  25. - it is too simplistic/systematic.
  26. Would you like to use it anyway?
  27. Please respond 'yes' or 'no': yes
  28. ================================================================================
  29. Installation
  30. 1) [x] Language settings 2) [x] Time settings
  31. (English (United States)) (Asia/Shanghai timezone)
  32. 3) [x] Installation source 4) [x] Software selection
  33. (Local media) (Minimal Install)
  34. 5) [x] Installation Destination 6) [x] Kdump
  35. (Automatic partitioning (Kdump is enabled)
  36. selected) 8) [x] Root password
  37. 7) [ ] Network configuration (Password is set.)
  38. (Not connected)
  39. 9) [ ] User creation
  40. (No user will be created)
  41. Please make your choice from above ['q' to quit | 'b' to begin installation |
  42. 'r' to refresh]:

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

  1. ================================================================================
  2. Installation
  3. 1) [x] Language settings 2) [x] Time settings
  4. (English (United States)) (Asia/Shanghai timezone)
  5. 3) [x] Installation source 4) [x] Software selection
  6. (Local media) (Minimal Install)
  7. 5) [x] Installation Destination 6) [x] Kdump
  8. (Automatic partitioning (Kdump is enabled)
  9. selected) 8) [x] Root password
  10. 7) [ ] Network configuration (Password is set.)
  11. (Not connected)
  12. 9) [ ] User creation
  13. (No user will be created)
  14. Please make your choice from above ['q' to quit | 'b' to begin installation |
  15. 'r' to refresh]: b
  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. Creating xfs on /dev/mapper/centos-root
  29. .
  30. Running pre-installation scripts
  31. .
  32. Starting package installation process
  33. Preparing transaction from installation source
  34. Installing libgcc (1/303)
  35. Installing grub2-common (2/303)
  36. Installing centos-release (3/303)
  37. Installing setup (4/303)
  38. Installing filesystem (5/303)
  39. ....................
  40. Installing iwl5150-firmware (302/303)
  41. Installing iwl4965-firmware (303/303)
  42. Performing post-installation setup tasks
  43. Installing boot loader
  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. ult. Update your scripts to load br_netfilter if you need this.
  63. CentOS Linux 7 (Core)
  64. Kernel 3.10.0-1127.el7.x86_64 on an x86_64
  65. localhost login: root
  66. Password: 123456
  67. [root@localhost ~]#
  68. //按“Ctrl+]”键,退出终端,回到宿主机。
  69. [root@kvm ~]#

CentOS 7安装完成。

任务5 虚拟机管理

1. KVM基本管理

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

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

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

  1. [root@localhost tmp]# virsh console test
  2. 连接到域 test
  3. 换码符为 ^]
  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@localhost tmp]# virt-clone --original test --name test02 --file
  2. usage: virt-clone --original [NAME] ...
  3. virt-clone: error: argument -f/--file: expected one argument
  4. [root@localhost tmp]#

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

  1. [root@localhost tmp]# virsh shutdown test
  2. test 被关闭
  3. [root@localhost tmp]# virt-clone --original test --name test02 --file /kvm_data/test02.img
  4. 正在分配 'test02.img' | 10 GB 00:00:04
  5. 成功克隆 'test02'
  6. [root@localhost tmp]#
  7. //克隆完成且成功

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

  1. [root@localhost tmp]# ls /etc/libvirt/qemu/
  2. networks test02.xml test.xml
  3. [root@localhost tmp]#

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

  1. [root@localhost tmp]# virsh list --all
  2. Id 名称 状态
  3. ----------------------------------------------------
  4. - test 关闭
  5. - test02 关闭
  6. [root@localhost tmp]# virsh start test02
  7. test02 已开始
  8. [root@localhost tmp]#

3. 快照管理

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