创建虚拟机

前提条件

操作步骤

  1. 以root用户将ISO镜像上传至服务器任意目录。
  2. 执行如下命令,检查selinux状态,确保selinux为关闭状态。

    getenforce

    [root@localhost qemu-4.1.0]# getenforce
    Enforcing

    若回显信息提示“command 'getenforce' not found”,请根界面据提示信息安装相关依赖。

    若显示信息为Enforcing,需要通过如下方式关闭selinux。

    • 执行如下命令,临时关闭selinux。
      setenforce 0
      [root@localhost qemu-4.1.0]# setenforce 0
      [root@localhost qemu-4.1.0]#
    • 执行如下命令,永久关闭selinux。

      vi /etc/selinux/config

      修改如下加粗字体为disabled,保存后执行reboot命令重启物理机。
      [root@localhost qemu-4.1.0]# vi /etc/selinux/config
      # 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 three values:
      #     targeted - Targeted processes are protected,
      #     minimum - Modification of targeted policy. Only selected processes are protected.
      #     mls - Multi Level Security protection.
      SELINUXTYPE=targeted

  3. 设置display环境变量。

    export DISPLAY=<客户端ip>:0.0

    客户端ip是指个人电脑的ip,可通过个人电脑的cmd输入ipconfig或者在服务器上输入who查看。

  4. 执行如下命令,查询当前环境剩余内存大小。

    free -h

                 total        used        free      shared    buff/cache   available
    Mem:         251Gi        1.5Gi       249Gi      19Mi       774Mi      248Gi
    Swap:        108Gi          0B        108Gi

    创建虚拟机时,虚拟机分配的内存ram_size请根据当前环境查询的内存大小进行配置。

  5. 执行如下命令,创建虚拟机。

    virt-install --name=<NAME> --vcpus=<VCPUS> --ram=<ram_size> --disk <DISK> --machine <Machine> --cdrom <CDROM> --network <NETWORK> --force --graphic <GRAPHICS> --input <INPUT>

    例如:

    virt-install --name=openeuler --vcpus=4 --ram=8192 --disk path=/home/kvm/images/openeuler.img,format=qcow2,size=50,bus=virtio --machine pc-q35-4.1 --cdrom /home/kvm/openEuler-22.03-LTS-x86_64-dvd.iso --network bridge=virbr0,model=virtio --force --graphic vnc,listen=0.0.0.0,port=5906 --input type=tablet,bus=usb --input type=keyboard,bus=virtio

    [root@localhost qemu-4.1.0]# virt-install --name=openeuler --vcpus=4 --ram=8192 --disk path=/home/kvm/images/openeuler.img,format=qcow2,size=50,bus=virtio --machine pc-q35-4.1 --cdrom /home/kvm/openEuler-22.03-LTS-x86_64-dvd.iso --network bridge=virbr0,model=virtio --force --graphic vnc,listen=0.0.0.0,port=5906 --input type=tablet,bus=usb --input type=keyboard,bus=virtio
    WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.
    
    Starting install...

    对于Atlas 200T A2 Box16 异构子框 veLinux 1.1系统时,请执行如下命令创建虚拟机。

    virt-install --name=<NAME> --vcpus=<VCPUS> --ram=<ram_size> --disk <DISK> --machine <Machine> --cdrom <CDROM> --network <NETWORK> --force --graphic <GRAPHICS> --input <INPUT> --cpu <CPU>

    例如:

    virt-install --name=debian --vcpus=160 --ram=65536 --disk path=/home/kvm/images/debian.img,format=qcow2,size=50,bus=virtio --machine pc-q35-4.1 --cdrom /home/kvm/velinux-1.1-business-amd64-DVD-1.iso --network bridge=br0,model=virtio --force --graphic vnc,listen=0.0.0.0,port=5901 --input type=tablet,bus=usb --input type=keyboard,bus=virtio --cpu host-passthrough

    virt-install --name=debian --vcpus=160 --ram=65536 --disk path=/home/kvm/images/debian.img,format=qcow2,size=50,bus=virtio --machine pc-q35-4.1  --cdrom /home/kvm/velinux-1.1-business-amd64-DVD-1.iso --network bridge=br0,model=virtio --force --graphic vnc,listen=0.0.0.0,port=5902 --input type=tablet,bus=usb --input type=keyboard,bus=virtio --cpu host-passthrough
    WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.
    Starting install...
    Domain creation completed.
    Restarting guest.
    表1 参数说明

    参数

    说明

    NAME

    指定新虚拟机的名称。

    说明:

    该名称必须保持唯一。

    VCPUS

    指定虚拟 CPU 数量。

    ram_size

    客户端虚拟机分配的内存,单位MB。

    DISK

    指定存储设备及其属性。

    Machine

    创建虚拟机时配置的机器类型。

    • aarch64架构:指定的机器类型需为virt-4.1及以上版本。
    • x86架构:指定的机器类型需为pc-q35-4.1及以上版本。

    可通过执行如下命令查询当前环境支持的机器类型。

    qemu-system-<ARCH> -machine help

    CDROM

    对应全虚拟化客户机,文件或设备作为一个虚拟化CD-ROM设备使用。

    可以是ISO映像路径或者一个CDROM设备,或是一个能够提取/访问最小引导ISO映像的URL。

    NETWORK

    网络配置:指定网络接口的网络类型及接口属性如MAC地址、驱动模式等。

    • bridge=BRIDGE:连接至名为“BRIDGE”的桥设备。
    • model:虚拟机中看到的网络设备型号。

    --force

    禁止命令进入交互式模式,如果有需要回答yes或no选项,则自动回答为yes。

    --autostart

    (可选)指定虚拟机是否在物理启动后自动启动。

    GRAPHICS

    定义虚拟机显示功能相关的配置,如VNC相关配置

    • -graphics TYPE,opt1=val1,opt2=val2:指定图形显示相关的配置,此选项不会配置任何硬件(如显卡),而是仅指定虚拟机启动后对其访问的接口。
    • TYPE:指定显示类型,如vnc, sdl, spice或none等,默认为vnc。
    • port:TPYE为VNC或spice时其监测的端口。
    • listen:TPYE为VNC或spice时所监测的IP地址,默认为127.0.0.1,可以通过修改/etc/libvirt/qemu.conf定义新的默认值。

    INPUT

    配置虚拟机输入设备。例如:

    • --input tablet
    • --input keyboard,bus=usb

    CPU

    指定CPU模式。例如:

    • --cpu host-passthrough
    • --cpu host-model-only

    建议根据使用场景选择配置模式。

  6. 在要求访问X server的弹窗中选择“是”(可能需要多次确认)。

后续操作

创建虚拟机以后,如果还需要在虚拟机内安装CANN、框架等软件,可以参考: