Wednesday 31 July 2013

Running Android in KVM... because I can

I have been toying with the idea of playing around with android and took a quick look at downloading the sourcecode. I decided to hold off as it seemed like a lot of time would need to be spent on this and I had a few other projects I wanted to look at first.

However I found the android x86 port and so I decided to get this running in KVM.
Why?
Because I can.

I downloaded the iso for version 4.2 (I had a few problems with 4.3, however this might be solved in the same way I solved booting 4.2).

I created a new qcow2 qemu image (the original instructions I was looking at suggested using this just to store data and emulate an sd card to save data as the iso is also a live image), however I decided to install it to this disk image.

Using qemu-kvm I booted the iso image and attached the qemu disk I had created. The live image did not seem to boot up at all. If I selected the debug option, this just dropped me into a command prompt. Therefore I decided to install the os to the qemu disk image using the option on the boot menu of the live image.

I also used virt-install to import this disk image which would make it easier to work with.
It still failed to boot correctly, so I checked the VM settings and checked what hardware had been assigned to the VM. I gave it a 32bit cpu as it had defaulted to the same cpu as my hypervisor and because it seemed to be complaining about some graphic functions I switched the video card type from Cirrus to VMVGA (VMWare). With these settings it booted up fine and I got the familiar android home screen.
Sound also seemed not to work so I switched over to using the SPICE protocol instead of VNC, which gave me sound but was very choppy (probably due to running in an emulated environment on my laptop which is not too powerful).

I installed a few apps to prove that this was functioning and challenged my wife to a game of Word Fued.

grub boot screen


lock screen
























home screen


















I have installed version 4.3-test over the top and it seems to work, although Google Play Services periodically crashed during setup.

As this is not on a phone/tablet rotation can be a bit of a headache (I noticed this with WordFued it wanted to start in landscape), therefore I downloaded a rotation locker app to keep the display in potrait and I also turned of auto rotation in android.

Monday 29 July 2013

Inkscape/Sozi presentation

Someone a while back showed me a presentation in a web browser which was designed and put together using only opensource tools. Therefore I decided to investigate doing this using Inkscape and Sozi.

The principle of this method is to layout your presentation like a poster and the get Sozi to animate the transitions between portions of this design which will become your "slides". So it all comes down to your flair for design.

I needed something to do a presentation on, so I settled on some of my interests and some of the technologies I have been using and blogging about here.

As soon as I can find a way to link an svg file to the blog I will upload a sample.

Tuesday 23 July 2013

libc version

While looking at different ways to find out what version of libc is installed on a system (especially useful on a system that is compiled from source such as LFS), I discovered that you can actually run libc and it will print out useful version info.

For example

# /lib/libc.so.6
GNU C Library (GNU libc) stable release version 2.17, by Roland McGrath et al.
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.7.2.
Compiled on a Linux 3.8.1 system on 2013-07-18.
Available extensions:
    crypt add-on version 2.1 by Michael Glad and others
    GNU Libidn by Simon Josefsson
    Native POSIX Threads Library by Ulrich Drepper et al
    BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.


Monday 22 July 2013

LFS - Importing disk image into KVM and booting

I thought would probably be the least challenging part of getting my LFS setup working under KVM as once everything is installed on the filesystem it should just be a matter of booting it.

However, when I tried the steps below I realised that grub had not been installed correctly and it dropped my into the grub rescue environment from which I did not seem to be able to fix the problem. Details on how I did fix it below.

First of all, details on how to use the image with KVM. The following steps can be done in graphical management utilities but it is just as easy from the commandline using virtinstall.

Run virtinstall and tell it to import rather than to run an installer, I gave the VM a basic set of requirements namely just 2GB of RAM.

virt-install --name LFS --ram 2048 --disk <location of image file> --import

This created my VM configuration and started the VM (if it had been working correctly the OS would have started) and launched virt-viewer so I could see the console.


