Skip to content

虚拟化迁移 #1

@syshack

Description

@syshack

引言

在虚拟化环境迁移(如 VMware/Hyper-V 到 KVM)中,程序注入是实现无缝迁移的关键步骤。无论是安装驱动、更新配置,还是部署自定义服务,都需要在目标虚拟机(客户机)中动态注入文件并执行逻辑。本文将深入探讨如何利用 virt-v2v 及其生态工具实现跨平台程序注入,并分析实际应用中的技术细节与避坑指南。

一、virt-v2v 程序注入的核心方法

1. 使用 --inject 参数实现静态文件注入

virt-v2v--inject 参数支持将宿主机文件或目录拷贝到客户机的指定路径。 

语法示例

# Linux 示例

virt-v2v -i ova source.ova -o local -os /var/lib/libvirt/images \

  --inject /host/config.yaml:/etc/app/config.yaml


# Windows 示例(注意路径转义)

virt-v2v -i libvirtxml win-vm.xml -o local -os /var/lib/libvirt/images \

  --inject C:\\host\\drivers:/drivers

关键注意点

  • Linux 路径区分大小写,Windows 需使用反斜杠和转义。

  • 注入前需确保目标路径存在。

2. 结合 virt-customize 实现动态后处理

virt-customize 支持对虚拟机磁盘进行深度定制,跨平台操作逻辑不同:

Linux 示例

virt-customize -a linux-disk.qcow2 \

  --upload setup.sh:/root/setup.sh \

  --run-command "chmod +x /root/setup.sh && /root/setup.sh"

Windows 示例

virt-customize -a windows-disk.qcow2 \

  --upload install.ps1:'C:\\scripts\\install.ps1' \

  --run-command "powershell -ExecutionPolicy Bypass -File C:\\scripts\\install.ps1" \

  --install virtio-win-guest-tools

3. Windows 驱动的特殊注入流程

Windows 虚拟机迁移需依赖 VirtIO 驱动,完整流程如下:

  1. 准备 VirtIO 驱动 ISO
   wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso
  1. 注入驱动并安装
   # 注入 ISO 到客户机

   virt-v2v [...] --inject virtio-win.iso:'C:\\virtio-drivers.iso'


   # 使用 virt-customize 安装驱动

   virt-customize -a windows-disk.qcow2 \

     --run-command "pnputil /add-driver C:\\virtio-drivers.iso\\*.inf /install /subdirs"

二、实战案例

案例 1:Linux 虚拟机自动化迁移(CentOS 7)

场景:迁移 VMware CentOS 7 到 KVM,并自动部署 Nginx 服务。 

操作

# 1. 转换虚拟机

virt-v2v -i ova centos7.ova -o local -os /var/lib/libvirt/images -on centos7-kvm


# 2. 注入配置与脚本

virt-customize -a centos7-kvm.qcow2 \

  --upload nginx.conf:/etc/nginx/nginx.conf \

  --run-command "yum install -y nginx && systemctl enable nginx"

案例 2:Windows 虚拟机迁移与自动化配置(Windows Server 2019)

场景:迁移 Hyper-V 中的 Windows Server 2019 到 KVM,实现以下目标:

  1. 自动安装 VirtIO 驱动

  2. 配置静态 IP 地址

  3. 启用远程桌面(RDP)

操作脚本

# 1. 转换并注入 VirtIO 驱动

virt-v2v -i libvirtxml win2019.xml -o local -os /var/lib/libvirt/images \

  --inject virtio-win.iso:'C:\\virtio.iso'


# 2. 安装驱动并配置系统

virt-customize -a win2019.qcow2 \

  --run-command "pnputil /add-driver C:\\virtio.iso\\*.inf /install /subdirs" \

  --upload set_ip.ps1:'C:\\scripts\\set_ip.ps1' \

  --run-command "powershell -ExecutionPolicy Bypass -File C:\\scripts\\set_ip.ps1" \

  --run-command "reg add 'HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server' /v fDenyTSConnections /t REG_DWORD /d 0 /f" \

  --run-command "netsh advfirewall firewall add rule name='Allow RDP' dir=in action=allow protocol=TCP localport=3389"

附:set_ip.ps1 脚本内容

# 配置静态 IP

New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress "192.168.1.100" -PrefixLength 24 -DefaultGateway "192.168.1.1"

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "8.8.8.8"

避坑指南

  • 驱动签名问题:若使用自定义驱动,需在首次启动时按 F8 禁用驱动签名验证。

  • PowerShell 执行策略:必须添加 -ExecutionPolicy Bypass 绕过安全限制。

  • 路径转义:Windows 路径中的反斜杠需转义为双反斜杠(C:\\)。

三、程序注入的局限性及解决方案

1. Windows 注册表操作的挑战

  • 问题virt-customize 无法直接修改注册表键值。 

  • 解决:通过 --run-command 调用 reg add 命令:

  virt-customize -a disk.qcow2 \

    --run-command "reg add 'HKLM\\SOFTWARE\\MyApp' /v Version /t REG_SZ /d 1.0 /f"

2. 安全机制冲突

  • Windows Defender:可能误删注入的脚本文件。 

  解决:临时禁用实时保护:

  virt-customize -a disk.qcow2 \

    --run-command "powershell -Command Set-MpPreference -DisableRealtimeMonitoring $true"

四、最佳实践总结

  1. 分层操作

   - 基础层:使用 virt-v2v 完成驱动和必要文件注入。

   - 配置层:通过 virt-customize 执行原子化操作(安装、脚本、注册表)。

   - 清理层:删除临时文件和敏感数据。

  1. 日志与调试
   virt-v2v [...] --verbose         # 输出详细日志

   virt-customize [...] --no-log    # 禁用日志(敏感操作)
  1. 跨平台测试矩阵

| 操作系统      | 驱动兼容性 | 脚本支持        | 已知问题                  |

   |---------------|------------|-----------------|---------------------------|

   | CentOS 7+     | VirtIO     | Bash/Python     | SELinux 上下文错误        |

   | Windows 2019+ | VirtIO-Win | PowerShell/Bat  | 驱动签名强制验证          |


结语

无论是 Linux 还是 Windows 虚拟机,virt-v2v 配合 virt-customize 都能实现高度自动化的迁移与配置。关键点在于理解目标系统的安全策略、路径规范和执行环境差异。通过本文的实战案例和避坑指南,希望能为您的虚拟化迁移之旅铺平道路!


延伸阅读: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions