目录
若还未部署KVM,请参考第一节:
KVM(一)Linux部署KVM及新建虚拟机_luo_guibin的博客-CSDN博客
一、准备工作
1.1 开启透传
开启主机“透传”功能,新建kvm-nested.conf文件,需要重启服务器:
echo "options kvm_intel nested=1" >> /etc/modprobe.d/kvm-nested.conf
“透传”:嵌套式虚拟nested是一个可通过内核参数来启用的功能。它能够使一台虚拟机具有物理机CPU特性,支持vmx或者svm(AMD)硬件虚拟化,比如具备在虚拟机里再建虚拟机的能力。
查询透传是否开启(Y或N):
cat /sys/module/kvm_intel/parameters/nested
如果你没有重启服务器,是显示N的,重启后就会显示Y了。
二、新建虚拟机
2.1 文件准备
创建文件夹,镜像需先提前传入iso文件夹,推荐使用CentOS7.9Minimal版,minimal表示最小安装版本,比较简洁,大概1G。
centos-7.9.2009-isos-x86_64安装包下载_开源镜像站-阿里云
#新建文件夹
mkdir /kvm
cd /kvm
mkdir iso img
cd /kvm/img
#新建8G的qcow2格式的虚拟机存储,不会占用真实存储空间,用来存储虚拟机。
qemu-img create -f qcow2 /kvm/vm2.qcow2 8G
#需提前将CentOS7.9镜像存放在/kvm/iso下
mv /kvm/iso/CentOS-7-x86_64-Minimal-2009.iso centos7.iso
2.2 正式安装
新建虚拟机,名为vm2,vcpu为1核,内存为2G,存放位置为/kvm/img/vm2.qcow2,镜像地址为/kvm/iso/centos7.iso。主要需要修改的地方是:n、r、vcpus、disk、location、network,根据实际情况更改。
virt-install \
-n vm2 \
-r 2048 \
--vcpus 1 \
--disk /kvm/img/vm2.qcow2 \
--location /kvm/iso/centos7.iso \
--nographics \
--network bridge=br0 \
--network bridge=br0 \
--os-type linux \
--os-variant rhel7 \
--console pty,target_type=serial \
--extra-args 'console=ttyS0,115200n8 serial'
命令解释
virt-install \ #创建命令
-n vm2 \ #虚拟机显示名(非虚拟机主机名)
-r 2048 \ #虚拟机内存大小
--vcpus 1 \ #虚拟机cpu个数
--disk /kvm/img/vm2.qcow2 \ #系统磁盘卷路径
--location /kvm/iso/centos7.iso \ #系统安装iso路径
--nographics \ #不调用图形化界面
--network bridge=br0 \ #网卡1指定网桥
--network bridge=br0 \ #网卡2指定网桥
--os-type linux \ #操作系统类型
--os-variant rhel7 \ #操作系统版本
--console pty,target_type=serial \ #console控制通道
--extra-args 'console=ttyS0,115200n8 serial' #文本输出
虚拟机安装前设置,[!]表示必填,[X]表示选填。
各选项含义如下:
1)语言设置 2)时区设置 3)安装源 4)软件包选择
5)系统分区 6)Kdump功能 7)网络配置 8)密码设置
9)创建用户
按“q”退出,按“b”开始安装。必须把带“!”号的都设置好才能开始安装。
2.3 时区设置
依次输入2->1->2->回车->65
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 2
================================================================================
================================================================================
Time settings
Timezone: not set
NTP servers:not configured
1) Set timezone
2) Configure NTP servers
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: 1
================================================================================
================================================================================
Timezone settings
Available regions
1) Europe 6) Pacific 10) Arctic
2) Asia 7) Australia 11) US
3) America 8) Atlantic 12) Etc
4) Africa 9) Indian
5) Antarctica
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 2
================================================================================
================================================================================
Timezone settings
Available timezones in region Asia
1) Aden 29) Hong_Kong 56) Pontianak
2) Almaty 30) Hovd 57) Pyongyang
3) Amman 31) Irkutsk 58) Qatar
4) Anadyr 32) Jakarta 59) Qostanay
5) Aqtau 33) Jayapura 60) Qyzylorda
6) Aqtobe 34) Jerusalem 61) Riyadh
7) Ashgabat 35) Kabul 62) Sakhalin
8) Atyrau 36) Kamchatka 63) Samarkand
9) Baghdad 37) Karachi 64) Seoul
10) Bahrain 38) Kathmandu 65) Shanghai
11) Baku 39) Khandyga 66) Singapore
12) Bangkok 40) Kolkata 67) Srednekolymsk
13) Barnaul 41) Krasnoyarsk 68) Taipei
14) Beirut 42) Kuala_Lumpur 69) Tashkent
15) Bishkek 43) Kuching 70) Tbilisi
16) Brunei 44) Kuwait 71) Tehran
17) Chita 45) Macau 72) Thimphu
18) Choibalsan 46) Magadan 73) Tokyo
19) Colombo 47) Makassar 74) Tomsk
20) Damascus 48) Manila 75) Ulaanbaatar
21) Dhaka 49) Muscat 76) Urumqi
22) Dili 50) Nicosia 77) Ust-Nera
Press ENTER to continue
23) Dubai 51) Novokuznetsk 78) Vientiane
24) Dushanbe 52) Novosibirsk 79) Vladivostok
25) Famagusta 53) Omsk 80) Yakutsk
26) Gaza 54) Oral 81) Yangon
27) Hebron 55) Phnom_Penh 82) Yekaterinburg
28) Ho_Chi_Minh 83) Yerevan
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 65
================================================================================
================================================================================
填写完成后!变成X
2.4 安装设置
按照最小化安装即可,全部默认即可,一直按c就可以。5->c->c->c
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 5
================================================================================
================================================================================
Probing storage...
Installation Destination
[x] 1) : 8192 MiB (vda)
1 disk selected; 8192 MiB capacity; 8192 MiB free ...
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
================================================================================
================================================================================
Autopartitioning Options
[ ] 1) Replace Existing Linux system(s)
[x] 2) Use All Space
[ ] 3) Use Free Space
Installation requires partitioning of your hard drive. Select what space to use
for the install target.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
================================================================================
================================================================================
Partition Scheme Options
[ ] 1) Standard Partition
[ ] 2) Btrfs
[x] 3) LVM
[ ] 4) LVM Thin Provisioning
Select a partition scheme configuration.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
Generating updated storage configuration
Checking storage configuration...
================================================================================
================================================================================
2.5 设置root用户密码
8->密码->密码->yes
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 8
================================================================================
================================================================================
Please select new root password. You will have to type it twice.
Password:
Password (confirm):
================================================================================
================================================================================
Question
The password you have provided is weak: The password fails the dictionary check
- it does not contain enough DIFFERENT characters.
Would you like to use it anyway?
Please respond 'yes' or 'no': yes
================================================================================
================================================================================
所有选项!变成X,输入“b”开始安装(大概5分钟)
安装完成,按下回车重启
2.6 vm2安装完成
重启后正常登录
宿主机查看虚拟机vm2
三、进入虚拟机vm2
宿主机登录虚拟机vm2
virsh console vm2
经常会出现下面这种情况,原因是进程占用。
#查看virsh进程号,kill即可
ps -ef | grep virsh
kill -9 ****
常用命令
#列出kvm虚拟机列表,查看是否开机状态。
virsh list --all
#进入虚拟机
virsh console ***
#虚拟机关机
virsh shutdown ***
#虚拟机重启
virsh reboot ***
#强制停止虚拟机
virsh destroy xxx
#彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘
virsh undefine xxx
#设置宿主机开机时该虚拟机也开机
virsh autostart xxx
#解除开机启动
virsh autostart --disable xxx
退出虚拟终端,按Ctrl + ]
四、网络设置
查看网络设置,无IP,
vi /etc/sysconfig/network-scripts/ifcfg-eth0
将ifcfg-eth0更改成下面内容,注意,只有后面有备注的才需要更改。
[root@localhost /]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #更改:此处更改为静态IP
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=0cd3a0e8-9595-4ed6-9f6c-b16d3590887c
DEVICE=eth0
ONBOOT=yes #更改:此处更改成开机自启动
IPADDR=192.168.136.61 #添加:注意要跟你宿主机在同一网段
NETMASK=255.255.255.0 #添加:掩码
GATEWAY=192.168.136.2 #添加:网关,物理主机VMnet8网卡、VMware虚拟宿主机、KVM虚拟机三者网关一致。
DNS2=8.8.8.8 #添加:DNS2,注意!此处必须有DNS2!有没有DNS都可以,原因未知!
[root@localhost /]# systemctl restart network
查看虚拟机vm2网络,无IP,设置vm2和宿主机为桥接网络。桥接网络意味着可以把vm2当做一台真正的物理主机,vm2与宿主机是平级关系,vm2同样会占有宿主机192.168.136.X/24该子网下的一个IP。
物理主机IP192.168.136.1 物理主机网关192.168.136.2
VMware宿主机IP 192.168.136.60
KVM虚拟机IP 192.168.168.61
生产两个网卡vnet0、vnet1,其中vnet0是我之前创建的vm1的虚拟网卡,而vnet2则是vm2的网卡。
VMware的VMnet8设置
物理主机网卡cmd输入ipconfig获取IP
查看kvm网络配置
virsh net-list
virsh net-info default
virsh net-dumpxml default
[root@localhost ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
[root@localhost ~]# virsh net-info default
Name: default
UUID: 130180ad-221a-416e-9895-a63eb0fcbc22
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0
#注意此处的192.168.22.X网段其实是KVM的NAT模式,此处没有启用。
[root@localhost ~]# virsh net-dumpxml default
<network>
<name>default</name>
<uuid>130180ad-221a-416e-9895-a63eb0fcbc22</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:af:48:81'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
五、参考链接
KVM部署:https://shaonaiyi.blog.csdn.net/article/details/117412805?spm=1001.2014.3001.5502
https://shaonaiyi.blog.csdn.net/article/details/117511250?spm=1001.2014.3001.5502
网络设置:kvm桥接网络和nat网络详解