Skip to content

Instantly share code, notes, and snippets.

@reyhansofian
Last active February 1, 2025 03:54
Show Gist options
  • Save reyhansofian/1a1344bde54a9ca68528c746555eed2d to your computer and use it in GitHub Desktop.
Save reyhansofian/1a1344bde54a9ca68528c746555eed2d to your computer and use it in GitHub Desktop.
#!/bin/bash
packer plugins install github.com/hashicorp/virtualbox
# Detect the active network interface
if ip link show eth0 | grep -q "state UP"; then
BRIDGE_INTERFACE="eth0"
elif ip link show en0 | grep -q "state UP"; then
BRIDGE_INTERFACE="en0"
else
echo "No active network interface found!"
exit 1
fi
# Run Packer with the detected interface
packer build -var "network_bridge_interface=$BRIDGE_INTERFACE" .
# main.pkr.hcl
# Define the VirtualBox ISO builder
source "virtualbox-iso" "ubuntu-2204" {
guest_os_type = "Ubuntu_64"
iso_urls = [
"https://releases.ubuntu.com/22.04/ubuntu-22.04.5-live-server-amd64.iso"
]
iso_checksum = var.iso_checksum
cd_label = "cidata"
cd_content = {
"meta-data" = file("./meta-data")
"user-data" = file("./user-data")
}
vm_name = "ubuntu-vm"
communicator = "ssh"
ssh_username = "user"
ssh_password = "user"
ssh_timeout = "10m"
ssh_handshake_attempts = 420
ssh_port = "22"
ssh_skip_nat_mapping = true
pause_before_connecting = "30s"
http_directory = ""
shutdown_command = "sudo -S shutdown -P now"
boot_wait = "10s"
boot_command = [
"<wait>c<wait>",
"linux /casper/vmlinuz --- autoinstall ds=\"nocloud\"",
"<enter><wait>",
"initrd /casper/initrd",
"<enter><wait>",
"boot",
"<enter>",
]
# VirtualBox VM settings
vboxmanage = [
["modifyvm", "{{.Name}}", "--memory", "2048"],
["modifyvm", "{{.Name}}", "--cpus", "2"],
["modifyvm", "{{.Name}}", "--cpus", "2"],
["modifyvm", "{{.Name}}", "--nat-localhostreachable1", "on"],
["modifyvm", "{{.Name}}", "--natpf1", "ssh,tcp,,2222,,22"],
["modifyvm", "{{.Name}}", "--nic2", "bridged", "--bridgeadapter2", var.network_bridge_interface]
]
}
# Define the build block
build {
sources = ["source.virtualbox-iso.ubuntu-2204"]
# Provisioner to configure UFW
provisioner "shell" {
inline = [
"sudo apt update",
"sudo apt install -y ufw",
"sudo ufw enable",
"sudo ufw allow ssh",
"ip addr show", # Display network interfaces and their IPs
]
}
}
# variables.pkr.hcl
variable "iso_checksum" {
description = "The SHA256 checksum of the Ubuntu 22.04 ISO"
type = string
default = "sha256:9bc6028870aef3f74f4e16b900008179e78b130e6b0b9a140635434a46aa98b0"
}
variable "network_bridge_interface" {
description = "The host network interface for bridged networking"
type = string
default = "en0: Wi-Fi (Wireless)"
}
variable "static_ip" {
default = "${env("STATIC_IP")}"
}
# Installation
# ============
# Installation of Ubuntu Server 13.04 “Raring Ringtail” is automated by
# 'preseeding' configuration options with the values expected for vagrant. The
# `ubuntu-server/raring64/preseed.cfg` file defines these options.
# Clock and Timezone
# ------------------
# ### Hardware Clock
# Setting the hardware clock to UTC is considered best practice and may be
# assumed by some pacakges
d-i clock-setup/utc boolean true
# ### Timezone
# Generally the server timezone should be set to a value which is logically tied
# to the purpose of the server. The generic nature of this vagrant box lends
# itself to 'GMT' as it is the global time standard.
d-i time/zone string GMT
# Account Setup
# -------------
# ### User Credentials
# It is conventional to create a user named 'vagrant' with a password of
# 'vagrant' which vagrant will login as. Please note that these credentials are
# public knowledge and you should never have this account on a public server.
d-i passwd/user-fullname string vagrant
d-i passwd/username string vagrant
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i user-setup/allow-password-weak boolean true
# ### Home Directory Encryption
# The home directory is not encrypted to aviod any issues that this may cause.
d-i user-setup/encrypt-home boolean false
# Partitioning
# ------------
# ### LVM Partitioning
# The entire disk is partitioned using LVM and any prompts that LVM partitioning
# raises are confirmed automatically.
d-i partman-auto/method string lvm
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
# Old LVM partitions are overwritten, pre-existing raid partitions are removed.
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
# The ubuntu installer will prompt for confirmation of the partitioning
# strategies.
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman/confirm_nooverwrite boolean true
# Packages & Updates
# ------------------
# ### No proxy
# By default there is no proxy set for the mirror.
d-i mirror/http/proxy string
# ### No automatic updates
# Automatic updates are not applied, everything is updated manually.
d-i pkgsel/update-policy select none
# ### Ubuntu server
# The standard ubuntu server
tasksel tasksel/first multiselect standard
# Static IP details
d-i netcfg/get_ipaddress string {{ user `static_ip` }} # Static IP for the bridge interface
d-i netcfg/get_netmask string 255.255.255.0 # Subnet mask
d-i netcfg/get_gateway string 192.168.1.1 # Gateway
d-i netcfg/get_nameservers string 8.8.8.8 # DNS server
d-i netcfg/confirm_static boolean true # Confirm static configuration
# ### OpenSSH Server
# Vagrant requires the OpenSSH server to be installed in order to be able to
# manage the VM.
d-i pkgsel/include string openssh-server
# Boot loader installation
# ------------------------
# The default boot loader (grub) is installed as the boot loader on the server
# and all prompts which it throws up are automatically confirmed.
d-i grub-installer/only_debian boolean true
# Complete installation
# ---------------------
# Ubuntu show a final message once the installation is complete. This message is
# affirmed so it doesn't block the installation process.
d-i finish-install/reboot_in_progress note
#cloud-config
autoinstall:
version: 1
refresh-installer:
update: yes
early-commands:
- sudo systemctl stop ssh
identity:
hostname: ubuntu-vm
password: "$6$FhcddHFVZ7ABA4Gi$9l4yURWASWe8xEa1jzI0bacVLvhe3Yn4/G3AnU11K3X0yu/mICVRxfo6tZTB2noKljlIRzjkVZPocdf63MtzC0"
username: ubuntu
locale: nb_NO.UTF8
keyboard:
layout: 'no'
ssh:
install-server: true
allow-pw: true
packages:
- linux-virtual
- linux-tools-virtual
- linux-cloud-tools-virtual
- build-essential
- openssh-server
- cloud-init
- whois
- zsh
- wget
- curl
- tasksel
late-commands:
- echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
- curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment