Difference between revisions of "Virtual Machine (KVM)"

From vwiki
Jump to navigation Jump to search
(Added Add Disk)
 
(6 intermediate revisions by the same user not shown)
Line 43: Line 43:


=== Create Server (no GUI) ===
=== Create Server (no GUI) ===
Update paths to reflect where install ISO, and where VM disk files are intended to be.   
Update <code>location</code> path to reflect where your install ISO is located, and the <code>disk</code> path for 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.
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.  Note that where installing Ubuntu guests v22.04 and upwards the kernel and initrd paths on the ISO need to be specified with the ISO location.


<pre>
<pre>
virt-install \
virt-install \
--virt-type=kvm \
--virt-type=kvm \
--name server-name \
--name ubuntu22-vm \
--ram 1024 \
--ram 2048 \
--vcpus=1 \
--vcpus=1 \
--os-variant=ubuntu16.04 \
--os-variant=ubuntu16.04 \
--network=bridge=br0,model=virtio \
--network=bridge=br0,model=virtio \
--graphics none \
--graphics none \
--disk path=/mnt/md0/kvm/vm/server-name.img,size=20,bus=virtio \
--disk path=/mnt/md0/kvm/vm/ubuntu22-vm.img,size=20,bus=virtio \
--extra-args "console=ttyS0" \
--extra-args "console=ttyS0" \
--location /mnt/md0/kvm/iso/ubuntu-16.04.3-server-amd64.iso
--location ubuntu-22.04-live-server-amd64.iso,kernel=casper/vmlinuz,initrd=casper/initrd
</pre>
</pre>


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.
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.
==== Eject CD-ROM ====
If you need to eject the CD-ROM towards the end o the installation process, first identify the disk target name (eg hda, hdb, etc) then eject
virsh edit server-name
virsh change-media server-name hda --eject


==== Console Access ====
==== Console Access ====
Line 83: Line 88:
--cdrom=/mnt/md0/kvm/iso/ubuntu-16.04.3-desktop-amd64.iso \
--cdrom=/mnt/md0/kvm/iso/ubuntu-16.04.3-desktop-amd64.iso \
--network=bridge=br0,model=virtio \
--network=bridge=br0,model=virtio \
--graphics vnc \
--graphics vnc,listen=0.0.0.0 \
--disk path=/mnt/md0/kvm/vm/ubuntu-desktop.img,size=40,bus=virtio
--disk path=/mnt/md0/kvm/vm/ubuntu-desktop.img,size=40,bus=virtio
</pre>
</pre>


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 anothet SSH session connected to the host (typically VNC uses ports starting from 5900 upwards).
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
   virsh dumpxml ubuntu-desktop | grep vnc


Line 109: Line 114:
== Add Disk ==
== Add Disk ==
# Create new disk image file
# Create new disk image file
#* EG <code> qemu-img create -f qcow2 /var/lib/libvirt/images/elk-disk1.img 20G </code>
#* EG <code> qemu-img create -f qcow2 /var/lib/libvirt/images/vm-name-disk1.img 20G </code>
# Attach disk image to virtual machine
# Attach disk image to virtual machine
#* Use <code>df</code> in the VM to determine next disk label, eg <code>vdb</code>
#* 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/elk-disk1.img 20G -o preallocation=full </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)
#** 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 elk /var/lib/libvirt/images/elk-disk1.img vdb --cache none </code>
#** 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
# Update file owner and group to match other disk images
#* EG <code>  chown libvirt-qemu /var/lib/libvirt/images/elk-disk1.img </code>
#* EG <code>  chown libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img </code>
#* EG <code>  chgrp libvirt-qemu /var/lib/libvirt/images/elk-disk1.img </code>
#* EG <code>  chgrp libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img </code>
# Attach disk
# Attach disk
#* EG <code> virsh attach-disk --domain elk /var/lib/libvirt/images/elk-disk1.img --target vdb --persistent --config --live </code>
#* 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..
# In the VM, format the disk using defaults..
#* EG <code> fdisk /dev/vdb </code>
#* EG <code> fdisk /dev/vdb </code>
Line 144: Line 149:
To disable issue
To disable issue
  virsh autostart vm-name --disable
  virsh autostart vm-name --disable
To see what machines are/aren't set to start automatically
virsh list --all --autostart
virsh list --all --no-autostart




[[Category:KVM]]
[[Category:KVM]]

Latest revision as of 12:33, 22 April 2023

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

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 location path to reflect where your install ISO is located, and the disk path for 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. Note that where installing Ubuntu guests v22.04 and upwards the kernel and initrd paths on the ISO need to be specified with the ISO location.

virt-install \
--virt-type=kvm \
--name ubuntu22-vm \
--ram 2048 \
--vcpus=1 \
--os-variant=ubuntu16.04 \
--network=bridge=br0,model=virtio \
--graphics none \
--disk path=/mnt/md0/kvm/vm/ubuntu22-vm.img,size=20,bus=virtio \
--extra-args "console=ttyS0" \
--location ubuntu-22.04-live-server-amd64.iso,kernel=casper/vmlinuz,initrd=casper/initrd

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.

Eject CD-ROM

If you need to eject the CD-ROM towards the end o the installation process, first identify the disk target name (eg hda, hdb, etc) then eject

virsh edit server-name
virsh change-media server-name hda --eject

Console Access

  1. Update the /etc/default/grub
  2. Add console=ttyS0 to the config line GRUB_CMDLINE_LINUX_DEFAULT
    • EG GRUB_CMDLINE_LINUX_DEFAULT="quiet splash console=ttyS0"
  3. Update grub
    • update-grub
  4. 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,listen=0.0.0.0 \
--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 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.

  1. Shutdown the virtual machine
    • Preferably from the OS so it gets a graceful shutdown, alternatively stop the VM from KVM
  2. Export the config
    • EG virsh dumpxl <VMname> > /tmp/VMName.xml
  3. 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
  4. 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.
  5. Import the VM
    • EG virsh define /path/to/VMName.xml
  6. Start the VM
    • EG virsh start <VMName>

Add Disk

  1. Create new disk image file
    • EG qemu-img create -f qcow2 /var/lib/libvirt/images/vm-name-disk1.img 20G
  2. Attach disk image to virtual machine
    • Use df in the VM to determine next disk label, eg vdb
    • EG qemu-img create -f qcow2 /var/lib/libvirt/images/vm-name-disk1.img 20G -o preallocation=full
      • 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 virsh attach-disk vm-name /var/lib/libvirt/images/vm-name-disk1.img vdb --cache none
  3. Update file owner and group to match other disk images
    • EG chown libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img
    • EG chgrp libvirt-qemu /var/lib/libvirt/images/vm-name-disk1.img
  4. Attach disk
    • EG virsh attach-disk --domain vm-name /var/lib/libvirt/images/vm-name-disk1.img --target vdb --persistent --config --live
  5. In the VM, format the disk using defaults..
    • EG fdisk /dev/vdb
      • EG Command: n
      • EG Partition type: p,
      • EG Partition number: <default>,
      • EG First sector: <default>
      • EG Last sector: <default>
      • EG Command: w
  6. Format new partition
    • EG mkfs.ext4 /dev/vdb1
  7. Create mount directory
    • EG mkdir /vdb1/
  8. Mount the the disk
    • EG mount /dev/vdb1 /vdb1/
  9. Add an appropriate entry to fstab so the disk gets mounted on next boot
    • EG /dev/vdb1 /vdb1 ext4 defaults 0 0

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

To see what machines are/aren't set to start automatically

virsh list --all --autostart
virsh list --all --no-autostart