Photonicat Debian + Virtual Machine

From Photonicat Wiki
Revision as of 12:46, 28 October 2024 by C2h2 (talk | contribs) (Created page with "==Install libvirtd== Install libvirtd using APT (require root privilege): <pre> apt-get install libvirt-daemon libvirt-clients bridge-utils </pre> ==Add user to libvirtd== This operation require root privilege. <pre> #Replace photonicat to your user name if you changed user name. usermod -a -G libvirt photonicat </pre> Then reboot to apply changes. ==Example: Run OpenWRT on libvirtd== The operations below require root privilege. ===Setup Network=== Add bridge configu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Install libvirtd

Install libvirtd using APT (require root privilege):

apt-get install libvirt-daemon libvirt-clients bridge-utils

Add user to libvirtd

This operation require root privilege.

#Replace photonicat to your user name if you changed user name.
usermod -a -G libvirt photonicat

Then reboot to apply changes.

Example: Run OpenWRT on libvirtd

The operations below require root privilege.

Setup Network

Add bridge configurations below to /etc/network/interfaces:

iface eth0 inet manual
iface eth1 inet manual

auto br-wan
iface br-wan inet dhcp
  bridge_ports eth0
  bridge_fd 0

auto br-lan
iface br-lan inet static
  bridge_ports eth1
  address 172.16.0.2
  netmask 255.255.255.0
  bridge_fd 0

Then reboot to apply changes.

Prepare kernel & rootfs image

mkdir -p /var/lib/libvirt/boot/openwrt
cd /var/lib/libvirt/boot/openwrt
wget https://downloads.openwrt.org/releases/23.05.5/targets/armsr/armv8/openwrt-23.05.5-armsr-armv8-generic-kernel.bin
wget https://downloads.openwrt.org/releases/23.05.5/targets/armsr/armv8/openwrt-23.05.5-armsr-armv8-generic-initramfs-kernel.bin

cd /var/lib/libvirt/images
wget https://downloads.openwrt.org/releases/23.05.5/targets/armsr/armv8/openwrt-23.05.5-armsr-armv8-generic-ext4-rootfs.img.gz
gunzip openwrt-23.05.5-armsr-armv8-generic-ext4-rootfs.img.gz

Setup VM

Create file /tmp/openwrt.xml with contents below:

<domain type='kvm' id='1'>
  <name>OpenWRT</name>
  <uuid>392d747-f6ae-469c-8024-647949f59ece</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://libosinfo.org/linux/2022"/>
    </libosinfo:libosinfo>
  </metadata>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='aarch64' machine='virt-7.2'>hvm</type>
    <kernel>/var/lib/libvirt/boot/openwrt/openwrt-23.05.5-armsr-armv8-generic-kernel.bin</kernel>
    <initrd>/var/lib/libvirt/boot/openwrt/openwrt-23.05.5-armsr-armv8-generic-initramfs-kernel.bin</initrd>
    <cmdline>root=fe00<cmdline>
    <boot dev='hd'/>
  </os>
  <features>
    <gic version='3'/>
  </features>
  <cpu mode='host-passthrough' check='none'/>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-system-aarch64</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/openwrt-23.05.5-armsr-armv8-generic-ext4-rootfs.img' index='1'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='qemu-xhci' ports='15'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
    </controller>
    <controller type='pci' index='0' model='pcie-root'>
      <alias name='pcie.0'/>
    </controller>
    <controller type='pci' index='1' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='1' port='0x8'/>
      <alias name='pci.1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0' multifunction='on'/>
    </controller>
    <controller type='pci' index='2' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='2' port='0x9'/>
      <alias name='pci.2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='pci' index='3' model='pcie-roo-port'>
      <model name='pcie-root-port'/>
     <target chassis='3' port='0xa'/>
      <alias name='ci.3'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='4' model='pcie-root-port'>
      <mode name='pcie-root-port'/>
      <target chassis='4' port='0xb'/>
      <alias name='pci.4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x3'/>
    </controller>
    <controller type='pci' index='5' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='5' port='0xc'/>
      <alias name='pci.5'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x4'/>
    </controller>
    <controller type='pci' index='6' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='6' port='0xd'/>
      <alias name='pci.6'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x5'/>
    </controller>
    <cotroller type='pci' index='7' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='7' port='0xe'/>
      <alias name='pci.7'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x6'/>
    </controller>
    <controller type='pci' index='8' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='8' port='0xf'/>
      <alias name='pci.8'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x7'/>
    </controller>
    <contoller type='pci' index='9' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='9' port='0x10'/>
      <alias name='pci.9'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0' multifunction='on'/>
    </controller>
    <controller type='pci' index='10' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='10' port='0x11'/>
      <alias name='pi.10'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x1'/>
    </controller>
    <controller type='pci' index='11' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='11' port='0x12'/>
      <alias name='pci.11'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x2'/>
    </controller>
    <controller type='pci' index='12' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='12' port='0x13'/>
      <alias name='pci.12'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x3'/>
    </controller>
    <controller type='pci' index='13' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='13' port='0x14'/>
      <alias name='pci.13'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x4'/>
    </controller>
    <controller type='pci' index='14' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='14' port='0x15'/>
      <alias name='pci.14'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x5'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:54:c1:e7'/>
      <source bridge='br-wan'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='52:54:00:2c:86:f2'/>
      <source bridge='br-lan'/>
      <target dev='vnet1'/>
      <model type='virtio'/>
      <alias name='net1'/>
      <address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
    </interface>
    <interface type="network">
      <mac address="52:54:00:2c:18:cc"/>
      <source network="default"/>
      <model type="virtio"/>
      <address type="pci" domain="0x0000" bus="0x08" slot="0x00" function="0x0"/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/0'/>
      <target type='system-serial' port='0'>
        <model name='pl011'/>
      </target>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/0'>
      <source path='/dev/pts/0'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <channel type='unix'>
      <source mode='bind'path='/var/lib/libvirt/qemu/channel/target/domain-1-OpenWRT/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0' state='disconnected'/>
      <alias name='channel0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <audio id='1' type='none'/>
   <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vendor id='0x2c7c'/>
        <product id='0x0125'/>
        <address bus='7' device='2'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='1'/>
    </hostdev>
    <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vedor id='0x0e8d'>
        <product id='0x7961'/>
        <address bus='2' device='3'/>
      </source>
      <alias name='hostdev1'/>
      <address type='usb' bus='0' port='2'/>
    </hostdev>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000'bus='0x05' slot='0x00' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='rndom'>/dev/urandom</backend>
      <alias name='rng0'/>
      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
    </rng>
  </devices>
  <seclabel type='dynamic' model='dac' relabel='yes'>
    <label>+64055:+64055</label>
    <imagelabel>+64055:+64055</imagelabel>
  </seclabel>
</domain>

Import VM:

virsh
connect qemu:///system
create /tmp/openwrt.xml

Setup VM

Use virt-manager or other frontend to login VM, set eth1 to 172.16.0.1. Access OpenWRT with 172.16.0.1 by SSH or browser.

Change VM configuration

virsh
connect qemu:///system
edit OpenWRT

Change system disk

If you want to change system disk image, an ext4 filesystem with or without partition image:

<source file='/var/lib/libvirt/images/openwrt-23.05.5-armsr-armv8-generic-ext4-rootfs.img' index='1'/>

Change file property to the system disk image you want.


If your system disk image is just a filesystem image (no partition table), set cmdline section to root=fe00: (like the one you downloaded: https://downloads.openwrt.org/releases/23.05.5/targets/armsr/armv8/openwrt-23.05.5-armsr-armv8-generic-ext4-rootfs.img.gz)

<cmdline>root=fe00<cmdline>

If your system disk image has a partition table, you should set cmdline section to setup rootfs device (e.g: /dev/vda1):

<cmdline>root=/dev/vda1<cmdline>

Sample Image

OpenWRT:
LAN IP: 172.16.0.1
User: root
Password: photonicat