├── .gitignore ├── LICENSE ├── README.md ├── centos8.json ├── centos8_uefi.json ├── http └── ks.cfg └── scripts ├── cleanup.sh └── setup.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # Cache objects 2 | packer_cache/ 3 | output-virtualbox-iso/ 4 | output-vmware-iso/ 5 | 6 | # For built boxes 7 | *.box 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 eaksel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # packer-CentOS8 2 | 3 | ## What is packer-CentOS8 ? 4 | 5 | packer-CentOS8 is a set of configuration files used to build an automated CentOS 8 virtual machine images using [Packer](https://www.packer.io/). 6 | This Packer configuration file allows you to build images for VMware Workstation and Oracle VM VirtualBox. 7 | 8 | ## Prerequisites 9 | 10 | - [Packer](https://www.packer.io/downloads.html) 11 | - 12 | - A Hypervisor 13 | - [VMware Workstation](https://www.vmware.com/products/workstation-pro.html) 14 | - [Oracle VM VirtualBox](https://www.virtualbox.org/) 15 | 16 | ## How to use Packer 17 | 18 | Commands to create an automated VM image: 19 | 20 | To create a CentOS 8 VM image using VMware Workstation use the following commands: 21 | 22 | ```cmd 23 | cd c:\packer-CentOS8 24 | packer build -only=vmware-iso centos8.json 25 | ``` 26 | 27 | To create a CentOS 8 VM image using Oracle VM VirtualBox use the following commands: 28 | 29 | ```cmd 30 | cd c:\packer-CentOS8 31 | packer build -only=virtualbox-iso centos8.json 32 | ``` 33 | 34 | *If you omit the keyword "-only=" both the Workstation and Virtualbox VMs will be created.* 35 | 36 | By default the .iso of CentOS 8 is pulled from 37 | 38 | You can change the URL to one closer to your build server. To do so change the **"iso_url"** parameter in the **"variables"** section of the centos8.json file. 39 | 40 | ```json 41 | { 42 | "variables": { 43 | "iso_url": "http://miroir.univ-paris13.fr/centos/8/isos/x86_64/CentOS-8.1.1911-x86_64-boot.iso" 44 | } 45 | ``` 46 | 47 | ## Keyboard configuration 48 | 49 | By default the keyboard is set to be US qwerty. 50 | To switch it to something else edit the following file: 51 | 52 | - ./http/ks.cfg 53 | 54 | Set the `keyboard` parameter as desired, for example: `keyboard --vckeymap=fr --xlayouts='fr'` 55 | 56 | ## Default credentials 57 | 58 | The default credentials for this VM image are: 59 | 60 | |Username|Password| 61 | |--------|--------| 62 | |packer|packer| 63 | |root|packer| 64 | -------------------------------------------------------------------------------- /centos8.json: -------------------------------------------------------------------------------- 1 | { 2 | "variables": { 3 | "boot_wait": "5s", 4 | "disk_size": "40960", 5 | "iso_checksum": "9602c69c52d93f51295c0199af395ca0edbe35e36506e32b8e749ce6c8f5b60a", 6 | "iso_url": "https://vault.centos.org/8.5.2111/isos/x86_64/CentOS-8.5.2111-x86_64-boot.iso", 7 | "memsize": "1024", 8 | "numvcpus": "1", 9 | "ssh_password" : "packer", 10 | "ssh_username" : "packer", 11 | "vm_name": "CentOS-8-x86_64-2111" 12 | }, 13 | "builders": [ 14 | { 15 | "type": "vmware-iso", 16 | "boot_command": [ 17 | "inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg" 18 | ], 19 | "boot_wait": "{{ user `boot_wait` }}", 20 | "disk_size": "{{ user `disk_size` }}", 21 | "disk_type_id": "0", 22 | "guest_os_type": "centos-64", 23 | "headless": false, 24 | "http_directory": "http", 25 | "iso_checksum": "{{ user `iso_checksum` }}", 26 | "iso_url": "{{ user `iso_url` }}", 27 | "shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p", 28 | "ssh_password": "{{ user `ssh_password` }}", 29 | "ssh_port": 22, 30 | "ssh_username": "{{ user `ssh_username` }}", 31 | "ssh_timeout": "30m", 32 | "vm_name": "{{ user `vm_name` }}", 33 | "vmx_data": { 34 | "memsize": "{{ user `memsize` }}", 35 | "numvcpus": "{{ user `numvcpus` }}", 36 | "virtualHW.version": "14" 37 | } 38 | }, 39 | { 40 | "type": "virtualbox-iso", 41 | "boot_command": [ 42 | "inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg" 43 | ], 44 | "boot_wait": "{{ user `boot_wait` }}", 45 | "disk_size": "{{ user `disk_size` }}", 46 | "guest_os_type": "RedHat_64", 47 | "headless": false, 48 | "http_directory": "http", 49 | "iso_checksum": "{{ user `iso_checksum` }}", 50 | "iso_url": "{{ user `iso_url` }}", 51 | "shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p", 52 | "ssh_password": "{{ user `ssh_password` }}", 53 | "ssh_port": 22, 54 | "ssh_username": "{{ user `ssh_username` }}", 55 | "ssh_timeout": "30m", 56 | "vm_name": "{{ user `vm_name` }}", 57 | "vboxmanage": [ 58 | ["modifyvm", "{{.Name}}", "--memory", "{{ user `memsize` }}"], 59 | ["modifyvm", "{{.Name}}", "--cpus", "{{ user `numvcpus` }}"] 60 | ] 61 | } 62 | ], 63 | "provisioners": [ 64 | { 65 | "type": "shell", 66 | "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'", 67 | "inline": [ 68 | "dnf -y update", 69 | "dnf -y install python3", 70 | "python3 -m pip install --upgrade pip", 71 | "alternatives --set python /usr/bin/python3", 72 | "pip3 install ansible" 73 | ] 74 | }, 75 | { 76 | "type": "ansible-local", 77 | "playbook_file": "scripts/setup.yml" 78 | }, 79 | { 80 | "type": "shell", 81 | "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'", 82 | "scripts": [ 83 | "scripts/cleanup.sh" 84 | ] 85 | } 86 | ] 87 | } 88 | -------------------------------------------------------------------------------- /centos8_uefi.json: -------------------------------------------------------------------------------- 1 | { 2 | "variables": { 3 | "boot_wait": "5s", 4 | "disk_size": "40960", 5 | "iso_checksum": "9602c69c52d93f51295c0199af395ca0edbe35e36506e32b8e749ce6c8f5b60a", 6 | "iso_url": "https://vault.centos.org/8.5.2111/isos/x86_64/CentOS-8.5.2111-x86_64-boot.iso", 7 | "memsize": "1024", 8 | "numvcpus": "1", 9 | "ssh_password" : "packer", 10 | "ssh_username" : "packer", 11 | "vm_name": "CentOS-8-x86_64-2111" 12 | }, 13 | "builders": [ 14 | { 15 | "type": "vmware-iso", 16 | "boot_command": ["einst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfgx"], 17 | "boot_wait": "{{ user `boot_wait` }}", 18 | "disk_size": "{{ user `disk_size` }}", 19 | "disk_type_id": "0", 20 | "guest_os_type": "centos-64", 21 | "headless": false, 22 | "http_directory": "http", 23 | "iso_checksum": "{{ user `iso_checksum` }}", 24 | "iso_url": "{{ user `iso_url` }}", 25 | "shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p", 26 | "ssh_password": "{{ user `ssh_password` }}", 27 | "ssh_port": 22, 28 | "ssh_username": "{{ user `ssh_username` }}", 29 | "ssh_timeout": "30m", 30 | "vm_name": "{{ user `vm_name` }}", 31 | "vmx_data": { 32 | "memsize": "{{ user `memsize` }}", 33 | "numvcpus": "{{ user `numvcpus` }}", 34 | "virtualHW.version": "14", 35 | "firmware": "efi" 36 | } 37 | }, 38 | { 39 | "type": "virtualbox-iso", 40 | "boot_command": ["einst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfgx"], 41 | "boot_wait": "{{ user `boot_wait` }}", 42 | "disk_size": "{{ user `disk_size` }}", 43 | "guest_os_type": "RedHat_64", 44 | "headless": false, 45 | "http_directory": "http", 46 | "iso_checksum": "{{ user `iso_checksum` }}", 47 | "iso_url": "{{ user `iso_url` }}", 48 | "shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p", 49 | "ssh_password": "{{ user `ssh_password` }}", 50 | "ssh_port": 22, 51 | "ssh_username": "{{ user `ssh_username` }}", 52 | "ssh_timeout": "30m", 53 | "vm_name": "{{ user `vm_name` }}", 54 | "iso_interface": "sata", 55 | "vboxmanage": [ 56 | ["modifyvm", "{{.Name}}", "--memory", "{{ user `memsize` }}"], 57 | ["modifyvm", "{{.Name}}", "--cpus", "{{ user `numvcpus` }}"], 58 | ["modifyvm", "{{.Name}}", "--firmware", "EFI"] 59 | ] 60 | } 61 | ], 62 | "provisioners": [ 63 | { 64 | "type": "shell", 65 | "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'", 66 | "inline": [ 67 | "dnf -y update", 68 | "dnf -y install python3", 69 | "python3 -m pip install --upgrade pip", 70 | "alternatives --set python /usr/bin/python3", 71 | "pip3 install ansible" 72 | ] 73 | }, 74 | { 75 | "type": "ansible-local", 76 | "playbook_file": "scripts/setup.yml" 77 | }, 78 | { 79 | "type": "shell", 80 | "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'", 81 | "scripts": [ 82 | "scripts/cleanup.sh" 83 | ] 84 | } 85 | ] 86 | } 87 | -------------------------------------------------------------------------------- /http/ks.cfg: -------------------------------------------------------------------------------- 1 | # https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/performing_an_advanced_rhel_installation/kickstart-commands-and-options-reference_installing-rhel-as-an-experienced-user 2 | 3 | # License agreement 4 | eula --agreed 5 | # Use network installation 6 | url --url="http://mirror.centos.org/centos/8/BaseOS/x86_64/os/" 7 | repo --name="AppStream" --baseurl=http://mirror.centos.org/centos/8/BaseOS/x86_64/os/../../../AppStream/x86_64/os/ 8 | # Use text mode install 9 | text 10 | # Disable Initial Setup on first boot 11 | firstboot --disable 12 | # Keyboard layout 13 | keyboard --vckeymap=us --xlayouts='us' 14 | # System language 15 | lang en_US.UTF-8 16 | # Network information 17 | network --bootproto=dhcp --device=link --activate 18 | network --hostname=centos8.localdomain 19 | # Root password 20 | rootpw $1$+xLTvuVv$vAMwt4RuJqO3qp9nLQj1U0 --iscrypted 21 | # SELinux configuration 22 | selinux --enforcing 23 | # Do not configure the X Window System 24 | skipx 25 | # System timezone 26 | timezone Europe/Paris --isUtc 27 | # Add a user named packer 28 | user --groups=wheel --name=packer --password=$6$Jaa5U0EwAPMMp3.5$m29yTwr0q9ZJVJGMXvOnm9q2z13ldUFTjB1sxPHvaiW4upMSwQ50181wl7SjHjh.BTH7FGHx37wrX..SM0Bqq. --iscrypted --gecos="packer" 29 | # System bootloader configuration 30 | bootloader --location=mbr --append="crashkernel=auto" 31 | # Clear the Master Boot Record 32 | zerombr 33 | # Remove partitions 34 | clearpart --all --initlabel 35 | # Automatically create partitions using LVM 36 | autopart --type=lvm 37 | # Reboot after successful installation 38 | reboot 39 | 40 | %packages --ignoremissing 41 | # dnf group info minimal-environment 42 | @^minimal-environment 43 | # Exclude unnecessary firmwares 44 | -iwl*firmware 45 | %end 46 | 47 | %post --nochroot --logfile=/mnt/sysimage/root/ks-post.log 48 | # Disable quiet boot and splash screen 49 | sed --follow-symlinks -i "s/ rhgb quiet//" /mnt/sysimage/etc/default/grub 50 | sed --follow-symlinks -i "s/ rhgb quiet//" /mnt/sysimage/boot/grub2/grubenv 51 | 52 | # Passwordless sudo for the user 'packer' 53 | echo "packer ALL=(ALL) NOPASSWD: ALL" >> /mnt/sysimage/etc/sudoers.d/packer 54 | %end -------------------------------------------------------------------------------- /scripts/cleanup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eux 2 | 3 | pip3 uninstall -y ansible 4 | 5 | dnf clean all 6 | 7 | # Zero out the rest of the free space using dd, then delete the written file. 8 | dd if=/dev/zero of=/EMPTY bs=1M 9 | rm -f /EMPTY 10 | 11 | # Add `sync` so Packer doesn't quit too early, before the large file is deleted. 12 | sync -------------------------------------------------------------------------------- /scripts/setup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Setup.yml" 3 | hosts: localhost 4 | gather_facts: yes 5 | become: yes 6 | tasks: 7 | - name: Colorize root shell prompt 8 | lineinfile: 9 | path: /root/.bashrc 10 | line: 'export PS1="\[\033[38;5;11m\]\u\[$(tput sgr0)\]\[\033[38;5;15m\]@\h:\[$(tput sgr0)\]\[\033[38;5;6m\][\w]:\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]"' 11 | state: present 12 | 13 | - name: Alias vi to vim 14 | lineinfile: 15 | path: /root/.bashrc 16 | line: "alias vi='vim'" 17 | state: present 18 | 19 | - name: Create vim configuration file 20 | file: 21 | path: /root/.vimrc 22 | state: touch 23 | owner: root 24 | group: root 25 | mode: 0644 26 | 27 | - name: Configure vim 28 | blockinfile: 29 | path: /root/.vimrc 30 | block: | 31 | set nocompatible 32 | set number 33 | set autoindent 34 | syntax on 35 | highlight Comment ctermfg=LightCyan 36 | marker: "" 37 | state: present 38 | 39 | - name: Disable GSSAPIAuthentication 40 | lineinfile: 41 | path: /etc/ssh/sshd_config 42 | regexp: "^GSSAPIAuthentication" 43 | line: "GSSAPIAuthentication no" 44 | state: present 45 | 46 | - name: Update CA bundle (RedHat) 47 | get_url: 48 | url: https://curl.haxx.se/ca/cacert.pem 49 | dest: /etc/pki/tls/certs/ca-bundle.crt 50 | force: yes 51 | when: ansible_os_family == "RedHat" 52 | 53 | - name: Install epel repo (RedHat) 54 | dnf: 55 | name: "epel-release" 56 | state: latest 57 | when: ansible_os_family == "RedHat" 58 | 59 | - name: Upgrade all packages (RedHat) 60 | dnf: 61 | name: "*" 62 | state: latest 63 | when: ansible_os_family == "RedHat" 64 | 65 | - name: Install packages (RedHat) 66 | dnf: 67 | name: "{{ packages }}" 68 | state: latest 69 | vars: 70 | packages: 71 | - bash-completion 72 | - curl 73 | - git 74 | - rsync 75 | - screen 76 | - tcpdump 77 | - vim 78 | - wget 79 | when: ansible_os_family == "RedHat" 80 | 81 | - name: Install dependencies for VirtualBox Guest Additions (RedHat) 82 | dnf: 83 | name: "{{ packages }}" 84 | state: latest 85 | vars: 86 | packages: 87 | - bzip2 88 | - dkms 89 | - kernel-devel 90 | - kernel-headers 91 | - gcc 92 | - make 93 | - perl 94 | when: 95 | - ansible_os_family == "RedHat" 96 | - ansible_virtualization_type == "virtualbox" 97 | 98 | - name: Mount VBoxGuestAdditions.iso 99 | mount: 100 | path: /mnt 101 | src: /home/packer/VBoxGuestAdditions.iso 102 | fstype: iso9660 103 | opts: ro,loop 104 | state: mounted 105 | when: ansible_virtualization_type == "virtualbox" 106 | 107 | - name: Install VBoxGuestAdditions.iso 108 | command: sh /mnt/VBoxLinuxAdditions.run 109 | when: ansible_virtualization_type == "virtualbox" 110 | 111 | - name: Umount VBoxGuestAdditions.iso 112 | mount: 113 | path: /mnt 114 | state: absent 115 | when: ansible_virtualization_type == "virtualbox" 116 | 117 | - name: Delete VBoxGuestAdditions.iso 118 | file: 119 | path: /home/packer/VBoxGuestAdditions.iso 120 | state: absent 121 | when: ansible_virtualization_type == "virtualbox" 122 | 123 | - name: Install open-vm-tools (RedHat) 124 | dnf: 125 | name: open-vm-tools 126 | state: latest 127 | when: 128 | - ansible_os_family == "RedHat" 129 | - ansible_virtualization_type == "VMware" 130 | --------------------------------------------------------------------------------