VirtualBox

From Klenwell Wiki
Revision as of 20:09, 7 March 2021 by Klenwell (talk | contribs) (Simulating an Ubuntu Server)
Jump to navigation Jump to search

Overview

VirtualBox is a free and open-source hypervisor that can be used to simulate a remote server as a virtual machine on your local development workstation. I use it primarily to develop and test Ansible playbooks and deployment scripts.

For more information, see:

Installation

See https://www.virtualbox.org/wiki/Downloads

Extension Pack

It is also recommend you install the VirtualBox Extension Pack:

  1. Go to https://www.virtualbox.org/wiki/Downloads and click "VirtualBox 6.x.x Oracle VM VirtualBox Extension Pack" to download.
  2. Launch the VirtualBox GUI.
  3. Go to VirtualBox > Preferences > Extensions > click the "+" sign at right side of dialog window.
  4. Navigate to where your extension pack was downloaded and click OK and agree to terms.

Simulating an Ubuntu Server

This section will outline how to create an Ubuntu virtual machine on your local machine to simulate a remote server. This is useful for developing and testing Ansible playbooks.

Create the Virtual Machine

Choose Operating System Version

Choose an operating system that matches as closely as possible the staging or production server used for the project. For new servers, we prefer to use the latest stable LTS (Long Term Support) version of Ubuntu. For this example, the OS will be Ubuntu 20.04.

Download Ubuntu Server Image

  1. Go to https://www.ubuntu.com/download/server
  2. Download latest stable LTS version Ubuntu Server

Create VM with VBoxManage

Open your terminal and execute the VBoxManage commands:

# Set VM name here as var. For this example, it will be local-vbox.
VBOX_NAME="ubuntu-2004-vbox"

# Create a new VM. Name should be consistent in commands that follow.
VBoxManage createvm --name "${VBOX_NAME}" --ostype "Ubuntu_64" --register

# Modify VM to set memory and RAM
VBoxManage modifyvm "${VBOX_NAME}" --memory 1024 --vram 16

# Add a SATA hard drive
VBoxManage storagectl "${VBOX_NAME}" --name "SATA" --add sata

# Create a Virtual Disk Image
VBoxManage createmedium --filename ${VBOX_NAME}.vdi --size 20000 --format VDI --variant Fixed

# Attach virtual disk image to SATA
VBoxManage storageattach "${VBOX_NAME}" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "${VBOX_NAME}.vdi"

# Create IDE controller for your Ubuntu iso file
VBoxManage storagectl "${VBOX_NAME}" --name "IDE" --add ide

# Attach Ubuntu iso file medium to IDE. ISO_PATH is set as var. 
ISO_PATH="${HOME}/Downloads/ubuntu-20.04.2-live-server-amd64.iso" 
VBoxManage storageattach "${VBOX_NAME}" --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium ${ISO_PATH}

# Set NAT Port Forwarding Rules
VBoxManage modifyvm "${VBOX_NAME}" --natpf1 "http,tcp,127.0.0.1,8000,10.0.2.15,80"
VBoxManage modifyvm "${VBOX_NAME}" --natpf1 "https,tcp,127.0.0.1,8443,10.0.2.15,443"
VBoxManage modifyvm "${VBOX_NAME}" --natpf1 "ssh,tcp,127.0.0.1,8022,10.0.2.15,22"

# Ensure the vm system clock stays in sync with the real world (host machine)
VBoxManage setextradata "${VBOX_NAME}" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 0
VBoxManage guestproperty set "${VBOX_NAME}" "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 10000

# Verify clock sync
grep GetHostTimeDisabled  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox
grep timesync  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox

Your VM is now configured, move onto the next section to install Ubuntu.

Install Ubuntu on VM

First, start your VM:

VBoxManage startvm "${VBOX_NAME}"

Your VM should open and display Ubuntu boot loader/installation. Follow the installation steps that Ubuntu will walk you through. In most cases you will choose the default. Exceptions are noted in bold below.

  • Choose your preferred language: <English>
  • Keyboard configuration
    • Layout: <English (US)>
    • Variant: <English (US)>
  • Install Ubuntu (hit enter)
  • Network connections
    • Leave default (ethernet, 10.0.2.15/24)
  • Configure proxy: (leave blank)
  • Configure Ubuntu archive mirror: leave default
  • Filesystem setup
    • Use an Entire Disk and Set Up LVM
    • Choose the disk to install to: select default (VBOX_HARDDISK...)
    • <Done>
    • Confirm destructive action? <Continue>
  • Profile setup
    • Your name: provision
    • Your server's name: ubuntu-2004-vbox
    • Pick a username: provision
    • Choose a password: provision
    • Confirm your password: provision
    • Check "Install OpenSSH server" (press "spacebar" or "enter" to select)
    • Import SSH identity: <No>
  • Featured Server Snaps: Don't select any. <Done>

VM will Reboot. Press "enter" when you see this message: "Please remove the installation medium, then press ENTER".

You may have to press "enter" again when the logging reaches "Reached target Cloud-init target."

Log in using credentials configured above. Note VM's IP address:

$ ifconfig -a
# Typically should be 10.0.2.15 for network IP and 127.0.0.1 for localhost

Take Snapshot

This snapshot represents your freshly minted VM.

SNAPSHOT_NAME="new-server"
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}