PoC: KVM: Difference between revisions

From Coolscript
Jump to navigation Jump to search
(Created page with "kvm testsetup at home ==System== vmadmin@ts01:~$ sudo -i root@ts01:~# cat /etc/os-release PRETTY_NAME="Ubuntu 25.10" NAME="Ubuntu" VERSION_ID="25.10" VERSION="25.10 (Questing Quokka)" VERSION_CODENAME=questing ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME...")
 
No edit summary
Line 99: Line 99:
==Installation Screenshots==
==Installation Screenshots==
*VM Installer01
*VM Installer01
[[image:KVMInstall03.png]]
[[image:KVMInstall03.png|800px]]


*VM Installer02
*VM Installer02
[[image:KVMInstall04.png]]
[[image:KVMInstall04.png|800px]]


*VM Installer05
*VM Installer05
[[image:KVMInstall05a.png]]
[[image:KVMInstall05a.png|800px]]


*VM Installer06
*VM Installer06
[[image:KVMInstall06b.png]]
[[image:KVMInstall06b.png|800px]]


*VM Installer07
*VM Installer07
[[image:KVMInstall07.png]]
[[image:KVMInstall07.png|800px]]


*VM Installer08
*VM Installer08
[[image:KVMInstall08.png]]
[[image:KVMInstall08.png|800px]]


*VM Installer09
*VM Installer09
[[image:KVMInstall09.png]]
[[image:KVMInstall09.png|800px]]


*VM Installer10
*VM Installer10
[[image:KVMInstall10a.png]]
[[image:KVMInstall10a.png|800px]]


*VM Installer11
*VM Installer11
[[image:KVMInstall11.png]]
[[image:KVMInstall11.png|800px]]


*VM Installer12
*VM Installer12
[[image:KVMInstall12.png]]
[[image:KVMInstall12.png|800px]]
 
 


==Advanced - Bridging==
==Advanced - Bridging==
Line 166: Line 164:
   <model type='virtio'/>
   <model type='virtio'/>
  </interface>
  </interface>
Domain 'debian13' XML configuration edited.
==Converting a VHDX Windows Disk==
*Inspect the disk:
root@ts01:~# qemu-img info /home/vmadmin/vm-surf01.vhdx
image: /home/vmadmin/vm-surf01.vhdx
file format: vhdx
virtual size: 127 GiB (136365211648 bytes)
disk size: 41 GiB
cluster_size: 33554432
Child node '/file':
    filename: /home/vmadmin/vm-surf01.vhdx
    protocol type: file
    file length: 41 GiB (43994054656 bytes)
    disk size: 41 GiB
*Get the disk ready for KVM
root@ts01:~# cp /home/admin/debian-13.3.0-amd64-netinst.iso /var/lib/libvirt/images
root@ts01:~# chmod 666 /var/lib/libvirt/images/debian-13.3.0-amd64-netinst.iso
root@ts01:~# chmod 777 /var/lib/libvirt/images
*Convert the disk
root@ts01:~# qemu-img convert -p -f vhdx -O qcow2 \
  /home/vmadmin/vm-surf01.vhdx \
  /var/lib/libvirt/images/vm-surf01.qcow2
    (100.00/100%)
*Inspect the converting results
root@ts01:~# qemu-img info /var/lib/libvirt/images/vm-surf01.qcow2
image: /var/lib/libvirt/images/vm-surf01.qcow2
file format: qcow2
virtual size: 127 GiB (136365211648 bytes)
disk size: 37.6 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: /var/lib/libvirt/images/vm-surf01.qcow2
    protocol type: file
    file length: 37.6 GiB (40399208448 bytes)
    disk size: 37.6 GiB
