May 26 2021

KVM – detach disk

sudo virsh detach-disk --domain test --persistent --live --target vdb

May 12 2021

KVM – Add / Attach Disk

Create disk:
qemu-img create \
-f qcow2 \
-o lazy_refcounts=on,preallocation=falloc \
$FILEPATH \
[size]G

qemu-img comes with various options for setting the allocation when creating new disk images.

preallocation=metadata – allocates the space required by the metadata but doesn’t allocate any space for the data. This is the quickest to provision but the slowest for guest writes.

preallocation=falloc – allocates space for the metadata and data but marks the blocks as unallocated. This will provision slower than metadata but quicker than full. Guest write performance will be much quicker than metadata and similar to full.

preallocation=full – allocates space for the metadata and data and will therefore consume all the physical space that you allocate (not sparse). All empty allocated space will be set as a zero. This is the slowest to provision and will give similar guest write performance to falloc.

Convert disk:
mv disk.qcow2 disk.qcow2.bak
qemu-img convert -O qcow2 -o lazy_refcounts=on,preallocation=falloc disk.qcow2.bak disk.qcow2

 

Attach disk:
virsh attach-disk [vm ID] \
--source /path/to/disk.qcow2 \
--target vd[x] \
--persistent \
--subdriver qcow2

Apr 29 2021

Enable Virsh Console Access For KVM Guests

To do so, log in to your guest machine via SSH or Virt-manager or Cockpit and run the following commands to enable and start a serial console:

systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service

connect to console from Host
virsh console

Feb 15 2021

EFI boot entry

show
efibootmgr
root@hphost:~# efibootmgr
BootCurrent: 0002
Timeout: 0 seconds
BootOrder: 0000,0004,0002,0001
Boot0000* debian
Boot0001* ubuntu
Boot0002* rEFInd Boot Manager
Boot0004* Windows Boot Manager

change order
sudo efibootmgr -o 0004,0002,0001,0000

change the EFI boot manager timeout
efibootmgr --timeout=4

delete entry
efibootmgr -Bb 0003

Feb 5 2021

KVM – set CPU resources

virsh vcpucount <vm_name>
virsh setvcpus <vm_name> <max-number-of-CPUs> –maximum –config
virsh setvcpus <vm_name> <number-of-CPUs> –config
virsh setvcpus <vm_name> <number-of-CPUs> –live

Jan 24 2021

KVM add interface live and permanent

virsh net-list --all

 Name State Autostart Persistent
----------------------------------------------------
default inactive yes yes
inner-network active yes yes
outer-network active yes yes


virsh attach-interface --domain debian11 --type bridge --source inner --mac 52:54:00:29:5c:2e --model virtio --config --live
Jan 22 2021

KVM – create new virtual network

Create new bridge

sudo ip link add <bridge-name> type bridge

Add interface to bridge

sudo ip link set <ethX> up
sudo ip link set <ethX> master <bridge-name>

Add IP address to bridge and bring it up

sudo ip address add dev <bridge-name> 192.168.0.90/24

sudo ip link set dev <bridge-name> up

 

Create file bridged-network.xml

<network>
    <name>bridged-network</name>
    <forward mode="bridge" />
    <bridge name="<bridge-name>" />
</network>

Add bridged-network to our KVM

sudo virsh net-define bridged-network.xml

 

Activate network and set autostart

sudo virsh net-start bridged-network 
sudo virsh net-autostart bridged-network

Verify

virsh net-list --all
Jan 22 2021

KVM – Migrate VM to another host

  1. copy the VM’s disks from /var/lib/libvirt/images on src host to the same dir on destination host
  2. on the source host run virsh dumpxml VMNAME > domxml.xml and copy this xml to the destination host
  3. on the destination host run virsh define domxml.xml
  4. start the VM.
  • If the disk location differs, you need to edit the xml’s devices/disk node to point to the image on the destination host
  • If the VM is attached to custom defined networks, you’ll need to either edit them out of the xml on the destination host or redefine them as well
    1. On source machine virsh net-dumpxml NETNAME > netxml.xml
    2. copy netxml.xml to target machine
    3. On target machine virsh net-define netxml.xml && virsh net-start NETNAME & virsh net-autostart NETNAME
Jan 8 2021

resize img size for QEMU / KVM

STOP VM !

Add +5G do disk

qemu-img resize chr-7.2.img +5G

Change disk size to 20G

qemu-img resize chr-7.2.img 20G

This example will convert a raw image file named image.img to a qcow2 image file.

qemu-img convert -f raw -O qcow2 image.img image.qcow2

Rescan resized partition

partprobe /dev/vdb

or

growpart /dev/vdb 1

Resize partition

resize2fs /dev/vdb1

Useful commands

virsh destroy vm_name
virsh list
virsh edit vm_name

Set CPU resources

virsh vcpucount <vm_name>
virsh setvcpus <vm_name> <max-number-of-CPUs> --maximum --config
virsh setvcpus <vm_name> <number-of-CPUs> --config
virsh setvcpus <vm_name> <number-of-CPUs> --live
Aug 8 2020

Mount partition and chroot in rescue mode

Run linux from Linux Live (debian.ubuntu, slackware tec.)

Determine what is your main partition

fdisk -l

Example output:

Disk /dev/md2: 9.77 GiB, 10495328256 bytes, 20498688 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/md3: 221.88 GiB, 238236860416 bytes, 465306368 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/md1: 9.77 GiB, 10495328256 bytes, 20498688 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/md0: 3.91 GiB, 4203020288 bytes, 8209024 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

create mount point and mount root partition

mkdir /mnt/md0

mount /dev/md0 /mnt/md0

Mount the necessary file system directories by running the following commands:

mount -t proc /proc /mnt/md0/proc

mount –rbind /sys /mnt/md0/sys

mount –rbind /dev /mnt/md0/dev

Mount /usr and /var partition if necessary

mount /dev/md1 /mnt/md0/usr

mount /dev/md2 /mnt/md0/var

Chroot to md0

chroot /mnt/md0