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, but this guide will get you up and running.
In this lab, you’ll create a virtual machine using Kernel-based Virtual Machine (KVM) virtualization in Linux.
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)
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. You can confirm what this is by running
whoami
in your terminal. - 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.
- The path of the install media. Installation disk images are located in
/home2/iso
(try runningls /home2/iso
to see what’s in there).
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 look almost exactly like normal characters but won’t work in your terminal.
$ virt-install \
--name=VMNAME \
--cpu host --vcpus=4 \
--memory=2000 \
--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-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.
TIP: the calculator on your computer probably has a programmer mode that can do the conversion, or you can do it from your terminal like this:
printf "%x\n" 105
(where 105 is the decimal ID you’re converting).
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.
- boot, console, graphics – configures the serial console we’ll use to set up our VM
- 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 1: if you need to exit the console for some reason, you can press Control + ]
(control and the right square bracket). If you rerun virsh console VMNAME
later, you’ll pick up in the same place – your VM keeps running on gojira even if you move on to doing something else.
NOTE 2: 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 -- standard on Unix systems).
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'
# This should output something like:
inet 192.168.122.105/24 brd 192.168.122.255 scope global ens3
# The last number of the IP address should be your 521 ID.
The last number of the IP address MUST be your 521 ID number!. In this case, mine is 105
. 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? 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 core software packages
- Configure the locale (timezone, language settings, etc.)
- Set up a bootloader so the VM can boot into the freshly-installed operating system.
You are responsible for:
- Setting up a user account
- Setting the hostname of the machine
- Configuring the network
Basic Configuration
Once the script finishes, you will see “Preliminary setup complete!” – that’s your cue to start the basic configuration.
First, we need to install some software packages on our new system. Let’s install them with pacman
:
[root@archiso /]# pacman -Sy \
base-devel clang gdb git grub inetutils \
man man-pages nano python rustup vi vim wget
# Note: you can accept the default packages (just press enter) for the
# 'base-devel' meta-package as well as for the 'man' package.
( ... lots of packages install ... )
Make sure you perform this step! Some of the following configuration steps require these packages.
Creating a User Account
# It's a bad idea to always be logged in as root, so let's create our own user
# account with the useradd command. The -m flag creates a home directory for
# the user (/home/<username>).
# *** Remember: replace VMUSERNAME with your usual CS username!! ***
[root@archiso /]# useradd -m VMUSERNAME -g wheel
# Set the password for our new user
[root@archiso /]# passwd VMUSERNAME
(enter whatever your VMPASSWORD is at the prompt)
# Let's test out the account. Since we're root, we can actually use the
# substitute user (su) command to switch over:
[root@archiso /]# su - VMUSERNAME
[VMUSERNAME@archiso ~]$
# Now let's make sure we can use sudo:
[VMUSERNAME@archiso ~]$ sudo ls /root
# (Nothing should display. If you get a permission denied error,
# something went wrong and you probably need to retrace your steps.)
# Great, it worked. Let's switch back to root. To do this, we exit from our
# current session back to the original login session.
[VMUSERNAME@archiso ~]$ exit
Setting the Hostname
There are two hard problems in computer science: cache invalidation, naming things, and off-by-one errors. We have to tackle one of these here: naming your VM. The /etc/hostname
file contains the hostname of the machine, so let’s create this file from our shell.
[root@archiso /]# echo "VMNAME" > /etc/hostname
# When I executed this command, I did the following:
# *** You should come up with your own name, though!!! ***
[root@archiso /]# echo "magical-unicorn" > /etc/hostname
Network Configuration
One of the last things we need to do is set up the network so we (1) assign an IP address to the VM on boot, and (2) access our VM remotely with ssh
so we don’t need to use the console.
[root@archiso /]# systemctl enable dhcpcd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpcd.service → /usr/lib/systemd/system/dhcpcd.service.
[root@archiso /]# systemctl enable sshd
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.
Alright, we should be all set with the network now.
Finishing Up
Once you’ve created a user account, set the hostname, and configured the network, you are done! Run the exit
command to finish the installation process.
Once the installation is over your VM will shut 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 105, I would do the following:
[mmalensek@gojira ~]$ ssh mmalensek@192.168.122.105
If you can log in, congratulations! You’ve brought a brand new VM into the world! Feel free to explore and exit
when you are ready to log off.
If the ssh
command did not work, double check that your IP address is correct. If it is, you can try running virsh console VMNAME
to see if you can log in to the console directly and then contact the course staff for help.