13
13
"disk-path" : "/machine01.img" ,
14
14
"disk-size" : "5G" ,
15
15
"memory" : "2G" ,
16
- "tap-index-fd " : [( 0 , 30 ), ( 1 , 40 ) ],
16
+ "lan_indices " : [0 , 1 ],
17
17
"serial-port" : 4000 ,
18
18
},
19
19
"machine02" : {
22
22
"disk-path" : "/machine02.img" ,
23
23
"disk-size" : "5G" ,
24
24
"memory" : "2G" ,
25
- "tap-index-fd " : [( 2 , 50 ), ( 3 , 60 ) ],
25
+ "lan_indices " : [2 , 3 ],
26
26
"serial-port" : 4001 ,
27
27
},
28
28
"machine03" : {
31
31
"disk-path" : "/machine03.img" ,
32
32
"disk-size" : "5G" ,
33
33
"memory" : "2G" ,
34
- "tap-index-fd " : [( 4 , 70 ), ( 5 , 80 ) ],
34
+ "lan_indices " : [4 , 5 ],
35
35
"serial-port" : 4002 ,
36
36
},
37
37
}
@@ -126,18 +126,6 @@ def _delete_vm_disk(path):
126
126
127
127
@staticmethod
128
128
def _start_vm (machine ):
129
- nics = []
130
- netdevices = []
131
- for tap in machine .get ("tap-index-fd" , []):
132
- ifindex = tap [0 ]
133
- fd = tap [1 ]
134
-
135
- mac = subprocess .check_output (["cat" , "/sys/class/net/macvtap{ifindex}/address" .format (ifindex = ifindex )]).decode ("utf-8" ).strip ()
136
- tapindex = subprocess .check_output (["cat" , "/sys/class/net/macvtap{ifindex}/ifindex" .format (ifindex = ifindex )]).decode ("utf-8" ).strip ()
137
-
138
- nics .append ("virtio-net,netdev=hn{ifindex},mac={mac}" .format (ifindex = ifindex , mac = mac ))
139
- netdevices .append ("tap,fd={fd},id=hn{ifindex} {fd}<>/dev/tap{tapindex}" .format (fd = fd , ifindex = ifindex , tapindex = tapindex ))
140
-
141
129
cmd = [
142
130
"qemu-system-x86_64" ,
143
131
"-name" , machine .get ("name" ),
@@ -153,13 +141,13 @@ def _start_vm(machine):
153
141
"-nographic" ,
154
142
]
155
143
156
- for nic in nics :
157
- cmd . append ( "-device" )
158
- cmd . append ( nic )
159
-
160
- for device in netdevices :
161
- cmd .append (" -netdev" )
162
- cmd .append (device )
144
+ for i in machine [ "lan_indices" ] :
145
+ with open ( f'/sys/class/net/lan { i } /address' , 'r' ) as f :
146
+ mac = f . read (). strip ( )
147
+ cmd . append ( '-device' )
148
+ cmd . append ( f'virtio-net,netdev=hn { i } ,mac= { mac } ' )
149
+ cmd .append (f' -netdev' )
150
+ cmd .append (f'tap,id=hn { i } ,ifname=tap { i } ,script=/mini-lab/mirror_tap_to_lan.sh,downscript=no' )
163
151
164
152
cmd .append ("&" )
165
153
0 commit comments