Virtual Machine Setup Guide
We’ll be using Arch Linux virtual machines (VMs) in CS 521. Arch Linux is a simple distribution that gives us the opportunity to configure our virtual machines exactly as we want. For your reference, the Arch Linux Wiki has a wealth of information on configuring the system, swapping out components, and installing a wide variety of software.
In this lab, you’ll create a virtual machine using Kernel-based Virtual Machine (KVM) virtualization in Linux.
If you’d like to go beyond the steps outlined on this page, check out the helpful Arch Linux Installation Guide.
Virtual Machine Setup
One of our goals in this class is to get more familiar with Linux (and Unix) systems, particularly with the command line interface (CLI). Instead of giving each student a Linux workstation to use over the course of the semester, we’ll use virtual machines. After all, virtualized infrastructure has revolutionized the computing landscape; rather than maintaining large datacenters with thousands of power-hungry servers, companies can start and stop virtual machines as needed to scale their infrastructure in or out as required.
KVM is a virtualization module that allows Linux to act as a hypervisor, or software component that supervises virtual machines. In VM terminology, you have two types of systems:
- Host - the physical machine VMs run on; it hosts the VMs.
- Guest - the VMs themselves are guests running on the host.
We will use our VM host gojira
to create a VM guest for each student in this lab.
Logging into the VM Host
Dust off your USF CS credentials and ssh
into our jump host, stargate.cs.usfca.edu
. Once logged in you can proceed to ssh
to our VM host, gojira
:
# Get to the CS network:
$ ssh mmalensek@stargate.cs.usfca.edu
# Log in to the VM host:
$ ssh mmalensek@gojira
# Let's see who else is logged in:
$ finger
Login Name Tty Idle Login Time
afedosov Alex Fedosov pts/0 4:04 Jan 9 12:20
mmalensek Matthew Malensek pts/1 Jan 9 17:31
# (This output will probably be a bit more exciting once classes start)
IMPORTANT: You will be using ssh
extensively in this class, so you need to set up passwordless ssh if you haven’t already. In other words, you should be able to type ssh stargate.cs.usfca.edu
followed by ssh gojira
without entering a password.
All of our VMs will be hosted on a single server (gojira
), which has 64 hardware threads (32 cores) and 128 GB of RAM. You will be able to log into the VM host to see all the VMs running.
Creating a new Virtual Machine
Before you start running commands, take a step back and determine the following:
- Your CS user name. We’ll create an account on your VM with the same user name. This replaces VMUSERNAME in the instructions.
- A name for your VM – replaces VMNAME in the instructions. This can be anything you want, but can only contain letters, numbers or dashes (-).
- A password to use for remote access,
ssh
, and theroot
account on the VM – replaces VMPASSWORD in the instructions. We recommend using the same password for all accounts/logins. It’s much easier to keep track of. - Your CS 521 Classroom ID. You will use this number to set your IP address, ports, etc – replaces VMID in the instructions.
Virtualization functionality is provided by the libvirt
toolkit. We’ll use the virt-install
command to set up our VM. Note that in the following example, the backslash (\
) denotes a command continuation, a way of breaking one very long command line into multiple lines. You will need to tweak the command a bit to customize it for your own VM; see the explanation below for details.
TIP: if you’re about to paste the following into a text editor to modify, make sure it is in plain text mode! If it isn’t, it’ll make replacements with special characters that won’t work in your terminal.
$ virt-install \
--name=VMNAME \
--cpu host --vcpus=2 \
--memory=1500 \
--disk=/raid/$(whoami)/VMNAME.qcow2,size=32 \
--cdrom=/home2/iso/archlinux-????????.iso \
--net model=virtio,bridge=virbr0,mac='52:54:00:CA:FE:VMIDHEX' \
--boot useserial=on \
--console pty,target_type=serial \
--graphics none \
--os-type=linux \
--os-variant=archlinux \
--noautoconsole
Be sure to update:
- name – Name of the VM. For example, I used
magical-unicorn
. - disk – This is where your VM disk is stored. Replace VMNAME with your VM’s name. Your VM’s virtual hard disk will be stored in
/raid/$(whoami)/VMNAME.qcow2
, where$(whoami)
will automatically be replaced with your user name. - cdrom – this is the path to the Arch Linux install CD image. You should
cd
into/home2/iso
and find newest version stored there, then replace the ???? above with the correct path. - net – It is very important to correctly set the MAC (media access control) address here so that you can access the network. Replace VMIDHEX with your 521 ID in hexadecimal; for example, if your ID is 105 (in decimal) then enter 69 as the last entry in the MAC address.
You can leave the rest of the command line flags as shown above. They do the following:
- cpu, vcpus – sets the model and number of CPUs available to the VM.
- memory – determines how much RAM the VM gets.
- cdrom – gives the path of the installation environment .iso (aka “Live CD”)
- boot, console, graphics – configures the serial console we’ll use to set up our VM
- os-type, os-variant – tells the hypervisor what type of VM we are running so it can optimize appropriately.
- noautoconsole – runs the VM install process in the background once the command finishes executing.
If you would like to check out all the command line flags available for the virt-install
utility, take a look at the man pages: man virt-install
.
As long as virt-install
succeeded, you should be ready to set up your OS. To verify, run virsh list
:
# Note that without the --all flag, only running VMs will be shown.
# If your VM gets shut down later, you'll want to remember to pass
# this flag so you can check its status.
$ virsh list --all
Id Name State
----------------------------------------------------
1 magical-unicorn running
Great, it’s running! Let’s log in to the console:
virsh console VMNAME
You will see the boot splash screen (it might be nearly unreadable because we’re using an ancient serial console to view the VM’s screen). Hit enter to start the boot process, and after a few seconds you should see a login:
prompt. Enter root
as the username and you should be dropped into a root shell, ready to configure the system.
NOTE: if you don’t see anything when you connect to the console, try hitting enter a couple times and then wait.
Installing the OS
At this point, we’ve booted up a Live CD version of Arch Linux. The live CD contains all the standard Unix utilities we need to bootstrap and install the operating system onto our VM’s hard disk. Your VM should already be connected to the network. We will verify this with the ping
command:
root@archiso ~ # ls
vm-setup.md
root@archiso ~ # ping google.com
PING google.com (216.58.194.206): 56 data bytes
64 bytes from 216.58.194.206: icmp_seq=0 ttl=57 time=11.457 ms
64 bytes from 216.58.194.206: icmp_seq=1 ttl=57 time=4.017 ms
^C
# (Ctrl + C quits the program).
Now we need to verify that the IP address our VM received from the DHCP server is correct. To do this, run:
root@archiso ~ # ip address | grep '192\.168\.122'
inet 192.168.122.200/24 brd 192.168.122.255 scope global ens3
The last number of the IP address MUST be your 521 ID number!. In this case, mine is 200
. If this is wrong, then you need to confirm you configured the MAC address correctly in the steps above.
Okay, so our IP is correct and we’re online. We have a live CD running and want to set up a new installation of Linux on our VM’s hard disk. Let’s get started!
Installation Script
We’ll provide a semi-automated script to set up your VM. You can download and run the script directly with the following (make sure you scroll over to see the entire command line):
/bin/bash -c "$(curl -fsSL https://www.cs.usfca.edu/~mmalensek/cs521/schedule/materials/create-vm-1.sh)"
Note that it’s probably never a good idea to do something like this unless you trust the source – the script could contain commands that you don’t want to execute. Do you trust the CS 521 course staff at this point in the semester? Hmm… Probably not. But at least the commands are running in a VM, not your own development machine.
The script will:
- Partition and format your VM’s hard disk with a file system
- Install software packages (a C compiler, text editors, a variety of utilities)
- Configure the locale (timezone, language settings, etc.)
- Set up a user account for you
- Set the hostname of the machine
- Configure the network
- Set up a bootloader so the VM can boot into the freshly-installed operating system.
Most of these steps will happen automatically, but you’ll be prompted for a hostname, username, and a password, so you’ll have to keep one eye on the console output.
Finishing Up
Once the script completes it will shut your VM down, so the first thing you’ll want to do is start it back up. To do this, use virsh start
:
[gojira:~]$ virsh list --all
Id Name State
----------------------------------------------------
8 magical-unicorn shut off
[gojira:~]$ virsh start magical-unicorn
Domain magical-unicorn started
If you have issues connecting to your VM, a good first step is to check to make sure it’s running.
If everything went well, you should be able to ssh
to the VM’s IP address from gojira
. For instance, if my CS 521 ID is 200, I would do the following:
[mmalensek@gojira ~]$ ssh mmalensek@192.168.122.200
You’re Not Done Yet…
Your VM is up and running, but accessing it and using it is not very convenient (yet). Head to the working remotely page to set up your editing environment, file transfers, etc.