Browsed by
Category: Linux

Cross Compile Allegro 5 Programs in Linux for Windows

Cross Compile Allegro 5 Programs in Linux for Windows

The Allegro game programming library has released v5 of their popular library and with it comes a whole mess of great changes. Thing is, since most of the applications you make with it are going to be games, your main audience lives in Windows. Since I am really upset with Microsofts offerings in this area I needed a way to capture this audience without having to drive myself insane.

What follows is how I am making Windows executables in Linux using Allegro. Please note that I live in Ubuntu (currently 10.10, Maverick Meerkat). You may have to make some slight changes to fit your distro but that should not be a big deal. These instructions assume a clean installation where no other copy of Allegro has been installed (not sure if that would be a problem or not as I have not tested).

  1. Install the required programs:
    sudo apt-get install cmake mingw32
  2. Retrieve and uncompress DirectX:
    Download and copy the DirectX headers and libraries to /usr/i586-mingw32msvc/. Note the file structure within the archive should compare to the mentioned directory. When prompted to overwrite any files do so but make sure you have a backup first in case something explodes.
  3. Retrieve and uncompress Allegro 5:
    Download allegro-5.x.x.tar.gz from their site. Uncompress some place easy to get to. I used my desktop as we can delete this when done.
  4. Compile from source:
    In a terminal type
    cd [path to uncompressed archive] && mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-mingw.cmake .. && make && sudo make install
    This may take a little while depending on your hardware.

You should now have a functioning cross compiler setup for Allegro 5. Just replace gcc with i586-mingw32msvc-gcc or g++ with i586-mingw32msvc-g++ (for example, I compiled my first test with i586-mingw32msvc-g++ alleg.cpp -lallegro.dll -lallegro_image.dll -lallegro_font.dll -o alleg.exe). The DLLs you will need are in /usr/i586-mingw32msvc/bin/. You may now delete all of our working files on your desktop (or wherever you put them).

There are still one or two things I need to figure out. For one, dynamically linked programs are peachy on Linux; I am comfortable in my assumption that most people using Linux either already expect this or are willing to learn. Windows, not so much. I want to statically link for that platform but I have yet to experiment with that. Another thing is the fact that my current method has Windows opening up a console window in addition to the “main” window. I am sure this is also very simple but have not yet played with it.

Easily Install An Updated Ubuntu Kernel

Easily Install An Updated Ubuntu Kernel

I recently bought a new laptop. Since this laptop is so new and Ubuntu normally ships with kernel versions a few revisions back not all of my laptops features were supported. Bluetooth, for example, is detected but just refuses to turn on. The solution? A newer kernel with newer everything. Sure, I could just update the drivers but this is more fun.

Canonical offers pre-compiled kernels for Ubuntu. All you have to do is go over to http://kernel.ubuntu.com/~kernel-ppa/mainline/v2.6.39.1-oneiric/ and download which ever version you want. Just make sure you grab the headers (all), headers (architecture), and the image (architecture) packages. So if you wanted to upgrade to 2.6.39 you would download linux-headers-2.6.39-02063901_2.6.39-02063901.201106030905_all.deb, linux-headers-2.6.39-02063901-generic_2.6.39-02063901.201106030905_amd64.deb, and linux-image-2.6.39-02063901-generic_2.6.39-02063901.201106030905_amd64.deb. Just make sure you install them in that order and reboot.

[Source]

Mounting Individual Parititons in RAW Disk Images

Mounting Individual Parititons in RAW Disk Images

Let us say, for the sake of simplicity, that you backup whole disks by using dd in Linux. Now let us assume that you need just a single file out of a backup and do not want to (or can not realistically) restore the whole backup to disk for the single file. You can not mount -o loop because the backup does not contain a single partition. “What to do” you ask? Well I will tell you, wary traveler.

  1. Mount your raw disk image as a loopback device: losetup /dev/loop0 [path to image]
  2. Run kpartx and: kpartx -va /dev/loop0
    This will add your partitions to /dev/mapper/loop0pX where each X is a different partition
  3. You can now mount each partition: mount /dev/mapper/loop0pX /media/partition
    If you want to make sure you do not write anything to your backup simply mount it as read-only: mount -o ro /dev/mapper/loop0pX /media/partition

Once you are finished you will need to do your clean up.

  1. Unmount: umount /media/partition
  2. Remove the mapper devices: kpartx -d /dev/loop0
  3. Remove the loopback device: losetup -d /dev/loop0

These commands may all need to be run as root. If loop0 reports that it is busy just pick another loop device (/dev/loop1, ect).

Crazy RAID Fun

Crazy RAID Fun

I have been experimenting with a number of things in Linux as of late. Breaking out my mad scientist cap– did I ever put it away?– one such thing I started screwing with was software-based RAID arrays in Linux. I wanted to see if I could create a number of encrypted, compressed files, spread them all over the world, and mount them in an RAID array (there is no way to say that any simpler without taking away from it). I put together a little plan in my head and I was off.

I stuck a bunch of Ubuntu installations all over different geographical locations and connected them via OpenVPN and NFS (CIFS would also have worked although probably with a noticeable performance hit). Using dd I created a 1GB file on each of them. Using losetup and its built-in encryption I mounted them all as loopback devices on my local machine. Using mdadm I turned the loopback devices into a RAID array and stuck ext4 on it. Using fusecompress I mounted said ext4 volume to compress everything on the fly.

What possible use could this serve? Well, there is the reason I did it: it is jawesome sauce and I wanted to see if I could. Those reasons, of course, are not a purpose. I suppose, if one really wanted to hide their data, they could use this in a RAID0– or a RAID5/6, even– to spread the data around using a very small cluster size. Theoretically, no two "neighbouring" clusters on the filesystem would be at the same geographical location. This means that if one– or more– sites were compromised not only would the attacker have so little of the data as to be useless but due to the way encryption tends to work (it is nearly impossible to unencrypted anything without the "neighbouring" data) it would be extra useless to them.

Like so many of my experiments this was never meant to be a practical anything. Yeah, I got it working with little difficulty– the real problem was driving around and convincing my friends to let me use their houses and bandwidth– but I can think of a number of issue that would arise in real-world use. First off, the whole concept is predicated on the use of files mounted on network exports. Files on exports potentially far, far away from you. If a network link goes down– or hiccups– you would probably have a bit of an issue. Sure it is all in a RAID but your NFS– or CIFS– mounts are going to wait a bit before they decide to let you know they have gone down. I imagine this would manifest itself as a temporary “freeze” of the filesystem but I have not tested it. A second issue is if you are using a RAID0 for maximum security (as mentioned above) losing anything at all would kill the whole setup. Consider that if your backup is not as secure as your primary then what is the point? Thirdly, depending on which RAID level you choose, you may quickly realise that not all link bandwidth or latency is created equal. I did run into a common mdadm issue where it did not release the devices but did not put any effort into fixing it.

All-in-all I am pretty excited for no good reason; I suppose I just thought it was neat. I do not recommend relying on this unless you can solve the above problems. iSCSI was designed for such things so if you are hell-bent on implementing this idea I suggest you use that instead of the loopback devices. You might have to find a new way to encrypt everything (I bet TrueCrypt would work).

I did write a few short scripts to make my lifer easier but they have no error checking so I decided not to post them. If there is enough interest I could finish them (IE add the error checking) and post them.

Clone, Wipe, or Image Media Using dd to Create RAW Disk Images

Clone, Wipe, or Image Media Using dd to Create RAW Disk Images

I recently came across five Windows machines I needed to get working. These were all to be used for the same purpose and thus I would have had to do the same work five times over. I decided to do the work once and then just clone the original disk using dd on Linux.

