虚拟化KVM

虚拟化

安装KVM
修改虚拟机内存,至少设置成2 GB,双击“编辑虚拟机设置”按钮
然后新建硬盘设置大小50G
检查CPU参数
启动虚拟机,我们查看以下,内存是否更改成功

[root@kvm ~]# free
       total      used     free      shared    buff/cache   available
Mem:  1868688  125360  1582372     8752     160956     1584208
Swap:  097148     0     2097148

检查CPU是否开启虚拟化支持,命令:

root@kvm ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
//vmx为英特尔的cpu;svm为AMD的cpu。
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 arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves
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 arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves

如果显示为空,就要检查你虚拟机设置,是否打“√”

挂载新磁盘
使用lsblk命令,检查虚拟机是否新增一块50 GB的磁盘

[root@kvm ~]# 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@kvm ~]# 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@kvm ~]# blkid /dev/sdb
//blkid+磁盘:查看磁盘信息
/dev/sdb: UUID="dab6d659-e334-41ed-a15a-96ce05b48c1a" TYPE="ext4" 

格式化完成后,我们挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载,命令如下:

[root@kvm ~]# mkdir /kvm_data
[root@kvm ~]# mount /dev/sdb /kvm_data/

这种方式挂载,重启虚拟机后,需要再次重新挂载,我们为了方便开机后可以自动挂载,编写/etc/fstab文件,添加文件最末一行,命令如下:

[root@kvm ~]# vi /etc/fstab 
[root@kvm ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Aug 22 22:51:46 2019
#
# 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=34f3cd91-b7f0-44dd-9334-2bb66e939898 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb     /kvm_data           ext4      defaults    0 0

关闭防火墙和selinux
关闭防火墙并设置开机不自启

[root@kvm ~]# systemctl stop firewalld
[root@kvm ~]# 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@kvm ~]# vi /etc/selinux/config
[root@kvm ~]# 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@kvm ~]# getenforce 
Disabled

安装KVM

上述操作完成后,我们使用Yum进行安装KVM,命令如下

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

启动KVM
配置网卡
增加桥接网卡ifcfg-br0

[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0

修改桥接网卡 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.16.5
NETMASK=255.255.255.0
GATEWAY=192.168.16.2
DNS1=114.114.114.114
DNS2=8.8.8.8

修改NAT网卡ifcfg-eno16777736

[root@kvm network-scripts]# vi ifcfg-en
[root@kvm network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=yes
BRIDGE=br0

修改完成网卡内容后,重新启动网卡服务并查看网卡信息,命令如下

[root@kvm network-scripts]# service network restart
Restarting network (via systemctl):  [  OK  ]
 	
[root@kvm network-scripts]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.16.5  netmask 255.255.255.0  broadcast 
192.168.16.255
        inet6 fe80::20c:29ff:fe8d:1f90  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8d:1f:90  txqueuelen 0  (Ethernet)
        RX packets 90  bytes 7092 (6.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 38  bytes 3604 (3.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  
mtu 1500
        ether 00:0c:29:8d:1f:90  txqueuelen 1000  (Ethernet)
        RX packets 126287  bytes 182444678 (173.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32532  bytes 2953477 (2.8 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 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

启动libvirtd服务
首先检查KVM模块是否加载,命令如下(lsmod命令用于显示已载入系统的模块):

[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
root      17270      1  1 04:36 ?        00:00:00 /usr/sbin/libvirtd
nobody    17383      1  0 04:36 ?        00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root      17384  17383  0 04:36 ?        00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root      17416   2264  0 04:36 pts/0    00:00:00 grep --color=auto libvirt

启动成功后,使用brctl命令可以看到两个网卡,如下代码所示:

[root@kvm ~]# cd
[root@kvm ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0            8000.000c298d1f90       no          eno16777736
virbr0          8000.525400c7e229       yes           virbr0-nic

命令行安装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镜像文件,命令如下:

[root@kvm ~]# ll /tmp/
total 4228096
-rw-r--r--  1 root root 4329570304 Apr 15  2016 CentOS-7-x86_64-DVD-1511.iso

镜像存在,接下来使用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"

打完上面那条命令后,等一会,就可以看见下面这段命令

Starting installer, one moment...
anaconda 21.48.22.56-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
17:01:51 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [!] Timezone 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 进行时区设置
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值