As noted earlier this failed to boot first time and dropped me into the grub rescue prompt, therefore I had to find a different way to get grub installed onto the MBR of my disk image.

Lots of posts on forums talk about mounting the disk using losetup, this is fine for raw images but I was using qcow2 format and had made it available via NBD. Other posts talk about installing the grub version from the host system onto the disk image, this would have worked, however why go to the effort of building a custom system if you are going to just copy files from the host system.

Whatever I tried inside a chroot onto my filesystem failed to work (even though grub-install returned success), so I looked for a way to run the grub utilities from the filesystem in my disk image by directly from my host system.

Fortunately it seems that grub-install is setup for running in just this manner with a number of optional arguments to override directory paths and locations of utilities. With my diskimage made available with qemu-nbd and mounted under /media/LFS I used the following command to install grub to the MBR of my disk image.

/media/LFS/usr/sbin/grub-install --boot-directory=/media/LFS/boot/ --directory=/media/LFS/usr/lib/grub/i386-pc --modules="ext2 part_msdos" --grub-mkimage=/media/LFS/usr/bin/grub-mkimage --grub-probe=/media/LFS/usr/sbin/grub-probe --grub-mkrelpath=/media/LFS/usr/bin/grub-mkrelpath /dev/nbd0 --debug

A quick explanation of the options (for more details check grub documentation)
--boot-directory  location of "/boot", where grub files will be installed
--directory  location to copy boot files from
--modules  which modules to load
--grub-*  locations of various grub utilities
--debug  undocumented (according to grub-install script) option so I can see exactly what commands it is running to make sure it does not damage grub on my host system

Now when I startup this VM under KVM it boots fine.

Grub screen

Login prompt - before compiling kernel to include nic driver




Sunday 21 July 2013

LFS - Kernel & grub install

I have a filesystem and a set of packages installed, now I need to get the kernel installed so I can boot this system.

The LFS instructions note that due to recent changes in udev the following kernel options need to be enabled
Device Drivers --->
    Generic Driver Options --->
         Maintain a devtmpfs filesystem to mount at /dev


**update**
Remember to compile in (or as module) your nic driver, doh!
 

As I am doing this in a virtual image I need to be careful I am specifying the right devices when it comes to installing and configuring grub.

Once the kernel has been compiled and the built kernel moved to /boot, grub needs to be installed in the MBR for this disk image. This is done with the command
grub-install <device>

As the disk image is mounted via nbd, the device is /dev/nbd0.

The first  time I tried to do this it failed, grub-install is a script which calls grub-probe and this claimed that the location where /boot/grub is, would not be accessible to grub.
I realised that I had been mounting the filesystem wrong and if I pass the parameter max_part=16 when I load the nbd module, devices are created for my partitions and I can mount them without having to use kpartx or partx.

When I did this, grub-install ran successfully within a chroot from the filesystem on my disk image.

**update**
This did not produce a bootable image, which I did not discover until I tried to boot it in KVM.

During one of my failed attempts to resolve this problem, I had created a custom devicemap entry for grub and when trying to do grub-install I got an error about blocklists similar to

Attempting to install GRUB to a partition disk or to a partition.  This is a BAD idea.
Embedding is not possible.  GRUB can only be installed in this setup by using blocklists. 
However, blocklists are UNRELIABLE and their use is discouraged.

There was a suggestion in the grub manual that I could safely use the option --force in this case, however this was not needed once I remove the custom devicemap.


Friday 19 July 2013

LFS - Installing software

