kvm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
kvm [2019/02/09 09:39] – [Table] adminkvm [2021/02/14 09:36] (current) – [Edit networks] dani
Line 1: Line 1:
 ====== KVM manual ====== ====== KVM manual ======
- 
- 
- 
- 
  
 ===== General ===== ===== General =====
  
 This document shows and describes the most important commands for a KVM environment. This document shows and describes the most important commands for a KVM environment.
- 
- 
- 
  
 ===== Host setup ===== ===== Host setup =====
  
 Before you can start with any virtual machine, you have to install the following packages first: Before you can start with any virtual machine, you have to install the following packages first:
- 
- 
  
 <code bash> <code bash>
 dnf -y install virt-install libvirt-daemon-kvm qemu-kvm libvirt-client qemu libvirt-daemon dnf -y install virt-install libvirt-daemon-kvm qemu-kvm libvirt-client qemu libvirt-daemon
 </code> </code>
- 
  
 It’s not a bad idea, to check, if your host system is virtualisation capable. It’s not a bad idea, to check, if your host system is virtualisation capable.
- 
- 
  
 <code bash> <code bash>
Line 68: Line 56:
    LXC: Checking for cgroup 'blkio' controller mount-point                   : PASS    LXC: Checking for cgroup 'blkio' controller mount-point                   : PASS
 </code> </code>
- 
  
 | **Note:**  | The warning about ''%%IOMMU%%'' can be ignored.  | | **Note:**  | The warning about ''%%IOMMU%%'' can be ignored.  |
- 
  
 ==== Test installation tree ==== ==== Test installation tree ====
- 
  
 <code bash> <code bash>
Line 80: Line 65:
 curl -O http://192.168.122.1/centos/7.4/os/ curl -O http://192.168.122.1/centos/7.4/os/
 </code> </code>
- 
- 
- 
- 
  
 ===== Environment ===== ===== Environment =====
- 
  
 <code bash> <code bash>
 virsh dumpxml <domain name> > <outputfile>.xml virsh dumpxml <domain name> > <outputfile>.xml
 </code> </code>
- 
- 
  
 <code bash> <code bash>
 virsh create <outputfile>.xml virsh create <outputfile>.xml
 </code> </code>
- 
- 
- 
  
 ===== Basic commands ===== ===== Basic commands =====
- +  * Completely delete a VM:<code bash>
- +
-<code bash>+
 virsh --connect qemu:///system destroy fedora27 virsh --connect qemu:///system destroy fedora27
 virsh --connect qemu:///system undefine fedora27 virsh --connect qemu:///system undefine fedora27
-</code> +</code>| **Note:**  | This does not delete the data on the disk (Logical Volume or image)! 
- +  * Unordered List ItemEdit/configure the settings of a virtual machine (domain):<code bash>
- +
-| **Note:**  | This does not delete the data on the disk (Logical Volume or image)! +
- +
- +
- +
-<code bash>+
 virsh edit <domain name> virsh edit <domain name>
 </code> </code>
