Difference between revisions of "Virtual Machine Management"

From CSLabsWiki
(Create a new VM)
(Create a new VM)
Line 33: Line 33:
# gateway XXX.XXX.XXX.2
# gateway XXX.XXX.XXX.2
ifconfig eth0 down
ifconfig eth0 down
/etc/init.d/networking down
ifconfig eth0 up
ifconfig eth0 up
/etc/init.d/networking down
/etc/init.d/networking up
/etc/init.d/networking up

Latest revision as of 18:16, 2 February 2016

There are few possible things you want to do. Create a new virtual machine instance on the existing infrastructure, create a new base image suitable for use with the infrastructure, or delete a VM.

Create a new VM

This assumes you already have a base image. In the ansible repository there is a script called make_vm. On the VM host, run the following:

sudo -i # root has all the goods in ~
cd ansible
cp /var/lib/libvirt/images/{basevm,newvm_name}.qcow2  # This will probably take a while, get a drink
env VM_NAME=newvm_name VM_DESC="A new VM" ./make_vm > /var/lib/libvirt/xml/newvm_name.xml
virsh define /var/lib/libvirt/xml/newvm_name.xml
virsh autostart newvm_name
virsh start newvm_name
virsh console newvm_name

You may need to hit "enter" for the login prompt to show up. Log in as root and run "dhclient eth0" then "ip address" and find the IP address of the new machine. The IP address can also be changed with the following (XXX are the normal IP inputs for the lab and YYY is the new IP ending):

vim /etc/network/interfaces
# # The loopback network interface
# auto lo
# iface lo inet loopback
# auto eth0
# # The primary network interface
# allow-hotplug eth0
# iface eth0 inet static
#     address XXX.XXX.XXX.YYY
#     netmask
#     gateway XXX.XXX.XXX.2
ifconfig eth0 down
ifconfig eth0 up
/etc/init.d/networking down
/etc/init.d/networking up

The ssh port should also be changed to 13699. This can be done with:

vim /etc/ssh/sshd_config # Change the port to 13699
/etc/init.d/ssh restart

Exit the VM console and on your Ansible machine (which is not the VM host!), make sure you're in a clone of the ansible repository and run:

echo VM_IP_ADDRESS >> inventory
./setup VM_IP_ADDRESS vm-hostname vm-staticip owner-username "owner-ssh-pubkey"

And you're good to go! Make sure to remove the temporary entry from the inventory and add the new static IP, and commit/push the change. You may also want to Create a DNS Entry for the new VM and allow access to it through the Firewall.

Creating a new base image

To the largest extent possible, we use ansible to manage service configuration and machine bringup. Because ansible does all of the necessary configuration, the base images should be as stock as possible.

Creating a VM Environment

The easiest way to use virtualization on Linux is with virt-manager. On Ubuntu, follow [1]. It is packaged on most distros. Make sure to check your distro's documentation, as you likely need to add yourself to a certain group to get permissions to manage virtual machines. Download a setup ISO for the distro you are creating a base image for. Here's some screenshots of the steps to follow.

Virtual Machine Manager 003.png

New VM 004.png

New VM 005.png

Select "browse local" and find the ISO you're using.

Locate ISO media volume 007.png

New VM 008.png

The settings you use here don't particularly matter, since only the resulting disk image will be used.

New VM 009.png

Select 16GiB, which is our default base disk size.

New VM 010.png

Click "finish"

Installing and Configuring Debian

Boot the new VM, and select "install".

VM 012.png

Select the locale.

VM 014.png

VM 015.png

VM 016.png


VM 017.png

The default hostname is fine, Ansible will change it when necessary.

VM 018.png

Leave this empty.

VM 019.png

Enter the COSI Default Root Password.

VM 020.png

VM 021.png

Create the ansible user.

VM 022.png

VM 023.png

Use cspassword.

VM 024.png

VM 025.png

The defaults here are fine.

VM 026.png

VM 027.png

VM 028.png

VM 029.png

Finish partitioning.

VM 030.png

Write changes.

VM 031.png


VM 032.png

Chose US.

VM 033.png

Chose the default.

VM 034.png

No proxy.

VM 035.png

Don't participte in the popularity contest.

VM 036.png

Use the arrow keys and spacebar to deselect everything and select SSH server, then continue.

VM 039.png

Use the defaults for GRUB.

VM 040.png

VM 041.png


VM 042.png

Installation complete!

VM 043.png

Now that installation is complete, some additional setup is needed. Log in as root.

apt install ca-certificates sudo python
gpasswd -a ansible sudo
systemctl enable getty@ttyS0
su ansible
mkdir .ssh
cd .ssh
wget https://raw.githubusercontent.com/COSI-Lab/ansible/master/authorized_keys

The first command installs TLS certificates (needed for the later wget), sudo (so the ansible user can elevate permissions), and Python (needed for ansible). The third command enables a login console on the ttyS0 device, which will be used for 'virsh console' later. The rest is setting up ansible's public key for configuration when a new VM is spinned up from this image.

Finding the finished disk image

It will probably be in /var/lib/libvirt/images/name.qcow2. If not, open the VM in virt-manager and go to the information pane, and select VirtIO Disk 1 and look at the source path.

Deleting a VM

First, triple-check with the maintainer that they want their VM deleted. Then, in a virsh console with suitable privileges, run shutdown vmname. Wait at least a minute for the VM to shutdown (which you can observe with list). If the machine doesn't shutdown quickly, use console or talk to the maintainer to figure out what is wrong. If the maintainer doesn't care about an unclean shutdown, do destroy vmname. Once the VM is off, you can use undefine vmname to remove the VM's definition from libvirtd. Exit the virsh console, and move the VM files /var/lib/libvirt/images/vmname.qcow2 and /var/lib/libvirt/xml/vmname.xml to /var/lib/libvirt/trash and modify the README there to have the date and the VM name. Remember to go through and actually delete these files after at least a week as passed -- if we aren't pressed for disk space, the trash never needs to be emptied, which gives a better experience for maintainers who accidentally forget important data.