I have a file system (http://paulsrandomcontent.blogspot.co.uk/2013/07/lfs-creating-disk-image.html) and I have a set of tools (http://paulsrandomcontent.blogspot.co.uk/2013/07/lfs-configuring-tool-chain.html) to build my system, so now is time time to start the build of the new system.

First off I need to create directories for the kernel's virtual filesystems (/proc, /dev, /sys) on my filesystem and also some initial device nodes (/dev/console and /dev/null). To find out the major/minor numbers for certain devices, check Documentation/devices.txt in the kernel source tree.

Entries in /dev need to be created so we can mount the virtual filesystems required, this can be achieved by creating a bind mount from my host system. Once we have this, the virtual filesystems /dev/pts, /dev/shm, /proc, and /sys can be mounted.

The install of the system now needs to happen within a chroot environment on the filesystem I have created which contains all the tools that were installed in a previous entry. A directory structure based on Filesystem Hierachy Standard (FHS) is created into which software will be installed.

As there are some hardwired paths for certain basic utilities in some programs, symlinks in /bin are created to point to our pre-built toolchain.

A basic /etc/passwd and /etc/group is created.

Some programs require log files to have already been created before they try to write to them (such as agetty, login), therefore we need we need to create log files such as /var/log/lastlog, /var/log/btmp, /var/log/wtmp.

The kernel headers are extracted for use by userspace programs and in particular glibc when it is built.

The following packages are installed (watch out for programs shipped as part of multiple packages)
man pages (not sure why first, check??)
glibc (configure timezone and setup /etc/ld.so.conf, gcc was also modified to ensure that no references to /tools exists)
zlib
file
binutils
gmp
mpfr
mpc
gcc (rebuilt so it now refers to installed glibc)
sed
bzip2
pkg-config
ncurses
util-linux
ps-misc
procps-ng
e2fsprogs
shadow (I installed cracklib and enabled support in shadow)
coreutils
iana-etc
m4
bison
grep
readline
bash
libtool
gdbm
inetutils
perl
autoconf
automake
diffutils
gawk
findutils
flex
gettext
groff
xz
grub
less
gzip
iproute2
kbd
kmod
libpipeline
make
man-DB
patch
sysklogd
sysvinit
tar
texinfo
systemd(udev)
vim

Some of these packages took quite some time to compile on my host system.


I ran into a problem when compiling glibc, it seemed to cause gnome-terminal to segfault (once this build is complete I will have to see if I can reproduce the problem and find out what is going on).


Wednesday 17 July 2013

LFS - Configuring tool chain

Continuing on building my LFS virtual image.

I am using version 7.3 of the LFS documentation, they have helpfully provided a file to use with wget to download all required packages and a separate md5sum list to verify once they have downloaded.

Files have all been downloaded to directory sources on my new filesystem (created in http://paulsrandomcontent.blogspot.co.uk/2013/07/lfs-creating-disk-image.html).

A new directory (tools) for tools has also been created and a symlink /tools has been created pointing to this location, e.g.

ln -sv /mnt/LFS/tools /

Instructions suggest the creation of a new non-root user for building packages so as not to damage the host system, which is a very good idea. I also want to keep this separate from my normal user on my system just in case.

I have created the user lfs to work with.

The following is also suggested to remove stray settings from our build host and create a simple bash_profile
cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF



and the following to create a simple bashrc (note the mount location of the new filesystem for $LFS)

cat > ~/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF


First step is to build our host independent toolchain which will be used for building tools needed to build our new system, these will be installed under $LFS/tools on our new filesystem.

While building binutils for a 64bit system it is recommended to create a 64bit lib location (which will be used by other tools as well) using the following

case $(uname -m) in
x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
esac


The list below marks the order in which the software packages were installed, however it should be noted that certain critical packages are install a couple of times to build a known good set of tools on our filesystem, which we use to build other software.

Binutils is required by both gcc and glibc configuration so must be built first. Once gcc is built it is used to build glibc. This new glibc can be used when rebuilding binutils and gcc, these tools will then be referencing only our new filesystem and not the host system. Effectively we are bootstrapping our compiler.


Tools built in the following order
binutils
gcc (only C language support)
Linux API headers (from kernel source, so glibc can interface with features provided by Linux kernel)
glibc

tcl (for binutils, gcc and other test suites)
expect
dejagnu
check
ncurses
bash
bzip2
coreutils
diffutils
file
findutils
gawk
msg-fmt binary (from gettext package)
grep
gzip
m4
make
patch
perl5 (only some tools and libraries for now)
sed
tar
texinfo
xz


firefox keyword trick

I was wondering if you could add a proxy server as a new custom search to the address bar of firefox and while it is possible for me to write a few lines in a plugin to do this I found another easier way to do this with bookmarks and keywords.

Create a bookmark giving the full url of the proxy server and where you wish to pass the url place %s, give the bookmark a keyword, such as proxy.


Bookmark properties















To use this proxy bookmark, in the address bar type

proxy <url you want to view>

LFS - Creating Disk Image

Since I came across it a number of years ago I have always wanted to have a go at this but never found the time.

Now is the time, I am going to install LFS onto a virtual disk and run it under KVM. This post and others will be used to document the process and any issues encountered.

First off I need a virtual disk image to install onto, this can be created with qenu-img, e.g.

qemu-img create -f <fmt> <image filename> <size of disk>

I have gone with qcow2 format and a size of 10GB to start with. There are some other interesting options which I will look at later including encryption.

I now have a disk image (/tmp/LFS.img), but to make use of it I really need to partition it. So how do I partition my image file, I need to manipulate it so that I can run fdisk or parted on it. Fortunately I have found an application called qemu-nbd which exports the qemu disk image using NBD protocol, therefore I should be able to access the block device it creates.

To make use of network block devices I need to make sure the nbd module is loaded and if not insert it using modprobe -v nbd, this creates multiple nbd devices (/dev/nbd0-15). Using one of these devices we connect to the exported image using qmeu-nbd.

qemu-nbd -c /dev/nbd0 /tmp/LFS.img

This now means we can use our partitioning tools on /dev/nbd0 to manipulate our disk image.

# fdisk -l /dev/nbd0

Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


I have created a 2GB swap partition and 8GB partition for the OS.

# fdisk -l /dev/nbd0

Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x03db5ccc

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1            2048    16779263     8388608   83  Linux
/dev/nbd0p2        16779264    20971519     2096128   82  Linux swap / Solaris


To be able to use these partitions kernel needs to know about them, therefore I have used kpartx to add these to devicemapper and I now have the devices /dev/mapper/nbd0p1 & /dev/mapper/nbd0p2.
**update**
While this works and gives me access to the partitions I created on my disk image, I ran into a problem installing grub. Looking into this there is an easier way to make the partitions available. When loading the nbd module, if I pass the argument max_part=16 (this tells will create a maximum of 16 partitions for this block device, the default is 0, hence I did not see my partitions) device entries are created for my partitions so I can access them via /dev/nbd0p1 & /dev/nbd0p2 once I have run qemu-nbd command as above.

I have created a swap area on nbd0p2 using mkswap and created an ext3 filesystem on nbd0p1 using mke2fs -j .

There is a note on LFS documentation about making sure the version of e2fsprogs on the host system does not add any custom features which could cause problems with the final system.

# debugfs -R feature /dev/mapper/nbd0p1
debugfs 1.42.3 (14-May-2012)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype sparse_super large_file


This filesystem should be fine. If not e2fsprogs would need to be built from source and used instead.

Note for future reference, to disconnect this disk image, first we need to make sure that nothing is using it, then remove the devicemapper entries created by kpartx, then to disconnect the NBD using qemu-nbd. If the nbd module was loaded manually then remove it using modprobe -rv nbd.

To create my workspace I now need to mount the filesystem and activate the swap partition (swapon).

Now on to installing the OS.

Tuesday 16 July 2013

Watching X events

I was watching a presentation on Weyland and I wondered about how to see X events as they happen, I seem to recall doing this many years ago supporting customers but have not had a reason to do it recently.

Looking into this, it should be possible with xev.

xev allows you to monitor an existing window, however I wanted to watch events when a program starts up, e.g. possibly diagnose a slow starting application which you suspect to be caused by X.

I believe this can be done by getting xev to monitor you root window (putting it in the background) and then launching the application that you wish to monitor, e.g.

xev -r &
<graphical application>

Monday 15 July 2013

C and Python

Not really sure why I would ever want to do this but here is how to run python code from within C Code. Here is a test program that prints a string using python being called from C.

/*
 * Test program to run python code from C
 * Compile with gcc -Wall -I /usr/include/python2.7/ -lpython2.7 -o runpython runpython.c
 *
 */

#include <Python.h>

int main() {
  Py_Initialize();
  PyRun_SimpleString("print('print using python')");
  return 0;
}



Friday 12 July 2013

Python Easter Eggs

There are some amusing Easter Eggs in Python.

There is the "Zen of Python" obtained by
import this

This prints out Python programming principles that have been rot13 encoded in the module.

While checking out pypy, I came across this one
import antigravity

This opens a web browser and opens http://xkcd.com/353/

Compiling Python code

I know that compiling python code to bytecode speeds loading of code, however I never really thought about how to do this until now.
There appears to be multiple ways to do this according to the documentation.

The first method runs the code and generate an .pyc file.
In the python interpreter just import your .py file, e.g.
>>> import myfile.py


The next method I have come across is to use the py_compile module, e.g
import py_compile

py_compile.compile("myfile.py")



The next method is to use the compiler package, note this is marked as deprecated since version 2.6 and has been removed from version 3, e.g.
import compiler

compiler.compileFile(myfile.py")

With multiple files this could become time consuming, therefore there is another module compileall, which will compile all files in a directory, e.g.
import compileall 

compileall.compile_dir("mylib")

In the above example it will not recompile exisiting .pyc files if the corresponding .py timestamp has not been updated (timestamp is stored in .pyc file). To force compilation add argument force=1 when calling compile_dir().
See documentation for more useful options.

Wednesday 10 July 2013

screen capture on Linux

There are many different ways to do a screen capture of the desktop using various command line utilities which grab a portion of the display such as ImageMagick or utilise built-in functionality of the Window Manager being used. However, I have discovered another cool command line tool to do this called scrot.

With it you can do the usual create an image of the desktop after a certain length of time, but the features I have found useful are the ability to select an area of the screen to save (-s option) and the ability to execute a command on the saved image (-e option).

e.g.
scrot -s <new image file> -e 'gpicview %f'

From the example, scrot also can use some format specifiers for -e and filename options, here I use %f to get the filename.

It would be easy to add a scrot command to scripts for easy quick screen capture.

Tuesday 9 July 2013

Fun with Fractals

I saw an article on drawing fractals (Julia sets) using the Scratch programming language so I thought I would have a go at doing this in python using the pygame libraries. Program at the bottom of the page.

While playing around with the code and testing different values I came up with some pretty cool images. The first few attempts had it painting black pixels when they should have been coloured.

first test image (incorrect code)
first image zoomed x axis

experimenting with colours
different colours


Favourite colour selection
random 

looks like a chinese dragon
Filled Julia set for c=-1+0.1*i




#!/usr/bin/python

import os, pygame, sys;

from pygame.locals import *

def main():

    pygame.init()

    #set environment variable for SDL to position screen
    os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'

    pygame.display.set_caption(os.path.split(os.path.abspath(__file__))[01])

    #windowed mode, 500x500, automatic colour depth
    screen = pygame.display.set_mode([500,500],0,0)

    #equation c=-0.7467+0.3515i
    #some default values
    #c_real = -0.1
    #c_im = 0.651
    c_real = -0.7467

    c_im = 0.3515
    max_It = 20
    col_offset= 5
    x_zoom = 3
    y_zoom = 1
    normalise=255.0/max_It

    #filename to store generated image
    filename='fractal-'+str(os.getpid())+'.png'

    screen.fill((255,255,255))
    pygame.display.flip()

    x = 0
    while x < 200:
        y= 0
        while y < 180:
           z_re = 1.5*(x - 200)/ (0.5 *x_zoom*200)
           z_im = (y - 180 / 2)/(0.5 * y_zoom *180)
           zi2 = z_im*z_im
           zr2 = z_re*z_re
           iterations = 0
           for iterations in range (0, max_It):

           zi = z_im*z_re
                z_re=(zr2-zi2)+c_real
                z_im=(2*zi)+c_im
                zi2 = z_im*z_im
                zr2=z_re*z_re
                iterations+=1
                if (zi2+zr2 >4):
                    break
           print('iterations=%d'%iterations)
           #convert iterations to a colour value [0-255]
           colour=iterations*normalise
           #if zi*zi + zr2*zr2 stays below 4 uptill max iterations, it has
           #'escaped' and pixel is painted black, otherwise paint with 'colour'
           if iterations >= max_It:
                print('colour=black')
                pygame.draw.line(screen, (0,0,0),(x,y),(x,y))
           else:
                print('colour=%d'%colour)
                pygame.draw.line(screen, (255-colour,colour,colour),(x,y),(x,y))
           pygame.display.flip()
           y+=1
        x+=1

    print('fractal drawn')
    while True:
        #exit for loop, e.g. close window
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        pygame.image.save(screen, filename)
        pygame.time.wait(10000)

if __name__ == '__main__':
    main()

Friday 5 July 2013

Remember to define functions before trying to use them...

I was going over a sample piece of C code and I could not understand why it compiled without errors yet did not seem to function as expected.
After a while of head scratching and double checking my code with the listing provided (it was identical), I came to the conclusion that the listing was wrong so I enabled warnings in gcc (-Wall) and up popped the problem.

The error was
testhist.c: In function ‘main’:
testhist.c:9:4: warning: implicit declaration of function ‘initHist’ [-Wimplicit-function-declaration]


So we had an implicit function declared. It appears the original programmer had forgotten to define the function in their header file, doh!
Putting this in solved the issue.

Rather than turn on all warnings I could have just enabled the implicit declaration of function warning (-Wimplicit-function-declaration).

Another point to remember is don't assume the programmer knew what they were doing.

Wednesday 3 July 2013

Removing files via inode number

While trying out some code, I accidentally created some files containing meta characters in the filename, which in turn causes problems if you want to remove them. So I dug out an old script I wrote many years ago to delete the file using its inode number. The script has absolutely no error handling or sanitisation, use at your own risk.

#!/bin/bash

inum=$1
echo "Inode: $1"

echo "remove `ls -i | grep $inum`"
rm `ls -i | grep $inum | awk '{ print $2 }' `

sysinfo()

I was reminded in an article about sysinfo() on Linux, also gave me a change to remind myself of some basic C programming principles, e.g. bitwise shifts.

#include <stdio.h>
#include <sys/sysinfo.h>


int main() {
   struct sysinfo info;
   float todivideby = 0.00;
   sysinfo(&info);
   printf("Uptime (s) = %ld\n",info.uptime);
   printf("SI_LOAD_SHIFT = %d\n", SI_LOAD_SHIFT);
   todivideby = 1<<SI_LOAD_SHIFT;
   //printf("divide by: %.2f\n", todivideby);
   printf("Load Averages %.2f,%.2f,%.2f\n",info.loads[0]/todivideby, info.loads[1]/todivideby, info.loads[2]/todivideby);
   printf("Totalram = %ld memory units\n",info.totalram);
   printf("Freeram = %ld memory units\n",info.freeram);
   printf("Memory used = %ld memory units\n", info.totalram - info.freeram);
   printf("Sharedram = %ld memory units\n",info.sharedram);
   printf("bufferram = %ld memory units\n",info.bufferram);
   printf("totalswap = %ld memory units\n",info.totalswap);
   printf("freeswap = %ld memory units\n",info.freeswap);
   printf("totalhigh = %ld memory units\n",info.totalhigh);
   printf("freehigh = %ld memory units\n",info.freehigh);
   printf("Memory unit = %d bytes\n", info.mem_unit);
   printf("Number of procs = %d\n", info.procs);
   return 0;
}

Tuesday 2 July 2013

Learning Ada

I saw an article today about Ada which I have not looked at before, so I decided to try and write a couple of simple programs. It was not that difficult to write some very basic programs having seen Pascal and Oberon before.
The source code is stored in files matching the procedure name and ending in .adb "Ada Body" and to compile them I used gnatmake <filename>.
gnatmake is part of GNAT compiler tools and appears to be a front end to gcc.

Compulsory Hello World program

with Ada.Text_IO;
use Ada.Text_IO;

procedure Hello is
begin
  Put_Line("Hello World");
end Hello;



Program with Basic I/O (plus comments so I don't forget)

with Ada.Text_IO;
use Ada.Text_IO;
--following lines are needed for unbound string
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO;
use Ada.Strings.Unbounded.Text_IO;

procedure input is
   Buffer : Unbounded_String;
   --fixed length string
   --S: String(1..10) := (others => ' ');
   --Last: Natural;
begin
   Put("enter some text: ");
   --read in a fixed length string
   --Get_Line(S, Last);
   --read in entire line, careful what is done with it
   Buffer := get_line;
   --output our fixed length string, input truncated
   --Put_Line("text entered: " & S);
   --output unbound string
   Put_Line("text entered: " & Buffer);


end input;

 Note: Lines that start "--" are comments.

Simple animated graphic using pygame

Re-implementation of an exercise that was given to some university students while I worked there, the original was implemented as a Java applet using 2D graphics. I was looking at the pygame libraries and decided to test this out on the RaspberryPi, there are plenty of improvements that could be made, but the libraries seem easy to pick up.

#!/usr/bin/python
#
# draw castle shape using pygame library
# animate castle around the screen, including collision detction of borders
#

import os,pygame,sys

from pygame.locals import *


def main():

    pygame.init()

    #set environment variable for SDL to position screen
    os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'

    pygame.display.set_caption(os.path.split(os.path.abspath(__file__))[01])

    #windowed mode, 400x200, automatic colour depth
    screen = pygame.display.set_mode([400,200],0,0)

   #fullscreen mode, same size as screen resolution, auto colour depth
   #screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN,0)

    size = screen.get_size()

    points = [[0,50],[60,50],[60,20],[50,20],[50,30],[40,30],[40,20],[30,20],[30,30],[20,30],[20,10],[10,0],[0,10]]
    points2 = [[50,100],[110,100],[100,70],[100,70],[100,80],[90,80],[90,70],[80,70],[80,80],[70,80],[70,60],[60,50],[50,60]]

    bgcolour = [255,255,255]

    castleSurf = pygame.Surface((60,50))
    castleSurf.set_colorkey((0,0,0))
    pygame.draw.polygon(castleSurf, (0,255,0), points)

    x = 0
    y = 0
    positivex = 1
    positivey = 1

    while True:

       #exit for loop, e.g. close window
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_q:
                  pygame.quit()
                  sys.exit()
            if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()

        screen.fill(bgcolour)

        #debug
        #print x,y,positivex,positivey

        screen.blit(castleSurf,(x,y))

        pygame.display.flip()

        pygame.time.wait(60)

        #move bounding rectancle and collision detection
        #print size[0], size[1]

        if positivex:
           if (x+60)>=size[0]:
                positivex=0
                x-=5
           else:
                x+=5
        else:
           if x<=0:
                positivex=1
                x+=5
           else:
                x-=5  

        if positivey:
           if (y+50)>=size[1]:
                positivey=0
                y-=5
           else:
                y+=5
        else:
           if y<=0:
                positivey=1


if __name__ == '__main__':
    main()