你好,游客 登录 注册 搜索
背景:
阅读新闻

在 FreeBSD bhyve 下运行 Windows

[日期:2015-12-23] 来源:oschina.net  作者:ostatsu, 无若, lombreeze [字体: ]

正如我之前所写的那样,FreeBSD 下的虚拟机管理程序 bhyve 即将支持 Windows 了。在其通过 FreeBSD Virtualization 邮件列表公布时,FreeBSD 11.0-CURRENT r288524 版已对其正式支持。不久之后,Windows 的外交人员 Michael Dexter 就在 FreeBSD Wiki 上编写了一个关于在 bhyve 下运行 Windows 的很棒的指引手册。

在 bhyve 下运行 Windows 的秘密武器就是全新的 UEFI 支持。这真是个相当棒的消息,因为当你在 bhyve 中采用 UEFI 时, 你已不必先在 bhyveload 或 grub-bhyve 中加载操作系统。下面是在命令行运行一个 Windows bhyve 实例的示例:

bhyve \
      -c 2 \
      -s 0,hostbridge \
      -s 3,ahci-hd,windows2016.img \
      -s 4,ahci-cd,install.iso \
      -s 10,virtio-net,tap0 \
      -s 31,lpc \
      -l com1,/dev/nmdm0A \
      -l com2,/dev/nmdm1A \
      -l bootrom,BHYVE_UEFI_20151002.fd \
      -m 2G -H -w \
      windows2016

在我得业余时间里,我正在开发一个叫做 iohyve 小的 shell 脚本,所以当此支持到来时,我欣喜若狂。我开始开发 iohyve 是因为当进行沙箱测试时,我不想使用 Oracle 的 VirtualBox。有时我喜欢点击一些不可靠的链接并写邮件给滥用的部门并标记为垃圾邮件,我希望让尽可能多的任何可能的潜在恶意软件远离我。由于大多数恶意软件都是为 Windows 用户所写,在 bhyve 中支持它意味着我已经慢慢的接近实现这一目标了。现在 iohyve 还不能将客户机放在优秀的虚拟 NAT 网络,但我最近已经把 UEFI 支持 加入到了 FreeBSD ports 中的 iohyve v0.7 版本了。对于今天这个教程,我将使用 GitHub 上的版本来完成在 FreeBSD 中创建一个 bhyve Windows 客户机的任务,因为这个 iohyve UEFI 命令的版本的 bug 比较少。请注意,由于 UEFI 固件还是很新的,是实验性质的,所以 iohyve 对它的支持也是实验性质的。现在的局限在于你只能有一个 HDD,一个虚拟 NIC(使用 tap 设备),以及没有支持 pass-through。其他操作系统若通过 bhyveload 或 grup-bhyve 命令启动则可以支持多个虚拟 HDD,甚至是 passthrough 支持。

主机和安装 ISO 准备。

为了利用在 bhyve 中启动芯片(UEFI)的功能,你需要运行FreeBSD 11.0-CURRENT(至少是r288524版本)。自从 iohyve 使用了ZFS, 如果你还在运行 11-CURRENT,你真应该跳过它,使用 zpool 去设置。在我的笔记本上,我选择使用 Root on ZFS 来安装FreeBSD,使用它非常简单。稍后,我将会进入 iohyve 的设置。在这之前,我会先运行 bhyve,我需要先创建一个自定义的 Windows 安装 ISO。因为 bhyve 不能输出视频,我们必须创建一个“无人值守的”安装 ISO。因为我们会重新制作新的 ISO,我们将会在重新制作的 ISO 中安装 VirtIO NIC 驱动来确保 Windows guest 能连接到网络。

