使用 Packer 创建本地 Windows 镜像导入金山云

最近更新时间:2020-06-04 10:46:07

背景

本文举例描述您通过使用 Packer 如何制作 Windows 本地镜像,并上传导入本地镜像到您的金山云账号成为自定义镜像。

本文示例步骤使用了如下版本平台。

服务器操作系统: Ubuntu 18.04 待制作的镜像: Windows 2016 数据中心中文版

前提条件

已创建 AccessKey。 已经开通 KS3 权限。开通ks3权限 已授权 KEC 官方服务账号可以访问您的 ks3 的权限。

使用示例

1.确认是否支持 KVM。 执行 egrep "(svm|vmx)" /proc/cpuinfo 命令,查看您的正在使用的本地服务器是否支持 KVM。 返回如下信息时,表示当前服务器支持 KVM。

flags:fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d

2.配置本地服务器安装 KVM

如果您的本地服务器支持KVM,依次执行以下命令安装 KVM。

sudo apt-get install qemu-kvm qemu libvirt-bin bridge-utils virt-manager virt-viewer  #安装 KVM、libvirt,bridge等相关控件.

如果可以通过 GUI 工具 virt-manager 创建出虚拟机,表示您已经成功安装 KVM。

3.获取 Packer。 参考 Packer 创建自定义镜像 步骤一:安装Packer。

4.定义 Packer 镜像模板。 此步骤大概需要准备如下文件

$ tree
.
├── http
│   └── windows2016zh
│       └── Autounattend.xml
├── iso
│   ├── cn_windows_server_2016_x64_dvd_9718765.iso
│   └── virtio-win-0.1.110.iso
└── Windows-2016-x86_64_zh-iso.json

①准备 windows 2016 的 iso 镜像文件保存为iso/cn_windows_server_2016_x64_dvd_9718765.iso。

$ md5sum iso/cn_windows_server_2016_x64_dvd_9718765.iso
4f1e348b1333a19169f16145635c36c1   iso/cn_windows_server_2016_x64_dvd_9718765.iso

②准备 virtio 驱动 virtio-win-0.1.110.iso 文件。可以从官网下载 https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/

