基于内核的虚拟机 Kernel-based Virtual Machine(KVM)是一种内建于 Linux® 中的开源虚拟化技术。使主机计算机能够运行多个隔离的虚拟环境,即虚拟客户机或虚拟机(VM)。

环境准备

检查该服务器是否支持虚拟化,尤其是cpu,这里如果没有输出则需要进入BIOS去调整cpu的虚拟化,具体百度即可,虚拟机则很方便在虚拟机设置里即可打开cpu虚拟化

egrep -o 'vmx | svm' /proc/cpuinfo | wc -l

关闭防火墙和selinux

# systemctl stop firewalld
# systemctl disable firewalld
# setenforce 0
# vim /etc/selinux/config
# 修改以下内容
SELINUX=disabled

安装KVM

# yum install -y kvm virt-* libvirt bridge-utils qemu-img

查看kvm是否被加载到内核

# lsmod | grep kvm

网络设置

和VMware一样有桥接模式和nat模式

桥接模式

就是添加了新网卡br0,将br0绑定到ens33上

绑定网卡--以太网通道--聚合链路--桥接网络
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33 
# 更改,把原有ip删了
ONBOOT=none
BRIDGE=br0
BOOTPROTO=dhcp
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0
# 更改
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
# 添加:
IPADDR=192.168.1.1
NETMASK=255.255.255.0
# 删除
BRIDGE=br0
# 重启网卡
systemctl restart network

这样在查看网络设备时,会多处一块br0设备

查看虚拟网络设备brctl show

NAT模式

启动libvirtd服务后,自会生成一个virbr0的虚拟网络设备

# systemctl start libvirtd
# systemctl disable ilbvirtd
# systemctl startmessagebus

如果启动libvirtd报错如下

libvirtd: relocation error: libvirtd: ... version ... libdevmapper.so.1.02   [失败]

只需要更新对应软件包即可

# yum upgrade device-mapper-libs

然后查看虚拟设备

# brctl show
bridge name    bridge id        STP enabled    interfaces
virbr0        8000.52540037d52e    yes        virbr0-nic

安装虚拟机

创建虚拟机磁盘镜像文件

执行此命令会在指定路径下生成一个文件,大小为指定大小,指定的大小须在物理机容量范围

# qemu-img create -f qcow2 -o preallocation=metadata /root/test.qcow2 20G

将使用该磁盘镜像文件来安装虚拟机

执行安装虚拟机

图形化安装

需要连接vnc等图形界面工具

# virt-install --name test_1 --ram 8192 --vcpus 4 \
-f /root/test_1.qcow2 --os-type linux \
--os-variant rhel7 --network bridge=virbr0 \
--cdrom CentOS-7-x86_64-DVD-1810.iso \
--graphics vnc,listen=0.0.0.0,port=5920
# 输出如下
WARNING  Graphics requested but DISPLAY is not set. Not running virt-viewer.
WARNING  No console to launch for the guest, defaulting to --wait -1

Starting install...
Domain installation still in progress. Waiting for installation to complete.
# 这个位置需要连接vnc,进行选择语言/硬盘等界面的操作

需要准备一个vnc工具连接宿主机的ip和上面设置的port,即可进行图形化界面的安装

命令行安装

命令行安装的结果是我把电脑放置了一夜然后才看到成功的

使用以下会进入命令行的系统安装

# virt-install --virt-type=kvm --name test --vcpus=2 --memory=8192 \
--location=/root/CentOS-7-x86_64-DVD-1810.iso \
--disk path=/root/test.qcow2,size=4,format=qcow2 \
--network bridge=virbr0 --graphics none \
--extra-args='console=ttyS0' --force

进入命令行选择

需要选择序号后面带感叹号的进行设置,下面的3&4不用进行设置

时区设置
===================================================================================================
===================================================================================================
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
# 选择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   # 选择设置时区,如果有NTP服务器则选择2
===================================================================================================
===================================================================================================
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)  Qyzylorda
 5)  Aqtau                 33)  Jayapura              60)  Riyadh
 6)  Aqtobe                34)  Jerusalem             61)  Sakhalin
 7)  Ashgabat              35)  Kabul                 62)  Samarkand
 8)  Atyrau                36)  Kamchatka             63)  Seoul
 9)  Baghdad               37)  Karachi               64)  Shanghai
