KVM (2) command line to create a new virtual machine

Table of contents

1. Preparation

2. Create a new virtual machine

2.1 Document preparation

2.2 Formal installation

2.3 Time zone setting

 2.4 Installation settings

2.5 Set root user password

2.6 vm2 installation complete

3. Enter the virtual machine vm2

4. Network settings

5. Reference link


If KVM has not been deployed yet, please refer to the first section:

KVM (1) Linux deployment KVM and new virtual machine - luo_guibin's blog - CSDN blog

1. Preparation

1.1 Enable transparent transmission

Enable the "transparent transmission" function of the host, create a new kvm-nested.conf file, and restart the server:

echo "options kvm_intel nested=1" >> /etc/modprobe.d/kvm-nested.conf

"Transparent transmission": Nested virtual nested is a function that can be enabled through kernel parameters. It can make a virtual machine have the CPU characteristics of a physical machine, support vmx or svm (AMD) hardware virtualization, such as the ability to rebuild a virtual machine in a virtual machine.

Query whether transparent transmission is enabled (Y or N):

cat /sys/module/kvm_intel/parameters/nested


If you have not restarted the server, it will display N, and it will display Y after restarting.

2. Create a new virtual machine

2.1 Document preparation

To create a folder, the image needs to be transferred to the iso folder in advance. It is recommended to use the CentOS7.9Minimal version. minimal means the minimum installation version, which is relatively simple, about 1G.

centos-7.9.2009-isos-x86_64 installation package download_Open Source Mirror Station-Alibaba Cloud

#新建文件夹
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 Formal installation

Create a new virtual machine named vm2, vcpu is 1 core, memory is 2G, the storage location is /kvm/img/vm2.qcow2, and the image address is /kvm/iso/centos7.iso. The main places that need to be modified are: n, r, vcpus, disk, location, network, which should be changed according to the actual situation.

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'

command explanation

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'		#文本输出

Set before virtual machine installation, [!] means required, [X] means optional.

The meanings of each option are as follows:
1) Language setting 2) Time zone setting 3) Installation source 4) Software package selection 5)
System partition 6) Kdump function 7) Network configuration 8) Password setting
9) Create user
Press "q" to exit, press " b" to start the installation. You must set all the items marked with "!" to start the installation.

2.3 Time zone setting

Enter 2->1->2->Enter->65 in turn

  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
================================================================================
================================================================================

After filling out! become x

 2.4 Installation settings

According to the minimum installation, all can be defaulted, just press c all the time. 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 Set root user password

8->Password->Password->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
================================================================================
================================================================================

All options! Change to X, enter "b" to start the installation (about 5 minutes)

The installation is complete, press Enter to restart

2.6 vm2 installation complete

Log in normally after reboot

Host view virtual machine vm2 

3. Enter the virtual machine vm2

The host logs in to the virtual machine vm2

virsh console vm2

The following situation often occurs because the process is occupied.

#查看virsh进程号,kill即可

ps -ef | grep virsh
kill -9 ****

Common commands

#列出kvm虚拟机列表,查看是否开机状态。
virsh list --all

#进入虚拟机
virsh console ***

#虚拟机关机
virsh shutdown ***

#虚拟机重启
virsh reboot ***

#强制停止虚拟机
virsh destroy xxx

#彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘
virsh undefine xxx

#设置宿主机开机时该虚拟机也开机
virsh autostart xxx

#解除开机启动
virsh autostart --disable xxx 

退出虚拟终端,按Ctrl + ]

4. Network settings

Check the network settings, no IP,

vi /etc/sysconfig/network-scripts/ifcfg-eth0

Change ifcfg-eth0 to the following content, note that only those with notes later need to be changed .

[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

View the virtual machine vm2 network, no IP, set vm2 and the host as a bridged network . The bridging network means that vm2 can be regarded as a real physical host. The relationship between vm2 and the host is at the same level. vm2 will also occupy an IP under the subnet of 192.168.136.X/24 of the host.

Physical host IP192.168.136.1 Physical host gateway 192.168.136.2 

VMware host IP 192.168.136.60

KVM virtual machine IP 192.168.168.61

Produce two network cards vnet0 and vnet1, where vnet0 is the virtual network card of vm1 I created before, and vnet2 is the network card of vm2.

VMware's VMnet8 setup

Physical host network card cmd input ipconfig to get IP

View kvm network configuration

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>

5. Reference link

KVM deployment: 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

Network settings: Detailed explanation of kvm bridge network and nat network

                  https://www.cnblogs.com/FengGeBlog/p/10452700.html

Guess you like

Origin blog.csdn.net/weixin_48878440/article/details/130748575