summaryrefslogtreecommitdiff
path: root/doc/note/qemu/qemu.txt
blob: 728e97e88f2d2e15dbdafffa04b6d6452db7b45b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

Qemu
================

## TODO move this to the NFS section

Example fstab entries:

//10.0.2.2/sharename  /mnt/cee-misc-lib  nfs  user,noauto,forceuid,uid=1000,vers=3.0  0  0


## Qemu Setup

  apt install qemu-system-aarch64 qemu-efi-aarch64


## Manage Images

### 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

### 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

## Convert to based from standalone
  qemu-img create -f qcow2 -b old.qcow2 new.qcow2
  qemu-img rebase -b base.qcow2 new.qcow2

## Shrink/compact img

Normal systems:
  qemu-img convert -O qcow2 input.qcow2 output.qcow2

Windoof:
  sdelete -z C:
  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) --no-reboot \
    `# Drives & Boot.` \
    -boot order=dc \
    -cdrom "path/to/cd.iso" \
    -hda "$(dirname "$(realpath "$0")")/hda.qcow2" \
    -nic none `# remove if network wanted` \
    `# 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:?}-:22 -device e1000,netdev=n0 \
    `# socket mcast shared network adapter` \
    -netdev socket,id=n1,mcast=230.0.0.1:1234 -device e1000,netdev=n1 \
    `# USB pass-through by port or device. Use 'lsusb -t' or 'lsusb' to find IDs` \
    `# qemu needs permission: 'chmod o+w /dev/bus/usb/002/035'` \
    `# Or via "/etc/udev/rules.d/foo.rule": SUBSYSTEM=="usb", ATTR{idVendor}=="HEX1", ATTR{idProduct}=="HEX2", ATTR{serial}=="1234", MODE="0666"` \
    -device nec-usb-xhci \
    -device usb-host,hostbus=999,hostaddr=999 \
    `# Choose ONE of those outputs (or none for defaut)` \
    -monitor stdio -serial stdio `# coose ONE` \
    -display none \
    -nographic -serial mon:stdio `# HINT remove other monitor arg` \
    -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 \

[src udev rules](https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/udev/udev.html)
[TODO mcast broken](https://serverfault.com/q/1002977/673216)


## 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

(see also "../../../doc/note/rasbpi/rasbpi.txt")

# Example Raspi
qemu-system-aarch64
    -nodefaults -accel tcg,thread=multi `# TODO confirm thread=multi works ` \
    -machine type=raspi3b -cpu cortex-a53 -smp 4 -m 1G \
    -kernel kernel8.img -dtb bcm2710-rpi-3-b.dtb -sd "$(dirname "$(realpath "$0")")/sd.qcow2" \
    -device usb-net,netdev=net0 \
    -netdev user,id=net0,hostfwd=tcp:127.0.0.1:${SSH_PORT:?}-:22 \
    -append "console=ttyAMA0,115200 root=PARTUUID=fb33757d-02 rootfstype=ext4 rootwait" \
    -no-reboot -nographic \


## Shared host directory via CIFS/SMB

  true `# SMB server debian` \
  && hostpath=/path/to/host/dir \
  && sharename=work \
  && 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

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` \
  && hostUsername=yourHostUser \
  && smbServer=10.0.2.2 \
  && sharename=work \
  && mountpoint=/mnt/${sharename:?} \
  && guestUid=$(id -u) \
  && guestGid=$(id -g) \
  && true \
  && $SUDO mkdir -p "${mountpoint:?}" \
  && $SUDO apt install --no-install-recommends -y cifs-utils psmisc \
  && $SUDO mount -t cifs -o username=${hostUsername:?},uid=${guestUid:?},gid=${guestGid:?} "//${smbServer:?}/${sharename:?}" "${mountpoint:?}" \
  && true


## Example smb config for smb server (TODO verify)

[vm-share]
   path = /path/to/vm-share
   browseable = yes
   read only = no
   guest ok = yes
   force user = usernameFromHost


### 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


## Shared host dir via NFS (handy for broken windoof hosts)

For windoof HOST install WinNFSd, then (TODO verify):
  cd your/hostpath
  WinNFSd -addr 127.0.0.1 . /your/nfs/sharename

Or for whole drive:
  cd C:/
  WinNFSd -addr 127.0.0.1 . /c

In GUEST to access it do:
  $SUDO apt install -y --no-install-recommends nfs-common
  apk add rpcbind && /etc/init.d/rpcbind start
  mkdir /mnt/host
  $SUDO mount -t nfs -o vers=3 10.0.2.2:/path/to/share /mnt/host


## USB pass-through

    -device nec-usb-xhci \
    -device usb-host,hostbus=999,hostaddr=999 `# tested, seems to work` \

### By physical port (TODO broken)
  -usb -device usb-host,hostbus=1,hostport=2.3

### By device (TODO broken)
  -usb -device usb-host,vendorid=123,productid=456


## Alpine PostInstall

  && `# Likely handy to put those vars also in dotfiles in users home` \
  && export no_proxy='127.0.0.1,10.0.2.*' \
  && export  http_proxy="http://10.0.2.2:3128/" \
  && export https_proxy="http://10.0.2.2:3128/" \
  && apk add openssh-server \
  && rc-update add sshd \
  && 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 \
  && printf 'user:12345\n' | chpasswd \
  && /etc/init.d/sshd restart \

  && `# host auto-mount helper script ` \
  && mkdir ~/.local ~/.local/bin \
  && echo H4sIAAUQ5GYAA0WOsQ7CMAxE93yFCUgFiTbQEaliYWArA8wRFJNkaFI1CR2Af8cpSJ1sv/P5PJ+Jm7HCa+YxQI7s2R6dDxePfXWuD7XsYpCaiIyEpMYemY+nYRJVRFJ754Lsht9CNPeKL5bmDnlccaamUdGIjXbAF690Zv/h8AYfIfkhbyBrXbTUBWjMw0PuIOXaa4tVRp7pO3Jm6xSUMNXdCNQfqD8ALsR2U2yKsijF+LLX1x7TPU5aa8OPjqmdMzbwjH0BleXnHxQBAAA= |base64 -d|gzip -d > ~/.local/bin/setup \
  && printf '\n  TODO: Resolve the todos in %s\n\n' ~/.local/bin/setup \


## Debian PostInstall

TODO: move this to a better place. Eg: debian/setup.txt or whatever.

  true \
  && SUDO= \
  && 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" \
  && 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 \
  && $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 \
  && $SUDO update-grub \
  && true


## Qemu-Monitor in combo with VNC

  --monitor stdio
  --monitor telnet:127.0.0.1:55555,server,nowait


## Sources

- [apt via proxy](https://computingforgeeks.com/how-to-set-system-proxy-on-debian-linux/?expand_article=1)
- [Qemu ssh from host](https://wiki.qemu.org/Documentation/Networking)
- [shared host dir](https://superuser.com/a/628381/1123359)
- [NFS server for windoof](https://github.com/winnfsd/winnfsd)
- [NFS server for debian](https://vitux.com/debian_nfs_server/)
- [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)