Difference between revisions of "Running Alpine Linux As a QEMU networked Guest"

From Alpine Linux
Jump to: navigation, search
(Category:Virtualization)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
To run an alpinelinux iso from within qemu is trivial:
 
 
 
  qemu  -boot d -cdrom alpine*.iso
 
 
 
To get networking running correctly, you can use the tun/tap interface, which then becomes a real interface.  The key is to define the virtual network interface on the correct virtual vlan, and the correct ifup script.
 
To get networking running correctly, you can use the tun/tap interface, which then becomes a real interface.  The key is to define the virtual network interface on the correct virtual vlan, and the correct ifup script.
  
 
You need 2 net commands on the command line interface, one for the host:
 
You need 2 net commands on the command line interface, one for the host:
  
  -net tap,vlan=[somenumber],ifname=[host if],script=[some script]
+
-net tap,vlan=[somenumber],ifname=[host if],script=[some script]
  
 
one for the guest
 
one for the guest
 
    
 
    
  -net nic,vlan=[samenumber]
+
-net nic,vlan=[samenumber]
 
 
  
 +
So to have a single NIC on the qemu virtual system that is connected to tap0 on the physical host:
  
So to have a single nic on the virtual box that is connected to tap0 on the physical host:
+
qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
 
+
     -boot d -cdrom alpine*.iso}}
  qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
 
     -boot d -cdrom alpine*.iso
 
  
  
 
To create a qemu guest with more than one nic, just repeat the -net commands
 
To create a qemu guest with more than one nic, just repeat the -net commands
  
  qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
+
qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
 
       -net tap,vlan=0,ifname=tap1,script=./qemu-ifup -net nic,vlan0 \
 
       -net tap,vlan=0,ifname=tap1,script=./qemu-ifup -net nic,vlan0 \
 
       -net tap,vlan=0,ifname=tap2,script=./qemu-ifup -net nic,vlan0 \
 
       -net tap,vlan=0,ifname=tap2,script=./qemu-ifup -net nic,vlan0 \
       -boot d -cdrom alpine*.iso
+
       -boot d -cdrom alpine*.iso}}
  
Now your alpine guest will have 3 nics, mapped to tap0, tap1, and tap2 respectively.
+
Now your alpine guest will have 3 NICs, mapped to tap0, tap1, and tap2 respectively.
  
 
What's actually happening is you are effectively creating a point-to-point tunnel, with the phys tap0 device being one endpoint, and the virtual box's eth0 being on the other point of the tunnel.
 
What's actually happening is you are effectively creating a point-to-point tunnel, with the phys tap0 device being one endpoint, and the virtual box's eth0 being on the other point of the tunnel.
  
 
So you need to assign ip addresses to BOTH sides of the tunnel.  The qemu-ifup script is what does that for the host.  Here's an example:
 
So you need to assign ip addresses to BOTH sides of the tunnel.  The qemu-ifup script is what does that for the host.  Here's an example:
 
+
<pre>
  #!/bin/sh
+
#!/bin/sh
  case $1 in
+
case $1 in
        tun0 | tap0 )
+
      tun0 | tap0 )
                sudo /sbin/ip addr add 192.168.1.100/24 dev $1
+
              sudo /sbin/ip addr add 192.168.1.100/24 dev $1
                sudo /sbin/ip link set $1 up
+
              sudo /sbin/ip link set $1 up
                ;;
+
              ;;
        tap1 | tun1 )
+
      tap1 | tun1 )
                sudo /sbin/ip addr add 192.168.2.100/24 dev $1
+
              sudo /sbin/ip addr add 192.168.2.100/24 dev $1
                sudo /sbin/ip link set $1 up
+
              sudo /sbin/ip link set $1 up
                ;;
+
              ;;
        tap2 | tun2 )
+
      tap2 | tun2 )
                sudo /sbin/ip addr add 192.168.3.100/24 dev $1