- +  * Unordered List ItemList all (also not started VM's):<code bash>
- +
- +
-<code bash>+
 virsh --connect qemu:///system list --all virsh --connect qemu:///system list --all
  Id    Name                           State  Id    Name                           State
Line 128: Line 92:
      fedora27                       shut off      fedora27                       shut off
 </code> </code>
- +  * Start a console session for a VM:<code bash>
- +
- +
-<code bash>+
 virsh --connect qemu:///system console centos7 virsh --connect qemu:///system console centos7
 +</code>
 +  * Setup autostart (when booting the KVM server) for a VM:<code bash>
 +virsh --connect qemu:///system autostart centos7
 </code> </code>
  
- +| **Note:**  | To remove autostart toggle, just run this command again!  |
  
 ===== Installation of VM’s ===== ===== Installation of VM’s =====
  
 If you want to add a second network interface, you should add it after the ''%%kickstart%%'' installation. If there are two or more interfaces configured, the installer (''%%dracut%%'') does not find the network based installation files. Here two example domains, one is a Fedora server and one is a CentOS server. If you want to add a second network interface, you should add it after the ''%%kickstart%%'' installation. If there are two or more interfaces configured, the installer (''%%dracut%%'') does not find the network based installation files. Here two example domains, one is a Fedora server and one is a CentOS server.
- 
  
 ==== Fedora ==== ==== Fedora ====
  
 In this example the new virtual machine or domain (fedora27) will be installed using kickstart. Following features are given: In this example the new virtual machine or domain (fedora27) will be installed using kickstart. Following features are given:
- 
  
   * The disk is a logical volume (''%%/dev/mapper/sysvg-fedora27%%'')   * The disk is a logical volume (''%%/dev/mapper/sysvg-fedora27%%'')
Line 152: Line 113:
   * The VM has two network interfaces, one for internal guest/host comunication (the default network), and one for external connections via ''%%macvtap%%'' protocol   * The VM has two network interfaces, one for internal guest/host comunication (the default network), and one for external connections via ''%%macvtap%%'' protocol
   * The installation tree lifes on a webserver ( http://dangiga.danlocal.net/fedora/27/os/ )   * The installation tree lifes on a webserver ( http://dangiga.danlocal.net/fedora/27/os/ )
- 
- 
  
 <code bash> <code bash>
Line 165: Line 124:
 virsh --connect qemu:///system console fedora27 virsh --connect qemu:///system console fedora27
 </code> </code>
- 
  
 | **Note:**  | In this example, we install a second network interface for direct connection to physical interface of the KVM host!  | | **Note:**  | In this example, we install a second network interface for direct connection to physical interface of the KVM host!  |
  
- +**Info:**  | If you want to configure a fix MAC address, use ''%%--mac=52:54:00:f3:30:0f%%'' switch in the ''%%--network%%'' context!  |
-||If you want to configure a fix MAC address, use ''%%--mac=52:54:00:f3:30:0f%%'' switch in the ''%%--network%%'' context!| +
- +
  
 ==== Centos ==== ==== Centos ====
- 
 In this example the new virtual machine or domain (centos7) will be installed using kickstart. Following features are given: In this example the new virtual machine or domain (centos7) will be installed using kickstart. Following features are given:
- 
  
   * The disk is a logical volume (''%%/dev/mapper/sysvg-centos7%%'')   * The disk is a logical volume (''%%/dev/mapper/sysvg-centos7%%'')
Line 183: Line 136:
   * The VM has one network interfaces for internal guest/host comunication (the default network) only   * The VM has one network interfaces for internal guest/host comunication (the default network) only
   * The installation tree lifes on a webserver ( http://dangiga.danlocal.net/centos/7.4/os/ )   * The installation tree lifes on a webserver ( http://dangiga.danlocal.net/centos/7.4/os/ )
- 
- 
  
 <code bash> <code bash>
Line 195: Line 146:
 virsh --connect qemu:///system console centos7 virsh --connect qemu:///system console centos7
 </code> </code>
- 
- 
- 
- 
  
 ===== Networking ===== ===== Networking =====
- +  * List all networks:<code bash>
- +
-<code bash>+
 virsh net-list --all virsh net-list --all
 </code> </code>
- +  * Attach a network interface to a virtual machine (domain):<code bash>
- +
- +
-<code bash>+
 virsh attach-interface --domain <vm name> --type bridge --source virbr0 --model virtio --config --live virsh attach-interface --domain <vm name> --type bridge --source virbr0 --model virtio --config --live
-</code> +</code><code bash>
- +
- +
- +
-<code bash>+
 virsh attach-interface --domain <vm name> --type direct --source enp0s31f6 --model virtio --config --live virsh attach-interface --domain <vm name> --type direct --source enp0s31f6 --model virtio --config --live
 </code> </code>
  
 +| **Note:**  | The ''%%--live%%'' switch only works, if the VM is started!  |
  
-||The ''%%--live%%'' switch only works, if the VM is started!| +  * Unordered List ItemIf you want to figure out the IP addresss of one or more VM’s, you can use the following command executed on the KVM host:<code bash>
- +
- +
-If you want to figure out the IP addresss of one or more VM’s, you can use the following command executed on the KVM host+
- +
- +
- +
-<code bash>+
 vm=fedora27 vm=fedora27
 for mac in `virsh domiflist $vm |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do for mac in `virsh domiflist $vm |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do
Line 233: Line 165:
 done done
 </code> </code>
- 
  
 ==== Edit networks ==== ==== Edit networks ====
  
-The ''%%virsh%%'' command also provides a editor for a network configuration. In this example the default network is edited. +  * Unordered List ItemThe ''%%virsh%%'' command also provides a editor for a network configuration. In this example the default network is edited.<code bash>
- +
- +
- +
-<code bash>+
 virsh net-edit default virsh net-edit default
 </code> </code>
- +  * Show the network configuration of a network (here ''default'' net):<code bash>
- +
- +
-<code bash>+
 virsh net-dumpxml default virsh net-dumpxml default
-</code> +</code>Output:<code bash>
- +
- +
- +
-<code bash>+
 <network> <network>
   <name>default</name>   <name>default</name>
Line 264: Line 184:
   <bridge name='virbr0' stp='on' delay='0'/>   <bridge name='virbr0' stp='on' delay='0'/>
   <mac address='52:54:00:65:80:59'/>   <mac address='52:54:00:65:80:59'/>
-  <ip address='192.168.122.1' netmask='255.255.255.0'> (1)+  <ip address='192.168.xxx.1' netmask='255.255.255.0'> (1)
     <dhcp>     <dhcp>
-      <range start='192.168.122.40' end='192.168.122.254'/> +      <range start='192.168.122.40' end='192.168.xxx.254'/> 
-      <host mac='52:54:00:f3:30:0f' ip='192.168.122.10'/> (2) +      <host mac='52:54:00:f3:30:0f' ip='192.168.xxx.xx'/> (2) 
-      <host mac='52:54:00:f3:30:1f' ip='192.168.122.11'/> +      <host mac='52:54:00:f3:30:1f' ip='192.168.xxx.xx'/> 
-      <host mac='52:54:00:f3:30:2f' ip='192.168.122.12'/> +      <host mac='52:54:00:f3:30:2f' ip='192.168.xxx.xx'/>
-      <host mac='52:54:00:f3:30:3f' ip='192.168.122.13'/> +
-      <host mac='52:54:00:f3:30:4f' ip='192.168.122.14'/> +
-      <host mac='52:54:00:f3:30:5f' ip='192.168.122.15'/> +
-      <host mac='52:54:00:f3:30:6f' ip='192.168.122.16'/> +
-      <host mac='52:54:00:43:56:0e' ip='192.168.122.17'/> +
-      <host mac='52:54:00:43:56:1e' ip='192.168.122.18'/> +
-      <host mac='52:54:00:43:56:2e' ip='192.168.122.19'/> +
-      <host mac='52:54:00:43:56:3e' ip='192.168.122.20'/> +
-      <host mac='52:54:00:43:56:4e' ip='192.168.122.21'/> +
-      <host mac='52:54:00:43:56:5e' ip='192.168.122.22'/> +
-      <host mac='52:54:00:43:56:6e' ip='192.168.122.23'/> +
-      <host mac='52:54:00:43:56:7e' ip='192.168.122.24'/>+
     </dhcp>     </dhcp>
   </ip>   </ip>
 </network> </network>
 </code> </code>
- 
  
 |**1**|Network IP and netmask                         | |**1**|Network IP and netmask                         |
 |**2**|Define a fix IP address for a given MAC address| |**2**|Define a fix IP address for a given MAC address|
  
- +  * Unordered List ItemAfter changing anything, you must activate the changed configuration, you have to reload the network:<code bash>
-After changing anything, you must activate the changed configuration, you have to reload the network: +
- +
- +
- +
-<code bash>+
 virsh net-destroy default && virsh net-start default virsh net-destroy default && virsh net-start default
 </code> </code>
- +  * Delete a vm (host) in a network (here ''default'' net):<code bash> 
- +virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:f3:30:2f' name='hostname' ip='192.168.xxx.xxx'/>" --live --config 
- +</code> 
-<code bash> +  * Add a network configuration (host) live:<code bash> 
-virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:f3:30:2f' name='ds' ip='192.168.122.12'/>" --live --config+virsh net-update default add ip-dhcp-host "<host mac='52:54:00:43:56:84' name='hostname' ip='192.168.xxx.xxx'/>" --live --config
 </code> </code>
- 
- 
- 
- 
  
 ===== Harddisks ===== ===== Harddisks =====
Line 314: Line 212:
 It’s also possible to add a disk to a running VM. The disk can be a disk image or a LVM volume. It’s possible to resize the disk, but, you have to reboot the client or relaod the kernel disk table (''%%partx%%'', only CentOS >=7). It’s also possible to add a disk to a running VM. The disk can be a disk image or a LVM volume. It’s possible to resize the disk, but, you have to reboot the client or relaod the kernel disk table (''%%partx%%'', only CentOS >=7).
  
- +  * Add (persistent) a disk to a VM:<code bash>
- +
-<code bash>+
 virsh attach-disk ds --source /dev/sysvg/ds_test --target vdb --persistent virsh attach-disk ds --source /dev/sysvg/ds_test --target vdb --persistent
-</code>+</code> \\ This adds a new disk to the ''%%ds%%'' VM as ''%%/dev/vdb%%'' persistently.
  
 +| **Note:**  | The disk is a logical volume on the KVM host  |
  
-This adds a new disk to the ''%%ds%%'' VM as ''%%/dev/vdb%%'' persistently.+==== Resize (LVM) disk ====
  
 +  * First increase size of the LV on KVM host:<code bash>
 +lvresize -G <size>G /dev/sysvg/<lv name>
 +</code>
 +  * Configure the size of that domain and disk using ''virsh'':<code bash>
 +virsh blockresize <domain> /dev/sysvg/<lv name> --size <size>G
 +</code> \\ Now, you can resize the disk on the domain (VM).
  
-||The disk is a logical volume on the KVM host| 
  
  • kvm.1549701561.txt.gz
  • Last modified: 2019/02/09 09:39
  • by admin