CentOS 7.5 安装KVM虚拟机

目的与目标

在实体机上安装KVM虚拟,并启动一个centos虚拟机。

前置说明

安装KVM对机器有一定的要求,比如svm, selinux等,这些可以忽略,只要不是虚拟机一般都没啥问题。

管理KVM使用libvirt来实现,否则一个KVM啥用没有。

依赖安装

安装依赖会导致一大波更新出现,安装之前可以考虑先升级一波。

yum install qemu-kvm libvirt virt-install bridge-utils

安装成功之后,重启之,如下命令:

reboot

查看KVM安装的状态,可以使用lsmod查看状态

lsmod | grep kvm

例如我这里输出的,妥妥的。

kvm_amd              2176416  0 
kvm                   578558  1 kvm_amd
irqbypass              13503  1 kvm

服务管理

由于安装了libvirt,需要启动对应的服务才能正常稳定的运行,例如:

systemctl start libvirtd
systemctl status libvirtd

此时输出一堆状态信息,正常不正常,看天命了,出错了就继续折腾吧。

虚拟机安装

CentOS镜像下载

下载虚拟机,使用aira2,使用阿里云的源,方便快捷,不过也开始以使用wget,如:

wget https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso
CentOS 7.5 镜像 Aiar2 下载
CentOS 7.5 镜像 Aiar2 下载

使用sha1校验文件是否完整,sha1sum命令,如下。可以对比一下是否正确。

sha1sum CentOS-7-x86_64-Minimal-1804.iso
13675c6f74880e7ff3481b91bdaf925ce81bda8f  CentOS-7-x86_64-Minimal-1804.iso

安装虚拟机

直接贴抄袭的安装配置

错误的配置

virt-install \
--virt-type=kvm \
--name=CentOS75 \
--vcpus=2 \
--memory=2048 \
--location=/home/loveyu/iso/CentOS-7-x86_64-Minimal-1804.iso \
--disk path=/home/loveyu/vms/centos75.qcow2,size=15,format=qcow2 \
--network bridge=br0 \
--graphics none \
--extra-args='console=ttyS0' \
--force

上面的配置会报如下错误,如:

开始安装......
搜索文件 .treeinfo......                                                                                                          |  354 B  00:00:00     
搜索文件 vmlinuz......                                                                                                            | 5.9 MB  00:00:00     
搜索文件 initrd.img......                                                                                                         |  50 MB  00:00:00     
正在分配 'centos75.qcow2'                                                                                                         |  15 GB  00:00:00     
ERROR    Cannot access storage file '/home/loveyu/vms/centos75.qcow2' (as uid:107, gid:107): 权限不够
正在删除磁盘 'centos75.qcow2'                                                                                                   |    0 B  00:00:00     
域安装失败,您可以运行下列命令重启您的域:
'virsh start virsh --connect qemu:///system start CentOS75'
否则请重新开始安装。

原因是啥呢?简单命令权限原因。

修改正确配置如:

virt-install \
--virt-type=kvm \
--name=CentOS75 \
--vcpus=2 \
--memory=2048 \
--location=/opt-data/Storage/CentOS-7-x86_64-Minimal-1804.iso \
--disk path=/home/vms/centos75.qcow2,size=15,format=qcow2 \
--network bridge=virbr0 \
--graphics none \
--extra-args='console=ttyS0' \
--force

如果提示出现无法在 'virbr0 ' 获取接口 MTU: 没有那个设备,可以尝试使用ip addr查看现在的桥接设备是什么。

如果一切正常,会进入安装iso安装界面。

配置安装虚拟机

启动后,会出现如下内容:

Starting installer, one moment...
anaconda 21.48.22.134-1 for CentOS 7 started.
 * 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
18:21:12 Not asking for VNC because we don't have a network
================================================================================
================================================================================
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]: 

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

此时进入主题,随便玩玩就可以进入了。

一些基本的设置以后,如下显示:

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 disabled)
        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]: 

基本设置如下:

  1. 语言保持不变
  2. 时区选择上海
  3. 安装源选择本地镜像
  4. 软件选择最小安装
  5. 安装磁盘选择自动分别,且使用LVM逻辑卷管理
  6. 关闭Kdump,虚拟机关系不大
  7. 网络先不管,安装之后处理
  8. Root 密码设置为 123456
  9. 不创建新用户

最后,按下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/300)
Installing grub2-common (2/300)

....

Installing iwl6000g2b-firmware (299/300)
Installing iwl5150-firmware (300/300)
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

几分钟后,安装结束。

各种回车后进入登陆界面,输入弱口令密码,搞定。

      CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)                             
      CentOS Linux (0-rescue-8707fc0ebc304aecae24959960933077) 7 (Core)     

......

[   10.953832] Netfilter messages via NETLINK v0.30.
[   10.967303] ip_set: protocol 6

CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
Password: 
[root@localhost ~]# 

查看与连接虚拟机

  1. 查看虚拟机

    virsh list

    Id    名称                         状态
    ----------------------------------------------------
    8     CentOS75                       running
  2. 连接虚拟机

    virsh console CentOS75

接下来就是设置网卡之类的步骤,关于端口转发和IP地址之类的东西。

附录

4条评论在“CentOS 7.5 安装KVM虚拟机”

写下你最简单的想法