Introduction

Photon OS is a great minimal distribution of Linux for DevOPS, especially for Docker and Kubernetes, but it’s not been available as a option on VagrantCloud.com until now.

To use the version I created here, from your CLI type:

vagrant init virtualmountain/photon3 vagrant up vagrant ssh # you're in!

Some of the following is not original to me and I’d like to shout out to this article from 2014 by Tyler Bird from which I have taken some of the blog post and updated and modified it for PhotonOS version 3.

The goal of Vagrant is to make it so simple to create a local development environment, you’d never want to do it another way again. With two simple commands you can quickly setup your first vagrant environment and with a third command, be connected into your first vagrant box in under a few minutes.

This power is amazing and so useful for freelancers who work on many different projects, or companies that need to bring new members of a team up to speed as quickly as possible. With the use of Vagrant, the configuration of the development and the production environment can be mirrored as closely as possible. And errors like “works on my machine” can become a thing of the past.

The Base Build

Before you modify PhotonOS, you need to install it once on virtualbox. What I did was create the base image of PhotonOS, updated to the latest patches and then shut it down and cloned it as Photon-Vagrant and worked from that to the final image.

Here are the settings I used for the base image in virtualbox:

Create a new Virtual Machine with the following settings:

Name: Photon-base

Type: Linux

Version: Other Linux (64-bit)

Memory Size: 1024MB

New Virtual Disk: [Type: VDI, Size: 40 GB]

Modify the hardware settings of the virtual machine for performance and because SSH needs port-forwarding enabled for the vagrant user:

Disable audio

Disable USB

Ensure Network Adapter 1 is set to NAT

Add this port-forwarding rule: [Name: SSH, Protocol: TCP, Host IP: blank, Host Port: 2222, Guest IP: blank, Guest Port: 22]

Once built and cloned to Photon-vagrant, you’re ready to modify the virtual machine for vagrant

Vagrantize the OS

Login as root (the only account created by default in PhotonOS) and change the password

passwd

and change the password to ‘vagrant’.

Yes, you’re going to get a complaint about the password being too simple and a dictionary word, but ignore this.

Change the hostname

Not strictly essential but I like to give the hostname something generic

hostnamectl set-hostname photon

Next you will need to install sudo because vagrant boxes don’t use the root account

tdnf install sudo -y

Create the vagrant account

Next you need to create the default vagrant user account with a home directory and add it to the sudo group so it can elevate privileges on demand

useradd -m -G sudo vagrant

and set the default password for the vagrant account to ‘vagrant’ using

passwd vagrant

and again, ignore the warnings about the password being too simple.

Next, we need to setup the vagrant account to use sudo without having to type in the password to elevate the privileges every time.

Execute the following commands to create a file under /etc/sudoers.d and negate the need for a password when invoking sudo

vi /etc/sudoers.d/vagrant

and in the file add the following and save it:

# add vagrant user vagrant ALL=(ALL) NOPASSWD:ALL

Test that this all works by logging out as root and logging as vagrant with password ‘vagrant’ and running the following command:

sudo pwd

It will return the home folder /home/vagrant without prompting you for a password if everything is setup correctly.

Setup SSH for vagrant

By default the SSH daemon will be running but will not allow access via root so you must use the vagrant user.

You can check the SSH daemon is running via

systemctl status sshd

From your host OS (in my case Windows 10), invoke your favourite SSH client (in my case Putty), connect to 127.0.0.1 with port 2222 (as per the port forwarding setup earlier) and login as vagrant with vagrant as the password

Install the Vagrant Key

The only way that all the vagrant commands will be able to communicate over ssh from the host machine to the guest server is if the guest server has an initial “insecure vagrant key” installed.

It’s called “insecure” because essentially everyone has this same initial key and in the past theoretically anyone can hack into everyone’s vagrant box if you use it.

But now, when vagrant issues a command to create a new vagrant box, it detects this initial key and creates a new one just for this VM.

Even so, Vagrant boxes are meant solely for software development, not production.

First we will be using wget to download the insecure key so we need to install the package

sudo tdnf install wget -y

In the SSH session, copy and paste the following commands to setup the box for the vagrant key:

mkdir -p /home/vagrant/.ssh chmod 0700 /home/vagrant/.ssh

wget --no-check-certificate \ https://raw.github.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

Next we need to tell the SSH server that the location where the insecure key we’ve just downloaded is authorized for SSH access.

sudo vi /etc/ssh/sshd_config

and find and change the following line (around line 41):

AuthorizedKeysFile .ssh/authorized_keys

to

AuthorizedKeysFile %h/.ssh/authorized_keys

Save the file and restart the ssh daemon

sudo systemctl restart sshd

Finally add VirtualBoxadditions

This part is required if you are using the Shared Folder between the Host OS and the Guest OS (/vagrant)

Go to your virtualbox menu for the VM and select Devices –> Insert Guest Additions CD Image

Next in the console, logout and then login as root or just ‘sudo su –‘

mount –o loop /dev/cdrom /mnt/cdrom

The VirtualBoxAdditions installer uses ‘awk’ and ‘tar’ so we’ll have to install those first

tdnf install awk tar-y

and we will also need to install development tools and the linux development headers since Virtualbox adds modules to the kernel with the additional functionality

tdnf install build-essential linux-devel -y

when that has installed, then you can run the virtualbox additions installer

cd /mnt/cdrom

./VBoxLinuxAdditions.run

This takes a few minutes to do, but when it is done, we’re going to have to reboot the VM so it recognizes the extra modules added to the kernel (because kernel-devel was for the next version of the kernel, not the one I’m on – why VMware? Why? )

The home stretch

The vagrantized VM is complete, we just to package it and put it in our home repo

The packaging process involves compression so to improve the compression we’re going to zero out the disks and then delete the zeros.

Before you package the box you’ll want to “zero out” the drive. This fixes fragmentation issues with the underlying virtual disk, which allows it to compress much more efficiently later.

sudo dd if=/dev/zero of=/EMPTY bs=1M sudo rm -f /EMPTY

Package the Vagrantized VM

We’re ready to package the box. I usually make a folder to hold my boxes like so:

mkdir ~/code/personal/vagrant_boxes cd ~/code/personal/vagrant_boxes

This is the command that finally packages up the box for you as we defined above into the compressed gzip tarball file, it also generates and includes the Vagrantfile and the metadata.json file.

vagrant package –base “Name of VM in Virtualbox”

Vagrant will then check VirtualBox for any instances of the name vagrant-ubuntu64 and attempt to ssh into them and control them.

→ vagrant package –base “photon 3 vagrant build” [Photon 3 Vagrant Build] Attempting graceful shutdown of VM... [Photon 3 Vagrant Build] Forcing shutdown of VM... [Photon 3 Vagrant Build] Clearing any previously set forwarded ports... [Photon 3 Vagrant Build] Exporting VM... [Photon 3 Vagrant Build] Compressing package to: /path/to/directory/package.box

You are left with the package.box file in your ~/code/personal/vagrant_boxes folder.

Test the package by uploading it to your local repository

From your same vagrant_boxes folder you can run these final test commands. All the heavy lifting is really done at this point. If you’ve screwed up something it’s probably in a step up above.

You should be in ~/code/personal/vagrant_boxes/ and type:

vagrant box add photon3 package.box vagrant init photon3 vagrant up

Connect to the server you created from start to finish!

vagrant ssh

The End (for now)

I’d like to give special thanks to Tyler Bird for his original post and to ‘andyp73’ on the VirtualBox forums who helped immensely by finding the missing step that enabled Photon to install the VirtualBoxAdditions.