A simple Packer + Vagrant VirtualBox install of Ubuntu live-server 22.04 (x86) using Cloud-Init, and documentation of my host desktop.
A fresh install of any supported LTS or the latest Ubuntu desktop version is good. I have tested this on ubuntu 18.04.6 running on an i5-4570 (4 core 3.20GHz) with 16GB of RAM, a nearly 10 year old computer. Get a dedicated machine, it need not be fancy or new.
Update and get setup with some tools + deps first:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y \
apt-transport-https ca-certificates tcpdump wireshark nmap software-properties-common build-essential \
htop nethogs iotop ccze tmux git vim make curl wget jq expect dos2unix lsof strace tree entr bats \
python3 python3-pip python3-dev python3-venv python3-passlib squashfs-tools minicom screen \
libssl-dev p7zip-full unrar fakeroot whois pwgen binutils debootstrap mtools \
cloud-init xorriso genisoimage mkisofs isolinux syslinux-common grub-pc-bin grub-efi-amd64-bin;
Install VS-Code:
snap install --classic code
Install Hashicorp tools:
mkdir ~/src/
curl --output ~/src/apt-hashicorp.gpg -fsSL https://apt.releases.hashicorp.com/gpg
gpg --show-keys ~/src/apt-hashicorp.gpg
# check key ID
sudo cp -v ~/src/apt-hashicorp.gpg /usr/local/share/keyrings/hashicorp.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/local/share/keyrings/hashicorp.asc] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt-get update && sudo apt-get install packer vagrant -y
packer version
vagrant --version
Install Ansible from PPA if you wish:
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
ansible --version
Install Virtual Box from either
Option 1) Ubuntu mirror
Use Ubuntu package:
sudo apt-get install virtualbox -y
Option 2) Oracle mirror
Get direct from Oracle:
curl --output ~/src/oracle_vbox_2016.asc -fsSL https://www.virtualbox.org/download/oracle_vbox_2016.asc
curl --output ~/src/oracle_vbox.asc -fsSL https://www.virtualbox.org/download/oracle_vbox.asc
gpg --show-keys ~/src/oracle_vbox_2016.asc
gpg --show-keys ~/src/oracle_vbox.asc
# check key ID
sudo cp -v ~/src/oracle_vbox_2016.asc /usr/local/share/keyrings/oracle_vbox_2016.pgp
sudo cp -v ~/src/oracle_vbox.asc /usr/local/share/keyrings/oracle_vbox.pgp
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/local/share/keyrings/oracle_vbox_2016.pgp] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
sudo apt-get update
sudo apt-get install virtualbox-6.1 -y
Create files:
git clone https://gist.github.com/craig-m/eb4a0840fa03a82398423769676ef426 ubuntu-2204-vm
cd ubuntu-2204-vm/
touch meta-data
mkdir upload
Create a .gitignore
file with:
.vagrant
packer_cache/
output-*
local/
venv/
*.iso
*.log
*.box
Install some Python tools into the folder venv/
:
python3 -m venv venv && source venv/bin/activate
python3 -m pip install -U setuptools pip tox
python3 -m pip install \
ansible-runner ansible-lint molecule ansible-core molecule-vagrant paramiko invoke
The end result:
ubuntu-vm/
├── upload/
├── venv/
├── .gitignore
├── meta-data
├── user-data
├── README.md
├── playbook-packer.yml
├── playbook-vagrant.yml
├── Ubuntu.pkr.hcl
└── Vagrantfile
If you want to change the password in user-data
you can run mkpasswd -m sha-512
but note what the documentation has to say about this password hash:
# Please note: while the use of a hashed password is better than
# plain text, the use of this feature is not ideal. Also,
# using a high number of salting rounds will help, but it should
# not be relied upon.
#
# In other words, this feature is a potential security risk and is
# provided for your convenience only. If you do not fully trust the
# medium over which your cloud-config will be transmitted, then you
# should not use this feature.
Building:
Validate the Cloud-Init user-data
file:
cloud-init schema --config-file user-data
Create the Vagrant ubuntu.box
file:
packer init .
export PACKER_LOG=1
export PACKER_LOG_PATH=$(pwd)/packer_$(date +%H%M%d%m).log
packer validate . && packer build Ubuntu.pkr.hcl
Running:
The Vagrantfile supports multiple VM, each with own static IP. Edit as needed.
vagrant validate Vagrantfile && vagrant up
vagrant status
This will start the virtual machine/s and log you into them.
Cleaning:
Rinse and repeat.
vagrant destroy -f
vagrant box remove ubuntu.box -f
rm -fv -- *.log ubuntu.box
Very interesting... But it does not work. I had to change the keyboard layout and update to the last ubuntu LTS image (+ checksum), but that is not enough. There is a SSH issue after initial build. Indeed, once the VMs is built and after cloud-init ran, the machine reboot, and then
ubuntu
user is not available anymore.I thought to use a multi stage build with virtualbox, but that is not easy... I also tried to push my SSH key, but the examples I saw are using either preseed or kickstart and I would like to stay with cloud-init.
Any update here would be useful !
BTW, the playbook vagrant does not seem to be used here, and I think a true github repository would be much appreciated, allowing us to create real issue.
Anyway, thanks for sharing.