Jump to navigation Jump to search

Virtual Machine (KVM)

6,486 bytes added, 12:47, 17 August 2020
Create Workstation (GUI): Added note on cancelling he command
Note that VMs are known as '''domains''' in the KVM world.
== Prerequisites ==
This guide assumes you have a basic working environment, run the <code>kvm-ok</code> command to sanity check...
== Create Virtual Machine ==
{| class="vwikitable"
| <code>os-type</code> || Linux || OS family, can be Linux, Solaris, Unix or Windows
| <code>os-variant</code> || ubuntu16.04 || Distribution type for the above (run <code>osinfo-query os</code> to view what is available)|-| <code>ram</code> || 2048 || vRAM in GB|-| <code>vcpus</code> || 2 || vCPUs (cores)|-| <code>vcpus</code> || 2 || vCPUs (cores)|-| <code>disk path</code> || /vm-store/images/VM-Name.img,bus=virtio,size=50 || Virtual disk path, using virtio bus and with a 50GB disk|-| <code>graphics</code> || none || If noneset, VM will be created with a serial display output (as opposed to VNC window)|-| <code>cdrom</code> || /home/user/cdrom.iso || Path to installation ISO|-| <code>network</code> || bridge:br0 || Network connection details
=== Create Server (no GUI) ===
Update paths to reflect where install ISO, and where VM disk files are intended to be.
The <code>--extra-args "console=ttyS0"</code> option allows a local console to be accessed from the host machine (to allow OS install etc before the VM is on a network), though note that it can't be used with <code>--cdrom</code>, so <code>--location</code> has been used instead.
virt-install \
--virt-type=kvm \
--name server-name \
--ram 1024 \
--vcpus=1 \
--os-variant=ubuntu16.04 \
--network=bridge=br0,model=virtio \
--graphics none \
--disk path=/mnt/md0/kvm/vm/server-name.img,size=20,bus=virtio \
--extra-args "console=ttyS0" \
--location /mnt/md0/kvm/iso/ubuntu-16.04.3-server-amd64.iso
You should be presented with the console of the VM as it installs, however if you lose connection etc, connect to the console of the server using <code>virsh console <server-name></code>. Make sure you set a static IP and install SSH during setup (select '''OpenSSH server''' during ''Software selection'' section). Once installation is completed, SSH to the server and setup normal console access (as the instructions in the section below). Its highly recommended that you follow the steps below to ensure that Console access is available via the KVM host if needed.
==== Console Access ====
# Update the <code>/etc/default/grub</code>
# Add <code>console=ttyS0</code> to the config line <code>GRUB_CMDLINE_LINUX_DEFAULT</code>
#* EG <code> GRUB_CMDLINE_LINUX_DEFAULT="quiet splash console=ttyS0" </code>
# Update grub
#* <code>update-grub</code>
# Restart the guest machine
Connect using <code>virsh console <server-name></code>, you may need to hit Return once to show the logon prompt.
=== Create Workstation (GUI) ===
virt-install \
--virt-type=kvm \
--name ubuntu-desktop \
--ram 2048 \
--vcpus=1 \
--os-variant=ubuntu16.04 \
--cdrom=/mnt/md0/kvm/iso/ubuntu-16.04.3-desktop-amd64.iso \
--network=bridge=br0,model=virtio \
--graphics vnc,listen= \
--disk path=/mnt/md0/kvm/vm/ubuntu-desktop.img,size=40,bus=virtio
Once the command has got as far as <code>Waiting for installation to complete.</code> you need to connect to the VNC console session, to find the port number to connect on query the config from ''another'' SSH session connected to the host (typically VNC uses ports starting from 5900 upwards). Cancelling the command causes the VM/domain to power off, which you won't want to happen until you've completed setup of the VM.
virsh dumpxml ubuntu-desktop | grep vnc
== Move Virtual Machine ==
If you want to move your domain/VM to another KVM host and don't have shared storage you can manually copy the data and VM config across to another host and import it.
# Shutdown the virtual machine
#* Preferably from the OS so it gets a graceful shutdown, alternatively stop the VM from KVM
# Export the config
#* EG <code> virsh dumpxl <VMname> > /tmp/VMName.xml</code>
# Copy the VM disk file(s) and config XML to their new host
#* Disk file(s) should go the location from which your VM's will run from
# Update the config, if necessary...
#* Disk file path - If the VM disks are in a different path on the new server up date the path in the XML file, look for <code>source file=</code>
#* CPU type - If the physical CPU type is different on the new host, you may need to update the VM config to allow for this, update the <code>cpu mode</code> config to match the capabilities of the destination host, or just set to <code><cpu mode='host-passthrough'/></code>. See for more info.
# Import the VM
#* EG <code> virsh define /path/to/VMName.xml </code>
# Start the VM
#* EG <code> virsh start <VMName> </code>
== Add Disk ==
# Create new disk image file
#* EG <code> qemu-img create -f qcow2 /var/lib/libvirt/images/vm-name-disk1.img 20G </code>
# Attach disk image to virtual machine
#* Use <code>df</code> in the VM to determine next disk label, eg <code>vdb</code>
#* EG <code> qemu-img create -f qcow2 /var/lib/libvirt/images/vm-name-disk1.img 20G -o preallocation=full </code>
#** To create a thin provisioned file use the following (however you may find the disk the OS sees is small (~200K), if so, use the command above)
#** EG <code> virsh attach-disk vm-name /var/lib/libvirt/images/vm-name-disk1.img vdb --cache none </code>
# Update file owner and group to match other disk images
#* EG <code> chown libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img </code>
#* EG <code> chgrp libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img </code>
# Attach disk
#* EG <code> virsh attach-disk --domain vm-name /var/lib/libvirt/images/vm-name-disk1.img --target vdb --persistent --config --live </code>
# In the VM, format the disk using defaults..
#* EG <code> fdisk /dev/vdb </code>
#** EG <code>Command: n </code>
#** EG <code>Partition type: p, </code>
#** EG <code>Partition number: <default>, </code>
#** EG <code>First sector: <default> </code>
#** EG <code>Last sector: <default> </code>
#** EG <code>Command: w </code>
# Format new partition
#* EG <code> mkfs.ext4 /dev/vdb1 </code>
# Create mount directory
#* EG <code> mkdir /vdb1/ </code>
# Mount the the disk
#* EG <code> mount /dev/vdb1 /vdb1/ </code>
# Add an appropriate entry to fstab so the disk gets mounted on next boot
#* EG <code> /dev/vdb1 /vdb1 ext4 defaults 0 0 </code>
== Other Config ==
=== Auto Start ===
To ensure that a VM domain starts with the host server issue the following commands (replace <code>vm-name</code> with the name of your VM
virsh autostart vm-name
To disable issue
virsh autostart vm-name --disable

Navigation menu