使用Packer为 VMware Workstation Pro自动化构建openSUSE 15.3 虚拟机映像

autoinst.xml

<?xml version="1.0"?>
<!DOCTYPE profile>
<profile xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns">
  <scripts>
    <chroot-scripts config:type="list">
      <script>
        <chrooted config:type="boolean">true</chrooted>
        <filename>add_vagrant_sudo_rule.sh</filename>
        <interpreter>shell</interpreter>
        <source>
<![CDATA[
#!/bin/sh
echo "Defaults:vagrant !targetpw
vagrant ALL=(ALL,ALL) NOPASSWD: ALL" > /etc/sudoers.d/vagrant
]]>
          </source>
      </script>
    </chroot-scripts>
  </scripts>
  <general>
    <mode>
      <confirm config:type="boolean">false</confirm>
      <forceboot config:type="boolean">true</forceboot>
      <final_reboot config:type="boolean">false</final_reboot>
    </mode>
  </general>
  <report>
    <messages>
      <show config:type="boolean">false</show>
      <timeout config:type="integer">10</timeout>
      <log config:type="boolean">true</log>
    </messages>
    <warnings>
      <show config:type="boolean">false</show>
      <timeout config:type="integer">10</timeout>
      <log config:type="boolean">true</log>
    </warnings>
    <errors>
      <show config:type="boolean">false</show>
      <timeout config:type="integer">10</timeout>
      <log config:type="boolean">true</log>
    </errors>
  </report>
  <keyboard>
    <keymap>english-us</keymap>
  </keyboard>
  <language>
    <language>en_US</language>
    <languages>en_US</languages>
  </language>
  <timezone>
    <hwclock>UTC</hwclock>
    <timezone>Etc/UTC</timezone>
  </timezone>
  <partitioning config:type="list">
    <drive>
      <enable_snapshots config:type="boolean">false</enable_snapshots>
      <initialize config:type="boolean">true</initialize>
      <partitions config:type="list">
        <partition>
          <create config:type="boolean">true</create>
          <crypt_fs config:type="boolean">false</crypt_fs>
          <filesystem config:type="symbol">ext4</filesystem>
          <format config:type="boolean">true</format>
          <loop_fs config:type="boolean">false</loop_fs>
          <mount>/</mount>
          <mountby config:type="symbol">device</mountby>
          <partition_id config:type="integer">131</partition_id>
          <partition_nr config:type="integer">2</partition_nr>
          <raid_options />
          <resize config:type="boolean">false</resize>
          <size>max</size>
          <subvolumes config:type="list">
            <listentry>boot/grub2/i386-pc</listentry>
            <listentry>boot/grub2/x86_64-efi</listentry>
            <listentry>home</listentry>
            <listentry>opt</listentry>
            <listentry>srv</listentry>
            <listentry>tmp</listentry>
            <listentry>usr/local</listentry>
            <listentry>var/crash</listentry>
            <listentry>var/log</listentry>
            <listentry>var/opt</listentry>
            <listentry>var/spool</listentry>
            <listentry>var/tmp</listentry>
          </subvolumes>
        </partition>
      </partitions>
      <pesize />
      <type config:type="symbol">CT_DISK</type>
      <use>all</use>
    </drive>
  </partitioning>
  <bootloader>
    <loader_type>grub2</loader_type>
		<global>
			<activate>true</activate>
			<timeout config:type="integer">1</timeout>
			<boot_mbr>true</boot_mbr>
		</global>
  </bootloader>
  <networking>
    <ipv6 config:type="boolean">false</ipv6>
    <keep_install_network config:type="boolean">true</keep_install_network>
    <dns>
      <dhcp_hostname config:type="boolean">true</dhcp_hostname>
      <resolv_conf_policy>auto</resolv_conf_policy>
      <domain>vagrantup.com</domain>
      <hostname>opensuse-leap-x64</hostname>
    </dns>
    <interfaces config:type="list">
      <interface>
        <bootproto>dhcp</bootproto>
        <name>eth0</name>
        <startmode>auto</startmode>
        <usercontrol>no</usercontrol>
      </interface>
    </interfaces>
  </networking>
  <firewall>
    <enable_firewall config:type="boolean">false</enable_firewall>
    <start_firewall config:type="boolean">false</start_firewall>
  </firewall>
  <software>
    <packages config:type="list">
      <package>apparmor-parser</package>
      <package>grub2</package>
      <package>glibc-locale</package>
      <package>iputils</package>
      <package>openssh</package>
      <package>kernel-default</package>
      <package>sudo</package>
      <package>yast2</package>
      <package>yast2-firstboot</package>
      <package>zypper</package>
      <package>yast2-trans-en_US</package>
      <package>wget</package>
      <package>curl</package>
      <package>grub2-branding-openSUSE</package>
      <package>less</package>
      <package>growpart</package>
      <package>open-vm-tools</package>
      <package>open-iscsi</package>
      <package>insserv-compat</package>
      <package>cloud-init</package>
      <package>docker</package>
    </packages>
    <patterns config:type="list">
      <pattern>sw_management</pattern>
      <pattern>yast2_install_wf</pattern>
      <pattern>minimal_base</pattern>
      <pattern>devel_basis</pattern>
    </patterns>
    <remove-packages config:type="list">
      <package>bash-completion</package>
      <package>telnet</package>
      <package>virtualbox-guest-kmp-default</package>
      <package>virtualbox-guest-tools</package>
      <package>snapper</package>
      <package>snapper-zypp-plugin</package>
    </remove-packages>
  </software>
  <services-manager>
    <default_target>multi-user</default_target>
    <services>
      <disable config:type="list" />
      <enable config:type="list">
        <service>sshd</service>
        <service>docker</service>
        <service>cloud-init</service>
        <service>cloud-config</service>
        <service>cloud-final</service>
      </enable>
    </services>
  </services-manager>
  <groups config:type="list">
    <group>
      <gid>100</gid>
      <groupname>users</groupname>
      <userlist />
    </group>
  </groups>
  <user_defaults>
    <expire />
    <group>100</group>
    <groups />
    <home>/home</home>
    <inactive>-1</inactive>
    <no_groups config:type="boolean">true</no_groups>
    <shell>/bin/bash</shell>
    <skel>/etc/skel</skel>
    <umask>022</umask>
  </user_defaults>
  <users config:type="list">
    <user>
      <user_password>vagrant</user_password>
      <username>root</username>
    </user>
    <user>
      <fullname>Built by Packer</fullname>
      <gid>100</gid>
      <home>/home/vagrant</home>
      <password_settings>
        <expire />
        <flag />
        <inact>-1</inact>
        <max>99999</max>
        <min>0</min>
        <warn>7</warn>
      </password_settings>
      <shell>/bin/bash</shell>
      <uid>1000</uid>
      <user_password>vagrant</user_password>
      <username>vagrant</username>
    </user>
  </users>
  <kdump>
    <add_crash_kernel config:type="boolean">false</add_crash_kernel>
  </kdump>
