Skip to content

Instantly share code, notes, and snippets.

@mbodo
Last active May 1, 2024 19:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbodo/f0c1432a52cc62737cb6084e043ef3e0 to your computer and use it in GitHub Desktop.
Save mbodo/f0c1432a52cc62737cb6084e043ef3e0 to your computer and use it in GitHub Desktop.
KVM Cheatsheet

QEMU/KVM Cheatsheet

Create KVM image with virt-build

cd /var/lib/libvirt/qemu/

sudo virt-builder fedora-26 \
--arch x86_64 \
--size 20G \
-m 8192 \
--root-password password:{some_password} \
--format raw

sudo virt-install --name fedora26 \
--ram 8192 ]
--vcpus=4 \
--disk path=/var/lib/libvirt/qemu/fedora-26.img \
--rng /dev/urandom \
--import

Create KVM template

  1. Run default virt-sysprep

    sudo virt-sysprep -d {your_vm}
    

    To change default operations:

    • First list available operations
    sudo virt-sysprep --list-operations
    
    Output:
    abrt-data * Remove the crash data generated by ABRT
    bash-history * Remove the bash history in the guest
    blkid-tab * Remove blkid tab in the guest
    ca-certificates   Remove CA certificates in the guest
    ...
    
    • Than specify the operations, in this case only bash_history and blkid will run
    sudo virt-sysprep --operations bash_history,blkid-tab -d {your_vm}
    
  2. Rename vm

    sudo virsh domrename old_name_vm new_name_vm
    

Clonning KVM image from template

sudo virt-clone -o template_vm -n your_new_vm_name -f /var/lib/libvirt/qemu/your_new_vm_name.img
sudo virsh list --all (you should see)

Id    Name                           State
---------------------------------------------
-     your_new_vm_name               shut off

Edit KVM image

sudo virsh edit your_vm

<domain type='kvm'>
  <name>fedora26-vm1</name>
...
</domain>

Example: Change KVM img path

we have

sudo ls -la /var/lib/libvirt/your_vm.img

rw-------.  1 qemu qemu  21474836480 Aug 29 17:33 your_vm.img

we want to move img to var/lib/libvirt/qemu/your_vm.img

  1. Shutdown vm to edit
sudo virsh shutdown your_vm
  1. Enter
sudo virsh edit your_vm
  1. Edit old: new:
  old:
  
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/your_vm.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    ...
  </devices>
  
  new:
  
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/qemu/your_vm.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    ...
  </devices>

  1. Start vm
sudo virsh start your_vm

Setup KVM image after clonning (RHEL based for testing)

Log to KVM console:

sudo virsh console {your newly created KVM VM} - e.g. sudo virsh console centos7.3-vm1

Note: to escape press Ctrl+]

Stop(as root):

systemctl stop firewalld.service
systemctl disable firewalld.service

systemctl stop NetworkManager.service
systemctl disable NetworkManager.service

Edit:

vi /etc/sysconfig/network-scripts/ifcfg-etho0
HWADDR="{ get HW address from ip a}

read !ip a 

then

systemctl start network.service
systemctl enable network.service

KVM change default pool for libvirt

  1. Backup existing one
virsh pool-dumpxml default > default.xml
virsh pool-destroy default
virsh pool-undefine default

Links:

KVM snapshots

  • List KVM image snapshots

your_vm_name - e.g. centos7

sudo virsh snapshot-list your_vm_name 
 Name                 Creation Time             State
------------------------------------------------------------
 base                 2017-05-15 22:50:43 +0200 shutoff
 base-upgrade         2017-05-16 08:34:53 +0200 shutoff
 some-next-tag        2017-05-21 22:15:00 +0200 shutoff
  • Create KVM image snapshot with custom parameters
sudo virsh snapshot-create-as your_vm_name --name "{name}" --description "{description}" --atomic

list to verify

sudo virsh snapshot-list your_vm_name
 Name                 Creation Time             State
------------------------------------------------------------
 {name}               2017-05-15 22:50:43 +0200 shutoff

  • Revert to previous KVM image snapshot
sudo virsh snapshot-revert your_vm_name --snapshotname "{snapshot}"

KVM convert raw image to VirtualBox VDI format

your_vm_name - e.g. centos7.4

sudo qemu-img convert -O vdi {your_vm_name}.img {your_vm_name}.vdi

Libvirt Networking

  • Get network address for running domain
Example:

sudo virsh domifaddr your_vm

Output:

 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:41:24:c8    ipv4         192.168.122.28/24

Links:

KVM Commands

  • List vm block devices
Example:

sudo virsh domblklist your_vm

Output:

Target     Source
------------------------------------------------
hda        /var/lib/libvirt/qemu/your_vm.img
  • Get qemu image info (for qcow2 format)
Example:

sudo qemu-img info /var/lib/libvirt/qemu/your_vm.qcow2

Output:

image: /var/lib/libvirt/qemu/your_vm.qcow2
file format: qcow2
virtual size: 100G (107374182400 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
  • Get qemu image info (for img format)
Example:

sudo qemu-img info /var/lib/libvirt/qemu/your_vm.img

Output:

image: /var/lib/libvirt/qemu/your_vm.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 1.9G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment