“Centos7 使用kvm创建虚拟机”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
1. KVM环境配置
3. 创建NAT模式KVM虚拟机
 
(未显示2个用户的13个中间版本)
第1行: 第1行:
==1. KVM环境配置==
+
==基础配置==
关闭Selinux
+
*[[Linux基础设置]]
<nowiki>
 
[root@ localhost ~]#vim /etc/sysconfig/selinux
 
# 修改SELINUX=disabled
 
# 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 these two values:
 
#    targeted - Targeted processes are protected,
 
#    mls - Multi Level Security protection.
 
SELINUXTYPE=targeted
 
  
  </nowiki>
+
===查看是否支持虚拟机===
 +
[root@localhost ~]# egrep -c '(vmx|svm)' /proc/cpuinfo
 +
 
 +
2
 +
 
 +
如果执行这条命令的返回结果为0则表示CPU不支持虚拟化,如果返回结果为1或者大于1到数字则表示CPU支持虚拟。其中vmx为Intel的CPU指令集,svm为AMD的CPU指令集。
 +
 
 +
如果显示为0,需关机并做如下设置:
 +
 
 +
[[文件:cloud1-20.png|600px]]
 +
 
 +
==2. 安装KVM ==
 +
 
 +
===安装KVM软件包===
 +
yum install qemu-kvm openssl libvirt
 +
 
 +
查看模块是否加载:
 +
 
 +
lsmod | grep kv
 +
 
 +
  <nowiki>kvm_intel            183621  0
 +
kvm                  586948  1 kvm_intel
 +
irqbypass              13503  1 kvm</nowiki>
 +
 
 +
 
 +
===查看虚拟机环境===
 +
[root@localhost ~]#systemctl start libvirtd      启动libvirt服务
 +
 
 +
[root@localhost ~]#systemctl enable libvirtd
 +
 
 +
将/usr/libexec/qemu-kvm链接为/usr/bin/qemu-kvm:
 +
 
 +
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm   
 +
 
 +
[root@localhost ~]# systemctl status libvirtd
 +
 
 +
<nowiki>● libvirtd.service - Virtualization daemon
 +
  Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
 +
  Active: active (running) since Tue 2019-11-19 05:49:47 CST; 15min ago
 +
    Docs: man:libvirtd(8)
 +
          https://libvirt.org
 +
Main PID: 6988 (libvirtd)
 +
  CGroup: /system.slice/libvirtd.service
 +
          ├─6988 /usr/sbin/libvirtd
 +
          ├─7166 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa...
 +
          └─7168 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa..</nowiki>
 +
 
 +
==3. 创建NAT模式KVM虚拟机 ==
 +
 
 +
将cirros-0.3.3-x86_64-disk.img镜像与qemu-ifup-NAT脚本文件上传到系统/root目录下。
 +
 
 +
(下载地址如下:链接: https://pan.baidu.com/s/1I8n853xgyNdOPvCI8y7sog 提取码: frh9 )
 +
 
 +
[root@localhost ~]# ls
 +
 
 +
cirros-0.3.3-x86_64-disk.img  qemu-ifup-NAT.txt
 +
 
 +
[root@localhost ~]#mv qemu-ifup-NAT.txt qemu-ifup-NAT
 +
 
 +
[root@localhost ~]# ls
 +
 
 +
cirros-0.3.3-x86_64-disk.img  qemu-ifup-NAT
 +
 
 +
给脚本赋予执行权限。示例代码如下:
 +
 
 +
[root@localhost ~]# chmod +x /root/qemu-ifup-NAT
 +
 
 +
通过qemu-kvm命令启动KVM虚拟机。示例代码如下:
 +
 
 +
[root@localhost ~]# qemu-kvm -m 1024 -drive file=/root/cirros-0.3.3-x86_64-disk.img,if=virtio -net nic,model=virtio -net tap,script=/root/qemu-ifup-NAT -nographic -vnc :1
 +
 
 +
创建虚拟机完成后,cirros用户登录虚拟机,输入用户名为cirros,密码为cubswin:)。
 +
 
 +
<nowiki>############ debug end  ##############
 +
  ____              ____  ____
 +
/ __/ __ ____ ____ / __ \/ __/
 +
/ /__ / // __// __// /_/ /\ \
 +
\___//_//_/  /_/  \____/___/
 +
  http://cirros-cloud.net
 +
 
 +
 
 +
login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
 +
cirros login:
 +
</nowiki>
 +
 
 +
然后输入ip addr list命令查询IP地址,最后输入route -n命令查询路由表。示例代码如下:
 +
 
 +
$ ip addr list
 +
 
 +
<nowiki>1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
 +
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 +
    inet 127.0.0.1/8 scope host lo
 +
    inet6 ::1/128 scope host
 +
      valid_lft forever preferred_lft forever
 +
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
 +
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
 +
    inet 192.168.122.89/24 brd 192.168.122.255 scope global eth0
 +
    inet6 fe80::5054:ff:fe12:3456/64 scope link
 +
      valid_lft forever preferred_lft forever</nowiki>
 +
 
 +
 
 +
$ route -n
 +
 
 +
<nowiki>Kernel IP routing table
 +
Destination    Gateway        Genmask        Flags Metric Ref    Use Iface
 +
0.0.0.0        192.168.122.1  0.0.0.0        UG    0      0        0 eth0
 +
192.168.122.0  0.0.0.0        255.255.255.0  U    0      0        0 eth0</nowiki>

2019年12月4日 (三) 04:11的最新版本

基础配置

查看是否支持虚拟机

[root@localhost ~]# egrep -c '(vmx|svm)' /proc/cpuinfo

2

如果执行这条命令的返回结果为0则表示CPU不支持虚拟化,如果返回结果为1或者大于1到数字则表示CPU支持虚拟。其中vmx为Intel的CPU指令集,svm为AMD的CPU指令集。

如果显示为0,需关机并做如下设置:

Cloud1-20.png

2. 安装KVM

安装KVM软件包

yum install qemu-kvm openssl libvirt

查看模块是否加载:

lsmod | grep kv

kvm_intel             183621  0
kvm                   586948  1 kvm_intel
irqbypass              13503  1 kvm


查看虚拟机环境

[root@localhost ~]#systemctl start libvirtd 启动libvirt服务

[root@localhost ~]#systemctl enable libvirtd

将/usr/libexec/qemu-kvm链接为/usr/bin/qemu-kvm:

[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm

[root@localhost ~]# systemctl status libvirtd

● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-11-19 05:49:47 CST; 15min ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 6988 (libvirtd)
   CGroup: /system.slice/libvirtd.service
           ├─6988 /usr/sbin/libvirtd
           ├─7166 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa...
           └─7168 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa..

3. 创建NAT模式KVM虚拟机

将cirros-0.3.3-x86_64-disk.img镜像与qemu-ifup-NAT脚本文件上传到系统/root目录下。

(下载地址如下:链接: https://pan.baidu.com/s/1I8n853xgyNdOPvCI8y7sog 提取码: frh9 )

[root@localhost ~]# ls

cirros-0.3.3-x86_64-disk.img  qemu-ifup-NAT.txt

[root@localhost ~]#mv qemu-ifup-NAT.txt qemu-ifup-NAT

[root@localhost ~]# ls

cirros-0.3.3-x86_64-disk.img  qemu-ifup-NAT

给脚本赋予执行权限。示例代码如下:

[root@localhost ~]# chmod +x /root/qemu-ifup-NAT

通过qemu-kvm命令启动KVM虚拟机。示例代码如下:

[root@localhost ~]# qemu-kvm -m 1024 -drive file=/root/cirros-0.3.3-x86_64-disk.img,if=virtio -net nic,model=virtio -net tap,script=/root/qemu-ifup-NAT -nographic -vnc :1

创建虚拟机完成后,cirros用户登录虚拟机,输入用户名为cirros,密码为cubswin:)。

############ debug end   ##############
  ____               ____  ____
 / __/ __ ____ ____ / __ \/ __/
/ /__ / // __// __// /_/ /\ \
\___//_//_/  /_/   \____/___/
   http://cirros-cloud.net


login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
cirros login:

然后输入ip addr list命令查询IP地址,最后输入route -n命令查询路由表。示例代码如下:

$ ip addr list

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.89/24 brd 192.168.122.255 scope global eth0
    inet6 fe80::5054:ff:fe12:3456/64 scope link 
       valid_lft forever preferred_lft forever


$ route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG    0      0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0