Projecte

General

Perfil

Virtualbox » Historial » Revisió 17

Revisió 16 (Pau Escrich, 06-01-2013 21:44) → Revisió 17/18 (Pau Escrich, 06-01-2013 22:36)

{{toc}} 

 h1. Virtualbox 

 It is possible to emulate qMp with Virtual Box. 

 First of all open your virtualbox (VB) and create a new machine (type linux 2.6). 
 If you are using the VB wizard, do not select any storage device, we will do it latter. 

 h3. Storage 

 To make the things easy it is better to use IDE instead of SATA.  
 Create a new IDE disk and browse between your foldersand select the qMp vdi image file. 

 The vdi file can be obtained here: http://fw.qmp.cat/testing with the name VBox-qMp_testing-factory-XXXXX.bin 
 It should be renamed to something such as VBox-qMp1.vd1 

 h3. Network Configuration 

 First of all we create a virtual tun/tap device in the host machine. 

 <pre>ip tuntap add mode tap dev vbox  
 ip link set vbox up 
 ip addr add 172.30.22.10/24 dev vbox 
 </pre> 

 In VB machine configuration at least one network interface must be added. 
 It should be configured as bridged interface to our new "vbox" network device. 
 The driver "PCnet-PCI II" works fine. 


 h3. Serial 

 A serial port is needed because OpenWRT expects it. So it should be added in the VB machine configuration. 

 It is possible to use it to connect with the qMp node from the host machine (useful for debug), but it is NOT NEEDED. 
 To do so you should configure the VB serial as host pipe. 
 Check the option "create pipe" and add the path "/tmp/vboxS0". 
 Then in the host machine execute: 
 <pre>socat UNIX-CONNECT:/tmp/vboxS0 TCP-LISTEN:7777 & 
 netcat localhost 7777 
 </pre> 

 And you will get serial! 

 h3. Start VM 

 At this point you can start your virtual machine with qMp, open a browser and go to http://172.30.22.1 

 If after the first reboot (automatic reboot) it does not boot (stays in "GRUB LOADING") reboot manually the VM. 

 h3. Clone 

 Ok, but just one node is quite boring so let's go to create more of them. Just switch-off the virtual machine, right click and Clone it. 
 Select the option "regenerate MAC addresses", boot the new    machine and then execute: 

 <pre>rm /qmp_configured 
 /etc/init.d/qmp_autoconf start 
 </pre> 

 Then both nodes should see each other! 

 h1. QEMU 

 It is possible to use the same VirtualBox VDI image file. So again, you can get it from http://fw.qmp.cat 
 Copy it to somewhere and change the name to, for instance "qMp1.vdi". 

 Then just start QEMU: 

 <pre>sudo qemu -net nic,model=pcnet -m 64 -hda qMp1.vdi</pre> 

 You can add "-nographic" option and "-daemonize". 

 h3. Serial 

 Get serial is very easy, just add this option to QEMU 

 <pre>-serial pty</pre> 

 The qmeu will show you the pty device attached to the serial port of the virtual machine, then just use it: 

 <pre>screen /dev/pts/3</pre> 

 Amazing! 

 h3. Network 

 There are several ways to get the network working, I use the combo bridge+tap. 
 In the host machine we will have a bridge named "qemu" and the network interface of the virtual machine 
 will be a tap device attached to such bridge. 

 The next options must be added to the qemu exec command line 

 <pre>-net tap,ifname=qmp1</pre> 

 And these two scripts are needed: 

 */etc/qemu-ifup* 
 <pre> 
 #!/bin/sh 
 if ! brctl show | grep qemu; then 
	 brctl addbr qemu 
	 ip link set qemu up 
 fi 
 echo "Executing /etc/qemu-ifup" 
 echo "Bringing up $1 for bridged mode..." 
 sudo /sbin/ip link set $1 up promisc on 
 echo "Adding $1 to br0..." 
 sudo brctl addif qemu $1 
 sleep 2 
 </pre> 

 */etc/qemu-ifdown* 
 <pre> 
 #!/bin/sh  
 echo "Executing /etc/qemu-ifdown" 
 sudo /sbin/ip link set $1 down 
 sudo /usr/sbin/brctl delif qemu $1 
 sudo /sbin/ip link delete dev $1 
 </pre> 

 And make them executable 

 <pre>chmod +x /etc/qemu-*</pre> 

 To reach the node it is needed to add an IP to the host machine bridge: 
 <pre>ip addr add 172.30.22.10/24 dev qemu</pre> 

 h3. Multiple nodes 

 Each virtual machine needs its own disk file, so copy the VirtualBox vdi image to, for instance "qMpv2.vdi". 
 You should use a new one, do not copy the image from another existing machine! 

 To skip the problem of duplicate MAC addresses, the option "macaddr" should be added to the previous net option. 

 <pre>-net tap,ifname=qmp1,macaddr=$(/etc/qemu-mac)</pre> 

 And another script is needed (make it executable too!) 

 */etc/qemu-mac* 
 <pre> 
 #!/bin/sh 
 # generate a random mac address for the qemu nic 
 printf 'CA:FE:BA:BE:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256)) 
 </pre> 

 I use this little script to launch all the virtual machines from a directory: 

 <pre> 
 #!/bin/sh 
 VMDIR="${1:-.}" 

 for IMG in $VMDIR/*.vdi 
   do 
	 echo "Starting image $IMG" 
	 sudo qemu -net nic,model=pcnet,macaddr=$(/etc/qemu-mac) \ 
	 -net tap,ifname=qmp$(($RANDOM%30)) -m 64 -hda $IMG -nographic -serial pty -daemonize 
   done 
 </pre> 

 Example of execution: 

 <pre> 
 p4u@eni4c:~/virtual $ ls 
 qMp1.vdi    qMp2.vdi    qmpvirt.sh 

 p4u@eni4c:~/virtual $ sh qmpvirt.sh  
 Starting image ./qMp1.vdi 
 Executing /etc/qemu-ifup 
 Bringing up qmp4306 for bridged mode... 
 Adding qmp13 to br0... 
 char device redirected to /dev/pts/5 

 Starting image ./qMp2.vdi 
 Executing /etc/qemu-ifup 
 Bringing up qmp21308 for bridged mode... 
 Adding qmp21 to br0... 
 char device redirected to /dev/pts/7 
 </pre> 

 A more advanced version of this scripts is attached as file to this article.