7.15.4.4. 使用 CLI 导入虚拟机

您可以通过 CLI 创建 SecretVirtualMachineImport 自定义资源(CR)来导入虚拟机。Secret CR 存储 RHV Manager 凭证和 CA 证书。VirtualMachineImport CR 定义 VM 导入过程的参数。

可选:您可以创建一个与 VirtualMachineImport CR 分开的 ResourceMapping CR。ResourceMapping CR 提供了更大的灵活性,例如导入额外的 RHV VM。

重要

默认目标存储类必须是 NFS。Cinder 不支持 RHV VM 导入。

流程

  1. 运行以下命令来创建 Secret CR:

    $ cat <<EOF | oc create -f -
    apiVersion: v1
    kind: Secret
    metadata:
      name: rhv-credentials
      namespace: default 1
    type: Opaque
    stringData:
      ovirt: |
        apiUrl: <api_endpoint> 2
        username: ocpadmin@internal
        password: 3
        caCert: |
          -----BEGIN CERTIFICATE-----
          4
          -----END CERTIFICATE-----
    EOF
    1
    可选。您可以在所有 CR 中指定不同的命名空间。
    2
    指定 RHV Manager 的 API 端点,如 \"https://www.example.com:8443/ovirt-engine/api"
    3
    指定 ocpadmin@internal 的密码。
    4
    指定 RHV Manager CA 证书。您可以运行以下命令来获取 CA 证书:
    $ openssl s_client -connect :443 -showcerts < /dev/null
  2. 可选:运行以下命令,创建 ResourceMapping CR,以将资源映射与 VirtualMachineImport CR 分开:

    $ cat <<EOF | kubectl create -f -
    apiVersion: v2v.kubevirt.io/v1alpha1
    kind: ResourceMapping
    metadata:
      name: resourcemapping_example
      namespace: default
    spec:
      ovirt:
        networkMappings:
          - source:
              name: <rhv_logical_network>/<vnic_profile> 1
            target:
              name: <target_network> 2
            type: pod
        storageMappings: 3
          - source:
              name: <rhv_storage_domain> 4
            target:
              name: <target_storage_class> 5
            volumeMode: <volume_mode> 6
    EOF
    1
    指定 RHV 逻辑网络和 vNIC 配置集。
    2
    指定 OpenShift Virtualization 网络。
    3
    如果在 ResourceMappingVirtualMachineImport CR 中都指定了存储映射,则 VirtualMachineImport CR 将具有优先权。
    4
    指定 RHV 存储域。
    5
    指定 nfsocs-storagecluster-ceph-rbd
    6
    如果指定了 ocs-storagecluster-ceph-rbd 存储类,则必须将 Block 指定为卷模式。
  3. 运行以下命令来创建 VirtualMachineImport CR:

    $ cat <<EOF | oc create -f -
    apiVersion: v2v.kubevirt.io/v1beta1
    kind: VirtualMachineImport
    metadata:
      name: vm-import
      namespace: default
    spec:
      providerCredentialsSecret:
        name: rhv-credentials
        namespace: default
    # resourceMapping: 1
    #   name: resourcemapping-example
    #   namespace: default
      targetVmName: vm_example 2
      startVm: true
      source:
        ovirt:
          vm:
            id: <source_vm_id> 3
            name: <source_vm_name> 4
          cluster:
            name: <source_cluster_name> 5
          mappings: 6
            networkMappings:
              - source:
                  name: <source_logical_network>/<vnic_profile> 7
                target:
                  name: <target_network> 8
                type: pod
            storageMappings: 9
              - source:
                  name: <source_storage_domain> 10
                target:
                  name: <target_storage_class> 11
                accessMode: <volume_access_mode> 12
            diskMappings:
              - source:
                  id: <source_vm_disk_id> 13
                target:
                  name: <target_storage_class> 14
    EOF
    1
    如果创建一个 ResourceMapping CR,取消 resourceMapping 部分的注释。
    2
    指定目标虚拟机名称。
    3
    指定源虚拟机 ID,例如 80554327-0569-496b-bdeb-fcbbf52b827b。您可以通过在 Manager 机器的网页浏览器中输入 https://www.example.com/ovirt-engine/api/vms/ 来列出所有虚拟机来获取虚拟机 ID。找到您要导入的虚拟机及其对应的虚拟机 ID。您不需要指定虚拟机名称或集群名称。
    4
    如果指定源虚拟机名称,还必须同时指定源集群。不要指定源虚拟机 ID。
    5
    如果指定源集群,还必须指定源虚拟机名称。不要指定源虚拟机 ID。
    6
    如果创建一个 ResourceMapping CR,注释掉 mappings 部分。
    7
    指定源虚拟机的逻辑网络和 vNIC 配置集。
    8
    指定 OpenShift Virtualization 网络。
    9
    如果在 ResourceMappingVirtualMachineImport CR 中都指定了存储映射,则 VirtualMachineImport CR 将具有优先权。
    10
    指定源存储域。
    11
    指定目标存储类。
    12
    指定 ReadWriteOnceReadWriteManyReadOnlyMany。如果没有指定访问模式,{virt} 会根据 RHV VM 的 HostMigration 模式 或虚拟磁盘访问模式决定正确的卷访问模式:
    • 如果 RHV VM 迁移模式是 Allow manual and automatic migration,则默认访问模式为 ReadWriteMany
    • 如果 RHV 虚拟磁盘访问模式是 ReadOnly,则默认访问模式为 ReadOnlyMany
    • 对于所有其他设置,默认的访问模式为 ReadWriteOnce
    13
    指定源虚拟机磁盘 ID,例如 8181ecc1-5db8-4193-9c92-3ddab3be7b05。您可以通过在 Manager 机器的网页浏览器中输入 https://www.example.com/ovirt-engine/api/vms/vm23 并查看虚拟机详情来获取磁盘 ID。
    14
    指定目标存储类。
  4. 按照虚拟机导入的过程,以验证导入是否成功:

    $ oc get vmimports vm-import -n default

    显示成功导入的输出结果类似如下:

    输出示例

    ...
    status:
      conditions:
      - lastHeartbeatTime: "2020-07-22T08:58:52Z"
        lastTransitionTime: "2020-07-22T08:58:52Z"
        message: Validation completed successfully
        reason: ValidationCompleted
        status: "True"
        type: Valid
      - lastHeartbeatTime: "2020-07-22T08:58:52Z"
        lastTransitionTime: "2020-07-22T08:58:52Z"
        message: 'VM specifies IO Threads: 1, VM has NUMA tune mode specified: interleave'
        reason: MappingRulesVerificationReportedWarnings
        status: "True"
        type: MappingRulesVerified
      - lastHeartbeatTime: "2020-07-22T08:58:56Z"
        lastTransitionTime: "2020-07-22T08:58:52Z"
        message: Copying virtual machine disks
        reason: CopyingDisks
        status: "True"
        type: Processing
      dataVolumes:
      - name: fedora32-b870c429-11e0-4630-b3df-21da551a48c0
      targetVmName: fedora32

