Skip to content

Instantly share code, notes, and snippets.

@chuckg
Last active December 20, 2024 10:09
Show Gist options
  • Save chuckg/7902165 to your computer and use it in GitHub Desktop.
Save chuckg/7902165 to your computer and use it in GitHub Desktop.
Bootstrap an image for use as a Vagrant base box

Requirements

Prepare virtual machine

  • Open VirtualBox and click New
  • Give the virtual machine a Name: ubuntu-x.x.x-server-amd64
  • From the Type dropdown menu choose Linux
  • From the Version dropdown menu choose Ubuntu (64 bit) and click Continue
  • Leave RAM at 512 MB (Vagrant can change this on-the-fly later) and click Continue
  • Select Create a virtual hard drive now and click Create
  • Select VDI (VirtualBox Disk Image) and click Continue
  • Select Dynamically allocated and click Continue
  • Change the disk size to 40.00 GB and click Create

The virtual machine definition has now been created. Click the virtual machine name and click Settings.

  • Go to the Storage tab, click Empty just under Controller: IDE, then on the right hand side of the window click the CD icon, select Choose a virtual CD/DVD disk file…, and navigate to where the Ubuntu Server ISO was downloaded, select it, and click Open.
  • Go to the Audio tab and uncheck Enable Audio.
  • Go to the Ports tab, then go to the USB tab, and uncheck Enable USB Controller.
  • Click Ok to close the Settings menu.

Finally, start up the virtual machine to begin installation.

Install Ubuntu Server

Begin the Ubuntu Server install. Most of the installation defaults can be used.

Two things to note:

  • When you get to the Set up users and passwords section, create the vagrant user with whatever password you want. Vagrant will ultimately use your SSH key to communicate with the virtual machines.
  • When you get to the Software selection section only select OpenSSH server.

At the end of the install, shutdown the virtual machine, and open the Settings again for the virtual machine.

  • Go to the Storage tab, select Controller: IDE, and click the green square with red minus icon in the lower right hand corner of the Storage Tree section of the Storage tab.
  • Click OK to close the Settings menu and start up the virtual machine again.

Configure Ubuntu Server

Once Ubuntu Server has finished installing and has booted we need to add the vagrant user to the sudoer's file and setup the vagrant public key (so we can SSH in later):

$ sudo su -
$ wget http://git.io/II08-A -O vagrant_bootstrap.sh
$ chmod u+x vagrant_bootstrap.sh
$ ./vagrant_bootstrap.sh
$ exit

# Clear the history for the vagrant user
$ history -c
$ sudo shutdown -h now

Note: http://git.io/II08-A is a pointer to vagrant_bootstrap.sh

Create the Vagrant Box

Make sure the -–base parameter matches the name of the virtual machine in VirtualBox:

vagrant package --output vagrant-ubuntu-x.x.x-server-amd64.box --base ubuntu-x.x.x-server-amd64

Add the Vagrant Box

vagrant box add  vagrant-ubuntu-x.x.x-server-amd64 vagrant-ubuntu-x.x.x-server-amd64.box

Create a Vagrant Project and Configure Vagrantfile

You can have as many vagrant projects as you want. Each will contain different Vagrantfiles and different virtual machines. So, create a directory somewhere to house your Vagrantfile and associative virtual machines:

mkdir -p ~/Development/vagrant-test
cd ~/Development/vagrant-test

Create the Vagrantfile:

vagrant init vagrant-ubuntu-x.x.x-server-amd64

You now have a Vagrantfile that points to the vagrant-ubuntu-x.x.x-server-amd64 Base Box you just created.

Lastly, if you do not want Shared Folders setup between your workstation and virtual machine, disable it by adding the following to your Vagrantfile:

config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

Start Using Vagrant

Spin up your first virtual machine:

vagrant up

If everything spins up properly you can see the status of your virtual machine using vagrant status, you can SSH into your virtual machine using vagrant ssh, or you can destroy your virtual machine using vagrant destroy.

Reference

#!/bin/bash
# Run with root privileges
# ------------------------
# SUDO
echo "vagrant ALL=NOPASSWD: ALL" >> /etc/sudoers
# SSH: Add vagrant public key to vagrant user
mkdir -m 0700 -p /home/vagrant/.ssh
chown vagrant /home/vagrant/.ssh
curl https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys
history -c
@AHassanSOS
Copy link

It's missing dding the image to make it small.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment