Difference between revisions of "VirtualBox"

From Klenwell Wiki
Jump to navigation Jump to search
Line 17: Line 17:
 
# Go to VirtualBox > Preferences > Extensions > click the "+" sign at right side of dialog window.
 
# Go to VirtualBox > Preferences > Extensions > click the "+" sign at right side of dialog window.
 
# Navigate to where your extension pack was downloaded and click OK and agree to terms.
 
# 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 ====
 +
# Go to https://www.ubuntu.com/download/server
 +
# Download latest stable LTS version Ubuntu Server
 +
 +
==== Create VM with VBoxManage ====
 +
Open your terminal and execute the VBoxManage commands:
 +
 +
<pre># 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
 +
// To verify: grep GetHostTimeDisabled  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox
 +
VBoxManage guestproperty set "${VBOX_NAME}" "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 10000
 +
// To verify: grep timesync  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox
 +
</pre>
 +
 +
Your VM is now configured, move onto the next section to install Ubuntu.

Revision as of 20:07, 7 March 2021

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
// To verify: grep GetHostTimeDisabled  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox
VBoxManage guestproperty set "${VBOX_NAME}" "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 10000
// To verify: grep timesync  ~/VirtualBox\ VMs/${VBOX_NAME}/${VBOX_NAME}.vbox

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