3
3
In manual part we are going to see two base example of using ``` QEMU ``` in order to virtualize
4
4
our system.
5
5
6
- ## VM Creation
6
+ ## 1. VM Creation
7
7
8
8
In first example we are going to create a virtual machine on our system using ``` QEMU ``` .
9
9
@@ -46,7 +46,59 @@ After installing Ubuntu, make sure you remove the -cdrom flag from the qemu comm
46
46
qemu-system-x86_64 -enable-kvm -boot menu=on -drive file=Image.img -m 4G -cpu host -vga virtio -display sdl,gl=on
47
47
```
48
48
49
- ## CPU Hotplugin
49
+ ## 2. Hosting QEMU VMs with Public IP Addresses using TAP Interfaces
50
+
51
+ To allow VMs to have their own IP addresses while using the same physical link, we need to introduce a bridge into this setup.
52
+ Both the VM’s network interface as well as the host’s interface will be connected to the bridge.
53
+
54
+ ### create bridge
55
+
56
+ ``` shell
57
+ ip link add br0 type bridge
58
+ ip link set br0 up
59
+ ```
60
+
61
+ Next, we want to connect the eth0 interface to the bridge and re-assign the IP address from eth0 to br0. Please note that these commands
62
+ will drop internet connectivity of your machine, so either only run them on a local PC or ensure that in case of disaster
63
+ you can reboot the machine somehow.
64
+ ``` 192.168.0.10 ``` is just an example IP address, you need to use your own of course.
65
+
66
+ ### add ip to bridge
67
+
68
+ ``` shell
69
+ ip link set eth0 up
70
+ ip link set eth0 master br0
71
+
72
+ ip addr flush dev eth0
73
+
74
+ ip addr add 192.168.0.10/24 brd + dev br0
75
+ ip route add default via 192.168.0.1 dev br0
76
+ ```
77
+
78
+ ### tap interface
79
+
80
+ Next, we can create the TAP interface to be used by the VMs. ``` $YOUR_USER ``` is used to allow an unprivileged user to connect to the TAP device.
81
+ This is important for QEMU, since QEMU VMs should be started as non-root users.
82
+
83
+ ``` shell
84
+ ip tuntap add dev tap0 mode tap user $YOUR_USER
85
+ ip link set dev tap0 up
86
+ ip link set tap0 master br0
87
+ ```
88
+
89
+ ### start
90
+
91
+ And now we can start a VM. The MAC address could e.g. be generated randomly:
92
+
93
+ ``` shell
94
+ RAND_MAC=$( printf ' DE:AD:BE:EF:%02X:%02X\n' $(( RANDOM% 256 )) $(( RANDOM% 256 )) )
95
+
96
+ qemu-system-x86_64 -m 1024 -cdrom archlinux-2020.05.01-x86_64.iso \
97
+ -device virtio-net-pci,netdev=network0,mac=$RAND_MAC \
98
+ -netdev tap,id=network0,ifname=tap0,script=no,downscript=no
99
+ ```
100
+
101
+ ## 3. CPU Hotplugin
50
102
51
103
Run ``` qmp-shell ``` (located in the source tree, under ``` /scripts/qmp/ ``` ) to connect to the just-launched ** QEMU** :
52
104
@@ -63,7 +115,7 @@ query-hotpluggable-cpus
63
115
The ``` query-hotpluggable-cpus ``` command returns an object for CPUs that are present
64
116
(containing a ``` qom-path ``` member).
65
117
66
- ## Adding a new vCPU
118
+ ### adding a new vCPU
67
119
68
120
From its output, we can see that ** IvyBridge-IBRS-x86_64-cpu** is present in socket 1,
69
121
while hot-plugging a CPU into socket 1 requires passing the listed properties to QMP device_add:
@@ -72,7 +124,7 @@ while hot-plugging a CPU into socket 1 requires passing the listed properties to
72
124
device_add id=cpu-2 driver=IvyBridge-IBRS-x86_64-cpu socket-id=0 core-id=1 thread-id=0
73
125
```
74
126
75
- ### Question 1
127
+ #### Question 1
76
128
77
129
<details >
78
130
@@ -95,10 +147,14 @@ Optionally, run QMP ```query-cpus-fast``` for some details about the vCPUs:
95
147
query-cpus-fast
96
148
```
97
149
98
- ### Removing vCPU
150
+ ### removing vCPU
99
151
100
152
From the ``` qmp-shell ``` , invoke the QMP ``` device_del ``` command:
101
153
102
154
``` shell
103
155
device_del id=cpu-2
104
156
```
157
+
158
+ ## Resources
159
+
160
+ - [ blog.stefan-koch.name] ( https://blog.stefan-koch.name/2020/10/25/qemu-public-ip-vm-with-tap )
0 commit comments