正如我前面提到的,FreeBSD 的 wiki 条目 细节已经达到了这种程度。自从 bhyve 开始支持 Windows 之后,为了尝试在 bhyve 下运行不同版本的 Windows 操作系统,我编写了一系列小的脚本和文件。我发现自己一遍又一遍的尝试不同的 AutoUnattend.xml 文件。这个 XML 文件基本上是当在裸机上安装 Windows 时一系列点击按钮的“响应”。我在一个被我称为 Yabs 的脚本里简化了这些流程。我会在本教程中使用那些脚本,但我会随时检查并与 wiki 上的文章同步。你也需要找一个 Windows ISO 安装文件的副本。在本教程中,我将使用 Windows 2008,尽管我也已经用 Windows 2012 试过了。

-用 git 克隆 Yabs 的 repo,并开始你的初始工作目录。

$ git clone https://github.com/pr1ntf/YetAnotherBhyveScript.git
Cloning into 'YetAnotherBhyveScript'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 22 (delta 9), reused 17 (delta 4), pack-reused 0
Unpacking objects: 100% (22/22), done.
Checking connectivity... done.$ mv YetAnotherBhyveScript/ win2k8auto$ cd win2k8auto/

-使用 fetch 命令从 Peter 那里取得固件,并从 Fedora Project 取得驱动。我将使用 0.1-94 版驱动,因为较新版的驱动在 Windows 2008 下不能正常工作。

$ fetch https://people.freebsd.org/~grehan/bhyve_uefi/BHYVE_UEFI_20151002.fd
$ fetch https://fedorapeople.org/groups/virt/virtio-win/deprecated-isos/archives/virtio-win-0.1-94/virtio-win-0.1-94.iso

-我找到的 ISO 安装文件叫做 Win2k8R2.iso,其目录内容如下:

$ ls
BHYVE_UEFI_20151002.fd  Win2k8-AutoUnattend.xml extract.sh              remaster.sh             yabs.sh
README.txt              Win2k8R2.iso            null.iso                virtio-win-0.1-94.iso

-我们现在必须修改 extract.sh 脚本来指向正确的位置。要注意运行 extract.sh 需要 FreeBSD 的 Port archivers/pz7ip。我的文件如下:

#!/bin/sh
 
# Extract important stuff to remaster folder
 
folder=win2k8
iso=Win2k8R2.iso
drivers=virtio-win-0.1-94.iso
 
mkdir -p ${folder}/virtio
 
7z x ${iso} -o${folder}
 
tar xf $drivers -C ${folder}/virtio

-在我们执行 ./extract.sh 之后我们可以将 AutoUnattend.xml 文件复制到我们的 win2k8 目录下。Yabs 中包含的 AutoUnattend.xml 文件会将管理员密码设置为 R3dm0nd!,并会将默认网卡 IP 设置为 192.168.0.111 并具有指定网关和子网掩码。因此一定要先对其进行修改。你可以在此文件中删除第二个 <SynchronousCommand wcm:action="add">。(在第一次登陆时执行这个命令 netsh interface ipv4 set address name="local area connection" source=static address=192.168.0.111 mask=255.255.255.0 gateway=192.168.0.1)。
 
$ cat Win2k8-AutoUnattend.xml
<?xml version="1.0" encoding="utf-8"?><unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="windowsPE">
        <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <DiskConfiguration>
                <Disk wcm:action="add">
                    <DiskID>0</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                    <CreatePartitions>
                        <CreatePartition wcm:action="add">
                            <Order>1</Order>
                            <Size>400</Size>
                            <Type>EFI</Type>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>2</Order>
                            <Size>128</Size>
                            <Type>MSR</Type>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>3</Order>
                            <Extend>true</Extend>
                            <Type>Primary</Type>
                        </CreatePartition>
                    </CreatePartitions>
 
                    <ModifyPartitions>
                      <!-- EFI system partition (ESP) -->
                      <ModifyPartition wcm:action="add">
                        <Order>1</Order>
                        <PartitionID>1</PartitionID>
                        <Label>System</Label>
                        <Format>FAT32</Format>
                      </ModifyPartition>
 
                      <!-- Windows partition -->
                      <ModifyPartition wcm:action="add">
                        <Order>2</Order>
                        <PartitionID>3</PartitionID>
            </WindowsFeatures>
            <Themes>
              <ThemeName>Classic Theme</ThemeName>
              <DefaultThemesOff>true</DefaultThemesOff>
            </Themes>
            <ShowWindowsLive>false</ShowWindowsLive>
 
            <FirstLogonCommands>
              <SynchronousCommand wcm:action="add">
                  <CommandLine>cmd /C bcdedit /emssettings emsport:1 emsbaudrate:115200</CommandLine>
                  <Description>Enable EMS</Description>
                  <Order>1</Order>
              </SynchronousCommand>
              <SynchronousCommand wcm:action="add">
                    <CommandLine>netsh interface ipv4 set address name="local area connection" source=static
