Showing posts with label debian. Show all posts
Showing posts with label debian. Show all posts

12.07.2009

Building a virtual network with qemu

I do not know much about computer networks, but I try to learn things about them. They say, the best way to do is by building a real local network and experimenting with the settings of the software and the hardware. Unfortunately I just cannot do this. I do not have enough space, money, etc. As an alternative I have considered to play with emulated machines which I can run inside my desktop computer, so they do not require additional space. I am somewhat familiar with qemu, I used it earlier to have a look on different Linux distributions without rebooting my PC. I have chosen Debian Lenny as a base system, and I have downloaded the install CD iso from their project page. I have created a 3G size hard-disk image and I have installed the system in the same way, as if I would have done it on a real computer.

qemu-img create qemu_debian_501.img 3G # creating hdd image
qemu -hda qemu_debian_501.img -cdrom debian_cd.iso -boot d -m 256 # installing debian

I have done a minimal installation, since I do not really need any graphic program or interface at this stage on the system. I have run the installed system with the most basic method:

qemu -hda qemu_debian_501.img -m 256 # running debian

This way I will have an internet connection, so I can access anything on the net from the guest system. For example it is not a problem to use the apt to update the system and use the latest packages from the repositories. The first difference of this virtual computer to a real one is the way how it can be reached from the outside world. I had to learn that it is not as simple as I thought earlier. Since (in this configuration) the virtual machine does not have an own IP, we cannot reach it via the network as a normal physical computer. There are several solutions for this problem depending on our needs. Now I do not need much, I just want to access the virtual machine via the standard ssh from outside, mostly from the host machine itself. I have learnt that it can be done easily by redirecting some of the free ports of the host to any ports of the guest system:

qemu -hda qemu_debian_501.img -m 256 -redir tcp:2222::22 # running debian with 2222 to 22 port redirection

With this command the 2222 port of the host is redirected to the port 22 of our virtual debian, so the ssh access becomes possible. So now this simple command on the host will connect to the virtual system:

ssh -p 2222 localhost

Now I have the basic system and I can boot it up with a proper reach from outside. Now I have made three copies of the hard-disk image as sys1, sys2 and sys3.

cp qemu_debian_501.img qemu_debian_501_sys1.img
cp qemu_debian_501.img qemu_debian_501_sys2.img
cp qemu_debian_501.img qemu_debian_501_sys3.img

I will organize these three virtual system into a local network. There are also several solutions to this problem, but it was really hard (to me) to figure out the proper way from the qemu documentation. But it is there, and my efforts were rewarded with success! So I have dreamed a LAN, where sys1 is a gateway to the internet, while sys2 and sys3 are inside my virtual LAN. I want to reach sys1 with the aforementioned way by ssh, but in other respects the LAN should not be reachable from outside. For this configuration I need two network card in sys1, one connecting to the WAN, and the other is connecting to the LAN. Now we have to mention that the qemu environment have some mostly hidden, but very useful features, which are very handy in basic usage. When we fire up a virtual machine, qemu emulate us a network environment with a DHCP server. Since most operating systems tries to configure the first available network card with some DHCP client, this means that in most cases we have immediately network connection, so we can access the Internet from our guest system without any manual configuration. So in our case with the virtual network, connecting the sys1 with its first network card toward the WAN is easy. The other network card of sys1, and in addition the sys2 and sys3 systems will form a separate vlan, therefore we have to configure them independently from the aforementioned network environment. The simplest solution is to add static IP to the three cards from the 192.168.0.1... range, as we would do in a real physical network. Additionally we have to set up sys1 as a router between the two vlans.
The qemu virtual machines are independent program processes on the host system, so we have to let them know how they can communicate with each other. For that purposes if we create a vlan (as in this case), we have to assign to it a physical port on the host system. One of our qemu virtual machines will listen on that port, and the others can connect there, so they can do their network communication via this solution. The problem is that we have to start the instance of listening qemu first, and all the others just after that. (But we do not have to wait at all till the OS on the listening qemu boot up fully.) In addition if the listening qemu process crash for whatever reason, the vlan, which was administered by that process will fail to function, we have to reboot all the systems in the proper order to restart its functionality. In our scenario I have chosen sys1 as the listening qemu process for our vlan.
I can reach sys1 via ssh. I have tarted the three machines like this:

qemu -hda qemu_debian_501_sys1.img -m 256 -net nic,vlan=1 -net user,vlan=1 -net nic,vlan=2,macaddr=52:54:00:12:34:57 -net socket,vlan=2,listen=localhost:1234 -redir tcp:2222::22
qemu -hda qemu_debian_501_sys2.img -m 256 -net nic,vlan=2,macaddr=52:54:00:12:34:01 -net socket,vlan=2,connect=localhost:1234
qemu -hda qemu_debian_501_sys3.img -m 256 -net nic,vlan=2,macaddr=52:54:00:12:34:02 -net socket,vlan=2,connect=localhost:1234

I set up the network cards on the systems as follows:

On sys1 the eth0 card which connects the virtual network to the outside. It gets IP by DHCP client. I put this into the /etc/network/interfaces file:

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

The second card connects to the LAN, so the virtual network itself. I cannot set it up properly with the /etc/network/interfaces settings (maybe my fault), so I just put this line into /etc/rc.local:

ifconfig eth1 192.168.10.2

On the sys2 and sys3 I specified static IP for the network cards. On sys2 I put this into the /etc/network/interfaces :

# The primary network interface
allow-hotplug eth0
auto eth0
iface eth0 inet static
address 192.168.10.3
gateway 192.168.10.2
netmask 255.255.255.0
network 192.168.10.0
broadcast 192.168.10.255

Sys3 was set up in a similar way, with a different IP (192.168.10.4).

After these settings, my virtual network is ready to work.

Additionally I wanted to boot up the virtual machines in “headless mode”, so without the graphical window of qemu. This way I can set up the whole network with three computers, and I can log out from my account on the desktop, and the system is still up and running.

qemu -hda qemu_debian_501_sys1.img -m 256 -net nic,vlan=1 -net user,vlan=1 -net nic,vlan=2,macaddr=52:54:00:12:34:57 -net socket,vlan=2,listen=localhost:1234 -redir tcp:2222::22 -nographic
qemu -hda qemu_debian_501_sys2.img -m 256 -net nic,vlan=2,macaddr=52:54:00:12:34:01 -net socket,vlan=2,connect=localhost:1234 -nographic
qemu -hda qemu_debian_501_sys3.img -m 256 -net nic,vlan=2,macaddr=52:54:00:12:34:02 -net socket,vlan=2,connect=localhost:1234 -nographic

Now everything is perfect! I have a nice system, where I can experiment with the network. I can go in via ssh to localhost port 2222. The systems can see the internet, so I can update them etc.

I have used this page as reference.

10.16.2008

Adding programs to Eee PC 901

So I am a happy owner of an Eee PC 901 with the original linux (almost Xandros) operating system. Before I have bougth this small laptop, I have read a lot of information, how much tweakable this toy is. So it is, and I have found nice pagen on the net providing the details. Unfortunately almost all the documentations are focused on the first generation Eee PCs (700 and 701 if I am correct), and there were some complains in the comments that applying them has made their 901 model broken. So I has become cautious.
I have learned that Eee PC has an operating system derived from Debian Etch via Xandros 4. However the version on the PC is not fully compatible either with Debian or Xandros, therefore I cannot install anything from those distributions. But I have learned from this that Eee PC uses apt as the package mangment system, which is known to be one of the best such package managers. I am quite familiar with rpm and yum from Fedora, so apt is something new for me to learn.
As maybe many of you know, programs in linux are organized to so called packages, which are compressed archives containing the binaries, libraries and usually the documentation necessary for usage of the programs. Package managers are downloading these packages from repositories, therefore the easy way to install new programs to a system to add the correct repos.
The first thing I have made was to replace the Finnish language repositories to the English one to be able to change the language of the system as I have written earlier. For that I have edited /etc/apt/sources.list file as root:

sudo -i

vim /etc/apt/sources.lost

You can use nano instead of vim if you are not familiar with this text editor.
I have changed the line for the Finnish files:
deb http://update.eeepc.asus.com/1.6/ fi main
to the English one:
deb http://update.eeepc.asus.com/1.6/ en main
After this I have installed several programs and packages with the synaptic program, which is a kind of graphical interface for the apt system.
After this I still had a lot of missings from my favourite programs, so I have decided to cross the dangerous line to install packages from Debian Etch. For this I have added a new entry to the sources.list file:

sudo -i

cat >> /etc/apt/sources.list

deb ftp://ftp.us.debian.org/debian/ stable main contrib non-free


Ctrl+D


After this I have downloaded the list of the available packages with
apt-get update
And intstalled some programs:

apt-get install emacs

apt-get install steghide

apt-get install unison


Since that was all what I needed, I have decided to comment out the debian repo from the sources.list file, so it won't offend the updates. To do this I simply put a hashmark (#) symbol to the beginning of the the offending line:
#deb ftp://ftp.us.debian.org/debian/ stable main contrib non-free
I updated again the package list with
apt-get update
And I am back again to the safe waters.
I have also installed the debian packages for FBReader from their site to have a decent ebook reader. So that is all. Now I have everything I need for my work on the Eee PC 901.