V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Apol1oBelvedere
V2EX  ›  Windows

记一次将某主机系统盘备份为 wim 文件后在 Hyper-V 中运行的实践

  •  
  •   Apol1oBelvedere · 2020-10-10 00:35:54 +08:00 · 1373 次点击
    这是一个创建于 1524 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1.通过 Windows PE 系统里的 CGI 备份工具,将系统盘备份为.wim 文件,我对文件选择了“高度压缩”。
    2.将文件复制到宿主机,执行以下命令:

    set-executionpolicy unrestricted -force
    . .\Convert-WindowsImage.ps1
    convert-windowsimage -sourcepath D:\SYSTEM.wim -workingdirectory D:\ -VHDType fixed -vhdformat vhdx -vhdpartitionstyle GPT  -edition Professional
    

    运行结果出错,如下所示:

    Windows(R) Image to Virtual Hard Disk Converter for Windows(R) 10
    Copyright (C) Microsoft Corporation.  All rights reserved.
    Version 10.0.9000.0.amd64fre.fbl_core1_hyp_dev(mikekol).141224-3000 Beta
    
    INFO   : Image 1 selected (Professional)...
    INFO   : Creating sparse disk...
    INFO   : Attaching VHDX...
    INFO   : Disk initialized with GPT...
    INFO   : Disk partitioned
    INFO   : System Partition created
    INFO   : Boot Partition created
    INFO   : System Volume formatted (with DiskPart)...
    INFO   : Boot Volume formatted (with Format-Volume)...
    INFO   : Access path (G:\) has been assigned to the System Volume...
    INFO   : Access path (\\?\Volume{1bd3b598-bdb9-40a4-bcba-此处消隐}\) has been assigned to the Boot Volume...
    INFO   : Applying image to VHDX. This could take a while...
    ERROR  : 使用“1”个参数调用“Apply”时发生异常:“无法创建目录或文件。”
    注:英文错误为:Exception calling "Apply" with "1" argument(s): "The directory or file cannot be created"
    INFO   : Log folder is C:\Users\ADMIN~1\AppData\Local\Temp\Convert-WindowsImage\67ca19bf-c94b-46a8-b2e9-此处消隐
    
    INFO   : Closing Windows image...
    INFO   : Closing VHDX...
    INFO   : Done.
    

    3.错误原因:
    解决办法网页: https://gallery.technet.microsoft.com/scriptcenter/Convert-WindowsImageps1-0fe23a8f/view/Discussions
    正如 cryptonym 所说,需要将微软官方下载的Convert-WindowsImage.ps1文件进行如下修改后运行才能成功。

    cryptonym said:
    The fix for the "Apply" with "1" argument error in Win10 1709 appears to be due to a misuse of the arrays. This is totally unrelated to the old "1 image" bug. I changed:
    Line 4205:
    from: $drive           = $(Get-Partition -Disk $disk).AccessPaths[0]
    to: $drive           = $(Get-Partition -Disk $disk)[0].AccessPaths[0]
    
    line 4216 
    from: $driveSystem = $(Get-Partition -Disk $disk).AccessPaths[1]
    to: $driveSystem = $(Get-Partition -Disk $disk)[1].AccessPaths[0]
    
    line 4220 
    from: $drive = $(Get-Partition -Disk $disk).AccessPaths[2]
    to: $drive = $(Get-Partition -Disk $disk)[2].AccessPaths[0]
    
    line 4229
    from: $drive = $(Get-Partition -Disk $disk).AccessPaths[1]
    to: $drive = $(Get-Partition -Disk $disk)[1].AccessPaths[0]
    
    This indexes into the partition list, then gets the 1st (zero based) entry in the list. Before I think it assumed the list only could have 1 entry per partition. It now seems to have 2 for each partition. I have not yet confirmed this works on older OS, but it appears to fix the 1709 issue. Line 4229 is also untested, as I only am doing VMs, but it should be correct.
    
    Hopefully some of the fixes in the comments section make it into the download codebase. This script is a real time saver.
    
    I really won't be testing this, since it is in the MBR partitioning section. The same logic should still apply I believe though.
    
    
    I installed a 1703 Win10 to test my suggested fix on an older release, and it worked the same. So my proposed changes are not specific to running on 1709, and can safely be used. No need to test what OS you are running if implemented in this manner.
    

    4.对Convert-WindowsImage.ps1文件进行修改后,运行正确,得到结果如下:

    Windows(R) Image to Virtual Hard Disk Converter for Windows(R) 10
    Copyright (C) Microsoft Corporation.  All rights reserved.
    Version 10.0.9000.0.amd64fre.fbl_core1_hyp_dev(mikekol).141224-3000 Beta
    
    INFO   : Image 1 selected (Professional)...
    INFO   : Creating fixed disk...
    INFO   : Attaching VHDX...
    INFO   : Disk initialized with GPT...
    INFO   : Disk partitioned
    INFO   : System Partition created
    INFO   : Boot Partition created
    INFO   : System Volume formatted (with DiskPart)...
    INFO   : Boot Volume formatted (with Format-Volume)...
    INFO   : Access path (G:\) has been assigned to the System Volume...
    INFO   : Access path (H:\) has been assigned to the Boot Volume...
    INFO   : Applying image to VHDX. This could take a while...
    INFO   : Signing disk...
    INFO   : Image applied. Making image bootable...
    INFO   : Drive is bootable. Cleaning up...
    INFO   : Generating name for VHDX...
    INFO   : Closing VHDX...
    
    INFO   : Closing Windows image...
    INFO   : Done.
    

    5.使用 Hyper-V 管理器新建二代虚拟机,并选择生成的 VHDX 文件作为硬盘文件,启动虚拟机,成功运行系统。 以上实测可行,在此记录供大家参考。

    1 条回复    2020-10-10 10:50:28 +08:00
    Shilion
        1
    Shilion  
       2020-10-10 10:50:28 +08:00 via iPhone
    好文章,谢谢分享
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2681 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 00:33 · PVG 08:33 · LAX 16:33 · JFK 19:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.