③准备用于自动化安装 windows 系统的 Sysprep 文件 http/windows2016zh/Autounattend.xml。

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="windowsPE">
    <component name="Microsoft-Windows-International-Core-WinPE" 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">
      <SetupUILanguage>
        <UILanguage>zh-CN</UILanguage>
      </SetupUILanguage>
      <SystemLocale>zh-CN</SystemLocale>
      <UILanguage>zh-CN</UILanguage>
      <UserLocale>zh-CN</UserLocale>
    </component>
    <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>
        <WillShowUI>OnError</WillShowUI>
        <Disk wcm:action="add">
          <DiskID>0</DiskID>
          <WillWipeDisk>true</WillWipeDisk>
          <CreatePartitions>
            <CreatePartition wcm:action="add">
              <Order>1</Order>
              <Type>Primary</Type>
              <Size>500</Size>
            </CreatePartition>
            <CreatePartition wcm:action="add">
              <Order>2</Order>
              <Type>Primary</Type>
              <Extend>true</Extend>
            </CreatePartition>
          </CreatePartitions>
          <ModifyPartitions>
            <ModifyPartition wcm:action="add">
              <Order>1</Order>
              <PartitionID>1</PartitionID>
              <Label>系统保留</Label>
              <Format>NTFS</Format>
              <Active>true</Active>
            </ModifyPartition>
            <ModifyPartition wcm:action="add">
              <Order>2</Order>
              <PartitionID>2</PartitionID>
              <Label>本地磁盘</Label>
              <Letter>C</Letter>
              <Format>NTFS</Format>
            </ModifyPartition>
          </ModifyPartitions>
        </Disk>
      </DiskConfiguration>
      <ImageInstall>
        <OSImage>
          <InstallFrom>
            <MetaData wcm:action="add">
              <Key>/IMAGE/NAME</Key>
              <Value>Windows Server 2016 SERVERDATACENTER</Value>
            </MetaData>
          </InstallFrom>
          <InstallTo>
            <DiskID>0</DiskID>
            <PartitionID>2</PartitionID>
          </InstallTo>
          <WillShowUI>OnError</WillShowUI>
          <InstallToAvailablePartition>false</InstallToAvailablePartition>
        </OSImage>
      </ImageInstall>
      <UserData>
        <AcceptEula>true</AcceptEula>
        <ProductKey>
          <Key>CB7KF-BWN84-R7R2Y-793K2-8XDDG</Key>
        <WillShowUI>OnError</WillShowUI>
        </ProductKey>
      </UserData>
    </component>
    <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="neutral" name="Microsoft-Windows-PnpCustomizationsWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" versionScope="nonSxS">
      <DriverPaths>
        <PathAndCredentials wcm:action="add" wcm:keyValue="1">
          <Path>E:\NetKVM\2k12R2\amd64\</Path>
        </PathAndCredentials>
        <PathAndCredentials wcm:action="add" wcm:keyValue="3">
          <Path>E:\viostor\2k12R2\amd64\</Path>
        </PathAndCredentials>
        <PathAndCredentials wcm:action="add" wcm:keyValue="4">
          <Path>E:\vioserial\2k12R2\amd64\</Path>
        </PathAndCredentials>
      </DriverPaths>
    </component>
  </settings>
  <settings pass="oobeSystem">
    <component name="Microsoft-Windows-Shell-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">
      <UserAccounts>
        <AdministratorPassword>
          <Value>HelloWorld123</Value>
          <PlainText>true</PlainText>
        </AdministratorPassword>
        <LocalAccounts>
          <LocalAccount wcm:action="add">
            <Password>
              <Value>kingsoft</Value>
              <PlainText>true</PlainText>
            </Password>
          <Description>KSC</Description>
          <DisplayName>kingsoft</DisplayName>
          <Group>Administrators</Group>
          <Name>kingsoft</Name>
          </LocalAccount>
        </LocalAccounts>
      </UserAccounts>
      <AutoLogon>
        <Password>
          <Value>HelloWorld123</Value>
          <PlainText>true</PlainText>
        </Password>
        <Enabled>true</Enabled>
        <LogonCount>1</LogonCount>
        <Username>Administrator</Username>
      </AutoLogon>
      <FirstLogonCommands>
        <SynchronousCommand wcm:action="add">
          <Order>19</Order>
          <Description>Remote Desktop</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>20</Order>
          <Description>Remote Desktop1</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /f /v UserAuthentication /t REG_DWORD /d 0</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>26</Order>
          <Description>startup script</Description>
          <CommandLine>cmd /C type NUL > C:\boot.bat</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>27</Order>
          <Description>startup script1</Description>
          <CommandLine>cmd /C attrib +h C:\boot.bat</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>28</Order>
          <Description>startup script2</Description>
          <CommandLine>cmd /C start "" "C:\Windows\System32\gpedit.msc"</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>29</Order>
          <Description>startup script3</Description>
          <CommandLine>cmd /C md C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>30</Order>
          <Description>startup script4</Description>
          <CommandLine>cmd /C md C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>31</Order>
          <Description>startup script5</Description>
          <CommandLine>cmd /C echo. > C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>32</Order>
          <Description>startup script6</Description>
          <CommandLine>cmd /C echo [Startup] >> C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>33</Order>
          <Description>startup script7</Description>
          <CommandLine>cmd /C echo 0CmdLine=C:\boot.bat >> C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>34</Order>
          <Description>startup script8</Description>
          <CommandLine>cmd /C echo 0Parameters= >> C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>35</Order>
          <Description>startup script9</Description>
          <CommandLine>cmd /C attrib -a +h C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>36</Order>
          <Description>startup script10</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown" /f /t REG_SZ</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>37</Order>
          <Description>startup script11</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0\0" /f /v Script /t REG_SZ /d C:\boot.bat</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>38</Order>
          <Description>startup script12</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0\0" /f /v Parameters /t REG_SZ</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>39</Order>
          <Description>startup script13</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0\0" /f /v IsPowershell /t REG_DWORD /d 0</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>40</Order>
          <Description>startup script14</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v SOM-ID /t REG_SZ /d Local</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>41</Order>
          <Description>startup script15</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v PSScriptOrder /t REG_DWORD /d 1</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>42</Order>
          <Description>startup script16</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v GPOName /t REG_SZ /d 本地组策略</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>43</Order>
          <Description>startup script17</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v GPO-ID /t REG_SZ /d LocalGPO</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>44</Order>
          <Description>startup script18</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v FileSysPath /t REG_SZ /d C:\Windows\System32\GroupPolicy\Machine</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>45</Order>
          <Description>startup script19</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0" /f /v DisplayName /t REG_SZ /d 本地组策略</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>46</Order>
          <Description>startup script20</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown" /f /t REG_SZ</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>47</Order>
          <Description>startup script21</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0" /f /v Script /t REG_SZ /d C:\boot.bat</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>48</Order>
          <Description>startup script22</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0" /f /v Parameters /t REG_SZ</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>49</Order>
          <Description>startup script23</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v SOM-ID /t REG_SZ /d Local</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>50</Order>
          <Description>startup script24</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v PSScriptOrder /t REG_DWORD /d 1</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>51</Order>
          <Description>startup script25</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v GPOName /t REG_SZ /d 本地组策略</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>52</Order>
          <Description>startup script26</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v GPO-ID /t REG_SZ /d LocalGPO</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>53</Order>
          <Description>startup script27</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v FileSysPath /t REG_SZ /d C:\Windows\System32\GroupPolicy\Machine</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>54</Order>
          <Description>startup script28</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0" /f /v DisplayName /t REG_SZ /d 本地组策略</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>55</Order>
          <Description>startup script29</Description>
          <CommandLine>cmd /C reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\GPO-List\0" /f /v Version /t REG_DWORD /d 65537</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>56</Order>
          <Description>startup script30</Description>
          <CommandLine>cmd /C echo Windows Registry Editor Version 5.00 > C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>57</Order>
          <Description>startup script31</Description>
          <CommandLine>cmd /C echo. >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>58</Order>
          <Description>startup script32</Description>
          <CommandLine>cmd /C echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0\0] >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>59</Order>
          <Description>startup script33</Description>
          <CommandLine>cmd /C echo "ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>60</Order>
          <Description>startup script34</Description>
          <CommandLine>echo. >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>61</Order>
          <Description>startup script35</Description>
          <CommandLine>cmd /C echo [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0] >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>62</Order>
          <Description>startup script36</Description>
          <CommandLine>cmd /C echo "ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >> C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>63</Order>
          <Description>startup script37</Description>
          <CommandLine>cmd /C regedit /s C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>64</Order>
          <Description>startup script38</Description>
          <CommandLine>cmd /C gpupdate /force</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>65</Order>
          <Description>startup script39</Description>
          <CommandLine>cmd /C del /F C:\addQword.reg</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>66</Order>
          <Description>startup script40</Description>
          <CommandLine>cmd /C echo [Genera1] > C:\Windows\System32\GroupPolicy\gpt.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>67</Order>
          <Description>startup script41</Description>
          <CommandLine>cmd /C echo gPCMachineExtensionNames= >> C:\Windows\System32\GroupPolicy\gpt.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>68</Order>
          <Description>startup script42</Description>
          <CommandLine>cmd /C echo Version=1 >> C:\Windows\System32\GroupPolicy\gpt.ini</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>69</Order>
          <Description>firewall</Description>
          <CommandLine>cmd /C net stop MpsSvc</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>70</Order>
          <Description>firewall1</Description>
          <CommandLine>cmd /C  reg add "HKLM\SYSTEM\CurrentControlSet\services\MpsSvc" /v Start /t REG_DWORD /d 4 /f</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>71</Order>
          <Description>Time zone</Description>
          <CommandLine>cmd /C tzutil /s "China Standard Time"</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>73</Order>
          <Description>file log</Description>
          <CommandLine>cmd /C del /F /Q C:\Windows\Logs\CBS\*</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>74</Order>
          <Description>file log1</Description>
          <CommandLine>cmd /C del /F /Q C:\Windows\Temp\*</CommandLine>
        </SynchronousCommand>

        <SynchronousCommand wcm:action="add">
          <Order>75</Order>
          <Description>event log</Description>
          <CommandLine>cmd /C for /F "tokens=*" %1 in ('wevtutil.exe el') DO wevtutil.exe cl "%1"</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>76</Order>
          <Description>event log1</Description>
          <CommandLine>cmd /C wevtutil.exe cl "System"</CommandLine>
        </SynchronousCommand>
        <SynchronousCommand wcm:action="add">
          <Order>99</Order>
          <Description>Shutting down Windows</Description>
          <CommandLine>cmd /C shutdown /s /t 0</CommandLine>
        </SynchronousCommand>
      </FirstLogonCommands>
    </component>
  </settings>
</unattend>

④准备制作镜像的 Packer 模板文件 Windows-2016-x86_64_zh-iso.json。

{
  "variables": {
    "output_image_name": "win_2016_dc_zh_64bit.img.org",
    "packer_images_output_dir": "output_image",
    "qemu_dir": "/usr/bin/qemu-system-x86_64",
    "cpus": "4",
    "memory": "4096",
    "vnc_address": "0.0.0.0",
    "virtio_win_iso": "iso/virtio-win-0.1.110.iso",
    "iso_url": "iso/cn_windows_server_2016_x64_dvd_9718765.iso",
    "iso_checksum": "4f1e348b1333a19169f16145635c36c1",
    "windows_version": "windows2016zh",
    "autounattend": "http/{{ user `windows_version` }}/Autounattend.xml",
    "winrm_username": "kingsoft",
    "winrm_password": "kingsoft",
    "disk_size": "51200"
  },
  "builders":
  [
    {
      "vm_name": "{{ user `output_image_name`}}",
      "type": "qemu",
      "qemu_binary": "{{ user `qemu_dir` }}",
      "iso_url": "{{ user `iso_url` }}",
      "iso_checksum": "{{ user `iso_checksum` }}",
      "iso_checksum_type": "md5",
      "communicator": "none",
      "winrm_username": "{{ user `winrm_username` }}",
      "winrm_password": "{{ user `winrm_password` }}",
      "winrm_timeout": "12h",
      "winrm_use_ssl": "true",
      "winrm_insecure": "true",
      "vnc_bind_address": "{{ user `vnc_address`}}",
      "output_directory": "{{ user `packer_images_output_dir`}}",
      "disk_size": "{{ user `disk_size`}}",
      "format": "qcow2",
      "shutdown_timeout": "1h",
      "qemuargs": [
        [ "-m", "{{ user `memory` }}" ],
        [ "-smp", "{{ user `cpus` }}" ],
        [ "-drive", "file={{ user `virtio_win_iso` }},media=cdrom,index=3" ],
        [ "-drive", "file={{ user `packer_images_output_dir` }}/{{ user `output_image_name` }},if=virtio,cache=writeback,discard=ignore,format=qcow2,index=1" ]
      ],
      "floppy_files": [
        "{{ user `autounattend` }}"
      ]
    }
  ],
  "post-processors":[
    {
      "type": "kscloud-import",
      "access_key": "xxxxx",
      "secret_key": "xxxx",
      "ks3_region": "BEIJING",
      "ks3_endpoint": "ks3-cn-beijing.ksyun.com",
      "ks3_bucket_name": "helloworld-test-ks3sdk",
      "ks3_key_name": "hello_test_image",
      "upload_file_path": "{{ user `packer_images_output_dir` }}/{{ user `output_image_name` }}",
      "kec_region": "cn-beijing-6",
      "image_name": "sdk-test-import-win2016-image",
      "image_architecture": "x86_64",
      "image_platform": "windows-server_2016_r2_datacenter_64_zh",
      "image_format": "qcow2"
    }
  ]
}

5.制作镜像 执行如下命令即可创建并导入本地镜像。

packer build Windows-2016-x86_64_zh-iso.json

运行中程序显示如下信息:

qemu output will be in this color.
==> qemu: Retrieving ISO
==> qemu: Trying iso/cn_windows_server_2016_x64_dvd_9718765.iso
.................
==> qemu: Converting hard drive...
==> qemu: Running post-processor: kscloud-import
==> qemu (kscloud-import): Image file output_image/win_2016_dc_zh_64bit.img.org has been uploaded to KS3
==> qemu (kscloud-import): Create kec image success
==> qemu (kscloud-import): Region: cn-beijing-6, ImageId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
==> qemu (kscloud-import): KEC RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
Build 'qemu' finished.

金山云,开启您的云计算之旅

免费注册