KVM虚拟化

一.KVM的安装

1.虚拟机调整

修改虚拟机内存,至少设置成2 GB,双击“编辑虚拟机设置”按钮

KVM虚拟化_第1张图片

内存修改为“2048

KVM虚拟化_第2张图片

在“硬件”选项卡中单击“处理器”,	在虚拟化引擎中勾选如红框内

KVM虚拟化_第3张图片

添加硬盘

KVM虚拟化_第4张图片

选择硬盘,单击“下一步”按钮

KVM虚拟化_第5张图片

选择“SCSI”,单击“下一步”按钮

KVM虚拟化_第6张图片

选中“创建新虚拟磁盘”选项,单击“下一步”按钮

KVM虚拟化_第7张图片

将最大磁盘大小修改为“50”,然后点击“下一步”按钮

KVM虚拟化_第8张图片

单击“完成”按钮,磁盘添加完成

KVM虚拟化_第9张图片

单击“确定”按钮

KVM虚拟化_第10张图片

2.检查CPU参数

启动虚拟机,查看一下内存是否更改成功
[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1868688      124980     1585124        8664      158584     1586188
Swap:       2097148           0     2097148
检查CPU是否开启虚拟化支持(如果显示为空,就要检查你虚拟机设置)

KVM虚拟化_第11张图片

3. 挂载新磁盘

用lsblk命令,检查虚拟机是否新增一块50 GB的磁盘
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   50G  0 disk 
sr0              11:0    1    4G  0 rom  
新增了一块磁盘名为sdb,接下来格式化这块磁盘,格式化成ext4格式
[root@localhost ~]# 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@localhost ~]# blkid /dev/sdb
/dev/sdb: UUID="0cd71265-2183-46aa-b75b-74056fabf82d" TYPE="ext4" 
格式化完成后,我们挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载
[root@localhost ~]# mkdir /kvm_data
[root@localhost ~]# mount /dev/sdb /kvm_data/
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  855M   17G   5% /
devtmpfs                 903M     0  903M   0% /dev
tmpfs                    913M     0  913M   0% /dev/shm
tmpfs                    913M  8.5M  904M   1% /run
tmpfs                    913M     0  913M   0% /sys/fs/cgroup
/dev/sda1                497M  125M  373M  25% /boot
tmpfs                    183M     0  183M   0% /run/user/0
/dev/sdb                  50G   53M   47G   1% /kvm_data
这种方式挂载,重启虚拟机后,需要再次重新挂载,
我们为了方便开机后可以自动挂载,编写/etc/fstab文件,添加文件最末一行
[root@localhost ~]# cat /etc/fstab

#
#/etc/fstab
#Created by anaconda on Thu Nov 19 00:48:57 2020
#
#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=44be7c0d-0204-406e-87f8-ece95ce8f77c /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb     `/kvm_data`           `ext4`   defaults    0 0

4. 关闭防火墙和selinux

关闭防火墙并设置开机不自启
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
关闭SELinux,编辑/etc/selinux/config,将enforcing修改为disabled
[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 two values:
#targeted - Targeted processes are protected,
#minimum - Modification of targeted policy. Only selected processes are protected. 
#mls - Multi Level Security protection.
SELINUXTYPE=targeted 
修改完成后,重启虚拟机,使其生效,使用getenforce查看
[root@localhost ~]# reboot
[root@localhost ~]# getenforce
Disabled

5. 安装KVM

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

二.启动KVM

1.配置网卡

增加桥接网卡ifcfg-br0:
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-eno16777728 ifcfg-br0
//ifcfg-eno16777736为配置IP的网卡
修改桥接网卡ifcfg-br0的内容:
[root@kvm network-scripts]# vi ifcfg-br0 
[root@kvm network-scripts]# cat ifcfg-br0 
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.200.27
PREFIX=24
GATEWAY=192.168.200.2
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
DNS1=114.114.114.114
修改NAT网卡ifcfg-eno16777736:
[root@kvm network-scripts]# vi ifcfg-eno16777728 
[root@kvm network-scripts]# cat ifcfg-eno16777728
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777728
DEVICE=eno16777728
ONBOOT=yes
BRIDGE=br0
修改完成网卡内容后,重新启动网卡服务并查看网卡信息:

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

[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: `eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000`
    link/ether 00:0c:29:34:53:6d 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:34:53:6d brd ff:ff:ff:ff:ff:ff
    inet `192.168.200.27/24 brd 192.168.200.255` scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::b0b3:90ff:fe40:c540/64 scope link 
       valid_lft forever preferred_lft forever

2.启动libvirtd服务

查KVM模块是否加载
[root@kvm network-scripts]# lsmod|grep kvm
kvm_intel             162153  0 
kvm                   525259  1 kvm_intel
启动libvirtd并检查是否成功启动
[root@kvm network-scripts]# systemctl start libvirtd
[root@kvm network-scripts]# ps -ef|grep libvirt

KVM虚拟化_第12张图片

启动成功后,使用brctl命令可以看到两个网卡
[root@kvm network-scripts]# cd
[root@kvm ~]# brctl show
bridge name	    bridge id		    STP enabled	   interfaces
br0		    8000.000c2934536d	      no		   eno16777728
virbr0		8000.000000000000	      yes		

3. 命令行安装CentOS 7

上传CentOS 7镜像到/tmp目录下,如果没有镜像的可以自行下载。
上传后,使用ll命令查看/tmp目录下是否存在CentOS 7镜像文件
[root@localhost ~]# mv CentOS-7-x86_64-DVD-1511.iso /tmp/

在这里插入图片描述

检测镜像已存在,接下来使用virt-install进行CentOS 7的安装

[root@kvm ~]# 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虚拟化_第13张图片

报错(ERROR Host does not support any virtualization options ): 解决办法: 题出在程序包上? 1.百度得知spice server是作为一个库提供给qemu使用的,编译出来就是libspice(新知识点get,然而这玩意咋调试排错是完全不懂的,那么先看看qemu-kvm有没有问题吧。 2.ll /usr/libexec/qemu-kvm 先看qemu-kvm是否有执行权限,发现是有的,那么正常。 3./usr/libexec/qemu-kvm --version 手动运行下试试,发现报错: version libssl.so.10 not defined in file libssl.so.10 4.依然通过百度获得解决方法解决办法: yum -y install openssl 如果已经安装,就更新一下 yum -y update openssl 5.我这里直接yum upgrade openssl,神奇的事情发生了, /usr/libexec/qemu-kvm --version 正常显示版本 virt-install 成功通过,问题解决。

打完上面那条命令后,等一会,就可以看见下面这段命令,这是系统基础设置,
[!]基本都是需要配置的,接下来我们开始配置“Timezone settings”,
输入“2”,按Enter键,命令如下:
  • installation log files are stored in /tmp during the installation
  • shell is available on TTY2
  • when reporting a bug add logs from /tmp as separate text/plain attachments
    17:01:51 Not asking for VNC because we don’t have a network
    ================================================================================
    ================================================================================
    Installation
  1. Language settings 2) [!] Timezone settings
    (English (United States)) (Timezone is not set.)
  2. [!] Installation source 4) [!] Software selection
    (Processing…) (Processing…)
  3. [!] Installation Destination 6) [x] Kdump
    (No disks selected) (Kdump is enabled)
  4. Network configuration 8) [!] Root password
    (Not connected) (Password is not set.)
  5. [!] User creation
    (No user will be created)
    Please make your choice from above [‘q’ to quit | ‘b’ to begin installation | ‘r’ to refresh]: 2
    =====================================================================
“Timezone settings”时区设置选择 5) Asia亚洲,再选择城市 
62) Shanghai上海,命令如下:

Timezone settings

Available regions

  1. Africa 6) Atlantic 10) Pacific
  2. America 7) Australia 11) US
  3. Antarctica 8) Europe 12) Etc
  4. Arctic 9) Indian
  5. Asia
    Please select the timezone.
    Use numbers or type names directly [b to region list, q to quit]: 5
    ================================================================================
    ================================================================================
    Timezone settings

Available timezones in region Asia

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

你可能感兴趣的:(kvm)