Skip to content

Commit

Permalink
Added VMware source
Browse files Browse the repository at this point in the history
  • Loading branch information
exdial committed Oct 29, 2023
1 parent 3fbf45e commit b32bd8b
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ It supports a large variety of cloud infrastructures.

## Building

`packer build -var-file=vars/(focal|jammy).pkvars.hcl ubuntu.pkr.hcl`
`packer build -var-file=vars/(focal|jammy).pkvars.hcl (vmware|virtualbox).pkr.hcl`
4 changes: 3 additions & 1 deletion http/user-data.pkrtpl.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ autoinstall:
network:
version: 2
ethernets:
enp0s3:
zz-all-en:
dhcp4: true
match:
name: en*
identity:
hostname: ${var.vm_name}
username: ${var.ssh_username}
Expand Down
5 changes: 3 additions & 2 deletions http/provision.sh → http/virtualbox-provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ ISO_FILE="/tmp/VBoxGuestAdditions.iso"
errdebug() {
echo "Entering debug mode"
echo "Connect via \"ssh vagrant@127.0.0.1 -p 22222\""
sleep 999999
sleep 3600
}

get_vagrant_key() {
mkdir -p "$HOME_DIR"/.ssh
curl -s -o "$HOME_DIR"/.ssh/authorized_keys \
https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
https://raw.githubusercontent.com/hashicorp/vagrant/main/keys/vagrant.pub.ed25519
}

if get_vagrant_key; then
Expand All @@ -36,6 +36,7 @@ mount_guest_additions() {

if mount_guest_additions; then
apt-get update
DEBIAN_FRONTEND=noninteractive && \
apt-get install -y --no-install-recommends --fix-missing \
ca-certificates gcc make bzip2 tar
# Hack: VBoxLinuxAdditions.run every time exited with non-zero code,
Expand Down
39 changes: 39 additions & 0 deletions http/vmware-provision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -exu

USERNAME="vagrant"
HOME_DIR="/home/${USERNAME}"

errdebug() {
echo "Entering debug mode"
echo "Connect via \"ssh vagrant@127.0.0.1 -p 22222\""
sleep 3600
}

get_vagrant_key() {
mkdir -p "$HOME_DIR"/.ssh
curl -s -o "$HOME_DIR"/.ssh/authorized_keys \
https://raw.githubusercontent.com/hashicorp/vagrant/main/keys/vagrant.pub.ed25519
}

if get_vagrant_key; then
chmod 0700 "$HOME_DIR"/.ssh
chmod 0600 "$HOME_DIR"/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME "$HOME_DIR"/.ssh
else
echo "Download failed!"
errdebug
fi

apt-get update
DEBIAN_FRONTEND=noninteractive && \
apt-get install -y --no-install-recommends --fix-missing \
ca-certificates open-vm-tools bzip2 tar

snap remove --purge lxd
snap remove --purge core20
snap remove --purge snapd
apt --purge autoremove -y snapd
truncate -s 0 /etc/resolv.conf
rm -rf /tmp/*
rm -f /var/log/wtmp /var/log/btmp .bash_history
4 changes: 2 additions & 2 deletions vars/focal.pkvars.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ boot_command = [
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"/casper/vmlinuz <wait>",
"initrd=/casper/initrd autoinstall <wait>",
"quiet fsck.mode=skip net.ifnames=0 <wait>",
"biosdevname=0 systemd.unified_cgroup_hierarchy=0 <wait>",
"quiet fsck.mode=skip <wait>",
"systemd.unified_cgroup_hierarchy=0 <wait>",
"ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ ---<wait3>",
"<enter>"
]
4 changes: 2 additions & 2 deletions vars/jammy.pkvars.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ boot_command = [
"c<wait2>",
"set gfxpayload=keep<enter><wait>",
"linux /casper/vmlinuz autoinstall <wait>",
"quiet fsck.mode=skip net.ifnames=0 <wait>",
"biosdevname=0 systemd.unified_cgroup_hierarchy=0 <wait>",
"quiet fsck.mode=skip <wait>",
"systemd.unified_cgroup_hierarchy=0 <wait>",
"ds=\"nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/\" ---<wait3>",
"<enter><wait>",
"initrd /casper/initrd<enter><wait>",
Expand Down
7 changes: 4 additions & 3 deletions ubuntu.pkr.hcl → virtualbox.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ packer {
}
}

# The default value is requred by `packer validate` command
# The default value is requred for the `packer validate` command
variable "iso_url" {
type = string
description = "The URL of the OS image file."
default = "null"
}

# The default value here is md5 from "null".
# The default value is required for the `packer validate` command.
# The value here is md5 of "null".
variable "iso_checksum" {
type = string
description = "The checksum value of `iso_url`."
Expand Down Expand Up @@ -135,7 +136,7 @@ build {
provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -E -S '{{ .Path }}'"
scripts = [
"http/provision.sh"
"http/virtualbox-provision.sh"
]
}

Expand Down
127 changes: 127 additions & 0 deletions vmware.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
packer {
required_version = ">= 1.9.0"

required_plugins {
vmware = {
version = "~> 1"
source = "github.com/hashicorp/vmware"
}
}
}

# The default value is requred for the `packer validate` command
variable "iso_url" {
type = string
description = "The URL of the OS image file."
default = "null"
}

# The default value is required for the `packer validate` command.
# The value here is md5 of "null".
variable "iso_checksum" {
type = string
description = "The checksum value of `iso_url`."
default = "674441960ca1ba2de08ad4e50c9fde98"
}

variable "vm_name" {
type = string
description = "The name of the host."
default = "null"
}

variable "boot_command" {
type = list(string)
description = "Keys to type when the vm is first booted."
default = []
}

variable "ssh_username" {
type = string
default = "vagrant"
}

variable "ssh_password" {
type = string
default = "vagrant"
}

variable "ssh_password_sha256" {
type = string
# First create a salt `openssl rand -base64 9`. Then create a password
# using the salt `mkpasswd -m sha-512 vagrant -S <output of openssl>`.
# Encrypted password here is vagrant.
default = "$6$ihLAVm9evpqz$tqwrwpxQ89UdQtIOdBohtHU/2xrQJ4RgPLpDUXtGc1AGi42U1TFqB2oupVOSdnfXvMPREVb1uL/E0lr37MQ840"
}

# Do not show GUI process by default
variable "headless" {
type = bool
default = true
}

source "vmware-iso" "ubuntu" {
vm_name = var.vm_name
#guest_os_type = "Ubuntu 64-bit"
headless = var.headless

iso_url = var.iso_url
iso_checksum = var.iso_checksum

ssh_username = var.ssh_username
ssh_password = var.ssh_password
ssh_port = 22
ssh_timeout = "20m"
ssh_handshake_attempts = "40"

cpus = "2"
memory = "2048"
disk_size = "7000"

shutdown_command = "echo '${var.ssh_password}' | sudo -S shutdown -P now"
output_directory = "builds"

# Instead of keeping an empty meta-data file in the repository,
# serve the empty location "/meta-data" by HTTP.
http_content = {
"/user-data" = templatefile("http/user-data.pkrtpl.hcl", { var = var }),
"/meta-data" = ""
}

# The type of VMware virtual disk to create.
# Growable virtual disk contained in a single file (monolithic sparse).
disk_type_id = 0

sound = false
usb = false

boot_wait = "5s"
boot_keygroup_interval = "500ms"
boot_command = var.boot_command
}

build {
sources = ["source.vmware-iso.ubuntu"]

provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -E -S '{{ .Path }}'"
inline_shebang = "/bin/sh -exu"
inline = [
"echo '${var.ssh_username} ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/${var.ssh_username}",
"chmod 0440 /etc/sudoers.d/${var.ssh_username}"
]
}

provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -E -S '{{ .Path }}'"
scripts = [
"http/vmware-provision.sh"
]
}

post-processor "vagrant" {
keep_input_artifact = false
compression_level = 9
output = "output/{{ .Provider }}-ubuntu-${var.vm_name}.box"
}
}

0 comments on commit b32bd8b

Please sign in to comment.