summaryrefslogtreecommitdiff
path: root/doc/note/qemu/qemu.txt
blob: 7d46d6ab30bd34682b7c57762fc4066543bbe14a (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

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

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

## 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) \
    -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
  && HDAIMG="-hda path/to/maindisk.qcow2" \
  && MACHINE="-m size=4G -smp cores=2" \
  && BIOSDIR="path/to/Bios"
  && 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=n0 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:2222-:22" \
  && HOSTSPECIFICOPTS="--enable-kvm" \
  && HOSTSPECIFICOPTS="-L ${BIOSDIR:?} -bios ${BIOSFILE:?}" \

Start with installation disk

  && CDROM="-cdrom path/to/installer.iso" \
  && qemu-system-x86_64 ${HOSTSPECIFICOPTS:?} ${MACHINE:?} -boot order=dc ${CDROM} ${HDAIMG:?} ${NETWORK:?} ${FIXMOUSEALIGN:?} \

Regular boot

  && qemu-system-x86_64 ${HOSTSPECIFICOPTS:?} ${MACHINE:?} -boot order=c ${HDAIMG:?} ${NETWORK:?} ${FIXMOUSEALIGN:?} \


## aarch64 (not working yet)

  #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

  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

### 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 directory via NFS

For debian HOST install 'nfs-kernel-system', then:
  vim /etc/exports
  Example: "/path/to/share 127.0.0.1(rw,no_subtree_check,insecure)"
  exportfs -a; /etc/init.d/nfs-kernel-server restart

For windoof HOST install WinNFSd, then:
  WinNFSd.exe -addr 127.0.0.1 C:\path\to\share /path/to/share

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

Keep build result inside vm:
  mkdir /tmp/build /mnt/host/build
  mount --bind /tmp/build /mnt/host/build


## USB pass-through

NOTE: Couldn't yet test any of those commands.

### By physical port
  -usb -device usb-host,hostbus=1,hostport=2

### By physical port via hub
  -usb -device usb-host,hostbus=1,hostport=2.3

### By device
  -usb -device usb-host,vendorid=123,productid=456


## 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 \
  && 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 \
  && true


## Debian PostInstall

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

  true \
  && 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 \
  && $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)