您的位置:首页 > 其它

创建WINDOWS 10的BASE BOX

2016-05-27 17:57 316 查看

创建WINDOWS 10的BASE BOX

原文链接:http://huestones.co.uk/node/305

按原文步骤即可生成base box。基本内容如下:

制作Windows Vagrant box

创建VirtualBox虚拟机

安装Windows

配置Windows

Package并导出box

创建虚拟机

打开virtual box并新建一个新的Windows 10虚拟机。名字随便起,但要简单易记,因为后面需要再次用到这个名字。可参考Vagrant文档中关于base boxes相关建议。

以下设置需要特别注意

磁盘空间:设置为动态分配的VDI,最大容量要足够用。默认为32GB,对于Windows来说太低。

内存:设置为一低值,用户在使用base box时,可根据需要自己调整(如在Vagrantfile中设定). 对于Win10来说,2048MB基本上是最低值。

CPU: 默认配置为1,不改变。同样,这个值用户也可以在Vagrantfile中定制。

禁用不需要的硬件,如音频等。保持默认的2D/3D加速的禁用状态。

配置的基本原则是:使创建的base box对资源的需求尽可能低,保留尽可能多的后续扩展空间。在创建新虚拟机时,VirtualBox的向导会自动生成虚拟机的默认配置选项,其中部分默认配置需要在向导完成后手动配置。在新建虚拟机的“设置”中,对下列选项进行配置:

设置 > 常规 > 高级: 开启剪贴板和拖放的双向支持.

设置 > 常规 > 系统 > 主板: 禁用软盘.

设置 > 常规 > 声音: 禁用声音.

base box的用户可在自己的Vagrantfile中定制这些选项.

安装Windows

启动虚拟机并安装Windows,不再赘述。

安装结束时,不要以Microsoft account登录。创建本地管理员账号vagrant ,密码vagrant. 如果想要Vagrant自动控制你的base box,这一点是必须的.

安装完毕后,登录进入桌面.

准备Vagrant的Windows

以下操作应在virtualbox的客户机中进行。

安装VirtualBox Guest Additions. 按提示重启后,断开所有仍连接的DVD驱动器.

控制面板 > 程序和功能 > 开启或关闭功能: 安装.NET framework 3.5.(原作者建议,应该不是必须,看base box的用途)

禁用UAC: 控制面板 > 用户帐户 > 更改用户帐户控制设置,将滑动条拉到底部:从不通知. 接着以管理员权限打开命令行,输入以下命令:

reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /d 0 /t REG_DWORD /f /reg:64


按提示重启。

- 配置并启动WinRM服务: 在管理员权限的命令行窗口中,输入以下命令:

winrm quickconfig -q

winrm set winrm/config/winrs @{MaxMemoryPerShellMB="512"}

winrm set winrm/config @{MaxTimeoutms="1800000"}

winrm set winrm/config/service @{AllowUnencrypted="true"}

winrm set winrm/config/service/auth @{Basic="true"}

sc config WinRM start= auto


最后一条命令配置winrm服务自动启动。这样Vagrant可连接并控制box. WinRm是windows boxes的SSH替代服务。

打开Powershell 执行策略: 以管理员权限运行Powershell,执行以下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted


按提示输入 Yes。这条命令取消了默认的Windows对运行Powershell脚本的限制。

开启远程连接功能:控制面板 > 系统 > 高级系统设置 > 远程 > 允许远程连接到此计算机, 取消“仅允许使用网络级别…”核选框, 这里并不关心安全。

建议关闭“系统属性”中的“系统保护”,在“高级 > 性能”中调整显示效果,在“高级”标签页中限定最大虚拟内存paging文件的大小为1024MB.

按提示重启(如果有)。

安装所有windows更新: 安装完毕重启后,更改Windows更新,不要自动安装,而是“通知以计划重启”. 在vagarnt停止box过程中安装windows更新,可能导致box变“砖”, 所以如果要安装更新应手动重启。

最后,删除无用的文件,并全零覆写空闲空间:这一步不是必须的,但可能优化base box的压缩文件大小. 在管理员权限的命令行窗口输入以下命令:

C:\Windows\System32\cleanmgr.exe /d c:


下载并运行Sysinternals Sdelete

sdelete.exe -z c:


以全0填写空闲空间,会减少导出box的压缩文件大小。

最后,确认没有连接和DVD和挂载的共享文件夹。重启虚拟机,确保所有更新安装完毕. 然后,正常关机,准备导出到vagrant box.

不要在base box中安装附加的软件,除了必须要用的. 你可以在任何时候通过provisioning脚本进行定制。 没人会想要一个超大的base box, 下载和维护都不容易。

导出base box

在导出之前,需要创建新的Vagrantfile,提供默认配置,使用户和Vagrant可以连接box. 下面是一个完整的Vagrantfile例子,配置了大部分必须的选项:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
config.vm.guest = :windows
config.vm.communicator = "winrm"
config.vm.boot_timeout = 600
config.vm.graceful_halt_timeout = 600

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 3389, host: 3389
config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true

# config.vm.provider "virtualbox" do |vb|
#    # Customize the name of VM in VirtualBox manager UI:
#    vb.name = "yourcompany-yourbox"
# end
end


下面详细说明一下配置内容:

config.vm.guest = :windows
config.vm.communicator = "winrm"


这两行告诉vagrant:这是一个windows box,需要通过winrm控制box.

config.vm.boot_timeout = 600
config.vm.graceful_halt_timeout = 600


Timeouts单位为秒。 Windows总是很讨厌地在启动和关闭时安装更新. 超时配置的太短,Vagrant会强制kill掉虚拟机,可能导致box变砖. 设置一个长的时间可预防这一问题。

config.vm.network :forwarded_port, guest: 3389, host: 3389
config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true


第一行打开RDP的3389端口,
vagrant rdp
命令可以连接base box. 3. 第二行Vagrant通过WinRm服务端口5985控制box.

vb.name = "yourcompany-boxos-version"


这一行配置是可选的,如果开启,在用户导入base box时,VirtaulBox界面中会显示这里配置的名称。否则,会自动生成一个随机名称。如果要配置这个选项,一定要确保名称的唯一性,避免与其他boxes冲突。

现在可以导出Vagrant base box了,使用带有
--base
选项的
package
命令:

vagrant package --base VirtualBoxVMName --output /path/to/output/windows.box --vagrantfile /path/to/initial/Vagrantfile


用VirtualBox界面中的名称替换
VirtualBoxVMName
(注意大小写,vagrant命令对大小写敏感)。

替换
/path/to/output/windows.box
为指定的路径和保存名称.

替换
/path/to/initial/Vagrantfile
为上面创建的Vagrantfile文件路径.

Vagran开始导出工作,可以看到类似如下的输出:

==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /path/to/output/windows.box
==> default: Packaging additional file: Vagrantfile


导出完成,会在指定路径下生成指定名称的.box文件。

使用base box

与其它box的使用没有区别。

添加box的命令如下:

vagrant box add /path/to/output/windows.box --name ANameForYourBox


启动:

vagrant init ANameForYourBox
vagrant up


如果一切正常,’vagrant up’会启动box并连接。你可以使用命令
vagrant rdp
进行远程连接。

因为在打包好的base box中包含一个默认的Vagrantfile, 端口、rdp和winrm配置都包含在base box中. 这样,
vagrant init
创建的Vagrantfile只包含配置命令。

当Vagrant管理box时,打开VirtualBox界面可以看到相应的过程。 可以随意进行配置并导出为新的box。

参见get provisioning.

试验环境

host: Win10 Pro X64

guest: Win10 Pro X64

Vagrant: v1.8.1

VirtualBox: 5.0.20

问题与解决

导出时VirtualBoxVMName不对:Vagrant命令对大小写敏感,应注意名称的的一致性

添加base box时遇到错误,输出如下:

D:\vatest\Win10boxtest>vagrant box add d:\windows.box --name Win10box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'Win10box' (v0) for provider:
box: Unpacking necessary files from: file://D:/windows.box
box:
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.


没有具体错误信息。G了半天,找到这个链接http://stackoverflow.com/questions/34556929/vagrant-box-add-fail,尝试替换
C:\HashiCorp\Vagrant\embedded\bin
下的curl后一切正常:

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'Win10box' (v0) for provider:
box: Unpacking necessary files from: file://D:/windows.box
box: Progress: 100% (Rate: 54.4M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'Win10entbox' (v0) for 'virtualbox'!


命令行下执行新版
curl -V
,输出如下:

curl 7.49.0 (x86_64-pc-win32) libcurl/7.49.0 OpenSSL/1.0.2h nghttp2/1.11.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM SSL HTTP2


命令行下执行原版
curl -V
,程序崩溃。

基本确定是curl版本的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息