==Install a Win10 VM from a previous disk==
*Install01
[[image:wininst01a.png|800px]]
*Install02
[[image:wininst02.png|800px]]
*Install03
[[image:wininst03.png|800px]]
*Install04
[[image:wininst04a.png|800px]]
*Win10 in running state
[[image:Windows10afterclone.png|1200px]]
==Attach a USB Stick to the VM==
*Get attached devices
**Note: We're after '''ID 0781:5591 SanDisk Corp.'''
root@ts01:~# lsusb
  Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  Bus 001 Device 002: ID 8087:800a Intel Corp. Hub
  Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  Bus 002 Device 002: ID 3938:1032 MOSART Semi. 2.4G RF Keyboard & Mouse
  Bus 002 Device 003: ID 04b3:3025 IBM Corp. NetVista Full Width Keyboard
  Bus 002 Device 004: ID 17aa:1034 VIA Labs, Inc.          USB Hub
  Bus 002 Device 005: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
  Bus 002 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver
  Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  Bus 003 Device 002: ID 17aa:1034 VIA Labs, Inc.          USB Hub
  '''Bus 003 Device 003: ID 0781:5591 SanDisk Corp. Ultra Flair'''
  Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  Bus 004 Device 002: ID 8087:8002 Intel Corp. 8 channel internal hub
*Attach SanDisk to win10
root@ts01:~# virsh attach-device win10 --file <(cat <<EOF
<hostdev mode='subsystem' type='usb'>
  <source>
    '''<vendor id='0x0781'/>'''
    '''<product id='0x5591'/>'''
  </source>
</hostdev>
EOF
) --persistent
Device attached successfully
*Shutdown Win10
root@ts01:~# virsh shutdown win10
Domain 'win10' is being shutdown
*Optional dump message to investigate the current settings
root@ts01:~# virsh dumpxml win10 | grep -A10 hostdev
    <hostdev mode='subsystem' type='usb' managed='no'>
      <source>
        <vendor id='0x0781'/>
        <product id='0x5591'/>
      </source>
      <address type='usb' bus='0' port='4'/>
      ....
*Start back win10
root@ts01:~# virsh start win10
Domain 'win10' started
*Get Status
root@ts01:~# virsh list --all
  Id  Name      State
---------------------------
  3    win10      running
  -    debian13  shut off


  Domain 'debian13' XML configuration edited.
*Attached USB Stick to Win10
[[image:Winusbb.png|1200px]]
 
==Detach a USB Stick from the VM==
 
*Detach device
root@ts01:~# virsh detach-device win10 --file <(cat <<EOF
  <hostdev mode='subsystem' type='usb'>
  <source>
    <vendor id='0x0781'/>
    <product id='0x5591'/>
  </source>
</hostdev>
EOF
) --persistent
Device detached successfully

Revision as of 18:33, 6 February 2026

kvm testsetup at home

System

vmadmin@ts01:~$ sudo -i
root@ts01:~# cat /etc/os-release
PRETTY_NAME="Ubuntu 25.10"
NAME="Ubuntu"
VERSION_ID="25.10"
VERSION="25.10 (Questing Quokka)"
VERSION_CODENAME=questing
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=questing
LOGO=ubuntu-logo

Install Packages

root@ts01:~# apt install -y   qemu-kvm \
libvirt-daemon-system \
libvirt-clients \
bridge-utils \
virt-manager \
cpu-checker

Libvirt

  • Enable
root@ts01:~# systemctl enable --now libvirtd
  • Status
root@ts01:~# systemctl status libvirtd
● libvirtd.service - libvirt legacy monolithic daemon
    Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; preset: enabled)
    Active: active (running) since Wed 2026-02-04 19:32:47 CET; 52min ago
 Invocation: de3b4269f9074f69aef468813b833a4b
TriggeredBy: ● libvirtd.socket
            ● libvirtd-admin.socket
            ● libvirtd-ro.socket
      Docs: man:libvirtd(8)
            https://libvirt.org/
  Main PID: 26280 (libvirtd)
     Tasks: 25 (limit: 32768)
    Memory: 24.1M (peak: 51M)
       CPU: 9.655s
    CGroup: /system.slice/libvirtd.service
            ├─26280 /usr/sbin/libvirtd --timeout 120
            ├─26388 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
            └─26389 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper

Feb 04 20:03:14 ts01 dnsmasq-dhcp[26388]: DHCPDISCOVER(virbr0) 52:54:00:2a:f9:66
Feb 04 20:03:14 ts01 dnsmasq-dhcp[26388]: DHCPOFFER(virbr0) 192.168.122.128 52:54:00:2a:f9:66root@ts01:~# kvm-ok



Virsh

  • List
root@ts01:~# virsh list --all
virsh list --all
Id   Name       State
--------------------------
  • Node Info
root@ts01:~# virsh nodeinfo
CPU model:           x86_64
CPU(s):              24
CPU frequency:       1197 MHz
CPU socket(s):       1
Core(s) per socket:  12
Thread(s) per core:  2
NUMA cell(s):        1
Memory size:         63375964 KiB
  • Net List
root@ts01:~# virsh net-list --all
Name      State    Autostart   Persistent

default   active   yes         yes
  • Set Start
root@ts01:~# virsh net-start default
root@ts01:~# virsh net-autostart default
  • Prepare for the first Debian Image
root@ts01:~# cp /home/admin/debian-13.3.0-amd64-netinst.iso /var/lib/libvirt/images
root@ts01:~# chmod 666 /var/lib/libvirt/images/debian-13.3.0-amd64-netinst.iso
root@ts01:~# #Do this only on a sandbox
root@ts01:~# chmod 777 /var/lib/libvirt/images

Prepare User

  • User admin:
vmadmin@ts01:~$ usermod -aG libvirt,kvm $USER
vmadmin@ts01:~$ sudo usermod -aG libvirt,kvm $USER
vmadmin@ts01:~$ less /etc/groups
  • User admin in xrdp:
virt-manager

Installation Screenshots

  • VM Installer01

  • VM Installer02

  • VM Installer05

  • VM Installer06

  • VM Installer07

  • VM Installer08

  • VM Installer09

  • VM Installer10

  • VM Installer11

  • VM Installer12

Advanced - Bridging

  • advanced, make the vm bridged
root@ts01:~# sudo virsh net-define /dev/stdin <<EOF
<network>
  <name>br0</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>
EOF
  • Start
root@ts01:~# sudo virsh net-start br0
sudo virsh net-autostart br0
Network br0 started

Network br0 marked as autostarted

  • Shutdown running VM
root@ts01:~# virsh shutdown debian13
Domain 'debian13' is being shutdown
  • Edit
virsh edit debian


  • Change:
<interface type='network'>
 <source network='default'/>
  • To:
<interface type='bridge'>
 <source bridge='br0'/>
 <model type='virtio'/>
</interface>

Domain 'debian13' XML configuration edited.

Converting a VHDX Windows Disk

  • Inspect the disk:
root@ts01:~# qemu-img info /home/vmadmin/vm-surf01.vhdx
image: /home/vmadmin/vm-surf01.vhdx
file format: vhdx
virtual size: 127 GiB (136365211648 bytes)
disk size: 41 GiB
cluster_size: 33554432
Child node '/file':
   filename: /home/vmadmin/vm-surf01.vhdx
   protocol type: file
   file length: 41 GiB (43994054656 bytes)
   disk size: 41 GiB


  • Get the disk ready for KVM
root@ts01:~# cp /home/admin/debian-13.3.0-amd64-netinst.iso /var/lib/libvirt/images
root@ts01:~# chmod 666 /var/lib/libvirt/images/debian-13.3.0-amd64-netinst.iso
root@ts01:~# chmod 777 /var/lib/libvirt/images
  • Convert the disk
root@ts01:~# qemu-img convert -p -f vhdx -O qcow2 \
 /home/vmadmin/vm-surf01.vhdx \
 /var/lib/libvirt/images/vm-surf01.qcow2
   (100.00/100%)


  • Inspect the converting results
root@ts01:~# qemu-img info /var/lib/libvirt/images/vm-surf01.qcow2
image: /var/lib/libvirt/images/vm-surf01.qcow2
file format: qcow2
virtual size: 127 GiB (136365211648 bytes)
disk size: 37.6 GiB
cluster_size: 65536
Format specific information:
   compat: 1.1
   compression type: zlib
   lazy refcounts: false
   refcount bits: 16
   corrupt: false
   extended l2: false
Child node '/file':
   filename: /var/lib/libvirt/images/vm-surf01.qcow2
   protocol type: file
   file length: 37.6 GiB (40399208448 bytes)
   disk size: 37.6 GiB


Install a Win10 VM from a previous disk

  • Install01

  • Install02

  • Install03


  • Install04


  • Win10 in running state

Attach a USB Stick to the VM

  • Get attached devices
    • Note: We're after ID 0781:5591 SanDisk Corp.
root@ts01:~# lsusb
 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 Bus 001 Device 002: ID 8087:800a Intel Corp. Hub
 Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 Bus 002 Device 002: ID 3938:1032 MOSART Semi. 2.4G RF Keyboard & Mouse
 Bus 002 Device 003: ID 04b3:3025 IBM Corp. NetVista Full Width Keyboard
 Bus 002 Device 004: ID 17aa:1034 VIA Labs, Inc.          USB Hub
 Bus 002 Device 005: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
 Bus 002 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver
 Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
 Bus 003 Device 002: ID 17aa:1034 VIA Labs, Inc.          USB Hub
 Bus 003 Device 003: ID 0781:5591 SanDisk Corp. Ultra Flair
 Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 Bus 004 Device 002: ID 8087:8002 Intel Corp. 8 channel internal hub
  • Attach SanDisk to win10
root@ts01:~# virsh attach-device win10 --file <(cat <<EOF
<hostdev mode='subsystem' type='usb'>
  <source>
    <vendor id='0x0781'/>
    <product id='0x5591'/>
  </source>
</hostdev>
EOF
) --persistent
Device attached successfully


  • Shutdown Win10
root@ts01:~# virsh shutdown win10
Domain 'win10' is being shutdown
  • Optional dump message to investigate the current settings
root@ts01:~# virsh dumpxml win10 | grep -A10 hostdev
   <hostdev mode='subsystem' type='usb' managed='no'>
     <source>
       <vendor id='0x0781'/>
       <product id='0x5591'/>
     </source>
     <address type='usb' bus='0' port='4'/>
     ....
  • Start back win10
root@ts01:~# virsh start win10
Domain 'win10' started
  • Get Status
root@ts01:~# virsh list --all
 Id   Name       State
---------------------------
 3    win10      running
 -    debian13   shut off




  • Attached USB Stick to Win10

Detach a USB Stick from the VM

  • Detach device
root@ts01:~# virsh detach-device win10 --file <(cat <<EOF
<hostdev mode='subsystem' type='usb'>
  <source>
    <vendor id='0x0781'/>
    <product id='0x5591'/>
  </source>
</hostdev>
EOF
) --persistent
Device detached successfully