</profile>

opensuse153.json

{
  "variables": {
      "boot_wait": "10s",
      "disk_size": "40960",
      "iso_checksum": "9d1aecaa42f322f034788e78fdbd81d7",
      "iso_url": "file://F:\\openSUSE-Leap-15.3-2-DVD-x86_64-Build24.5-Media.iso",
      "memsize": "1024",
      "numvcpus": "1",
      "ssh_password" : "vagrant",
      "ssh_username" : "vagrant",
      "vm_name": "openSUSE-Leap-15.3"
    },
    "builders": [
      {
        "type": "vmware-iso", 
        "boot_command": [
        "<esc><enter><wait>",
        "linux",
        " textmode=1",
        " lang=en_US",
        " netdevice=eth0",
        " netsetup=dhcp",
        " autoyast=http://{{ .HTTPIP }}:{{ .HTTPPort }}/autoinst.xml",
        "<enter><wait5>"
      ],
        "boot_wait": "{{ user `boot_wait` }}",
        "disk_size": "{{ user `disk_size` }}",
        "disk_type_id": "0",
        "guest_os_type": "opensuse-64",
        "headless": false,
        "http_directory": "http",
        "iso_checksum": "{{ user `iso_checksum` }}",
        "iso_url": "{{ user `iso_url` }}",
        "shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p",
        "ssh_password": "{{ user `ssh_password` }}",
        "ssh_port": 22,
        "ssh_username": "{{ user `ssh_username` }}",
        "ssh_timeout": "30m",
        "vm_name": "{{ user `vm_name` }}",
        "vmx_data": {
          "memsize": "{{ user `memsize` }}",
          "numvcpus": "{{ user `numvcpus` }}",
          "virtualHW.version": "14"
        }
      }
    ],
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'",
      "inline": [
        "zypper update"
      ]
    }
  ]
}

