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

2020-10-10 00:35:54 +08:00
 Apol1oBelvedere

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 文件作为硬盘文件,启动虚拟机,成功运行系统。 以上实测可行,在此记录供大家参考。

1365 次点击
所在节点    Windows
1 条回复
Shilion
2020-10-10 10:50:28 +08:00
好文章,谢谢分享

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/713480

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX