首页 > 代码库 > 安装kvm

安装kvm

一、安装kvm

1、系统环境:

[root@template ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@template ~]# uname -r
3.10.0-327.el7.x86_64

2、查看机器是否支持虚拟化

[root@template ~]# grep -E "svm | vmx" /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf 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 ida arat epb pln pts dtherm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt

3、环境要求

处理器开启vt-x功能

技术分享

blos 开启虚拟化

 技术分享

4、安装kvm管理软件

yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install -y

软件的功能介绍:
Libvirt管理kvm本身的工具
Virt-install 安装虚拟机
Virt-manager管理创建删除虚拟机的工具

5、查看模块是否被加载

[root@Centos01 ~]# lsmod |grep kvmkvm_intel             162153  0 kvm                   525259  1 kvm_intel

6、配置服务

#开机自启动服务[root@Centos01 ~]# systemctl enable libvirtd.service#启用服务[root@Centos01 ~]# systemctl start libvirtd.service

7、查看虚拟网卡

[root@Centos01 ~]# ifconfigeno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 192.168.30.138  netmask 255.255.255.0  broadcast 192.168.30.255        inet6 fe80::20c:29ff:fe86:12e5  prefixlen 64  scopeid 0x20<link>        ether 00:0c:29:86:12:e5  txqueuelen 1000  (Ethernet)        RX packets 2784  bytes 402493 (393.0 KiB)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 3094  bytes 646100 (630.9 KiB)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: 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#虚拟网卡virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255        ether 52:54:00:29:0e:3c  txqueuelen 0  (Ethernet)        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

8、查看服务是否激活

[root@Centos01 ~]# systemctl is-active libvirtd.serviceactive

二、添加第二块硬盘用于存放虚拟机

1、添加160G硬盘

技术分享

添加完硬盘,重启系统(reboot), 重启的作用是发现新硬盘。

2、对硬盘进行分区

[root@template ~]# fdisk -l
#自带的第一块硬盘磁盘 /dev/sda:171.8 GB, 171798691840 字节,335544320 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x00047142 设备 Boot Start End Blocks Id System/dev/sda1 * 2048 1026047 512000 83 Linux/dev/sda2 1026048 335544319 167259136 8e Linux LVM
#添加的第二块硬盘磁盘 /dev/sdb:171.8 GB, 171798691840 字节,335544320 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-home:113.4 GB, 113355259904 字节,221396992 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节
#对硬盘进行分区(160G大小,分成一个分区)[root@template ~]# fdisk /dev/sdb欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。Device does not contain a recognized partition table使用磁盘标识符 0xc5edf4d2 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):nPartition type: p primary (0 primary, 0 extended, 4 free) e extendedSelect (default p): p分区号 (1-4,默认 1):1起始 扇区 (2048-335544319,默认为 2048):直接回车将使用默认值 2048Last 扇区, +扇区 or +size{K,M,G} (2048-335544319,默认为 335544319):直接回车将使用默认值 335544319分区 1 已设置为 Linux 类型,大小设为 160 GiB命令(输入 m 获取帮助):w #保存The partition table has been altered!Calling ioctl() to re-read partition table.正在同步磁盘。[root@template ~]# mkfs.ext3 /dev/sdb1mke2fs 1.42.9 (28-Dec-2013)文件系统标签=OS type: Linux块大小=4096 (log=2)分块大小=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks10485760 inodes, 41942784 blocks2097139 blocks (5.00%) reserved for the super user第一个数据块=0Maximum filesystem blocks=42949672961280 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872Allocating group tables: 完成 正在写入inode表: 完成 Creating journal (32768 blocks): 完成Writing superblocks and filesystem accounting information: 完成
#挂载sdb1[root@template ~]# mount /dev/sdb1 /opt

#添加开机自启动
[root@template ~]# vi /etc/fstab

    # /etc/fstab
    # Created by anaconda on Mon Mar 20 21:34:44 2017
    #
    # 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=1def7626-55d2-4d85-b2b1-2d82ae268045 /boot xfs defaults 0 0
    /dev/mapper/centos-home /home xfs defaults 0 0
    /dev/mapper/centos-swap swap swap defaults 0 0
    /dev/cdrom /mnt/cdrom iso9660 defaults 0 0
    /dev/sdb1 /opt ext3 defaults 0 0     #增加这一行代码


#查看新挂载硬盘[root@template ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/mapper/centos-root 50G 1.2G 49G 3% /devtmpfs 2.0G 0 2.0G 0% /devtmpfs 2.0G 0 2.0G 0% /dev/shmtmpfs 2.0G 8.5M 2.0G 1% /runtmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup/dev/sr0 4.1G 4.1G 0 100% /mnt/cdrom/dev/mapper/centos-home 106G 33M 106G 1% /home/dev/sda1 497M 108M 390M 22% /boottmpfs 394M 0 394M 0% /run/user/0/dev/sdb1 158G 60M 150G 1% /opt

 

安装kvm