构建输出:

F:\packer-opensuse>packer.exe build opensuse153.json
Warning: Warning when preparing build: "vmware-iso"

Your vmx data contains the following variable(s), which Packer normally sets
when it generates its own default vmx template. This may cause your build to
fail or behave unpredictably: numvcpus, memsize, virtualHW.version


vmware-iso: output will be in this color.

==> vmware-iso: Retrieving ISO
==> vmware-iso: Trying file://F:\openSUSE-Leap-15.3-2-DVD-x86_64-Build24.5-Media.iso
==> vmware-iso: Trying file://F:/openSUSE-Leap-15.3-2-DVD-x86_64-Build24.5-Media.iso?checksum=md5%3A9d1aecaa42f322f034788e78fdbd81d7
==> vmware-iso: file://F:/openSUSE-Leap-15.3-2-DVD-x86_64-Build24.5-Media.iso?checksum=md5%3A9d1aecaa42f322f034788e78fdbd81d7 => F:/openSUSE-Leap-15.3-2-DVD-x86_64-Build24.5-Media.iso
==> vmware-iso: Configuring output and export directories...
==> vmware-iso: Creating required virtual machine disks
==> vmware-iso: Building and writing VMX file
==> vmware-iso: Starting HTTP server on port 8401
==> vmware-iso: Starting virtual machine...
==> vmware-iso: Connecting to VNC...
==> vmware-iso: Waiting 10s for boot...
==> vmware-iso: Typing the boot command over VNC...
==> vmware-iso: Waiting for SSH to become available...
==> vmware-iso: Connected to SSH!
==> vmware-iso: Provisioning with shell script: C:\Users\ADMINI~1\AppData\Local\Temp\packer-shell3282613005
    vmware-iso: Building repository 'openSUSE-Leap-15.3-2' cache [....done]
    vmware-iso: Loading repository data...
    vmware-iso: Reading installed packages...
    vmware-iso:
    vmware-iso: The following 4 items are locked and will not be changed by any action:
    vmware-iso:  Available:
    vmware-iso:   bash-completion snapper snapper-zypp-plugin virtualbox-guest-tools
    vmware-iso: Nothing to do.
==> vmware-iso: Gracefully halting virtual machine...
    vmware-iso: Waiting for VMware to clean up after itself...
==> vmware-iso: Deleting unnecessary VMware files...
    vmware-iso: Deleting: output-vmware-iso\vm.scoreboard
    vmware-iso: Deleting: output-vmware-iso\vmware.log
==> vmware-iso: Compacting all attached virtual disks...
    vmware-iso: Compacting virtual disk 1
==> vmware-iso: Cleaning VMX prior to finishing up...
    vmware-iso: Disabling VNC server...
==> vmware-iso: Skipping export of virtual machine...
Build 'vmware-iso' finished after 11 minutes 42 seconds.

==> Wait completed after 11 minutes 42 seconds

==> Builds finished. The artifacts of successful builds are:
--> vmware-iso: VM files in directory: output-vmware-iso

F:\packer-opensuse>

默认凭据

此 VM 映像的默认凭据为:

UsernamePassword
vagrantvagrant
rootvagrant
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值