7.15.4.4.1. 创建用于导入虚拟机的配置映射

如果要覆盖默认的 vm-import-controller 映射或添加额外的映射,您可以创建一个配置映射来将 Red Hat Virtualization(RHV)虚拟机操作系统映射到 OpenShift Virtualization 模板。

默认 vm-import-controller 配置映射包含以下 RHV 操作系统,及其对应的通用 OpenShift Virtualization 模板。

表 7.4. 操作系统和模板映射

RHV 虚拟机操作系统OpenShift Virtualization 模板

rhel_6_9_plus_ppc64

rhel6.9

rhel_6_ppc64

rhel6.9

rhel_6

rhel6.9

rhel_6x64

rhel6.9

rhel_7_ppc64

rhel7.7

rhel_7_s390x

rhel7.7

rhel_7x64

rhel7.7

rhel_8x64

rhel8.1

sles_11_ppc64

opensuse15.0

sles_11

opensuse15.0

sles_12_s390x

opensuse15.0

ubuntu_12_04

ubuntu18.04

ubuntu_12_10

ubuntu18.04

ubuntu_13_04

ubuntu18.04

ubuntu_13_10

ubuntu18.04

ubuntu_14_04_ppc64

ubuntu18.04

ubuntu_14_04

ubuntu18.04

ubuntu_16_04_s390x

ubuntu18.04

windows_10

win10

windows_10x64

win10

windows_2003

win10

windows_2003x64

win10

windows_2008R2x64

win2k8

windows_2008

win2k8

windows_2008x64

win2k8

windows_2012R2x64

win2k12r2

windows_2012x64

win2k12r2

windows_2016x64

win2k16

windows_2019x64

win2k19

windows_7

win10

windows_7x64

win10

windows_8

win10

windows_8x64

win10

windows_xp

win10

流程

  1. 在网页浏览器中,进入 http://<RHV_Manager_FQDN>/ovirt-engine/api/vms/<VM_ID> 找到RHV 虚拟机操作系统的 REST API 名称。操作系统名称会出现在 XML 输出的 <os> 部分,如下例所示:

    ...
    <os>
    ...
    <type>rhel_8x64</type>
    </os>
  2. 查看可用 OpenShift Virtualization 模板列表:

    $ oc get templates -n openshift --show-labels | tr ',' '\n' | grep os.template.kubevirt.io | sed -r 's#os.template.kubevirt.io/(.*)=.*#\1#g' | sort -u

    输出示例

    fedora31
    fedora32
    ...
    rhel8.1
    rhel8.2
    ...

  3. 如果与 RHV 虚拟机操作系统匹配的 OpenShift Virtualization 模板没有出现在可用的模板列表中,使用 OpenShift Virtualization Web 控制台创建一个模板。
  4. 创建配置映射将 RHV VM 操作系统映射到 OpenShift Virtualization 模板:

    $ cat <<EOF | oc create -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: os-configmap
      namespace: default 1
    data:
      guestos2common: |
        "Red Hat Enterprise Linux Server": "rhel"
        "CentOS Linux": "centos"
        "Fedora": "fedora"
        "Ubuntu": "ubuntu"
        "openSUSE": "opensuse"
      osinfo2common: |
        "<rhv-operating-system>": "<vm-template>" 2
    EOF
    1
    可选: 您可以更改 namespace 参数的值。
    2
    指定 RHV 操作系统的 REST API 名称及其对应的虚拟机模板,如下例所示。

    配置映射示例

    $ cat <<EOF | oc apply -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: os-configmap
      namespace: default
    data:
      osinfo2common: |
        "other_linux": "fedora31"
    EOF

  5. 验证是否已创建自定义配置映射:

    $ oc get cm -n default os-configmap -o yaml
  6. vm-import-controller-config 配置映射进行补丁以应用新的配置映射:

    $ oc patch configmap vm-import-controller-config -n openshift-cnv --patch '{
        "data": {
            "osConfigMap.name": "os-configmap",
            "osConfigMap.namespace": "default" 1
        }
    }'
    1
    如果在配置映射中更改了命名空间,请更新它。
  7. 验证模板是否出现在 OpenShift Virtualization web 控制台中:

    1. 从侧边菜单中点 WorkloadsVirtualization
    2. Virtual Machine Templates 标签页,在列表中找到模板。