kvm

kvm是linux内核提供的基于硬件辅助的虚拟化实现方案,主要组件包括:内核态(kvm.ko/kvm_inter.ko/kvm_amd.ko)及用户态(qemu)。关于kvm原理,推荐阅读KVM 虚拟化详解。本文主要提供kvm+centos7.2的安装过程。

NAT

原理图:
nat

网桥

原理图:
bridge

制作镜像(NAT)

先决条件

1
2
cpu支持:egrep -q "vmx|svm" /proc/cpuinfo && echo "yes"
内核支持:lsmod |grep kvm

准备基础ISO镜像

1
2
3
4
官方CentOS-7-x86_64-Minimal-1511.iso 
wget https://buildlogs.centos.org/rolling/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
mkdir -p /export/images/
cp CentOS-7-x86_64-Minimal-1511.iso /export/images/

安装依赖

安装

1
2
yum -y install qemu-kvm virt-install libvirt bridge-utils
systemctl enable libvirtd && systemctl start libvirtd

检查

1
2
3
4
5
6
7
1) /usr/sbin/libvirtd --version
  /usr/sbin/libvirtd (libvirt) 4.5.0
2) /usr/libexec/qemu-kvm --version
  QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-167.el7_7.4), Copyright (c) 2003-2008 Fabrice Bellard
3) virsh list –all
4) uname -r(大于等于2.6.20,自动包含kvm内核模块)
  3.10.0-327.28.3.el7.x86_64

注意

1
2
报错:version libssl.so.10 not defined in file libssl.so.10
修复:yum -y install openssl && yum -y update openssl

安装系统

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
virt-install \                                         
--name adns-vm \                                             //虚拟机名称 
--vcpus=8 \                                                  //虚拟机cpu个数
--memory 32768 \                                             //虚拟机内存大小,单位MB,32G
--disk /export/images/adns-vm.qcow2,size=100,format=qcow2 \  //存储磁盘格式及大小,单位GB, 100G
--network default \                                          //虚拟机网卡:默认为nat模式,网桥模式:--network bridge:br0
--os-type=linux \                                            //虚拟机操作系统类型
--os-variant=rhel7.2 \                                       //虚拟机操作系统具体版本
--location /export/images/CentOS-7-x86_64-Minimal-1511.iso \ //安装源地址
--initrd-inject=/export/images/ks.cfg \                      //系统自动安装配置脚本,见下节
--extra-args "ks=file:/ks.cfg console=ttyS0" \               //虚拟机登录字符终端
--graphics none \                                            //虚拟机不开启图形登录界面
--force                                                      //防止交互式提示

自动安装配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
vi /export/images/ks.cfg
###############################################################
#
# Environment setup
#
###############################################################

text
cdrom
skipx
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
eula --agreed
reboot

###############################################################
#
# network configuration
#
###############################################################

network --bootproto=dhcp --device=eth0 --onboot=on --ipv6=auto
network --hostname=adns
timezone Asia/Shanghai --isUtc

###############################################################
#
# partitioning
#
###############################################################

ignoredisk --only-use=vda
bootloader --location=mbr --boot-drive=vda
zerombr
clearpart --all --initlabel --drives=vda
autopart --type=lvm

# part swap --asprimary --fstype="swap" --size=1024
# part /boot --fstype xfs --size=200
# part pv.01 --size=1 --grow
# volgroup rootvg01 pv.01
# logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow


###########################################################################################
# 
# User Accounts
# Generate encrypted password: python -c 'import crypt; print(crypt.crypt("My Password"))'
# Or openssl passwd -1 password
#
###########################################################################################

rootpw adns 
# user --groups=wheel --name=josepy --password=password --gecos="Mutai Josphat"

###############################################################
#
# SELinux and Firewalld
#
###############################################################

#selinux --enforcing
#selinux --permissive
selinux --disabled

#firewall --enabled --http --ssh --ftp --port=https:tcp --port=ipp:tcp
firewall --disabled


###############################################################
#
# Software Packages
#
###############################################################

%packages --nobase --ignoremissing
@core
@base
vim 
bash-completion

%end