address=192.168.0.111 mask=255.255.255.0 gateway=192.168.0.1</CommandLine>
                    <Order>2</Order>
                </SynchronousCommand>
            </FirstLogonCommands>
        </component>
    </settings>
 
    <settings pass="offlineServicing">
      <component name="Microsoft-Windows-PnpCustomizationsNonWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="NonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <DriverPaths>
            <PathAndCredentials wcm:action="add" wcm:keyValue="1">
                <Path>d:\virtio</Path>
            </PathAndCredentials>
        </DriverPaths>
      </component>
    </settings></unattend>$ cp Win2k8-AutoUnattend.xml win2k8/AutoUnattend.xml

-现在我们按照所有正确的位置修改 remaster.sh 脚本来制作我们的无人值守 ISO 安装文件。执行 ./remaster.sh 后所有的东西都会打包在一个不错的 ISO 文件中。它需要 sysutils/cdrtools-devel port。我的如下:

#!/bin/sh
 
# Remaster new ISO
 
folder=win2k8
iso=win2k8.iso
 
mkisofs \
    -b boot/etfsboot.com -no-emul-boot -c BOOT.CAT \
    -iso-level 4 -J -l -D \
    -N -joliet-long \
    -relaxed-filenames -v \
    -V "Custom" -udf \
    -boot-info-table -eltorito-alt-boot -eltorito-platform 0xEF \
    -eltorito-boot efi/microsoft/boot/efisys_noprompt.bin \
    -no-emul-boot \
    -o ${iso} ${folder}

iohyve 的安装和准备

现在我们的工作目录看起来应该是这样的:

$ ls
BHYVE_UEFI_20151002.fd  Win2k8R2.iso            remaster.sh             win2k8.iso
README.txt              extract.sh              virtio-win-0.1-94.iso   yabs.sh
Win2k8-AutoUnattend.xml null.iso                win2k8

我们可以继续安装 iohyve 并为我们的 Windows 安装过程做准备。如前所述,我们要使用 GitHub 上的版本,而不是 FreeBSD ports 中的版本。 执行 iohyve version 命令会输出 0.7.1。

$ git clone https://github.com/pr1ntf/iohyve.git
Cloning into 'iohyve'...
remote: Counting objects: 904, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 904 (delta 0), reused 0 (delta 0), pack-reused 901
Receiving objects: 100% (904/904), 231.14 KiB | 0 bytes/s, done.
Resolving deltas: 100% (505/505), done.
Checking connectivity... done.$ cd iohyve/$ sudo make install clean
Password:
gzip -cn iohyve.8 > iohyve.8.gz
mkdir -p /usr/local/sbin
install -c -m 555 /usr/home/pr1ntf/iohyve/iohyve /usr/local/sbin/
install -c /usr/home/pr1ntf/iohyve/rc.d/* /usr/local/etc/rc.d/
install -c iohyve.8.gz /usr/local/man/man8/
rm -f iohyve.8.gz iohyve.8.cat.gz$ cd ~$ iohyve version
iohyve v0.7.1 2015/12/08 Im Here for the Party Edition

现在我们需要告诉 iohyve 三件事来设置它。

  • Zpool 存储池存储的 iohyve 数据集。 
  • 以太网接口连接的所有 tap 设备可以连接的硬编码(当前)bridge0 设备。
  • 告诉 iohyve 去加载 nmdm 及操作 iohyve 所需的 vmm 内核模块。

因为我们想要让 iohyve 在我每次启动电脑的时候都会设置网络和内核模块,我会将 iohyve 的设置分为两部分:为 zpool 执行 iohyve setup pool=zroot 命令,然后修改主机上的 /etc/rc.conf 文件并为接口和模块执行 service iohyve start 命令。

因为我的默认以太网接口是 em0,于是我在主机的 /etc/rc.conf 文件中添加了下面的几行代码。

iohyve_enable="YES"
iohyve_flags="kmod=1 net=em0"

现在我可以执行 iohyve setup pool=zroot 命令和 service iohyve start 命令了:

$ sudo iohyve setup pool=zroot
Setting up iohyve pool...$ sudo service iohyve start
Starting iohyve guests...
Loading kernel modules...
Seting up bridge0 on em0...
net.link.tap.up_on_open: 1 -> 1

在我们可以创建我们的 Windows iohyve 客户机之前,我们需要尽早在我们的 win2k8auto 文件夹中做以下三件事。

  •  最重要的是,我们需要一个秘密武器。我们需要 Peter 和 nahanni 已经提供给 FreeBSD 社区的 BHYVE_UEFI_20151002.fd UEFI 固件文件。
  • 我们需要我们早先已经创建的 win2k8.iso 无人值守 Windows ISO 安装文件。
  • 由于在这种方式中固件与 Windows 交互时会出现一个异常行为,我们会需要一个基本上是空白光盘的 null.iso 文件。

为了实现这一点,我们可以使用 iohyve cpiso 和 iohyve cpfw 命令:

$ ls
BHYVE_UEFI_20151002.fd  Win2k8R2.iso            remaster.sh             win2k8.iso
README.txt              extract.sh              virtio-win-0.1-94.iso   yabs.sh
Win2k8-AutoUnattend.xml null.iso                win2k8$ sudo iohyve cpiso win2k8.iso
Password:
Copying win2k8.iso from win2k8.iso...$ sudo iohyve cpiso null.iso
Copying null.iso from null.iso...$ sudo iohyve cpfw BHYVE_UEFI_20151002.fd
Copying BHYVE_UEFI_20151002.fd from BHYVE_UEFI_20151002.fd...$ iohyve isolist
Listing ISO's...
null.iso
win2k8.iso$ iohyve fwlist
Listing Firmware...
BHYVE_UEFI_20151002.fd

Windows 客户机的创建和准备

首先我们可以使用 32G 大小的虚拟 HDD 创建一个叫做 win2k8 的新的客户机。

$ sudo iohyve create win2k8 32G
Creating win2k8...

现在我们需要在客户机上设置一些属性,以便 bhyve 可以正常的启动客户机。

$ iohyve fwlist
Listing Firmware...
BHYVE_UEFI_20151002.fd$ sudo iohyve set win2k8 fw=BHYVE_UEFI_20151002.fd bargs="-H -w" ram=1024M
Setting win2k8 fw=BHYVE_UEFI_20151002.fd...
Setting win2k8 bargs=-H -w...
Setting win2k8 ram=1024M...$ iohyve getall win2k8
Getting win2k8 props...
description  Tue_Dec__8_10:30:51_MST_2015
fw          BHYVE_UEFI_20151002.fd
ram          1024M
os          default
cpu          1
size        32G
bargs        -H_-w
loader      bhyveload
name        win2k8
boot        0
tap          tap0
persist      1
con          nmdm0
autogrub    \ninstall      yes
 

现在我们可以开始第一次引导安装了。

Windows 安装过程

Windows 的安装过程分为三个主要的阶段:         

  • 第一个安装阶段(从 ISO 安装文件中复制文件)
  • 第二个安装阶段(Windows 实际解压并安装一些东西)
  • 第三个安装阶段(Windows 执行在 AutoUnattend.xml 中设置的“第一次登陆”命令)

在第一阶段开始之前,我喜欢打开另一个控制台并执行 iohyve console win2k8 命令。

$ sudo iohyve console win2k8
Password:
Starting console on win2k8...
~. to escape console [uses cu(1) for console]Connected

现在我们可以通过 iohyve uefi 命令开始安装 win2k8 客户机了。它应该会马上开始。

$ iohyve isolist
Listing ISO's...
null.iso
win2k8.iso$ sudo iohyve uefi win2k8 win2k8.iso

切换到你的其他控制台,过一会你应该会看到 Windows SAC:
 
Computer is booting, SAC started and initialized.                             
 
Use the "ch -?" command for information about using channels.                 
Use the "?" command for general help.                                         
 
 
SAC>                       
EVENT: The CMD command is now available.                                       
SAC>                                                                           
EVENT:  A new channel has been created.  Use "ch -?" for channel help.       
Channel: SACSetupAct                                                           
SAC>                                                                           
EVENT:  A new channel has been created.  Use "ch -?" for channel help.       
Channel: SACSetupErr                                                           
SAC>

你可以按 [Esc]+[Tab] 键然后回车来切换到显示了安装细节的 SACSetupAct 控制台。Calling WIMApplyImage 的部分会需要执行一段时间才会退出。

:\ProgramData\] doesn't exist; no need to move it before applying image.2015-12-08 10:51:09, Info                  IBS    MoveOldOSFiles:File/folder [E:\Recovery\] doesn't exist; no need to move it before applying image.2015-12-08 10:51:09, Info                  IBS    MoveOldOSFiles:File/folder [E:\Users\] doesn't exist; no need to move it before applying image.2015-12-08 10:51:09, Info                  IBS    MoveOldOSFiles:File/folder [E:\Windows\] doesn't exist; no need to move it before applying image.2015-12-08 10:51:09, Info      [0x06412c] IBSLIB SetCheckpoint: Checkpoint("WinPEArchiveOldWindowsFoldersStartCheckpoint") in progress...2015-12-08 10:51:09, Info      [0x06412e] IBSLIB SetCheckpoint: Checkpoint "WinPEArchiveOldWindowsFoldersStartCheckpoint" successfully set.2015-12-08 10:51:09, Info      [0x06412c] IBSLIB SetCheckpoint: Checkpoint("WinPEArchiveOldWindowsFoldersDoneCheckpoint") in progress...2015-12-08 10:51:09, Info      [0x06412e] IBSLIB SetCheckpoint: Checkpoint "WinPEArchiveOldWindowsFoldersDoneCheckpoint" successfully set.2015-12-08 10:51:09, Info      [0x06412c] IBSLIB SetCheckpoint: Checkpoint("WinPEImageApplyReadyCheckpoint") in progress...2015-12-08 10:51:09, Info      [0x06412e] IBSLIB SetCheckpoint: Checkpoint "WinPEImageApplyReadyCheckpoint" successfully set.2015-12-08 10:51:09, Info      [0x06009e] IBS    DeployWIMImage:Calling IDepWIMImageResolved::Apply...2015-12-08 10:51:10, Info      [0x0606cc] IBS    Calling WIMApplyImage (flags = 0x184)...

安装程序之后会继续将一些驱动程序文件复制到虚拟 HDD 中,之后你可以看到屏幕停在了下面的信息。需要注意的是,如果它似乎被冻结在了那里,那么客户机可能已经完成了它的第一个阶段安装并关机了。

Name:                  SACDescription:          Special AdminisType:                  VT-UTF8Channel GUID:          8472e3a1-9ddc-11e5-9c07-806e6f6e6963Application Type GUID: 63d02270-8aa4-11d5-bccf-806d6172696fPress <esc><tab> for next channel.Press <esc><tab>0 to return to the SAC channel.Use any other key to view this channel.

在第一个控制台上面,检查并确保客户机关机了。
 
$ iohyve list
Guest  VMM?  Running?  rcboot?  Description
win2k8  YES  NO        NO      Tue_Dec__8_10:30:51_MST_2015

从上面的信息我们可以看到 Running? 标志被设置为了 NO,这意味着客户机已经被关闭了。我们还需要运行 iohyve destroy win2k8 来从 VMM? 中移除它,不然客户机将在第二个安装阶段失败。
 
$ sudo iohyve destroy win2k8
Destroying win2k8...errno = 37

现在我们可以运行第二个阶段了:
 
$ sudo iohyve uefi win2k8 null.iso

如果你切换到了 SAC 控制台你可以改变 SACSetupAct 引导以及查看屏幕上滚过的东西。这个步骤是非常迅速的,而且有时候你并不能捕捉到 SACSetupAct 引导,所以当客户机关闭的时候你只能留下一个看起来如下的屏幕。
 
Setup is updating registry settings...
EVENT:  A new channel has been created.  Use "ch -?" for channel help.
Channel: SACSetupAct
SAC>
EVENT:  A new channel has been created.  Use "ch -?" for channel help.
Channel: SACSetupErr
SAC>
EVENT: The CMD command is now available.
SAC>
EVENT:  A channel has been closed.
Channel: SACSetupAct
SAC>
EVENT:  A channel has been closed.
Channel: SACSetupErr
SAC>
The SAC will become unavailable soon.  The computer is shutting down.
 
SAC>

切换到你的第一个控制,检查以确保其再次关闭,并再次移除它。然后,你可以开始第三个阶段的安装,并最终第一次启动 Windows。
 
$ iohyve list
Guest  VMM?  Running?  rcboot?  Description
win2k8  YES  NO        NO      Tue_Dec__8_10:30:51_MST_2015$ sudo iohyve destroy win2k8
Destroying win2k8...errno = 37$ sudo iohyve uefi win2k8 null.iso

第一次登陆安装阶段确实需要相当多的时间来完成,但最终,你的屏幕上会显示如下信息:

Computer is booting, SAC started and initialized.                             
 
Use the "ch -?" command for information about using channels.                 
Use the "?" command for general help.                                         
 
 
SAC>                                                                           
EVENT: The CMD command is now available.                                       
SAC>
 

 如果我运行 SAC 命令 i,我可以看到在之前的 AutoUnattend.xml 文件中设置的客户机的 IP 地址。默认情况下,Windows 客户机无法 ping 通网络,但执行 nmap 192.168.0.111 将会显示 Windows 远程桌面可用。

SAC>i                                                                         
Net: 12, Ip=192.168.0.111  Subnet=255.255.255.0  Gateway=192.168.0.1

就是这样!你现在可以用你惯用的远程桌面客户端使用你在 AutoUnattend.xml 中设置的默认密码 R3dm0nd! 去连接客户机的 Administrator 帐户了。

安装后你可以做的一些很酷的东西

尽管 iohyve uefi 功能尚未被正式支持,你依然可以使用内置的 iohyve 工具为 Windows 客户机做一些很酷的关于动态文件系统(ZFS)的一些事情,比如在安装更新之前创建快照:
 
$ sudo iohyve snap win2k8@preupdate
Password:
Taking snapshot win2k8@preupdate$ iohyve snaplist
win2k8@preupdate

你也可以制作一个这个客户机的独立的拷贝,一旦你制作了这个镜像,你就可以不必一遍又一遍的执行 Windows 的安装过程了:

$ sudo iohyve clone win2k8 win2k8-deploy                                                                                             
Cloning win2k8 to win2k8-deploy$ iohyve list
Guest          VMM?  Running?  rcboot?  Description
win2k8        YES  NO        NO      Tue_Dec__8_10:30:51_MST_2015
win2k8-deploy  NO    NO        NO      Tue_Dec__8_11:34:38_MST_2015

英文原文:Running Windows under FreeBSD's bhyve

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-12/126683.htm

linux
相关资讯       bhyve 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款