微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows 10使用 Packer 和 VMware Player 自动构建CentOS 8虚拟机镜像

 下载适用于 Windows 的 qemu-img

https://cloudbase.it/downloads/qemu-img-win-x64-2_3_0.zip

下载后解压文件到 packer.exe相同的工作目录

或者安装Windows 的 qemu

qemu-w64-setup-20211215.exe

json文件中需要设置"headless": true,设置false时启动VMware Player 界面后迅速退出

{
  "variables": {
      "boot_wait": "5s","disk_size": "40960","iso_checksum": "none","iso_url": "file://F:\\CentOS-Stream-8-x86_64-20211206-dvd1.iso","memsize": "1024","numvcpus": "1","ssh_password" : "packer","ssh_username" : "packer","vm_name": "CentOS-Stream-8-x86_64-20211206"
    },"builders": [
      {
        "type": "vmware-iso","boot_command": [
          "<tab><bs><bs><bs><bs><bs>text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
        ],"boot_wait": "{{ user `boot_wait` }}","disk_size": "{{ user `disk_size` }}","disk_type_id": "0","guest_os_type": "centos-64","headless": true,"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": "30000h","vm_name": "{{ user `vm_name` }}","vmx_data": {
          "memsize": "{{ user `memsize` }}","numvcpus": "{{ user `numvcpus` }}","virtualHW.version": "14"
        }
      },{
        "type": "virtualBox-iso","guest_os_type": "RedHat_64","headless": false,"ssh_timeout": "30m","vBoxmanage": [
          ["modifyvm","{{.Name}}","--memory","{{ user `memsize` }}"],["modifyvm","--cpus","{{ user `numvcpus` }}"]
        ]
      }
    ],"provisioners": [
    {
      "type": "shell","inline": ["echo 'packer'|sudo -S df -hT"]
    }
  ]
}

ks.cfg

#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network @R_912_4045@ion
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=sda
autopart
# Partition clearing @R_912_4045@ion
clearpart --none --initlabel


reboot --eject

# System timezone
timezone America/New_York --isUtc

network --device ens160 --onboot yes --bootproto dhcp --activate --noipv6 --hostname=CentOS8Packer

# Root password
rootpw --iscrypted $6$qC9lB22EyX3vb0wO$z/XXG4NZvVMz1wEFNfwYig9t0sYhzFMIfXkNfNAoBqxjfCY1USH2POrBNowaIC5w5QG0oYFnW3Ml6Ker/718y1
user --groups=wheel --name=packer --password=$6$AFD79/F4mP11CTLX$YgD6rTSaDj/kI0OIe22JhrlhYkBxMuut3phOGjQVx6IAtYrwGiVMAkEIY7HNWIiwi3r1JxZRlYJZ8vqJUtkZN0 --iscrypted --gecos="packer"

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

运行命令packer.exe build opensuse153.json后按提示记下VNC端口和密码

运行vncviewer64-1.10.1.exe,输入IP地址端口,密码后可以查看安装过程;

输出

F:\packer-CentOS8Stream>packer.exe build -only=vmware-iso centos8.json
Warning: Warning when preparing build: "vmware-iso"

A checksum of 'none' was specified. Since ISO files are so big,a checksum is highly recommended.

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:\CentOS-Stream-8-x86_64-20211206-dvd1.iso
==> vmware-iso: Trying file://F:/CentOS-Stream-8-x86_64-20211206-dvd1.iso
==> vmware-iso: file://F:/CentOS-Stream-8-x86_64-20211206-dvd1.iso => F:/CentOS-Stream-8-x86_64-20211206-dvd1.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 8000
==> vmware-iso: Starting virtual machine...
    vmware-iso: The VM will be run headless,without a GUI. If you want to
    vmware-iso: view the screen of the VM,connect via VNC with the password "Bvpc4sFE" to
    vmware-iso: vnc://127.0.0.1:5935
==> vmware-iso: Connecting to VNC...
==> vmware-iso: Waiting 5s 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-shell4232246848
==> vmware-iso:
==> vmware-iso: 我们信任您已经从系统管理员那里了解了日常注意事项。
==> vmware-iso: 总结起来无外乎这三点:
==> vmware-iso:
==> vmware-iso:     #1) 尊重别人的隐私。
==> vmware-iso:     #2) 输入前要先考虑(后果和风险)。
==> vmware-iso:     #3) 权力越大,责任越大。
==> vmware-iso:
    vmware-iso: 文件系统            类型      容量  已用  可用 已用% 挂载点
    vmware-iso: devtmpfs            devtmpfs  386M     0  386M    0% /dev
    vmware-iso: tmpfs               tmpfs     405M     0  405M    0% /dev/shm
    vmware-iso: tmpfs               tmpfs     405M  5.7M  399M    2% /run
    vmware-iso: tmpfs               tmpfs     405M     0  405M    0% /sys/fs/cgroup
    vmware-iso: /dev/mapper/cs-root xfs        37G  1.7G   36G    5% /
    vmware-iso: /dev/sda1           xfs      1014M  181M  834M   18% /boot
==> vmware-iso: [sudo] packer 的密码:
    vmware-iso: tmpfs               tmpfs      81M     0   81M    0% /run/user/1000
==> 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\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 12 minutes 9 seconds.

==> Wait completed after 12 minutes 9 seconds

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

F:\packer-CentOS8Stream>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