任务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型
Hypervisor直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的Linux系统。Xen和VMWare的ESXi 都属于这个类型。
Type-II型
物理机上首先安装常规的操作系统,比如 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. 架构
任务3 安装KVM
1. 调整虚拟机
修改虚拟机内存,至少设置成2 GB,双击“编辑虚拟机设置”按钮,如下图所示:
将虚拟机的内存修改为“2048”,如图所示:
在“硬件”选项卡中单击“处理器”, 在虚拟化引擎中勾选如红框内所示选项,如下图所示:
接下来,我们添加硬盘,单击“添加”,如下图所示:
然后选择硬盘,单击“下一步”按钮,如下图所示:
选择“SCSI”,单击“下一步”按钮,如下图所示:
选中“创建新虚拟磁盘”选项,单击“下一步”按钮,如下图所示:
将最大磁盘大小修改为“60”,然后点击“下一步”按钮,如下图所示:
单击“完成”按钮,磁盘添加完成。如下图所示:
单击“确定”按钮,如下图所示:
至此,虚拟机设置完成。如图所示:
配置完成,我们启动虚拟机。
2. 检查CPU参数
启动虚拟机,我们查看以下,内存是否更改成功,命令如下:
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1863040 138636 1579844 9744 144560 1573584
Swap: 2097148 0 2097148
[root@localhost ~]#
检查CPU是否开启虚拟化支持,命令如下:
[root@localhost ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
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
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
[root@localhost ~]#
3. 挂载新磁盘
使用lsblk命令,检查虚拟机是否新增一块60 GB的磁盘,命令如下:
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 60G 0 disk
sr0 11:0 1 1024M 0 rom
[root@localhost ~]#
我们可以看到确实新增了一块磁盘名为sdb。
接下来格式化这块磁盘,格式化成ext4格式,命令如下:
[root@localhost ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3932160 inodes, 15728640 blocks
786432 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2164260864
480 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: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]#
//blkid+磁盘:查看磁盘信息
[root@localhost ~]# blkid /dev/sdb
/dev/sdb: UUID="501c7dfb-805c-4383-bab6-723bd6a49040" TYPE="ext4"
[root@localhost ~]#
格式化完成后,我们挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载,命令如下:
[root@localhost ~]# mkdir /kvm_data
[root@localhost ~]# mount /dev/sdb /kvm_data/
[root@localhost ~]#
这种方式挂载,重启虚拟机后,需要再次重新挂载,我们为了方便开机后可以自动挂载,编写/etc/fstab文件,添加文件最末一行,命令如下:
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Aug 10 23:37:47 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=12ba9b17-8596-439c-bd06-cca6d607513b /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sdb /kvm_data ext4 defaults 0 0
[root@localhost ~]#
4. 关闭防火墙和selinux
关闭防火墙并设置开机不自启,命令如下:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]#
关闭SELinux,编辑/etc/selinux/config,将enforcing修改为disabled,命令如下:
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]#
5. 安装KVM
上述操作完成后,我们使用Yum进行安装KVM,命令如下:
[root@localhost ~]# yum install -y virt-* libvirt bridge-utils qemu-img
任务4 启动KVM
1. 配置网卡
增加桥接网卡ifcfg-br0,命令如下:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]#
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]#
//ifcfg-ens3为配置IP的网卡,你的网卡可能与我的不同
修改桥接网卡ifcfg-br0的内容,命令如下:
[root@localhost network-scripts]# vim ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
GATEWAY=192.168.100.2
DNS1=114.114.114.114
[root@localhost network-scripts]#
修改NAT网卡ifcfg-ens33,内容如下:
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@localhost network-scripts]#
修改完成网卡内容后,重新启动网卡服务并查看网卡信息,命令如下:
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@localhost network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::3c93:7bff:fe6f:23b2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:6e:81 txqueuelen 1000 (Ethernet)
RX packets 27 bytes 1842 (1.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28 bytes 2380 (2.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:58:6e:81 txqueuelen 1000 (Ethernet)
RX packets 96882 bytes 141115746 (134.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11850 bytes 1452899 (1.3 MiB)
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 1000 (Local Loopback)
RX packets 70 bytes 6090 (5.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 70 bytes 6090 (5.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost network-scripts]#
可以清楚的看到br0网卡出现并且带有IP,而以前的ens33网卡则没有IP。网卡配置完成
2. 启动libvirtd服务
首先检查KVM模块是否加载,命令如下:
[root@localhost network-scripts]# lsmod|grep kvm
kvm_intel 188688 0
kvm 636969 1 kvm_intel
irqbypass 13503 1 kvm
[root@localhost network-scripts]#
启动libvirtd并检查是否成功启动,命令如下:
[root@localhost network-scripts]# systemctl start libvirtd
[root@localhost network-scripts]# ps -ef |grep libvirt
root 3081 1 6 12:00 ? 00:00:00 /usr/sbin/libvirtd
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
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
root 3247 1508 0 12:00 pts/0 00:00:00 grep --color=auto libvirt
[root@localhost network-scripts]#
启动成功后,使用brctl命令可以看到两个网卡,如下代码所示:
[root@localhost network-scripts]# cd
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29586e81 no ens33
virbr0 8000.5254003e01aa yes virbr0-nic
[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版的
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
CentOS-7-x86_64-DVD-2003.iso
systemd-private-af176190b89c468baa54a54ae13d96f8-chronyd.service-f7vrhy
vmware-root_689-4021587913
[root@localhost tmp]# ll
总用量 4669440
-rw-r--r--. 1 root root 4781506560 10月 13 2020 CentOS-7-x86_64-DVD-2003.iso
drwx------. 3 root root 17 10月 1 11:29 systemd-private-af176190b89c468baa54a54ae13d96f8-chronyd.service-f7vrhy
drwx------. 2 root root 6 10月 1 11:29 vmware-root_689-4021587913
[root@localhost tmp]#
镜像存在,接下来使用virt-install进行CentOS 7的安装,命令如下:
[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键,命令如下:
================================================================================
Installation
1) [x] Language settings 2) [!] Time settings
(English (United States)) (Timezone is not set.)
3) [!] Installation source 4) [!] Software selection
(Processing...) (Processing...)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 2
================================================================================
Time settings
Timezone: not set
NTP servers:not configured
1) Set timezone
2) Configure NTP servers
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: 1
“Timezone settings”时区设置选择 2) Asia亚洲,再选择城市 65) Shanghai上海,命令如下:
================================================================================
Timezone settings
Available regions
1) Europe 6) Pacific 10) Arctic
2) Asia 7) Australia 11) US
3) America 8) Atlantic 12) Etc
4) Africa 9) Indian
5) Antarctica
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 2
================================================================================
Timezone settings
Available timezones in region Asia
1) Aden 29) Hong_Kong 56) Pontianak
2) Almaty 30) Hovd 57) Pyongyang
3) Amman 31) Irkutsk 58) Qatar
4) Anadyr 32) Jakarta 59) Qostanay
5) Aqtau 33) Jayapura 60) Qyzylorda
6) Aqtobe 34) Jerusalem 61) Riyadh
7) Ashgabat 35) Kabul 62) Sakhalin
8) Atyrau 36) Kamchatka 63) Samarkand
9) Baghdad 37) Karachi 64) Seoul
10) Bahrain 38) Kathmandu 65) Shanghai
11) Baku 39) Khandyga 66) Singapore
12) Bangkok 40) Kolkata 67) Srednekolymsk
13) Barnaul 41) Krasnoyarsk 68) Taipei
14) Beirut 42) Kuala_Lumpur 69) Tashkent
15) Bishkek 43) Kuching 70) Tbilisi
16) Brunei 44) Kuwait 71) Tehran
17) Chita 45) Macau 72) Thimphu
18) Choibalsan 46) Magadan 73) Tokyo
19) Colombo 47) Makassar 74) Tomsk
20) Damascus 48) Manila 75) Ulaanbaatar
21) Dhaka 49) Muscat 76) Urumqi
22) Dili 50) Nicosia 77) Ust-Nera
Press ENTER to continue
23) Dubai 51) Novokuznetsk 78) Vientiane
24) Dushanbe 52) Novosibirsk 79) Vladivostok
25) Famagusta 53) Omsk 80) Yakutsk
26) Gaza 54) Oral 81) Yangon
27) Hebron 55) Phnom_Penh 82) Yekaterinburg
28) Ho_Chi_Minh 83) Yerevan
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 65
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
输入完毕后,可以发现2的[!]变成了[x],证明配置完毕。
接下来我们配置“Installation Destination”,选择“5”,其余的依次选择“c”,按Enter键,命令如下:
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 5
================================================================================
Probing storage...
Installation Destination
[ ] 3) Use Free Space
Installation requires partitioning of your hard drive. Select what space to use
for the install target.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
================================================================================
Partition Scheme Options
[ ] 1) Standard Partition
[ ] 2) Btrfs
[x] 3) LVM
[ ] 4) LVM Thin Provisioning
Select a partition scheme configuration.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
Generating updated storage configuration
Checking storage configuration...
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [!] Root password
7) [ ] Network configuration (Password is not set.)
(Not connected)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
接下来配置“Root password”,选择8,按Enter键,命令如下:
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [!] Root password
7) [ ] Network configuration (Password is not set.)
(Not connected)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 8
依次输入两次密码,密码相同,我设置的为123456,命令如下:
================================================================================
Please select new root password. You will have to type it twice.
Password:
Password (confirm):
//这句话的意思:你的密码太过简单,是否使用它,输入yes即可。
================================================================================
Question
The password you have provided is weak: The password fails the dictionary check
- it is too simplistic/systematic.
Would you like to use it anyway?
Please respond 'yes' or 'no': yes
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [x] Root password
7) [ ] Network configuration (Password is set.)
(Not connected)
9) [ ] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
配置完成,选择“b”,按Enter键后,开始安装,命令如下:
================================================================================
Installation
1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [x] Root password
7) [ ] Network configuration (Password is set.)
(Not connected)
9) [ ] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: b
================================================================================
Progress
Setting up the installation environment
.
Creating disklabel on /dev/vda
.
Creating xfs on /dev/vda1
.
Creating lvmpv on /dev/vda2
.
Creating swap on /dev/mapper/centos-swap
.
Creating xfs on /dev/mapper/centos-root
.
Running pre-installation scripts
.
Starting package installation process
Preparing transaction from installation source
Installing libgcc (1/303)
Installing grub2-common (2/303)
Installing centos-release (3/303)
Installing setup (4/303)
Installing filesystem (5/303)
....................
Installing iwl5150-firmware (302/303)
Installing iwl4965-firmware (303/303)
Performing post-installation setup tasks
Installing boot loader
.
Performing post-installation setup tasks
.
Configuring installed system
.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs
.
Running post-installation scripts
.
Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
Installation complete. Press return to quit //这里按回车
..................
ult. Update your scripts to load br_netfilter if you need this.
CentOS Linux 7 (Core)
Kernel 3.10.0-1127.el7.x86_64 on an x86_64
localhost login: root
Password: 123456
[root@localhost ~]#
//按“Ctrl+]”键,退出终端,回到宿主机。
[root@kvm ~]#
任务5 虚拟机管理
1. KVM基本管理
完成虚拟机安装,已经退回到宿主机,接下来我们来通过宿主机virsh命令进行管理刚才安装的CentOS 7虚拟机。
查看虚拟机列表,命令如下:
[root@localhost tmp]# virsh list
//查看虚拟机列表,只能看到正在运行的虚拟机
Id 名称 状态
----------------------------------------------------
2 test running
[root@localhost tmp]# virsh list --all
//查看虚拟机列表,包括未运行的虚拟机
Id 名称 状态
----------------------------------------------------
2 test running
[root@localhost tmp]#
进入指定的虚拟,命令如下:
[root@localhost tmp]# virsh console test
连接到域 test
换码符为 ^]
//如果迟迟未动敲下回车就ok,下面就是正常登录时需要输入的用户名密码
CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64
localhost login:
若发生以下报错信息,命令如下:
[root@kvm ~]# virsh console test
Connected to domain test
Escape character is ^]
error: operation failed: Active console session exists for this domain
解决方式,命令如下:
[root@kvm ~]# ps -ef |grep console
root 11167 10987 0 01:55 tty1 00:00:00 virsh console test
root 16017 15750 0 10:01 pts/1 00:00:00 grep --color=auto console
[root@kvm ~]# kill -9 11167
vrish常用命令,命令如下:
virsh shutdown test
//关闭虚拟机
virsh start test
//开启虚拟机
virsh destroy test
//类似stop,这个是强制停止
virsh undefine test
//彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到
virsh autostart test
//宿主机开机该虚拟机也开机
virsh autostart --disable test
//解除开机启动
virsh suspend test
//挂起
virsh resume test
//恢复
2. 克隆虚拟机
克隆虚拟机之前,要先关闭虚拟机,不然会提示这种错误,命令如下:
[root@localhost tmp]# virt-clone --original test --name test02 --file
usage: virt-clone --original [NAME] ...
virt-clone: error: argument -f/--file: expected one argument
[root@localhost tmp]#
关闭虚拟机后,再次进行克隆,命令如下:
[root@localhost tmp]# virsh shutdown test
域 test 被关闭
[root@localhost tmp]# virt-clone --original test --name test02 --file /kvm_data/test02.img
正在分配 'test02.img' | 10 GB 00:00:04
成功克隆 'test02'。
[root@localhost tmp]#
//克隆完成且成功
命令解释:
—original:指定克隆源虚拟机。
—name:指定克隆后的虚拟机名字。
—file:指定目标虚拟机的虚拟磁盘文件。
查看虚拟机配置文件/etc/libvirt/qemu/,看是否增加test02.xml文件,命令如下:
[root@localhost tmp]# ls /etc/libvirt/qemu/
networks test02.xml test.xml
[root@localhost tmp]#
接下来,我们启动刚刚克隆的虚拟机test02,首先查看虚拟机列表,然后启动克隆的虚拟机test02,命令如下:
[root@localhost tmp]# virsh list --all
Id 名称 状态
----------------------------------------------------
- test 关闭
- test02 关闭
[root@localhost tmp]# virsh start test02
域 test02 已开始
[root@localhost tmp]#
3. 快照管理
快照是在我们使用Vmware的时候,很常用的一个功能。它可以回到之前的某一状态。在KVM中RAW格式的虚拟磁盘不支持做快照,qcow2支持。
创建快照,命令如下: