about summary refs log tree commit diff stats
path: root/tools/qemu.html
blob: b8b462b5c0608e6c355c69c0e5b5688a6ffb090e (plain) (blame)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>Qemu</title>
    </head>
    <body>

        <a href="index.html">Tools Index</a>

        <h1>Qemu</h1>

        <h2 id="kern">1. Host System</h2>

        <p>Prepare host system for virtual machines, this includes create new user,
        loading necessary modules and configure network. Load kvm module, in this example
        intel module is loaded but depends on host cpu;</p>

        <pre>
        # modprobe -a kvm-intel tun virtio
        </pre>

        <p>Add users to kvm group;</p>

        <pre>
        # usermod -a -G kvm machine-admin
        # usermod -a -G kvm username
        </pre>

        <h2 id="disk">2. Disk images</h2>

        <p>Qemu supports multiple disk images types.</p>
        <dl>
            <dt>img</dt>
            <dd>Raw disk image, allows dd to a physical device.</dd>
            <dt>raw</dt>
            <dd>Raw disk image, allows dd to a physical device.</dd>
            <dt>qcow2</dt>
            <dd>Qcow disk image file used by qemu.</dd>
        </dl>

        <p>Create hard disk image, there is different types,
        this describes how to create a qcow2 type;</p>

        <pre>
        $ qemu-img create -f qcow2 crux-img.qcow2 15G
        </pre>

        <h3 id="mount">2.1. Mount images</h3>

        <p>Qemu disk images can be treated as regular disks using
        qemu disk network block device server;</p>

        <pre>
        $ sudo modprobe nbd
        $ sudo qemu-nbd -c /dev/nbd0 crux-img.qcow2
        </pre>

        <p>Information about preparing
        <a href="../core/install.html#step2">partitions</a>
        and <a href="storage.html">storage</a> administration.
        You can use image as a normal disk, example how
        to use parted to create a gpt system table;</p>

        <pre>
	parted --script ${DEV} \
	     mklabel gpt \
	     unit mib \
	     mkpart primary 2 4 \
	     name 1 grub \
	     mkpart ESP fat32 4 128 \
	     name 2 efi \
	     mkpart primary ext4 128 1128 \
	     name 3 boot \
	     mkpart primary ext4 1128 12128 \
	     name 4 root \
	     mkpart primary ext4 12128 14128 \
	     name 5 var \
	     mkpart primary ext4 14128 100% \
	     name 6 lvm \
	     set 1 bios_grub on \
	     set 2 boot on \
	     set 6 lvm on
        </pre>

        <pre>
        # kpartx -a -s -l -u /dev/nbd0
        </pre>

        <p>Use /dev/mapper/$(name_of_device) to assign correct blocks;</p>

        <pre>
	mkfs.fat -F 32  /dev/mapper/${DEV_NAME}p2
	mkfs.ext4       /dev/mapper/${DEV_NAME}p3
	mkfs.ext4       /dev/mapper/${DEV_NAME}p4
	mkfs.ext4       /dev/mapper/${DEV_NAME}p5
	pvcreate        /dev/mapper/${DEV_NAME}p6
        </pre>

	<p>Read <a href="lvm.html">lvm</a> documentation on how to setup
	virtual group and logic volumes.</p>

        <p>Mount partition;</p>

        <pre>
	mount /dev/mapper/${DEV_NAME}p4 $CHROOT
	mkdir -p $CHROOT/proc
	mkdir -p $CHROOT/sys
	mkdir -p $CHROOT/dev
	mkdir -p $CHROOT/media

	mkdir -p $CHROOT/boot
	mount /dev/mapper/${DEV_NAME}p3 $CHROOT/boot
	mkdir -p $CHROOT/boot/efi
	mount /dev/mapper/${DEV_NAME}p2 $CHROOT/boot/efi
	mkdir -p $CHROOT/var
	mount /dev/mapper/${DEV_NAME}p5 $CHROOT/var
        </pre>

        <p>Before disconnecting image, clean dev mappings;</p>

        <pre>
        $ sudo kpartx -d /dev/nbd0
        $ sudo qemu-nbd -d /dev/nbd0
        </pre>

        <h3 id="resize">2.2. Resize images</h3>

        <p>Verify disk image information;</p>

        <pre>
        $ qemu-img info c1-storage.qcow2
        </pre>

        <pre>
	image: c1-storage.qcow2
	file format: qcow2
	virtual size: 10G (10737418240 bytes)
	disk size: 7.6G
	cluster_size: 65536
	Format specific information:
	    compat: 1.1
	    lazy refcounts: false
	    refcount bits: 16
	    corrupt: false
	$
	</pre>

	<p>In this example is added 25G to the image;</p>

	<pre>
	$ qemu-img resize c1-storage.qcow2 +25G
	</pre>

        <p>Read <a href="lvm.html#resize">lvm resize</a> if image
        is using lvm, or use resize2fs. If size is not provided to resize2fs,
        by default it will grow file system to all partition;</p>

        <pre>
        $ sudo qemu-nbd -c /dev/nbd0 /srv/qemu/img/c1-server.qcow2
        </pre>

        <pre>
        # kpartx -a -s -l -u /dev/nbd0
        GPT:Primary header thinks Alt. header is not at the end of the disk.
        GPT:Alternate GPT header not at the end of the disk.
        GPT: Use GNU Parted to correct GPT errors.

        # parted /dev/nbd0
        GNU Parted 3.2
        Using /dev/nbd0
        Welcome to GNU Parted! Type 'help' to view a list of commands.
        (parted) print
        Warning: Not all of the space available to /dev/nbd0 appears to be used, you can
        fix the GPT to use all of the space (an extra 16777216 blocks) or continue with
        the current setting?
        Fix/Ignore? Fix

        (parted) resizepart 3 100%
        (parted) quit
        </pre>

        <pre>
        # e2fsck /dev/mapper/nbd0p3
        # resize2fs /dev/mapper/nbd0p3
        </pre>

        <h2 id="net">3. Network</h2>

        <p>Network configuration;</p>

        <dl>
            <dt>slirp</dt>
            <dd>Default virtual NAT'd network.</dd>
            <dt>tun/tap</dt>
            <dd>Good performance to create virtually any type of network
            topology.</dd>
            <dt>vde</dt>
            <dd>The VDE networking backend.</dd>
        </dl>

        <pre>
        KERNEL=="tun", GROUP="kvm", MODE="0660", OPTIONS+="static_node=net/tun"
        </pre>

        <h3>3.1. Routing</h3>

        <p>Create interface with correct permissions set for kvm group.</p>

        <pre>
        # sysctl -w net.ipv4.ip_forward=1
        # iptables -A INPUT -i br0 -j ACCEPT
        # iptables -A FORWARD -i br0 -j ACCEPT
        # iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.0/24 -j ACCEPT
        # iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE
        </pre>

        <h3>3.2. Public Bridge</h3>

        <p>Create <a href="network.html#bridge">bridge</a>, create new
        tap and add it to bridge;</p>

        <pre>
        DEV="br0"

        ADDR=10.0.0.254
        NET=10.0.0.0
        GW=10.0.0.1
        MASK=24

        # one tap for each cpu core
        NTAPS=$((`/usr/bin/nproc`))

        case $1 in
            start)
                /sbin/ip link add name ${DEV} type bridge
                /sbin/ip addr add ${ADDR}/${MASK} dev ${DEV} broadcast +
                /sbin/ip link set dev ${DEV} up
                /bin/sleep 0.2s

                for i in `/usr/bin/seq $NTAPS`
                do
                    TAP="tap$i"
                    echo "Setting up ${TAP} tap interface."
                    /sbin/ip tuntap add ${TAP} mode tap group kvm
                    /sbin/ip link set ${TAP} up
                    /bin/sleep 0.2s
                    /sbin/ip link set ${TAP} master ${DEV}
                done

                exit 0
                ;;
            stop)

                for i in `/usr/bin/seq $NTAPS`
                do
                    TAP="tap$i"
                    echo "Deleting ${TAP} tap interface."
                    /sbin/ip link del ${TAP}
                done

                /sbin/ip link set dev ${DEV} down
                /sbin/ip route flush dev ${DEV}
                /sbin/ip link del ${DEV}
                exit 0
                ;;
            restart)
                $0 stop
                $0 start
                ;;
            *)
                echo "Usage: $0 [start|stop|restart]"
                ;;
        esac

        # End of file
        </pre>

        <h2 id="guest">4. Guest System</h2>

        <p>See <a href="scripts/runvm/runvm.sh">scripts/runvm/runvm.sh</a>,
        as template. Example scripts;</p>

        <p>runvm/profile/crux</p>
        <pre>
        mac=$(rmac_addr)
        memory=1024
        boot=d
        tap="tap1"
        iso=iso/crux-3.4.iso
        image=img/crux-standard.qcow2
        other="-vga std -display sdl"
        </pre>

        <p>runvm/runvm.sh</p>

        <pre>
        function rmac_addr (){
        printf '54:60:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))
        }

        source profile/$1

        qemu-system-x86_64 \
            -enable-kvm \
            -m ${memory} \
            -boot ${boot} \
            -cdrom ${iso} \
            -hda ${image} \
            -device e1000,netdev=t0,mac=${mac} \
            -netdev tap,id=t0,ifname=${tap},script=no,downscript=no \
            ${other} \
            &
        </pre>

        <p>Set guests machines to run under the total resolution provided
        by  host system configure grub on the guest with gfxmode;</p>

        <h3 id="graphics">4.1. Guest Graphics</h3>

        <p>Get current resolution on host machine;</p>

        <pre>
        $ xrandr --current | fgrep '*'
            1366x768      60.00*+
        </pre>

        <p>Set grub gfxmod on guest machine, edit /etc/default/grub;</p>

        <pre>
        GRUB_GFXMODE=1366x768
        GRUB_GFXPAYLOAD_LINUX=keep
        </pre>

        <p>Update grub configuration on guest machine;</p>

        <pre>
        # grub-mkconfig -o /boot/grub/grub.cfg
        </pre>

        <h3 id="sound">4.2. Guest Sound</h3>

        <p>Check if DMAR is enable on kernel configuration,
        Intel and AMD uses different technology. To check on
        Inter machine run;</p>

        <pre>
	# grep -e DMAR -e IOMMU
	</pre>

        <p>runvm/profile/crux</p>

        <pre>
        export QEMU_AUDIO_DRV=alsa
        memory=1024
        boot=c
        iso=iso/devuan_jessie_1.0.0_amd64_CD.iso
        image=img/c12-dvd.qcow2
        tap="tap2"
        mac="54:60:be:ef:5c:72"
        other="-soundhw hda -vga std -display sdl"
        </pre>

        <h3 id="usb">4.3. Guest USB</h3>

        <pre>
        # lsusb
        # ls /dev/v4l
        # ls /dev/bus/usb
        </pre>

        <pre>
        # chown root:kvm /dev/bus/usb/003/004
        </pre>

        <pre>
        export QEMU_AUDIO_DRV=alsa
        memory=1024
        boot=c
        iso=iso/devuan_jessie_1.0.0_amd64_CD.iso
        image=img/c12-dvd.qcow2
        tap="tap2"
        mac="54:60:be:ef:5c:72"
        other="-soundhw hda -vga std -display sdl -usb -device usb-host,vendorid=0x13d3,productid=0x5652"
        </pre>

        <h2 id="bootusb">5. Boot iso on usb</h2>

        <pre>
        # lsusb
        # ls /dev/bus/usb
        </pre>

        <pre>
        # chown root:kvm /dev/bus/usb/003/012
        </pre>


        <pre>
        $ qemu-system-x86_64 -m 512 -enable-kvm -vnc :0 -usb  -device usb-host,hostbus=3,hostaddr=12
        </pre>

        <a href="index.html">Tools Index</a>
        <p>This is part of the Tribu System Documentation.
        Copyright (C) 2020
        Tribu Team.
        See the file <a href="../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>
    </body>
</html>