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.

2.19.2009

Bye bye Xandros....

Two days ago my Eee PC went crazy. First it has showed that there are a lot of updates including an upgrade from firefox 2 to version 3. I said OK, it should be all right. But then it has shown that the upgrade has failed and started to behave in a strange way. Soon I had to realize that the writable part of the root partition is full.
Eee version of Xandros have an interesting partition scheme. The root partition is on the 4 Gb SSD disk and consist of two parts: an approx. 3.4 Gb read only part and a 600 Mb writable part. Any additional programs go to the writeable part, so there is no too much space. Contrary on the read only part there is some 2.5 Gb free space which is not accessible. I think this is a design error. Anyway, I have used the backup function which copies the files from the read only part therefore restoring everything but the user's data to the factory defaults. Otherwise it is not a bad idea, but I hate that I have just a very limited space for additions.
Anyway, I have restored the default program set. And again, I applied the upgrades without installing any additional software. And again! The writeable part become full, so I cannot use the system. That was the last drop, since it is obviously a design problem.
I was looking for a suitable alternative and finally I ended up with Eeebuntu (Netbook remix). I am really not a fan of Ubuntu, I do not like the hype around it, so I had doubts about it, but I gave it a try. Now I have installed everything, and even more. All the features has worked out of box, webcam, wifi, wired ethernet, hybernation etc. I dedicated the 4Gb disk to the system (never again too small space!) and the 16 Gb disk to home. I am a bit worried about the wearing of the SSD disks, so I will look around the right mount options like noatime, and the migration of logs to a tmpfs filesystem. At the moment I am very satisfied with Eeebuntu, I like some of the features I lacked from Xandros. For example I like that the system does not hybernate when it is on the charger and the lid is closed. This way I can listen for internet radios without keeping the stuff open. Since I do it a lot, now the LCD does not get dirty.
I hope I can use now the system even more efficiently, since I like a lot the hardware, just Xandros what made it slow and not suitable for me. And I can install even gramps for my family history research!

1.01.2009

Civic adventures

My favourite computer games are the ones where you guide a country, build towns, and research advances. So the Civilization and it clones. Up to now I played the different versions of Freeciv and I am really happy with that, except that its AI is quite week, plus I would like to see a bit more animation during playing. There is a single version of the official Sid Meyer's Civilization series which was released to Linux, and it was the chapter “Call to power”. It is and old Loki game, but still you can buy it from tuxgames, the supplier of commercial Linux games, although Loki itself has bankrupted a few years ago. I have decided that it is time to buy this piece of gold, and I have done so.
The box was delivered in a few days, and it contains a fancy handbook and big format charts explaining the game in addition to the installation CD. Since the game was developed almost a decade ago, it is a bit tricky to fire up. I the firs step I have installed it on my Eee PC.
Of course the Eee PC does not have an optical drive, so I had to create an iso file from the CD on my desktop:

dd if=/dev/sr1 of=civctp.iso

where /dev/sr1 is the device file of the cd-reader. After migrating the file to the Eee pc (quick LAN favoured!) I can easily mount the iso as a cd:

sudo -i
mkdir /mnt/cdrom
mount -o loop /path/to/civctp.iso /mnt/cdrom

After that the /mnt/cdrom directory is just like a normal CD-ROM, I can do the installation. The installer itself requires Tk/Tcl and the version from the deion repository refused to work, so I have installed the game manually. It means that I had to follow the instructions of the readme file on the CD, untar two archives, applying the last official patch from Loki (version 1.2a), and that is all. I have also installed the video files so I can enjoy the animations too. Alltogether the full installation requires some 700 Mb disk space. I have put it to the user's home directory, because there is enough space there. Xandros for Eee PC comes with glibc 2.1 (shame on Asus to supply so old buggy system), but luckily this is the version against the game was compiled so it runs without additional tweaks. The only thing which is bad is the sound, It is skipping which is annoying. I think it could be fixed using the old SDL mixer, but I do not care because I play with the sound off not to disturb the rest of the family.
Doing the installation on my desktop PC was a bit more tricky. This time the installer run normally, so I installed he game via the easy way into /usr/local/games as root. Again, I have installed the videos. This way I do not have to insert the CD every time I (or my son) play. After starting the game, we can see the opening film, but after that it crashes with error related to linking. I have found the solution on the old gentoo wiki which has vanished this year, so I summarize shortly to fellow players. The problem caused that the old Loki games are compiled with the old libraries and the new versions installed on the modern systems are not compatible with the games. The main guilty component is glibc which has broken compatibility with version 2.3 several years ago. The solution is to pre-load the old libraries for these games. The bright gurus of the Linux world have collected the compatibility libraries and they are available as a tar package: loki_compat_libs-1.3.tar.bz2. I have opened it and put them into the /usr/local/games/CivCTP/Loki_Compat/ directory. I have created a startup script called /usr/local/bin/civctp with this content:

LD_LIBRARY_PATH=/usr/local/games/CivCTP/Loki_Compat/ /usr/local/games/CivCTP/Loki_Compat/ld-linux.so.2 /usr/local/games/CivCTP/civctp.dynamic --nocdrom --nosound

I can point the start icons to that script and it will start the game properly, but again, without sounds. So that's all the tricks, and now we can enjoy this evergreen game, one of the bests of the series.