Skip to main content

Creating Vagrant Box from VirtualBox VM


This post describes on how to create Vagrant Box from scratch. For this post, we will be using VirtualBox 6.0 as the provider to create Lubuntu-16.04-Desktop vagrant box.


Create the Virtual Machine.
Create a new Virtual Machine in VirtualBox using the following details.
  • Name: Lubuntu 16.04.5 Desktop
  • Type: Linux
  • Version: Ubuntu (64-bits)
  • Memory: 1024 MB
  • Virtual Harddisk: 10 GB, VDI (Dynamically allocated) 
  • Network Adapter 1: Enabled as NAT
  • Processors: 1 CPU
You might also want to disable USB controller and audio if it's not used.


Installing the Guest OS (Linux 16.04 Desktop)
In the newly created virtual machine, install linux OS from lubuntu-16.04.5-desktop-amd64.iso
During installation, use the following details for login setup.
  • Full name: vagrant
  • User name: vagrant
  • Password: vagrant
  • Computer name: lubuntu-16.04.5-desktop


Setting up the Guest OS (Linux 16.04 Desktop)
Configure vagrant user to use sudo without tty and password.
$ sudo visudo

Add or edit the following lines:
Defaults    env_keep = "SSH_AUTH_SOCK"

vagrant  ALL=(ALL) NOPASSWD:ALL # <= Place this at the end of file

Install and configure SSH access for vagrant user.
# Configure ssh access for user vagrant
$ mkdir -p /home/vagrant/.ssh
$ wget --no-check-certificate https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
$ chmod 0600 /home/vagrant/.ssh/authorized_keys
$ chown -R vagrant /home/vagrant/.ssh

# Install ssh server
$ sudo apt-get install openssh-server


Configure ssh server to use key authentication
# /etc/ssh/sshd_config
Port 22
PubKeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
PermitEmptyPasswords no


Restart ssh service
$ sudo systemctl restart ssh


Installing VirtualBox Guest Tools
Guest Tools are used by Vagrant to create shares folder between Host and Guest OS.

Install dependencies for building/compiling Guest Tools.
$ sudo apt-get install -y gcc build-essential linux-headers-$(uname -r)

In VirtualBox menu bar, select Devices > Insert Guest Additions CD image...  to mount the Guest Additions CD.
$ cd /media/vagrant/VBox_GAs_6.0.4
$ sudo ./VBoxLinuxAdditions.run
$ cd ~
$ sudo umount /media/vagrant/VBox_GAs_6.0.4


Finalizing and clean up OS Guest
We need to make the box as small as possible by cleaning up the VM disk space. 
# Remove APT cache
$ sudo apt-get clean

# 'Zero out' the drive
$ sudo dd if=/dev/zero of=/EMPTY bs=1M
$ sudo rm -f /EMPTY

# Clear Bash history
$ cat /dev/null > ~/.bash_history && history -c

$ sudo shutdown -h now



Package the Vagrant Box
Change into directory where you want to store this vagrant box and run vagrant package command. In this case, the VM name is Lubuntu 16.04.5 Desktop.
$ vagrant package --base "Lubuntu 16.04.5 Desktop" --output lubuntu-16.04.5-desktop.box
$ vagrant box add lubuntu-16.04.5-desktop lubuntu-16.04.5-desktop.box


Using the Vagrant Box
$ vagrant init lubuntu-16.04.5-desktop
$ vagrant up
$ vagrant ssh


References:

  1. Creating a Custom Box from Scratch
  2. How to Create a Vagrant Base Box from an Existing One
  3. How to build a Vagrant base box from a VirtualBox VM






Comments

Popular posts from this blog

Server hosting for Noobs

My First Digital Ocean Droplet As a start for codelab project, I decided to start my own cloud server or VPS to be exact. This page served as my journal to explore into server hosting. Create Droplet (VPS) I have chosen Digital Ocean VPS provider as my service provider . There are many tutorials out there providing guide on account registering and starting up droplet (Digital Ocean is referring VPS node as droplet). It is pretty simple to setup a droplet in Digital Ocean. Step 1 : Register  Register an account at  Digital Ocean  and then login into your DO account. Step 2 : Create droplets Create droplets. D.O offered have 3 options to create a droplet:  Linux distribution - plain linux distribution Container - For docker and container type application One-click app - Linux with pre-installed application tailored for specific use case such as web hosting, etc. Step 3 : Choose your Linux flavor For testing, plain linux VPS shall serv...