virt-install例子, KVM虚拟化命令速查表

时间:2020-02-23 14:40:40  来源:igfitidea点击:

在本文中,将介绍使用RHEL/CentOS 8 Linux中的" virt-install"示例创建KVM虚拟机命令行的命令列表。

" virt-install"是用于使用" libvirt"虚拟机管理程序管理库创建新的KVM,Xen或者Linux容器客户机的命令行工具。
virt-install工具提供了许多可以在命令行上传递的选项。
要查看选项的完整列表,请运行以下命令:

# virt-install --help

前提条件

作为"准备工作",必须首先在Linux服务器主机上安装KVM并启用KVM虚拟化,然后才能使用" virt-install"或者RHEL/CentOS 8 Linux中的任何其他方法创建KVM虚拟机。

1. virt-install --name

要定义虚拟机的名称,请使用--namevirt-install命令

[root@rhel-8 ~]# virt-install --name centos8-2

2. virt-install --vcpus

要为VM定义CPU,请使用带有--virt-install命令的--vcpus命令。

[root@rhel-8 ~]# virt-install --name centos8-2 --vcpus 2

3. virt-install-内存

要为虚拟机定义内存,请使用带有--virt-install命令的--memory。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240

4. virt-install --os-type和--os-variant

我们可以在使用virt-install命令创建VM时定义OS变量和OS类型。
接下来分别使用--os-variant和--os-type来定义OS变量和OS类型。

要"获取支持的OS变体类型列表",请从终端执行以下命令

# osinfo-query os

在下面的" virt-install"示例速查表中,我将" --os-type"定义为Linux,将" --os-variant"定义为" Centos7.0"。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0

5. virt-install --network

要检查可用网络列表,请使用virsh命令

[root@rhel-8 ~]# virsh net-list
 Name                 State      Autostart     Persistent
---------------------------------------------------------
 default              active     yes           yes

在下面的" virt-install"示例速查表中,我们将创建KVM VM并为我们的虚拟机分配此" default"网络

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default --graphics=vnc -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz...                                                            | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                         |  52 MB  00:00:00
Allocating 'centos8-2-3.qcow2'                                                        |  10 GB  00:00:00

6.具有多个网络的virt-install示例

我们可以多次使用--network参数将多个网卡添加到虚拟机中。
在此" virt-install"示例速查表中,我使用一个网桥连接创建一个KVM VM,使用默认网络创建另一个网络

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default  --network bridge=nm-bridge --console pty,target_type=serial --graphics=vnc -v

提示:

我们可以使用--location或者--cdrom启动安装。
我观察到--location--cdrom的区别,使用--cdrom的VM只是使用提供的ISO引导,而使用--location的安装会自动开始

7. virt-install --location

  • 在" virt-install"示例下面,使用" --location"参数定义" ISO"的路径,该路径将用于创建KVM虚拟机。

  • 在此命令中,我们没有"定义"任何" --graphics"

  • 因此,此命令的问题是我们将使用"串行控制台"连接到新的VM,但由于未定义控制台,因此最终会出现" no STDOUT"。

  • 在这种情况下," virt-install"将卡在转义字符" ^]"提示符下

[root@rhel-8 ~]# virt-install --name centos8-20 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none  -v                                  
Using rhel7.6 default --disk size=10
Starting install...
Retrieving file vmlinuz...                                                                   | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                                |  52 MB  00:00:00
Allocating 'centos8-20.qcow2'                                                                |  10 GB  00:00:00
Connected to domain centos8-20
Escape character is ^]

另外,如果我们尝试使用virsh或者Virtual Manager连接到VM,我们将获得相同的输出

[root@rhel-8 ~]# virsh console centos8-2
Connected to domain centos8-2
Escape character is ^]
error: operation failed: Active console session exists for this domain

因此,不建议使用此组合来创建KVM虚拟机。

8. virt-install控制台ttyS0(--extra-args)

  • 现在,在前面的" virt-install"示例中,我们使用了" --location"来定义带有" --graphics = none"的ISO。

  • 由于这种结合,"我们最终获得了没有图形访问权的串行控制台连接"

  • 我们可以结合使用--location和--extra-args来定义其他内核参数,这些参数将在安装时考虑

  • 使用--extra-args console = ttyS0重定向终端窗口上的STDOUT

  • 该命令的问题是我们将再次使用"串行控制台"连接到新的VM。

  • 因此,如果我们必须选择自定义选项来执行安装,则使用串行控制台可能对我们来说很困难

  • 对于需要图形控制台执行安装的正常使用情况,请勿使用此命令。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none --extra-args console=ttyS0 -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00
Allocating 'centos8-2.qcow2'                                                                                                                          |  10 GB  00:00:00
Connected to domain centos8-2
Escape character is ^]
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct

使用Ctrl + 5退出会话。

要重新连接到会话,请使用以下命令

[root@rhel-8 ~]# virsh console centos8-2

说明:

我们也可以在这些命令中使用--cdrom,但是为了突出显示,我们不能将--cdrom与内核参数--extra-args一起使用,结果将相同,因此建议在使用virt-install创建VM时总是使用--graphics

我们可以根据需要选择使用此命令。

9. virt-install --noautoconsole

  • 不要自动尝试连接到访客控制台。

  • 默认情况下,创建虚拟机后," virt-install"命令将"继续等待安装完成"。

  • 如果我们希望脱离virt-install会话,则可以使用--noautoconsole

  • 如果命令要求进行多步安装,例如--cdrom或者--location,则在安装阶段完成后,VM将被关闭,无论VM是否请求重新启动。

  • 如果我们想重启虚拟机,virt-install必须保持运行状态。

  • 即使指定了--noautoconsole,也可以使用" --wait"来使" virt-install"保持活动状态。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge  --graphics=none --console pty,target_type=serial --noautoconsole --extra-args console=ttyS0  -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00
Allocating 'centos8-2-17.qcow2'                                                                                                                       |  10 GB  00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

如预期的那样,该命令在创建VM之后退出。
接下来使用virsh连接到控制台

[root@rhel-8 ~]# virsh console centos8-2

10.使用virt-viewer进行virt-install --graphics

  • 在较早的" virt-install"示例中,我们禁用了图形,这就是为什么最终结果"不是非常用户友好"的原因。

  • 在此命令中,我们将使用virt-viewer在创建虚拟机后查看控制台

  • 我们可以在RHEL/CentOS 7/8 Linux上使用yum -y install virt-viewer来安装virt-viewer

  • 我已经定义了--graphics以使用VNC执行安装

  • 该命令执行结束时,virt-viewer将自动连接到新VM的控制台

  • 在大多数用例中,使用virt-install创建KVM虚拟机时,建议使用此命令。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge  --graphics=vnc --console pty,target_type=serial -v
Starting install...
Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00
Allocating 'centos8-2-17.qcow2'                                                                                                                       |  10 GB  00:00:00
Domain creation completed.
Restarting guest.

下一个virt-viewer将自动连接到centos8-2控制台

11.使用VNC Viewer进行virt-install --graphics

  • virt-viewer类似,我们也可以使用VNC Viewer连接到新VM。

  • 在这个" virt-install"示例速查表中,我已经在一个终端中使用" --noautoconsole"从CLI中执行了" virt-install",以断开会话

  • 接下来,我将使用VNC查看器连接到VM控制台

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge --graphics type=vnc --console pty,target_type=serial --noautoconsole -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00
Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00
Allocating 'centos8-2-14.qcow2'                                                                                                                       |  10 GB  00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

要获取VNC端口号,VNC正在使用virsh命令进行监听

[root@rhel-8 ~]# virsh vncdisplay centos8-2
127.0.0.1:3

在Linux服务器上安装tigervnc

# yum -y install tigervnc

接下来执行vncviewer以连接到控制台

# vncviewer <address>:<port>
# vncviewer 127.0.0.1:3

12. virt-install --disk(路径,大小,格式)

  • 在以上所有" virt-install"示例中,我们都使用"默认存储路径和磁盘大小"来创建KVM虚拟机

  • 要定义存储磁盘的自定义路径,请使用--disk参数

  • 要定义虚拟机的磁盘大小,请使用带有--disk参数的大小。提供的值将以GB为单位

  • 我们可以使用'raw','qcow2','vmdk'作为受支持的磁盘格式

  • 在这个" virt-install"示例命令中,VM的20GB磁盘将在"/disks"下创建为" centos8-2.qcow2"。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc -v
Starting install...

13. virt-install-自动启动

默认情况下,VM被配置为在主机服务器重新引导时保持关闭电源。
当主机服务器在重新引导后启动时,我们也可以使用--autostart来启动VM。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2  --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc --autostart -v

14.具有多个磁盘的virt-install示例

  • 在较早的" virt-install"示例中,我们使用单个磁盘来创建KVM虚拟机。

  • 我们也可以使用--disk参数多次使用virt-install定义多个磁盘。

  • 在这个" virt-install"示例中,我将创建带有2个磁盘的KVM虚拟机,在/disks下有一个自定义路径。

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2  --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc  -v
Starting install...
Allocating 'centos8-2.qcow2'                                                                                                                          |  20 GB  00:00:00
Allocating 'centos8-2-1.qcow2'                                                                                                                        |  20 GB  00:00:00

验证分配给该虚拟机的磁盘列表

[root@rhel-8 ~]# ls -l /disks/
total 6808
-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2-1.qcow2
-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2.qcow2

15. virt-install --pxe和--boot网络

  • 使用virt-install命令创建VM以使用PXE执行安装

  • 因此,如果我们有可用的PXE服务器,则只需将--pxevirt-install一起使用

  • 如果我们还用--pxe定义--location,那么virt-install将从ISO提供的--location中获取initrdvmlinuz文件。

  • 要和PXE一起使用特定的kickstart文件,请使用--extra-args =

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default --pxe --extra-args "ks=/tmp/kickstart.conf" --console pty,target_type=serial  --graphics=vnc -v

或者,我们也可以使用--boot网络从PXE服务器启动

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0   --network network=default --boot network  --console pty,target_type=serial  --graphics=vnc -v