+
              sudo /sbin/ip addr add 192.168.3.100/24 dev $1
                sudo /sbin/ip link set $1 up
+
              sudo /sbin/ip link set $1 up
                ;;
+
              ;;
        esac
+
      esac
 
+
</pre>
 
 
 
In your alpinebox, create an interfaces file like this:
 
In your alpinebox, create an interfaces file like this:
  
  iface eth0 inet static
+
<pre>
        address 192.168.1.1
+
iface eth0 inet static
        netmask 255.255.255.0
+
      address 192.168.1.1
        gateway 192.168.1.100
+
      netmask 255.255.255.0
 +
      gateway 192.168.1.100
  
  iface eth1 inet static
+
iface eth1 inet static
        address 192.168.2.1
+
      address 192.168.2.1
        netmask 255.255.255.0
+
      netmask 255.255.255.0
 
 
  iface eth0 inet static
 
        address 192.168.3.1
 
        netmask 255.255.255.0
 
  
 +
iface eth0 inet static
 +
      address 192.168.3.1
 +
      netmask 255.255.255.0
 +
</pre>
 +
If on your host you now add a MASQUERADE rule for tap0 to your host's default nic, and you turn on ip_forward on your host, you can now do svn updates, surf, run tranmission, etc right from your qemu guest.
  
 
+
[[Category:Virtualization]]
If on your host you now add a MASQUERADE rule for tap0 to your host's default nic, and you turn on ip_forward on your host, you can now do svn updates, surf, run tranmission, etc right from your qemu guest.
 

Latest revision as of 07:52, 12 March 2012

To get networking running correctly, you can use the tun/tap interface, which then becomes a real interface. The key is to define the virtual network interface on the correct virtual vlan, and the correct ifup script.

You need 2 net commands on the command line interface, one for the host:

-net tap,vlan=[somenumber],ifname=[host if],script=[some script]

one for the guest

-net nic,vlan=[samenumber]

So to have a single NIC on the qemu virtual system that is connected to tap0 on the physical host:

qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
    -boot d -cdrom alpine*.iso}}


To create a qemu guest with more than one nic, just repeat the -net commands

qemu -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -net nic,vlan0 \
      -net tap,vlan=0,ifname=tap1,script=./qemu-ifup -net nic,vlan0 \
      -net tap,vlan=0,ifname=tap2,script=./qemu-ifup -net nic,vlan0 \
      -boot d -cdrom alpine*.iso}}

Now your alpine guest will have 3 NICs, mapped to tap0, tap1, and tap2 respectively.

What's actually happening is you are effectively creating a point-to-point tunnel, with the phys tap0 device being one endpoint, and the virtual box's eth0 being on the other point of the tunnel.

So you need to assign ip addresses to BOTH sides of the tunnel. The qemu-ifup script is what does that for the host. Here's an example:

#!/bin/sh
case $1 in
      tun0 | tap0 )
              sudo /sbin/ip addr add 192.168.1.100/24 dev $1
              sudo /sbin/ip link set $1 up
              ;;
      tap1 | tun1 )
              sudo /sbin/ip addr add 192.168.2.100/24 dev $1
              sudo /sbin/ip link set $1 up
              ;;
      tap2 | tun2 )
              sudo /sbin/ip addr add 192.168.3.100/24 dev $1
              sudo /sbin/ip link set $1 up
              ;;
      esac

In your alpinebox, create an interfaces file like this:

iface eth0 inet static
      address 192.168.1.1
      netmask 255.255.255.0
      gateway 192.168.1.100

iface eth1 inet static
      address 192.168.2.1
      netmask 255.255.255.0

iface eth0 inet static
      address 192.168.3.1
      netmask 255.255.255.0

If on your host you now add a MASQUERADE rule for tap0 to your host's default nic, and you turn on ip_forward on your host, you can now do svn updates, surf, run tranmission, etc right from your qemu guest.