Difference between revisions of "VirtualBox"

From Klenwell Wiki
Jump to navigation Jump to search
(Created page with "== 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 i...")
 
(Create VM with VBoxManage)
 
(7 intermediate revisions by the same user not shown)
Line 9: Line 9:
 
== Installation ==
 
== Installation ==
 
See https://www.virtualbox.org/wiki/Downloads
 
See https://www.virtualbox.org/wiki/Downloads
 +
 +
=== Extension Pack ===
 +
It is also recommend you install the VirtualBox Extension Pack:
 +
 +
# Go to https://www.virtualbox.org/wiki/Downloads and click "VirtualBox 6.x.x Oracle VM VirtualBox Extension Pack" to download.
 +
# Launch the VirtualBox GUI.
 +
# 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.
 +
 +
== Set Up Ubuntu Server with Vagrant ==
 +
(TODO)
 +
 +
== Manually Set Up 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
 +
VBoxManage guestproperty set "${VBOX_NAME}" "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 10000</pre>
 +
 +
Your VM is now configured, move onto the next section to install Ubuntu.
 +
 +
==== Install Ubuntu on VM ====
 +
First, start your VM:
 +
 +
<pre>VBoxManage startvm "${VBOX_NAME}"</pre>
 +
 +
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: <tt>provision</tt>
 +
** Your server's name: <tt>ubuntu-2004-vbox</tt>
 +
** Pick a username: <tt>provision</tt>
 +
** Choose a password: <tt>provision</tt>
 +
** Confirm your password: <tt>provision</tt>
 +
** 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:
 +
 +
<pre>$ ifconfig -a
 +
# Typically should be 10.0.2.15 for network IP and 127.0.0.1 for localhost</pre>
 +
 +
==== Take Snapshot ====
 +
This snapshot represents your freshly minted VM.
 +
 +
<pre>SNAPSHOT_NAME="new-server"
 +
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}</pre>
 +
 +
=== Pre-Ansible: Manually Configure VM ===
 +
==== Setup SSH Keys ====
 +
You may need to generate a new Github ssh key for deploy. If so, follow steps to generate a key and add to ssh-agent:
 +
 +
* https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
 +
 +
Then set up passwordless SSH key access for your playbook user:
 +
 +
* From your terminal, ssh into server
 +
 +
<pre># Say yes to adding server to known_hosts file.
 +
ssh provision@localhost -p 8022
 +
exit</pre>
 +
 +
You may get an error regarding authentication, especially if you've set up multiple VM's in the past and have copied your id_rsa key over to the remote server. If so, navigate to your known_hosts file:
 +
 +
<pre>
 +
vim ~/.ssh/known_hosts
 +
</pre>
 +
 +
If you have an existing [localhost]:8022 entry, select entire line, remove, and save file.
 +
 +
Try to ssh into your server now.
 +
 +
* Copy rsa public key to server
 +
 +
<pre># Your id_rsa filename may be different, if so, change it in the command below.
 +
ssh-copy-id provision@localhost -p 8022
 +
 +
# If that fails, try specifying your key file.
 +
ssh-copy-id -i ~/.ssh/id_rsa provision@localhost -p 8022</pre>
 +
 +
==== Install Core Packages ====
 +
* On the target VM (i.e., "remote") server, make sure to update Ubuntu repositories as follows so that Ansible will be able to bootstrap itself and do its magic:
 +
 +
<pre># SSH into VM server
 +
ssh provision@localhost -p 8022
 +
 +
# Run following sudo commands
 +
sudo apt-get install software-properties-common
 +
sudo apt-add-repository universe
 +
sudo apt-get update
 +
sudo apt-get install python
 +
sudo apt-get install python-pip
 +
sudo apt-get install python-apt</pre>
 +
 +
These are the only non-Ansible-driven changes you should need to make on that server.
 +
 +
==== Take Snapshot ====
 +
As you're building and troubleshooting your Ansible playbook, this is the snapshot you'll want to revert to when you want to test it from scratch:
 +
 +
<pre>SNAPSHOT_NAME="ansible-ready"
 +
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}</pre>
 +
 +
== Command Line Commands ==
 +
=== Start / Stop VM ===
 +
You'll usually want to run your new VM headless from the command line. To start and stop:
 +
 +
<pre># Start VM in headless mode
 +
VBoxManage startvm ${VBOX_NAME} --type headless
 +
 +
# Power off VM
 +
VBoxManage controlvm ${VBOX_NAME} poweroff
 +
 +
# List virtual machines
 +
VBoxManage list vms</pre>
 +
 +
=== Take / Restore Snapshots ===
 +
It is recommend you take a snapshot now so you can easily restore your VM to a new state for testing and troubleshooting DevOps scripts like Ansible.
 +
 +
<pre># Take Snapshot
 +
SNAPSHOT_NAME="fresh-install"
 +
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}
 +
 +
# Restore Snapshot
 +
VBoxManage snapshot ${VBOX_NAME} restore ${SNAPSHOT_NAME}
 +
 +
# List Snapshots
 +
VBoxManage showvminfo ${VBOX_NAME}
 +
 +
# Delete Snapshot
 +
VBoxManage snapshot ${VBOX_NAME} delete ${SNAPSHOT_NAME}</pre>

Latest revision as of 20:33, 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.

Set Up Ubuntu Server with Vagrant

(TODO)

Manually Set Up 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

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}

Pre-Ansible: Manually Configure VM

Setup SSH Keys

You may need to generate a new Github ssh key for deploy. If so, follow steps to generate a key and add to ssh-agent:

Then set up passwordless SSH key access for your playbook user:

  • From your terminal, ssh into server
# Say yes to adding server to known_hosts file.
ssh provision@localhost -p 8022
exit

You may get an error regarding authentication, especially if you've set up multiple VM's in the past and have copied your id_rsa key over to the remote server. If so, navigate to your known_hosts file:

vim ~/.ssh/known_hosts

If you have an existing [localhost]:8022 entry, select entire line, remove, and save file.

Try to ssh into your server now.

  • Copy rsa public key to server
# Your id_rsa filename may be different, if so, change it in the command below.
ssh-copy-id provision@localhost -p 8022

# If that fails, try specifying your key file.
ssh-copy-id -i ~/.ssh/id_rsa provision@localhost -p 8022

Install Core Packages

  • On the target VM (i.e., "remote") server, make sure to update Ubuntu repositories as follows so that Ansible will be able to bootstrap itself and do its magic:
# SSH into VM server
ssh provision@localhost -p 8022

# Run following sudo commands
sudo apt-get install software-properties-common
sudo apt-add-repository universe
sudo apt-get update
sudo apt-get install python
sudo apt-get install python-pip
sudo apt-get install python-apt

These are the only non-Ansible-driven changes you should need to make on that server.

Take Snapshot

As you're building and troubleshooting your Ansible playbook, this is the snapshot you'll want to revert to when you want to test it from scratch:

SNAPSHOT_NAME="ansible-ready"
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}

Command Line Commands

Start / Stop VM

You'll usually want to run your new VM headless from the command line. To start and stop:

# Start VM in headless mode
VBoxManage startvm ${VBOX_NAME} --type headless

# Power off VM
VBoxManage controlvm ${VBOX_NAME} poweroff

# List virtual machines
VBoxManage list vms

Take / Restore Snapshots

It is recommend you take a snapshot now so you can easily restore your VM to a new state for testing and troubleshooting DevOps scripts like Ansible.

# Take Snapshot
SNAPSHOT_NAME="fresh-install"
VBoxManage snapshot ${VBOX_NAME} take ${SNAPSHOT_NAME}

# Restore Snapshot
VBoxManage snapshot ${VBOX_NAME} restore ${SNAPSHOT_NAME}

# List Snapshots
VBoxManage showvminfo ${VBOX_NAME}

# Delete Snapshot
VBoxManage snapshot ${VBOX_NAME} delete ${SNAPSHOT_NAME}