├── Ubuntu.20.04 ├── http │ ├── meta-data │ └── user-data ├── bash │ ├── apt.sh │ └── cleanup.sh ├── vars.pkr.hcl ├── auto.pkrvars.hcl └── builder.pkr.hcl ├── .gitattributes ├── RHEL8 ├── http │ └── kickstart.cfg ├── vars.pkr.hcl ├── auto.pkrvars.hcl ├── bash │ └── prep.sh └── builder.pkr.hcl ├── RockyLinux.8.5 ├── http │ └── kickstart.cfg ├── bash │ └── prep.sh ├── vars.pkr.hcl ├── auto.pkrvars.hcl └── builder.pkr.hcl └── README.md /Ubuntu.20.04/http/meta-data: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Ubuntu.20.04/bash/apt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -x 5 | 6 | # Prevents popup questions 7 | export DEBIAN_FRONTEND="noninteractive" 8 | 9 | sudo apt-get update -y 10 | sudo apt-get upgrade -y 11 | -------------------------------------------------------------------------------- /RHEL8/http/kickstart.cfg: -------------------------------------------------------------------------------- 1 | lang en_US 2 | keyboard no 3 | timezone Europe/Oslo --isUtc 4 | rootpw hashed-password --iscrypted 5 | #platform x86, AMD64, or Intel EM64T 6 | reboot 7 | text 8 | cdrom 9 | bootloader --location=mbr --append="rhgb quiet crashkernel=auto" 10 | zerombr 11 | clearpart --all --initlabel 12 | autopart 13 | auth --passalgo=sha512 --useshadow 14 | user --name=username --password=password 15 | selinux --disabled 16 | firewall --enabled 17 | skipx 18 | firstboot --disable 19 | %post 20 | usermod -aG wheel username 21 | open-vm-tools -y 22 | %end 23 | %packages 24 | @^server-product-environment 25 | @development 26 | %end 27 | -------------------------------------------------------------------------------- /Ubuntu.20.04/http/user-data: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | autoinstall: 3 | version: 1 4 | early-commands: 5 | - sudo systemctl stop ssh 6 | locale: en_US 7 | keyboard: 8 | layout: en 9 | variant: us 10 | identity: 11 | hostname: ubuntu.template 12 | username: username 13 | password: 'hashed-password' 14 | ssh: 15 | install-server: yes 16 | allow-pw: yes 17 | storage: 18 | layout: 19 | name: direct 20 | apt: 21 | primary: 22 | - arches: [i386, amd64] 23 | uri: "http://ro.archive.ubuntu.com/ubuntu/" 24 | user-data: 25 | disable_root: false 26 | packages: 27 | - open-vm-tools 28 | - cloud-init -------------------------------------------------------------------------------- /RockyLinux.8.5/http/kickstart.cfg: -------------------------------------------------------------------------------- 1 | lang en_US 2 | keyboard no 3 | timezone Europe/Oslo --isUtc 4 | rootpw $hashed-passwd --iscrypted 5 | #platform x86, AMD64, or Intel EM64T 6 | reboot 7 | text 8 | cdrom 9 | bootloader --location=mbr --append="rhgb quiet crashkernel=auto" 10 | zerombr 11 | clearpart --all --initlabel 12 | autopart 13 | auth --passalgo=sha512 --useshadow 14 | user --name=username --password=passwd 15 | selinux --disabled 16 | firewall --enabled 17 | skipx 18 | firstboot --disable 19 | %post 20 | usermod -aG wheel username 21 | yum update -y 22 | yum upgrade -y 23 | open-vm-tools -y 24 | %end 25 | %packages 26 | @^server-product-environment 27 | @development 28 | %end 29 | -------------------------------------------------------------------------------- /RockyLinux.8.5/bash/prep.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -x 5 | 6 | ### Update 7 | echo "Starting update of the system!" 8 | yum -y update 9 | yum -y upgrade 10 | 11 | echo "Starting cleanup job..." 12 | ### Clean the /tmp directories. ### 13 | echo '> Cleaning the /tmp directories ...' 14 | rm -rf /tmp/* 15 | rm -rf /var/tmp/* 16 | rm -rf /var/log/rhsm/* 17 | rm -rf /var/cache/dnf/* 18 | 19 | ### Sets the hostname to localhost. ### 20 | echo '> Setting the hostname to localhost ...' 21 | cat /dev/null > /etc/hostname 22 | hostnamectl set-hostname localhost 23 | 24 | ### Clean the dnf cache. ### 25 | echo '> Cleaning the cache ...' 26 | dnf clean all 27 | 28 | ### Clean the machine-id. ### 29 | echo '> Cleaning the machine-id ...' 30 | truncate -s 0 /etc/machine-id 31 | rm /var/lib/dbus/machine-id 32 | ln -s /etc/machine-id /var/lib/dbus/machine-id -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Packer-vsphere Builder 2 | 3 | Packer vsphere builder is official supported by Hashicorp. 4 | 5 | Tested on:
6 | Packer - 1.7.6
7 | Packer - 1.7.8
8 | Packer - 1.8.0
9 |
10 | Platform:
11 | vSphere - 6.7
12 |
13 | OS:
14 | Ubuntu 20.04
15 | RHEL8 (8.4, 8.5, 8.6)
16 | Rocky Linux 8.5
17 |
18 |
19 | Resources:
20 | https://www.packer.io/docs/builders/vsphere
21 | https://ubuntu.com/download/server
22 | RHEL kickstart documentation
23 | https://access.redhat.com/labsinfo/kickstartconfig
24 | https://rockylinux.org/
25 |
26 | These templates are ready for use with Terraform. :) 27 | -------------------------------------------------------------------------------- /RHEL8/vars.pkr.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | variable "vcenter_server" {} 3 | variable "vcenter_username" {} 4 | variable "vcenter_password" {} 5 | variable "vcenter_cluster" {} 6 | variable "vcenter_datacenter" {} 7 | variable "vcenter_host" {} 8 | variable "vcenter_datastore" {} 9 | variable "vcenter_folder" {} 10 | 11 | #Vm Info 12 | variable "http_directory" {} 13 | variable "connection_username" {} 14 | variable "connection_password" {} 15 | variable "ssh_port" {} 16 | variable "os_version" {} 17 | variable "iso_file" {} 18 | variable "iso_checksum" {} 19 | variable "cdrom_type" {} 20 | variable "guest_os_type" {} 21 | variable "root_disk_size" { 22 | default = 15000 23 | } 24 | variable "nic_type" { 25 | default = "vmxnet3" 26 | } 27 | variable "vm_network" {} 28 | variable "num_cpu" { 29 | default = 1 30 | } 31 | variable "num_cores" { 32 | default = 1 33 | } 34 | variable "vm_ram" { 35 | default = 4096 36 | } 37 | variable "os_family" {} 38 | variable "boot_command" {} 39 | 40 | #Other Info: 41 | #variable "Output_Directory" {} 42 | variable "ip_wait" {} 43 | variable "ssh_timeout" {} 44 | variable "ssh_handshakes" {} 45 | variable "shutdown_timeout" {} -------------------------------------------------------------------------------- /RockyLinux.8.5/vars.pkr.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | variable "vcenter_server" {} 3 | variable "vcenter_username" {} 4 | variable "vcenter_password" {} 5 | variable "vcenter_cluster" {} 6 | variable "vcenter_datacenter" {} 7 | variable "vcenter_host" {} 8 | variable "vcenter_datastore" {} 9 | variable "vcenter_folder" {} 10 | 11 | #Vm Info 12 | variable "http_directory" {} 13 | variable "connection_username" {} 14 | variable "connection_password" {} 15 | variable "ssh_port" {} 16 | variable "os_version" {} 17 | variable "iso_url" {} 18 | variable "iso_checksum" {} 19 | variable "cdrom_type" {} 20 | variable "guest_os_type" {} 21 | variable "root_disk_size" { 22 | default = 15000 23 | } 24 | variable "nic_type" { 25 | default = "vmxnet3" 26 | } 27 | variable "vm_network" {} 28 | variable "num_cpu" { 29 | default = 2 30 | } 31 | variable "num_cores" { 32 | default = 1 33 | } 34 | variable "vm_ram" { 35 | default = 4096 36 | } 37 | variable "os_family" {} 38 | variable "boot_command" {} 39 | 40 | #Other Info: 41 | #variable "Output_Directory" {} 42 | variable "ip_wait" {} 43 | variable "ssh_timeout" {} 44 | variable "ssh_handshakes" {} 45 | variable "shutdown_timeout" {} -------------------------------------------------------------------------------- /RHEL8/auto.pkrvars.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | vcenter_server = "vcenter-ip" 3 | vcenter_username = "administrator@vsphere.local" 4 | vcenter_cluster = "cluster-name" 5 | vcenter_datacenter = "dc-name" 6 | vcenter_host = "esxi-host-ip" 7 | vcenter_datastore = "storage" 8 | vcenter_folder = "Templates" 9 | vcenter_password = "vcenter-pass" 10 | vm_network = "network-name" 11 | 12 | 13 | 14 | #VM Info Ubuntu: 15 | http_directory = "http" 16 | os_version = "8" 17 | os_family = "linux" 18 | guest_os_type = "rhel8_64Guest" 19 | cdrom_type = "sata" 20 | iso_file = "iso-directory" 21 | iso_checksum = "iso-checksum" 22 | root_disk_size = 15000 23 | connection_username = "ssh-username" 24 | connection_password = "ssh-password" 25 | boot_command = [ 26 | "", 27 | "vmlinuz initrd=initrd.img inst.geoloc=0 rd.driver.blacklist=dm-multipath net.ifnames=0 biosdevname=0 ", 28 | "ks=http://{{.HTTPIP}}:{{.HTTPPort}}/kickstart.cfg", 29 | "" 30 | ] 31 | 32 | #Timeouts: 33 | ip_wait = "20m" 34 | ssh_timeout = "20m" 35 | ssh_port = "22" 36 | ssh_handshakes = "40" 37 | shutdown_timeout = "15m" -------------------------------------------------------------------------------- /Ubuntu.20.04/vars.pkr.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | variable "vcenter_server" {} 3 | variable "vcenter_username" {} 4 | variable "vcenter_password" {} 5 | variable "vcenter_cluster" {} 6 | variable "vcenter_datacenter" {} 7 | variable "vcenter_host" {} 8 | variable "vcenter_datastore" {} 9 | variable "vcenter_folder" {} 10 | 11 | #Vm Info 12 | variable "http_directory" {} 13 | variable "connection_username" {} 14 | variable "connection_password" {} 15 | variable "ssh_port" {} 16 | variable "os_version" {} 17 | variable "iso_url" {} 18 | variable "iso_checksum" {} 19 | variable "cdrom_type" {} 20 | variable "guest_os_type" {} 21 | variable "root_disk_size" { 22 | default = 15000 23 | } 24 | variable "nic_type" { 25 | default = "vmxnet3" 26 | } 27 | variable "vm_network" { } 28 | variable "num_cpu" { 29 | default = 1 30 | } 31 | variable "num_cores" { 32 | default = 1 33 | } 34 | variable "vm_ram" { 35 | default = 4096 36 | } 37 | variable "os_family" { 38 | description = "OS Family builds the paths needed for packer" 39 | } 40 | variable "boot_command" {} 41 | 42 | #Other Info: 43 | #variable "Output_Directory" {} 44 | variable "ip_wait" {} 45 | variable "ssh_timeout" {} 46 | variable "ssh_handshakes" {} 47 | variable "shutdown_timeout" {} -------------------------------------------------------------------------------- /Ubuntu.20.04/auto.pkrvars.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | vcenter_server = "vcenter-ip" 3 | vcenter_username = "administrator@vsphere.local" 4 | vcenter_cluster = "clustername" 5 | vcenter_datacenter = "dc-name" 6 | vcenter_host = "esxi host" 7 | vcenter_datastore = "storage" 8 | vcenter_folder = "Templates" 9 | vcenter_password = "vcenter-password" 10 | vm_network = "networkname" 11 | 12 | #VM Info Ubuntu: 13 | http_directory = "http" 14 | os_version = "20.04" 15 | os_family = "ubuntu" 16 | guest_os_type = "ubuntu64Guest" 17 | cdrom_type = "sata" 18 | iso_url = "https://releases.ubuntu.com/focal/ubuntu-20.04.3-live-server-amd64.iso" 19 | iso_checksum = "f8e3086f3cea0fb3fefb29937ab5ed9d19e767079633960ccb50e76153effc98" 20 | root_disk_size = 24000 21 | connection_username = "username" 22 | connection_password = "password" 23 | boot_command = [ 24 | "", 25 | "", 26 | "/casper/vmlinuz ", 27 | "initrd=/casper/initrd ", 28 | "autoinstall ", 29 | "" 30 | ] 31 | 32 | #Other: 33 | #Output_Directory = "your-dirPacker/Templates" 34 | 35 | #Timeouts: 36 | ip_wait = "20m" 37 | ssh_timeout = "20m" 38 | ssh_port = "22" 39 | ssh_handshakes = "40" 40 | shutdown_timeout = "15m" -------------------------------------------------------------------------------- /RockyLinux.8.5/auto.pkrvars.hcl: -------------------------------------------------------------------------------- 1 | #vCenter Info 2 | vcenter_server = "vcenter-ip" 3 | vcenter_username = "administrator@vsphere.local" 4 | vcenter_cluster = "vcenter-cluster" 5 | vcenter_datacenter = "vcenter-dc" 6 | vcenter_host = "esxi-host-ip" 7 | vcenter_datastore = "storage" 8 | vcenter_folder = "Templates" 9 | vcenter_password = "vcenter-password" 10 | vm_network = "vcenter-network" 11 | 12 | #VM Info: 13 | http_directory = "http" 14 | os_version = "8" 15 | os_family = "rocky-linux" 16 | guest_os_type = "centos8_64Guest" 17 | cdrom_type = "sata" 18 | iso_url = "https://download.rockylinux.org/pub/rocky/8/isos/x86_64/Rocky-8.5-x86_64-dvd1.iso" 19 | iso_checksum = "0081f8b969d0cef426530f6d618b962c7a01e71eb12a40581a83241f22dfdc25" 20 | iso_checksum_type = "sha256" 21 | root_disk_size = 20000 22 | connection_username = "username" 23 | connection_password = "password" 24 | boot_command = [ 25 | "", 26 | "vmlinuz initrd=initrd.img inst.geoloc=0 rd.driver.blacklist=dm-multipath net.ifnames=0 biosdevname=0 ", 27 | "ks=http://{{.HTTPIP}}:{{.HTTPPort}}/kickstart.cfg", 28 | "" 29 | ] 30 | 31 | #Timeouts: 32 | ip_wait = "20m" 33 | ssh_timeout = "20m" 34 | ssh_port = "22" 35 | ssh_handshakes = "40" 36 | shutdown_timeout = "15m" -------------------------------------------------------------------------------- /RHEL8/bash/prep.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### Update and Upgrade the system 4 | echo "Updating System..." 5 | yum -y update 6 | yum -y upgrade 7 | 8 | ### Create a clean script. ### 9 | echo "Starting Cleanup..." 10 | 11 | ### Cleans the audit logs. ### 12 | echo '> Cleaning the audit logs ...' 13 | if [ -f /var/log/audit/audit.log ]; then 14 | cat /dev/null > /var/log/audit/audit.log 15 | fi 16 | if [ -f /var/log/wtmp ]; then 17 | cat /dev/null > /var/log/wtmp 18 | fi 19 | if [ -f /var/log/lastlog ]; then 20 | cat /dev/null > /var/log/lastlog 21 | fi 22 | 23 | ### Cleans the persistent udev rules. ### 24 | echo '> Cleaning persistent udev rules ...' 25 | if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then 26 | rm /etc/udev/rules.d/70-persistent-net.rules 27 | fi 28 | 29 | ### Clean the /tmp directories. ### 30 | echo '> Cleaning the /tmp directories ...' 31 | rm -rf /tmp/* 32 | rm -rf /var/tmp/* 33 | rm -rf /var/log/rhsm/* 34 | rm -rf /var/cache/dnf/* 35 | 36 | ### Clean the SSH keys. ### 37 | echo '> Cleaning the SSH keys ...' 38 | rm -f /etc/ssh/ssh_host_* 39 | 40 | ### Sets the hostname to localhost. ### 41 | echo '> Setting the hostname to localhost ...' 42 | cat /dev/null > /etc/hostname 43 | hostnamectl set-hostname localhost 44 | 45 | ### Clean the dnf cache. ### 46 | echo '> Cleaning the cache ...' 47 | dnf clean all 48 | 49 | ### Clean the machine-id. ### 50 | echo '> Cleaning the machine-id ...' 51 | truncate -s 0 /etc/machine-id 52 | rm /var/lib/dbus/machine-id 53 | ln -s /etc/machine-id /var/lib/dbus/machine-id 54 | 55 | ### Clean the shell history. ### 56 | echo '> Cleaning the shell history ...' 57 | unset HISTFILE 58 | history -cw 59 | echo > ~/.bash_history 60 | rm -fr /root/.bash_history 61 | -------------------------------------------------------------------------------- /RockyLinux.8.5/builder.pkr.hcl: -------------------------------------------------------------------------------- 1 | source "vsphere-iso" "rocky" { 2 | # vCenter settings 3 | vcenter_server = var.vcenter_server 4 | username = var.vcenter_username 5 | password = var.vcenter_password 6 | insecure_connection = true 7 | cluster = var.vcenter_cluster 8 | datacenter = var.vcenter_datacenter 9 | host = var.vcenter_host 10 | datastore = var.vcenter_datastore 11 | folder = var.vcenter_folder 12 | convert_to_template = true 13 | 14 | #Connection 15 | ip_wait_timeout = var.ip_wait 16 | ssh_username = var.connection_username 17 | ssh_password = var.connection_password 18 | ssh_timeout = var.ssh_timeout 19 | ssh_port = var.ssh_port 20 | ssh_handshake_attempts = var.ssh_handshakes 21 | 22 | #VM Settings 23 | shutdown_timeout = var.shutdown_timeout 24 | iso_url = var.iso_url 25 | iso_checksum = var.iso_checksum 26 | vm_name = "${ var.os_family }-${ var.os_version }-{{ isotime \"2006-01-02\" }}" 27 | guest_os_type = var.guest_os_type 28 | http_directory = var.http_directory 29 | disk_controller_type = ["pvscsi"] 30 | network_adapters { 31 | network = var.vm_network 32 | network_card = var.nic_type 33 | } 34 | storage { 35 | disk_size = var.root_disk_size 36 | disk_thin_provisioned = true 37 | } 38 | CPUs = var.num_cpu 39 | cpu_cores = var.num_cores 40 | CPU_hot_plug = true 41 | RAM = var.vm_ram 42 | RAM_hot_plug = true 43 | boot_wait = "5s" 44 | boot_command = var.boot_command 45 | } 46 | 47 | 48 | build { 49 | sources = [ 50 | "source.vsphere-iso.rocky", 51 | ] 52 | provisioner "shell" { 53 | execute_command = "echo '${var.connection_password}' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'" 54 | scripts = [ 55 | "bash/prep.sh" 56 | ] 57 | } 58 | } -------------------------------------------------------------------------------- /RHEL8/builder.pkr.hcl: -------------------------------------------------------------------------------- 1 | source "vsphere-iso" "rhel" { 2 | # vCenter settings 3 | vcenter_server = var.vcenter_server 4 | username = var.vcenter_username 5 | password = var.vcenter_password 6 | insecure_connection = true 7 | cluster = var.vcenter_cluster 8 | datacenter = var.vcenter_datacenter 9 | host = var.vcenter_host 10 | datastore = var.vcenter_datastore 11 | folder = var.vcenter_folder 12 | convert_to_template = true 13 | 14 | # VM Settings 15 | http_directory = var.http_directory 16 | ip_wait_timeout = var.ip_wait 17 | ssh_username = var.connection_username 18 | ssh_password = var.connection_password 19 | ssh_timeout = var.ssh_timeout 20 | ssh_port = var.ssh_port 21 | ssh_handshake_attempts = var.ssh_handshakes 22 | shutdown_timeout = var.shutdown_timeout 23 | iso_paths = [ 24 | var.iso_file, 25 | "[NAS] iso/" 26 | ] 27 | iso_checksum = var.iso_checksum 28 | vm_name = "${ var.os_family }-${ var.os_version }-{{ isotime \"2006-01-02\" }}" 29 | guest_os_type = var.guest_os_type 30 | disk_controller_type = ["pvscsi"] 31 | network_adapters { 32 | network = var.vm_network 33 | network_card = var.nic_type 34 | } 35 | storage { 36 | disk_size = var.root_disk_size 37 | disk_thin_provisioned = true 38 | } 39 | CPUs = var.num_cpu 40 | cpu_cores = var.num_cores 41 | CPU_hot_plug = true 42 | RAM = var.vm_ram 43 | RAM_hot_plug = true 44 | boot_wait = "5s" 45 | boot_command = var.boot_command 46 | } 47 | 48 | 49 | build { 50 | sources = [ 51 | "source.vsphere-iso.rhel", 52 | ] 53 | provisioner "shell" { 54 | execute_command = "echo '${var.connection_password}' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'" 55 | scripts = [ 56 | "bash/prep.sh" 57 | ] 58 | } 59 | } -------------------------------------------------------------------------------- /Ubuntu.20.04/builder.pkr.hcl: -------------------------------------------------------------------------------- 1 | source "vsphere-iso" "vm" { 2 | # vCenter settings 3 | vcenter_server = var.vcenter_server 4 | username = var.vcenter_username 5 | password = var.vcenter_password 6 | insecure_connection = true 7 | cluster = var.vcenter_cluster 8 | datacenter = var.vcenter_datacenter 9 | host = var.vcenter_host 10 | datastore = var.vcenter_datastore 11 | folder = var.vcenter_folder 12 | convert_to_template = true 13 | 14 | # VM Settings 15 | http_directory = var.http_directory 16 | ip_wait_timeout = var.ip_wait 17 | ssh_username = var.connection_username 18 | ssh_password = var.connection_password 19 | ssh_timeout = var.ssh_timeout 20 | ssh_port = var.ssh_port 21 | ssh_handshake_attempts = var.ssh_handshakes 22 | shutdown_timeout = var.shutdown_timeout 23 | iso_url = var.iso_url 24 | iso_checksum = var.iso_checksum 25 | vm_name = "${ var.os_family }-${ var.os_version }-{{ isotime \"2006-01-02\" }}" 26 | guest_os_type = var.guest_os_type 27 | disk_controller_type = ["pvscsi"] 28 | network_adapters { 29 | network = var.vm_network 30 | network_card = var.nic_type 31 | } 32 | storage { 33 | disk_size = var.root_disk_size 34 | disk_thin_provisioned = true 35 | } 36 | CPUs = var.num_cpu 37 | cpu_cores = var.num_cores 38 | CPU_hot_plug = true 39 | RAM = var.vm_ram 40 | RAM_hot_plug = true 41 | boot_wait = "5s" 42 | cd_files = [ 43 | "./vms/ubuntu/http/meta-data", 44 | "./vms/ubuntu/http/user-data"] 45 | cd_label = "cidata" 46 | boot_command = var.boot_command 47 | } 48 | 49 | 50 | build { 51 | sources = [ 52 | "source.vsphere-iso.vm", 53 | ] 54 | provisioner "shell" { 55 | execute_command = "echo '${var.connection_password}' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'" 56 | scripts = [ 57 | "bash/apt.sh", 58 | "bash/cleanup.sh" 59 | ] 60 | } 61 | } -------------------------------------------------------------------------------- /Ubuntu.20.04/bash/cleanup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # These scripts were modified from boxcutter 4 | 5 | set -e 6 | set -x 7 | 8 | DISK_USAGE_BEFORE_CLEANUP=$(df -h) 9 | 10 | 11 | echo "==> Blanking systemd machine-id" 12 | if [ -f "/etc/machine-id" ]; then 13 | truncate -s 0 "/etc/machine-id" 14 | fi 15 | 16 | echo "==> Cleaning up tmp" 17 | rm -rf /tmp/* 18 | 19 | # Cleanup apt cache 20 | apt-get -y autoremove --purge 21 | apt-get -y clean 22 | apt-get -y autoclean 23 | 24 | 25 | echo "==> Installed packages" 26 | dpkg --get-selections | grep -v deinstall 27 | 28 | 29 | # Clean up log files 30 | find /var/log -type f | while read f; do echo -ne '' > "${f}"; done; 31 | 32 | echo "==> Clearing last login information" 33 | >/var/log/lastlog 34 | >/var/log/wtmp 35 | >/var/log/btmp 36 | 37 | # # Whiteout /boot 38 | # count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}') 39 | # let count-- 40 | # dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count 41 | # rm /boot/whitespace 42 | 43 | echo '==> Clear out swap and disable until reboot' 44 | set +e 45 | swapuuid=$(/sbin/blkid -o value -l -s UUID -t TYPE=swap) 46 | case "$?" in 47 | 2|0) ;; 48 | *) exit 1 ;; 49 | esac 50 | 51 | set -e 52 | if [ "x${swapuuid}" != "x" ]; then 53 | # Whiteout the swap partition to reduce box size 54 | # Swap is disabled till reboot 55 | swappart=$(readlink -f /dev/disk/by-uuid/$swapuuid) 56 | /sbin/swapoff "${swappart}" 57 | dd if=/dev/zero of="${swappart}" bs=1M || echo "dd exit code $? is suppressed" 58 | /sbin/mkswap -U "${swapuuid}" "${swappart}" 59 | fi 60 | 61 | # Zero out the free space to save space in the final image 62 | dd if=/dev/zero of=/EMPTY bs=1M || echo "dd exit code $? is suppressed" 63 | rm -f /EMPTY 64 | 65 | # Make sure we wait until all the data is written to disk, otherwise 66 | # Packer might quite too early before the large files are deleted 67 | sync 68 | 69 | echo "==> Disk usage before cleanup" 70 | echo "${DISK_USAGE_BEFORE_CLEANUP}" 71 | 72 | echo "==> Disk usage after cleanup" 73 | df -h 74 | 75 | echo `> Preparing cloud-init ...` 76 | rm -rf /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg 77 | rm -rf /etc/cloud/cloud.cfg.d/99-installer.cfg 78 | rm -rf /etc/netplan/00-installer-config.yaml 79 | echo "disable_vmware_customization: false" >> /etc/cloud/cloud.cfg 80 | echo "datasource_list: [ VMware, OVF, None ]" > /etc/cloud/cloud.cfg.d/90_dpkg.cfg 81 | ### Modify GRUB ### 82 | echo `> Modifying GRUB ...` 83 | sed -i -e "s/GRUB_CMDLINE_LINUX_DEFAULT=\"\(.*\)\"/GRUB_CMDLINE_LINUX_DEFAULT=\"\"/" /etc/default/grub 84 | update-grub --------------------------------------------------------------------------------