Virtual Machine (KVM): Difference between revisions
(Added Move Virtual Machine) |
(→Move Virtual Machine: Updated CPU type / mode info) |
||
Line 101: | Line 101: | ||
# Update the config, if necessary... | # 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> | #* 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 | #* 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 https://www.berrange.com/posts/2018/06/29/cpu-model-configuration-for-qemu-kvm-on-x86-hosts/ for more info. | ||
# Import the VM | # Import the VM | ||
#* EG <code> virsh define /path/to/VMName.xml </code> | #* EG <code> virsh define /path/to/VMName.xml </code> | ||
# Start the VM | # Start the VM | ||
#* EG <code> virsh start <VMName> </code> | #* EG <code> virsh start <VMName> </code> | ||
== Other Config == | == Other Config == |
Revision as of 09:28, 13 April 2020
Note that VMs are known as domains in the KVM world.
Prerequisites
This guide assumes you have a basic working environment, run the kvm-ok
command to sanity check...
root@kvm-host:# kvm-ok INFO: /dev/kvm exists KVM acceleration can be used
Install Media
You need to have a local copy of the appropriate ISO. If you have the ISO file already, upload to your KVM server, alternatively download from the site using wget
- Ubuntu Desktop LTS - http://releases.ubuntu.com/16.04.3/ubuntu-16.04.3-desktop-amd64.iso
Create Virtual Machine
Parameter | Example | Usage |
---|---|---|
name |
VM-Name | Name of virtual machine (typically this should match the intended hostname of the VM) |
description |
"Test VM to be used for X" | Description of virtual machine's purpose etc |
os-type |
Linux | OS family, can be Linux, Solaris, Unix or Windows |
os-variant |
ubuntu16.04 | Distribution type for the above (run osinfo-query os to view what is available)
|
ram |
2048 | vRAM in GB |
vcpus |
2 | vCPUs (cores) |
vcpus |
2 | vCPUs (cores) |
disk path |
/vm-store/images/VM-Name.img,bus=virtio,size=50 | Virtual disk path, using virtio bus and with a 50GB disk |
graphics |
none | If noneset, VM will be created with a serial display output (as opposed to VNC window) |
cdrom |
/home/user/cdrom.iso | Path to installation ISO |
network |
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 --extra-args "console=ttyS0"
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 --cdrom
, so --location
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 virsh console <server-name>
. 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
/etc/default/grub
- Add
console=ttyS0
to the config lineGRUB_CMDLINE_LINUX_DEFAULT
- EG
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash console=ttyS0"
- EG
- Update grub
update-grub
- Restart the guest machine
Connect using virsh console <server-name>
, 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 \ --disk path=/mnt/md0/kvm/vm/ubuntu-desktop.img,size=40,bus=virtio
Once the command has got as far as Waiting for installation to complete.
you need to connect to the VNC console session, to find the port number to connect on query the config from anothet SSH session connected to the host (typically VNC uses ports starting from 5900 upwards).
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
virsh dumpxl <VMname> > /tmp/VMName.xml
- EG
- 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
source file=
- 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
cpu mode
config to match the capabilities of the destination host, or just set to<cpu mode='host-passthrough'/>
. See https://www.berrange.com/posts/2018/06/29/cpu-model-configuration-for-qemu-kvm-on-x86-hosts/ for more info.
- 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
- Import the VM
- EG
virsh define /path/to/VMName.xml
- EG
- Start the VM
- EG
virsh start <VMName>
- EG
Other Config
Auto Start
To ensure that a VM domain starts with the host server issue the following commands (replace vm-name
with the name of your VM
virsh autostart vm-name
To disable issue
virsh autostart vm-name --disable