使用Packer为KVM QEMU自动化构建CentOS 8 虚拟机映像

由于CentOS 8上没有找到/usr/bin/qemu-system-x86_64

可以使用Debian 11或OpenSUSE 15.3

如下是使用Debian 11上运行qemu

{
    "variables":
    {
      "cpu": "2",
      "ram": "2048",
      "name": "centos",
      "disk_size": "40000",
      "version": "8-stream",
      "iso_checksum_type": "sha256",
      "iso_checksum": "5d61940668bc5e40d1c974e42d39f048",
      "iso_url": "/root/CentOS-Stream-8-x86_64-20211206-dvd1.iso",
      "headless": "false",
      "config_file": "ks.cfg",
      "ssh_username": "packer",
      "ssh_password": "packer",
      "destination_server": "download.goffinet.org"
    },
  "builders": [
    {  
      "name": "{{user `name`}}{{user `version`}}",
      "type": "qemu",
      "format": "qcow2",
      "accelerator": "kvm",
      "qemu_binary": "/usr/bin/qemu-system-x86_64",
      "net_device": "virtio-net",
      "disk_interface": "virtio",
      "disk_cache": "none",
      "qemuargs": [[ "-m", "{{user `ram`}}M" ],[ "-smp", "{{user `cpu`}}" ]],
      "ssh_wait_timeout": "3000h",
      "http_directory": "http",
      "ssh_username": "{{user `ssh_username`}}",
      "ssh_password": "{{user `ssh_password`}}",
      "iso_url": "{{user `iso_url`}}",
      "iso_checksum": "{{user `iso_checksum`}}",
      "boot_wait": "40s",
      "boot_command": [
        " net.ifnames=0 biosdevname=0 text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/{{user `config_file`}}"
      ],
      "disk_size": "{{user `disk_size`}}",
      "disk_discard": "unmap",
      "disk_compression": true,
      "headless": "{{user `headless`}}",
      "shutdown_command": "shutdown -h now",
      "output_directory": "artifacts/qemu/{{user `name`}}{{user `version`}}"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'",
      "inline": [
        "dnf -y update"
      ]
    }
  ]
}

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 information
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

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

# System bootloader configuration
bootloader --location=mbr --append="crashkernel=no"
# Clear the Master Boot Record
zerombr
# Remove partitions
clearpart --all --initlabel
# Automatically create partitions using LVM
autopart --type=lvm


reboot --eject

# System timezone
timezone Asia/Shanghai --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为KVM QEMU自动化构建CentOS 8 虚拟机映像_第1张图片

使用Packer为KVM QEMU自动化构建CentOS 8 虚拟机映像_第2张图片

使用Packer为KVM QEMU自动化构建CentOS 8 虚拟机映像_第3张图片

使用Packer为KVM QEMU自动化构建CentOS 8 虚拟机映像_第4张图片

使用Packer为KVM QEMU自动化构建CentOS 8 虚拟机映像_第5张图片

你可能感兴趣的:(自动化,centos,ssh)