summaryrefslogtreecommitdiff
path: root/doc/note/qemu/qemu.txt
diff options
context:
space:
mode:
authorAndreas Fankhauser hiddenalpha.ch2024-05-12 12:55:07 +0200
committerAndreas Fankhauser hiddenalpha.ch2024-05-12 12:55:07 +0200
commit5cc4c2b95df9bf6998d92496ca0e0064314fba39 (patch)
treeac7d979f81a86703508ff23e2694a6b4d6bfe4d5 /doc/note/qemu/qemu.txt
parent1dc9f8c9db499a326ceee6cb3b48878ee17ff9fa (diff)
parent95d934e8e3918832c03f05b2fc32cb5d5272cb83 (diff)
downloadUnspecifiedGarbage-wip-LatitudeReSetup-20230620.zip
UnspecifiedGarbage-wip-LatitudeReSetup-20230620.tar.gz
Merge remote-tracking branch 'ha-public/master' into wip-LatitudeReSetup-20230620wip-LatitudeReSetup-20230620
Diffstat (limited to 'doc/note/qemu/qemu.txt')
-rw-r--r--doc/note/qemu/qemu.txt164
1 files changed, 141 insertions, 23 deletions
diff --git a/doc/note/qemu/qemu.txt b/doc/note/qemu/qemu.txt
index de7a71b..edaf9bc 100644
--- a/doc/note/qemu/qemu.txt
+++ b/doc/note/qemu/qemu.txt
@@ -7,12 +7,24 @@ Qemu
apt install qemu-system-aarch64 qemu-efi-aarch64
-## Create Image
+## Manage Images
- qemu-img create -f qcow2 my_disk.qcow2 16G
+### Create new image
+ qemu-img create -f qcow2 disk.qcow2 16G
+### Create new overlay image
+ qemu-img create -o backing_file=base.qcow2,backing_fmt=qcow2 -f qcow2 disk.qcow2
-## Shrink img
+### Convert qcow2 to raw
+ qemu-img convert -f qcow2 -O raw foo.qcow2 foo.img
+
+### Convert raw to qcow2
+ qemu-img convert -f raw -O qcow2 foo.img foo.qcow2
+
+### Create Standalone image based on snapshot image
+ qemu-img convert -O qcow2 derived.qcow2 standalone.qcow2
+
+## Shrink/compact img
Normal systems:
qemu-img convert -O qcow2 input.qcow2 output.qcow2
@@ -22,6 +34,74 @@ Windoof:
qemu-img convert -O qcow2 input.qcow output.qcow2
+## Shrink snapshot layer
+
+ qemu-img convert -O qcow2 snapLayer.qcow2 tmpFullClone.qcow2
+ qemu-img create -f qcow2 -b tmpFullClone.qcow2 diff.qcow2
+ qemu-img rebase -b base.qcow2 tmpDiff.qcow2
+ mv tmpDiff.qcow2 snapLayer.qcow2
+
+
+## Example Params (Usage: CopyPaste, then delege what is not needed)
+qemu-system-x86_64 \
+ -nodefaults `# <- TODO Fix network when using this` \
+ -accel kvm:whpx:hax:tcg -m size=2G -smp cores=$(nproc) \
+ -monitor stdio -serial stdio `# coose ONE` \
+ `# Drives & Boot.` \
+ -boot order=dc \
+ -cdrom "path/to/cd.iso" \
+ -hda "$(dirname "$(realpath "$0")")/hda.qcow2" \
+ `# Isolated Network plus host port/cmd reachable from guest` \
+ -netdev 'user,id=n1,ipv6=off,restrict=y,guestfwd=tcp:10.0.2.9:80-cmd:ncat 127.0.0.1 80' \
+ -device e1000,netdev=n1 \
+ `# Isolated Network with samba access to host` \
+ -netdev 'user,id=n2,ipv6=off,restrict=y,guestfwd=tcp:10.0.2.9:139-cmd:ncat 127.0.0.1 139,guestfwd=tcp:10.0.2.9:445-cmd:ncat 127.0.0.1 445' \
+ -device e1000,netdev=n2 \
+ `# 10.0.2.x network with host redirect` \
+ -netdev user,id=n0,ipv6=off,hostfwd=tcp:127.0.0.1:${SSH_PORT:-2222}-:22 \
+ -device e1000,netdev=n0 \
+ `# socket mcast shared network adapter` \
+ -netdev socket,id=n1,ipv6=off,mcast=230.0.0.1:1234 \
+ -device e1000,netdev=n1 \
+ `# USB pass-through` \
+ -usb -device usb-host,id=myUsbQemuId,vendorid=0xFFFF,productid=0xFFFF \
+ `# Choose ONE of those for graphic output` \
+ -nographic \
+ -device VGA \
+ -display sdl,grab-mod=rctrl \
+ -display gtk,show-menubar=on \
+ -display vnc=127.0.0.1:0,to=99 `#HINT: 0 is port 5900` \
+ ;
+
+## Broken systems likely need some of those too
+ `# Fix broken hosts` \
+ -L "${QEMU_HOME:?}/Bios" -bios "${QEMU_HOME:?}/Bios/bios-256k.bin" \
+ -accel whpx,kernel-irqchip=off `# "https://github.com/Tech-FZ/EmuGUI/issues/72#issuecomment-1940933918"` \
+ `# Fix broken guests` \
+ -device usb-ehci,id=usb,bus=pci.0,addr=0x4 -device usb-tablet \
+
+
+## Inspect qcow2 by host mounting it
+
+ $SUDO modprobe nbd
+ $SUDO qemu-nbd -c /dev/nbd__ /path/to/my.qcow2
+ echo 'p' | $SUDO fdisk /dev/nbd__
+ $SUDO mount -o ro /dev/nbd__p__ /mnt/q
+ $SUDO umount /mnt/q `# cleanup`
+ $SUDO qemu-nbd -d /dev/nbd__ `# cleanup`
+ $SUDO rmmod nbd `# cleanup`
+
+
+### Example manual adapter setup (inside VM) for socket mcast network:
+true \
+ && ADDR=192.168.42.101/24 \
+ && DEV=ens4 \
+ && SUDO=sudo \
+ && $SUDO ip a add dev "${DEV:?}" "${ADDR:?}" \
+ && $SUDO ip link set "${DEV:?}" up \
+ && true
+
+
## amd64
# Choose whichever fits the need
@@ -31,7 +111,7 @@ Windoof:
&& BIOSFILE="${BIOSDIR:?}/bios-256k.bin" \
&& FIXMOUSEALIGN="-device usb-ehci,id=usb,bus=pci.0,addr=0x4 -device usb-tablet" \
&& NETWORK="-net nic -net user" \
- && NETWORK="-device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp:127.0.0.1:10022-:22" \
+ && NETWORK="-device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:2222-:22" \
&& HOSTSPECIFICOPTS="--enable-kvm" \
&& HOSTSPECIFICOPTS="-L ${BIOSDIR:?} -bios ${BIOSFILE:?}" \
@@ -47,19 +127,46 @@ Regular boot
## aarch64 (not working yet)
- MAC='00:de:ad:de:ad:00'
- DISK=my_disk.qcow2
- cp /usr/share/AAVMF/AAVMF_CODE.fd ./flash1.img
- qemu-system-aarch64 -m 1G -cpu cortex-a57 -M virt \
- -pflash /usr/share/AAVMF/AAVMF_CODE.fd \
- -pflash flash1.img \
- -drive if=none,file=${DISK:?},id=hd0 \
- -device virtio-blk-device,drive=hd0 \
- -device virtio-net-device,netdev=net0,mac=${MAC:?}
-
- qemu-system-aarch64 -M virt -hda my_disk.qcow2 -cdrom debian.iso -boot c -m 1G
-
- qemu-system-aarch64 -M virt -cpu cortex-a57 -m 1G -bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd -hda my_disk.qcow2 -cdrom ~/images/debian-12.0.0-arm64-DVD/debian-12.0.0-arm64-DVD-1.iso
+ #apt install -y --no-install-recommends qemu-uefi-aarch64
+ curl -sSLO https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-5.10.63-bullseye
+ curl -sSLO https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/versatile-pb-bullseye-5.10.63.dtb
+ curl -sSLO https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/native-emulation/dtbs/bcm2711-rpi-4-b.dtb
+ curl -sSLO https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-5.4.51-buster
+ curl -sSLO https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/versatile-pb-buster-5.4.51.dtb
+ curl -sSLO https://downloads.raspberrypi.org/raspios_lite_arm64/images/raspios_lite_arm64-2023-05-03/2023-05-03-raspios-bullseye-arm64-lite.img.xz
+ xz -d 2023-05-03-raspios-bullseye-arm64-lite.img.xz
+ echo p | /sbin/fdisk 2023-05-03-raspios-bullseye-arm64-lite.img | egrep 'Linux$' | sed -E 's:^\S+\s+([0-9]+) .*$:\nmount -o offset=$(expr \1 \\* 512) ./2023-05-03-raspios-bullseye-arm64-lite.img /mnt/foo:'
+ qemu-img convert -f raw -O qcow2 2023-05-03-raspios-bullseye-arm64-lite.img raspbian-bullseye-lite.qcow2
+ qemu-img resize raspbian-bullseye-lite.qcow2 16G
+ mv raspbian-bullseye-lite.qcow2 hda.qcow2
+
+ qemu-system-aarch64 \
+ -m 256 -cpu arm1176 \
+ -M versatilepb \
+ -no-reboot \
+ -serial stdio \
+ -net nic -net user \
+ -drive file=2023-05-03-raspios-bullseye-arm64-lite.img,format=raw \
+ -boot 'dtb=versatile-pb-bullseye-5.10.63.dtb,kernel=kernel-qemu-5.10.63-bullseye,kernel_args=root=/dev/vda2 panic=1' \
+
+ qemu-system-aarch64 \
+ -dtb ./bcm2711-rpi-4-b.dtb \
+ -m 256 -cpu arm1176 -M versatilepb \
+ -kernel kernel-qemu-5.10.63-bullseye -append "root=/dev/sda2 rootfstype=ext4 rw" \
+ -serial stdio \
+ -drive file=2023-05-03-raspios-bullseye-arm64-lite.img,format=raw \
+ -net nic -net user \
+ -no-reboot \
+
+ qemu-system-arm \
+ -M versatilepb \
+ -cpu arm1176 -m 256 \
+ -drive "file=2023-05-03-raspios-bullseye-arm64-lite.img,if=none,index=0,media=disk,format=raw,id=disk0" \
+ -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
+ -net "user,hostfwd=tcp::5022-:2222" \
+ -dtb "./versatile-pb-buster-5.4.51.dtb" \
+ -kernel "./kernel-qemu-5.4.51-buster" -append 'root=/dev/vda2 panic=1' \
+ -no-reboot
## Shared host directory via CIFS/SMB
@@ -67,7 +174,7 @@ Regular boot
true `# SMB server debian` \
&& hostpath=/path/to/host/dir \
&& sharename=work \
- && apt install --no-install-recommends -y samba
+ && apt install --no-install-recommends -y samba \
&& printf '[%s]\npath = %s\npublic = no\nwriteable = yes\nguest ok = yes\nforce user = andreas\n' "${sharename:?}" "${hostpath:?}" | $SUDO tee -a /etc/samba/smb.conf >/dev/null \
&& $SUDO /etc/init.d/smbd restart \
&& true
@@ -75,7 +182,7 @@ Regular boot
TODO: SMB server windoof
DoesNotWork: "https://serverfault.com/questions/442664/virtualization-linux-kvm-qemu-host-windows-vm-guest-how-to-access-data-drive#comment479177_442678"
- true `# SMB client debian` \
+true `# SMB client debian` \
&& hostUsername=yourHostUser \
&& smbServer=10.0.2.2 \
&& sharename=work \
@@ -88,6 +195,11 @@ DoesNotWork: "https://serverfault.com/questions/442664/virtualization-linux-kvm-
&& $SUDO mount -t cifs -o username=${hostUsername:?},uid=${guestUid:?},gid=${guestGid:?} "//${smbServer:?}/${sharename:?}" "${mountpoint:?}" \
&& true
+### Add those in "/etc/fstab" to setup mount automatically at boot:
+### HINT: mkdir /home/user/build
+ //10.0.2.2/sharename /mnt/sharename cifs password=,uid=1000,gid=1000,user,vers=3.0 0 0
+ /home/user/build /mnt/sharename/build none bind 0 0
+
List smb shares (eg debugging)
smbclient -NL //10.0.2.2
@@ -130,10 +242,13 @@ NOTE: Couldn't yet test any of those commands.
## Alpine PostInstall
true \
+ && `# HINT: environ setup does not work autmoatically during login. has to be sourced manually.` \
+ && P="http://10.0.2.2:3128/" \
+ && printf 'export no_proxy=127.0.0.1,10.0.2.*\nexport http_proxy=%s\nexport https_proxy=%s\n' "${P:?}" "${P:?}" >> '/etc/environment' \
&& apk add openssh-server \
&& rc-update add sshd \
&& sed -i -E 's;^# *(PermitRootLogin).+$;\1 yes;' /etc/ssh/sshd_config \
- && sed -i -E 's;^# *(http://dl-cdn.alpinelinux.org/alpine/v[^/]+/community)$;\1;' /etc/apk/repositories \
+ && sed -i -E 's;^# *(http://dl-cdn.alpinelinux.org/alpine/v.*?/community)$;\1;' /etc/apk/repositories \
&& mkdir /home/user && chown 1000:1000 /home/user && chmod 755 /home/user \
&& printf 'user:x:1000:1000:user:/home/user:/bin/ash\n' >> /etc/passwd \
&& printf 'user:x:1000:user\n' >> /etc/group \
@@ -146,16 +261,16 @@ NOTE: Couldn't yet test any of those commands.
TODO: move this to a better place. Eg: debian/setup.txt or whatever.
true \
- && http_proxy= \
- && https_proxy= \
&& no_proxy=127.0.0.1,10.0.2.* \
+ && http_proxy=http://10.0.2.2:3128 \
+ && https_proxy=http://10.0.2.2:3128 \
&& SUDO= \
&& true \
&& if [ -n "$http_proxy" ]; then true \
&& (echo "Acquire::http::proxy \"${http_proxy}\";"
echo "Acquire::https::proxy \"${https_proxy}\";"
) | $SUDO tee /etc/apt/apt.conf.d/80proxy >/dev/null \
- fi \
+ ;fi \
&& $SUDO apt update \
&& $SUDO apt install -y --no-install-recommends vim openssh-server net-tools curl \
&& $SUDO sed -i -E 's;^GRUB_TIMEOUT=5$;GRUB_TIMEOUT=1;' /etc/default/grub \
@@ -179,4 +294,7 @@ TODO: move this to a better place. Eg: debian/setup.txt or whatever.
- [USB pass-through](https://unix.stackexchange.com/a/452946/292722)
- [qemu monitor via telnet](https://unix.stackexchange.com/a/426951/292722)
- [qemu monitor via stdio](https://unix.stackexchange.com/a/57835/292722)
+- [qemu raspberry pi TODO](https://blog.agchapman.com/using-qemu-to-emulate-a-raspberry-pi/)
+- [connect VM networks](https://qemu.weilnetz.de/doc/6.0/system/invocation.html#sec-005finvocation)
+- [inspect qcow2 mount host browse](https://www.jamescoyle.net/how-to/1818-access-a-qcow2-virtual-disk-image-from-the-host)