What follows are a number of example usages of the dd utility:

  • Make an ISO from a CD/DVD:

    dd if=[device] of=disk.iso bs=2048 conv=sync

  • Overwrite a disk with random data:

    dd if=/dev/urandom of=[device]

    One could use /dev/zero instead of /dev/urandom if they just wanted to zero out a disk.
    Darik’s Boot And Nuke might be a better tool for this.

  • Clone a partition:

    dd if=[device partition source] of=[device partition destination] bs=4K conv=noerror,sync

    Create unformatted partitions first.

  • Clone a disk:

    dd if=[device source] of=[device destination] bs=4K conv=noerror,sync

  • Create an image from a disk:

    dd if=[device source] of=[image destination] bs=4K conv=noerror,sync

  • Restore an image to a disk:

    dd if=[image source] of=[device destination] bs=4K

    .

  • Create a compressed image from a disk:

    dd if=[device source] bs=4K conv=noerror,sync | gzip -c > [image destination]

    It would be a good idea to have your image name end with .gz so you later remember it is a compressed image and also clear out temporary directories.

    Adding --fast to gzip will not stress your CPU as badly (good for netbooks and the like). Adding --best to gzip will give you the best compression but drastically slow you down.

    On a well-traveled filesystem zeroing out the free space can lead to huge space savings. In Linux you can use cd [mounted filesystem] && sudo dd if=/dev/zero of=delme-0 bs=1M &> /dev/null ; sudo dd if=/dev/zero of=delme-1 bs=1 &> /dev/null ; sync && sudo rm delme-1 delme-0 on the mounted filesystem (if you have multiple partitions you should do it for each) while Windows offers sdelete -z [drive letter]:. Depending on the amount of free space this could take a good long while.

  • Restore a compressed image to a disk:

    gzip -d -c [image source] | dd of=[image destination]

If you do not have a working Linux installation you can always use a Live-CD. Use fdisk -l or GParted to figure out what a device is named.

Setting a Static IP and DNS in Debian/Ubuntu over the CLI

Setting a Static IP and DNS in Debian/Ubuntu over the CLI

Some times you just need a static IP but have no GUI to easily set one up. Well it is not so hard to do in Debian-based distributions.

  1. Open /etc/network/interfaces in your favorite editor. I like nano.
  2. Find the line that starts with iface [interface]. The interface you are looking for is most likely eth0 or something similar.
  3. Change the line to read iface [interface] inet static.
  4. After it add your IP information. I have included a common example but just replace my information with your own.
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
  5. Open /etc/resolv.conf.
  6. In it use the nameserver directive to specify your name server(s). The nameserver directive(s) will be searched in the order entered here. Here is an example:
    nameserver 192.168.1.2
    nameserver 192.168.1.3
  7. Restart networking with sudo /etc/init.d/networking restart.

[Edit] 2013.08.10
Thanks to Renat Zaripov for pointing out my mistake in typing /etc/resolve.conf instead of /etc/resolv.conf.

Use GRUB2 to Boot ISOs off a Flash Drive

Use GRUB2 to Boot ISOs off a Flash Drive

I will soon need to build a few new PCs that do not have optical drives (one will be a firewall, for example). They do not need the drives for day-to-day usage but I do need to get an OS on them. Since virtually all consumer boards now have USB and I have piles of unused flash drives laying around I decided not to spend the money on an external drive and go the flash drive route.

Here are the steps I took to get my Linux ISOs to boot off a USB flash drive. For these exact steps to work you will need an existing Linux installation (a LiveCD will work just fine).

  1. Create a new, formatted partition big enough to store your ISOs. I am using EXT4 but any filesystem supported by GRUB2 will work. I prefer to use GParted for this.
  2. Mount your new partition. Most Linux desktop distributions will do this automatically for you if you just unplug the device and plug it back in.
  3. Install GRUB2: grub-install --force --no-floppy --root-directory=[path partition was mounted] /dev/sdX where X is the flash drive.
  4. Copy over any ISOs you want to be able to boot to the root of your new partition.
  5. Create [path partition was mounted]/boot/grub/grub.cfg and add the lines

    set timeout=5
    set default=0

  6. In [path partition was mounted]/boot/grub/grub.cfg add any of the following example entries for your favorite distributions:

    menuentry "Ubuntu 10.10 Desktop i386" {
    loopback loop /ubuntu-10.10-desktop-i386.iso
    linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.10-desktop-i386.iso noeject noprompt splash --
    initrd (loop)/casper/initrd.lz
    }

    menuentry "Ubuntu 10.10 Server i386" {
    loopback loop /ubuntu-10.10-server-i386.iso
    linux (loop)/install/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.10-server-i386.iso noeject noprompt splash --
    initrd (loop)/install/initrd.gz
    }

    menuentry "Darik's Boot And Nuke" {
    loopback loop /dban-2.2.6_i586.iso
    linux (loop)/DBAN.BZI nuke="dwipe" iso-scan/filename=/dban-2.2.6_i586.iso silent --
    }

    menuentry "Memtest 86+" {
    linux16 /memtest86+.bin
    }

I could not, for the life of me, get any Windows ISOs to boot using GRUB2. On top of that I came across a number of people trying to do the same thing whom all failed. If anyone gets this working please post in the comments. I may be able to chainload into another boot loader but that is for another day.

Update 2011.02.18
I found this. It may be nothing but it also may be a jumping off point from which to boot from a Windows ISO.

[Source]

Opening winmail.dat in Thunderbird in Linux

Opening winmail.dat in Thunderbird in Linux

Microsoft, being Microsoft, feels a need to not use the same standard that everyone else has been using for umpteen years. This, of course, creates problems for everyone. A great example of this is their TNEF format which we all see as winmail.dat files in e-mail attachments which does not even work correctly across different versions of Outlook in some cases.

Since I run Ubuntu in my day-to-day I needed a way around this. These are the steps I took.

  • Install the tnef package: sudo apt-get install tnef
  • Create a script in your home directory and make it executable: touch ~/tnef.sh;chmod +x ~/tnef.sh
  • In this newly created file stick

    #!/bin/bash

    SAVE_PATH=~/Desktop/winmail.dat
    mkdir $SAVE_PATH
    tnef -C $SAVE_PATH < $1

  • When you try to open the attachment in Thunderbird just tell it to open it with your new script.

Change SAVE_PATH to wherever you would like winmail.dat to be extracted to. You will have a new folder with the extracted contents where ever SAVE_PATH is pointing. If you wanted to, for example, have them extracted directly to your desktop you would want to comment out the mkdir line.

Running OpenVPN on your Motorola Droid 2.2

Running OpenVPN on your Motorola Droid 2.2

I have finally gotten OpenVPN working on my Motorola Droid. Here are a few short notes for anyone who is on a similar mission.

  • This method requires root access for which I used CyanogenMod 6.0. If your phone is not already rooted you will need to do this first (CyanogenMod or another ROM).
  • Since the TUN module was removed I had to switch to a different kernel. I choose the P3Droid 1250 2.2 low voltage (125Mhz) kernel using setCPU to manage my clock speeds. This kernel is important because it adds TUN support. I had no end of trouble trying to load the module myself with tun.ko and insmod which never ended up working any way.
  • Create some directories and symbolic links. To do this I ran adb remount; adb shell mkdir /system/xbin/bb; adb shell ln -s /system/xbin/ifconfig /system/xbin/bb/ifconfig; adb shell ln -s /system/xbin/route /system/xbin/bb/route from my host PC with the USB cable plugged into my Droid using the Android SDK. Note that I run Linux but if you are running Windows just run each command in between the semi-colons, one by one.
  • I ran OpenVPN Installer available in the Android Market. When asked make sure you answer /system/bin/, /system/xbin/bb.
  • Now you are ready to load up your OpenVPN-related stuff. Write a new configuration (named whatever.conf) and generate some new certificates for your new client. Stick them on your SD card in the openvpn directory.
  • Install OpenVPN Settings from the Android Market. Once opened it should now show the configuration file you stuck on your SD card. Just tap it and watch it connect.

You may want to tweak your configuration a bit to ping more often or the like to deal with the fact this is a cellular connection and it will be going up and down on a regular basis. Both TAP and TUN adapters worked great in my tests.

If any of this seems confusing or you do not understand what a line does you should not be doing any of this. A lot of these commands– if done in the wrong context, at the wrong time, in the wrong order, or if your chi is off– will brick your phone and your warranty will be voided. Hell, your warranty will be voided if everything goes right. Beware!

Force an fsck Check

Force an fsck Check

In order to force an fsck check on reboot simply stick a file in the root of the filesystem you wish checked named forcefsck and reboot. You can use the line touch /forcefsck to create it. If you have the filesystem mounted under another path edit the line accordingly. fsck will automatically remove this file when it is done with it.

If you wish to check a filesystem you can not mount for whatever reason boot into a live-CD (such as Ubuntu) and run fsck /dev/sdXY where X and Y are your device and partition. Assuming the issue is not with your partition table you can use fdisk -l /dev/sdX to help figure out which partition is which.

These commands may require root privileges depending on your distribution.