1313 "disk-path" : "/machine01.img" ,
1414 "disk-size" : "5G" ,
1515 "memory" : "2G" ,
16- "tap-index-fd " : [( 0 , 30 ), ( 1 , 40 ) ],
16+ "lan_indices " : [0 , 1 ],
1717 "serial-port" : 4000 ,
1818 },
1919 "machine02" : {
2222 "disk-path" : "/machine02.img" ,
2323 "disk-size" : "5G" ,
2424 "memory" : "2G" ,
25- "tap-index-fd " : [( 2 , 50 ), ( 3 , 60 ) ],
25+ "lan_indices " : [2 , 3 ],
2626 "serial-port" : 4001 ,
2727 },
2828 "machine03" : {
3131 "disk-path" : "/machine03.img" ,
3232 "disk-size" : "5G" ,
3333 "memory" : "2G" ,
34- "tap-index-fd " : [( 4 , 70 ), ( 5 , 80 ) ],
34+ "lan_indices " : [4 , 5 ],
3535 "serial-port" : 4002 ,
3636 },
3737}
@@ -126,18 +126,6 @@ def _delete_vm_disk(path):
126126
127127 @staticmethod
128128 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-
141129 cmd = [
142130 "qemu-system-x86_64" ,
143131 "-name" , machine .get ("name" ),
@@ -153,13 +141,13 @@ def _start_vm(machine):
153141 "-nographic" ,
154142 ]
155143
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' )
163151
164152 cmd .append ("&" )
165153
0 commit comments