10)  Bahrain               38)  Kathmandu             65)  Singapore
11)  Baku                  39)  Khandyga              66)  Srednekolymsk
12)  Bangkok               40)  Kolkata               67)  Taipei
13)  Barnaul               41)  Krasnoyarsk           68)  Tashkent
14)  Beirut                42)  Kuala_Lumpur          69)  Tbilisi
15)  Bishkek               43)  Kuching               70)  Tehran
16)  Brunei                44)  Kuwait                71)  Thimphu
17)  Chita                 45)  Macau                 72)  Tokyo
18)  Choibalsan            46)  Magadan               73)  Tomsk
19)  Colombo               47)  Makassar              74)  Ulaanbaatar
20)  Damascus              48)  Manila                75)  Urumqi
21)  Dhaka                 49)  Muscat                76)  Ust-Nera
22)  Dili                  50)  Nicosia               77)  Vientiane
23)  Dubai                 51)  Novokuznetsk          78)  Vladivostok
24)  Dushanbe              52)  Novosibirsk           79)  Yakutsk
25)  Famagusta             53)  Omsk                  80)  Yangon
26)  Gaza                  54)  Oral                  81)  Yekaterinburg
27)  Hebron                55)  Phnom_Penh            82)  Yerevan
28)  Ho_Chi_Minh
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 64
# 选择64为上海时区,然后会自动退回到设置界面
硬盘分区
===================================================================================================
===================================================================================================
Installation

 1) [x] Language settings                 2) [x] Time settings
        (English (United States))                (Asia/Shanghai timezone)
 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]: 5
  # 选择5,进行硬盘选择分区
===================================================================================================
===================================================================================================
Probing storage...
Installation Destination

[x] 1) : 20 GiB (vda)

1 disk selected; 20 GiB capacity; 20 GiB free ...

  Please make your choice from above ['q' to quit | 'c' to continue |  'r' to refresh]: c
  # 因为只有一个20G的qcow2文件,所以只有一块虚拟硬盘,c继续
===================================================================================================
===================================================================================================
Autopartitioning Options

[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 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
  # 选择2或者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]: 3
  # 选择3,使用LVM的方式进行分区,看情况定即可,然后选择c继续
设置root密码
===================================================================================================
===================================================================================================
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
  # 选择8进行设置密码
===================================================================================================
===================================================================================================
Please select new root password. You will have to type it twice.

Password:                      # 输入密码
Password (confirm):            # 再次输入确认密码
===================================================================================================
===================================================================================================
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    # 输入yes,确认使用这样的密码,复杂性要求够了就不会出现这一步
开始安装
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [x] Time settings
        (English (United States))                (Asia/Shanghai timezone)
        (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
  # 开始安装

开始安装后和正常服务器开机是一样的,这一步会卡很长时间

........
Installing iwl100-firmware (291/306)
Installing iwl2030-firmware (292/306)
Installing iwl5000-firmware (293/306)
Installing iwl6000g2b-firmware (294/306)
Installing iwl5150-firmware (295/306)
Installing iwl7260-firmware (296/306)
Installing iwl3160-firmware (297/306)
Installing iwl105-firmware (298/306)
Installing iwl3945-firmware (299/306)
Installing iwl7265-firmware (300/306)
Installing ivtv-firmware (301/306)
Installing iwl6050-firmware (302/306)
Installing iwl2000-firmware (303/306)
Installing iwl6000g2a-firmware (304/306)
Installing iwl6000-firmware (305/306)
Installing iwl135-firmware (306/306)
Performing post-installation setup tasks

[anaconda] 1:main* 2:shell  3:log  4:storage-lo> Switch tab: Alt+Tab | Help: F1 

就这个界面一直持续了一夜,当我想要放弃的时候(ctrl+c),突然好了,出现了以下内容,以下内容都是我多次按ctrl+c出来的。

也可以尝试在以上阻塞的地方一开始就按ctrl+c

.....
Installing iwl135-firmware (306/306)
Performing post-installation setup tasks
^CInstalling boot loader
.
Performing post-installation setup tasks
.

Configuring installed system
^C.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs


^C.
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

[anaconda] 1:main* 2:shell  3:log  4:storage-lo> Switch tab: Alt+Tab | Help: F1 

然后这个位置回车,就会重启VM

设置开机自启VM

# virsh autostart VM-NAME(虚拟机名)     // 开机自启
# virsh autostart --disable VM-NAME(虚拟机名)   // 开机不自启 

宿主机查看VM_IP

我是根据图形化安装成功的,所以在安装完成重启之后,还是通过vnc连接到了,之后就和正常的服务器一样的网络设置,因为没有IP,我直接使用dhcp获取了kvm的122网段的默认ip,设置与宿主机的ssh即可方便登录

通过宿主机查看ip

# virsh edit VM-NAME 
在这个编辑的文件中找到mac地址,通过mac地址查看ip
# arp -a | grep -i 52:54:00:d7:e6:bc
? (192.168.122.213) at 52:54:00:d7:e6:bc [ether] on virbr0

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0