Tuesday 29 May 2012

Pi but not on TV

Raspberry Pi has an HDMI socket and a composite socket, both can be used to connect up to a TV, but what if your TV is being used by the rest of the family? Well if you connect R-Pi to Ethernet you can use another computer to take control of your R-Pi.
When you first start R-Pi running Debian it presents you with a login prompt and a terminal session. You can start a similar remote session using Secure Shell, ssh. You need to run the ssh server on R-Pi and then use a ssh client on the monitoring computer.
On the SD card the boot partition has a file called boot_enable_ssh.rc. If you rename the file to boot.rc when the R-Pi boots it will start the ssh server automatically. You now need to know what the IP address is of the R-Pi. By default R-Pi connects to a network by getting its IP address from DHCP. The Linux command nmap will show you devices within a range on my home network I use nmap -sP 192.168.0.0/24 which pings addresses within the range and reports devices it finds. This should help you find the address of your R-Pi. To connect to R-Pi from a Linux terminal use ssh pi@192.168.0.2 (substitute the address of your R-Pi) and you should see a prompt for the password for the user pi. You can now run any command just as normal. To connect from Windows (or Linux) you could use PuTTY as your ssh client, which does work well.
The next issue is to see the actual desktop of R-Pi. This would let you use the graphical interface remotely. The usual way is to use VNC. To run a VNC server on the R-Pi you need to install it, using the command sudo apt-get install tightvncserver. This will fetch the tightvncserver software from a repository and install it.
The command to start the VNC server is
vncserver :1 -geometry 1024x768 -depth 16 -pixelformat rgb565
The first time you run this you will be prompted to set access passwords. Once it is running you can now connect from a PC to see the GUI interface. The :1 above sets the port to use, the screen size, colour depth and pixel format are all set, you can change these to suit your needs and your monitor size. To access the screen you need to run a VNC client, I just use the Remote Desktop Viewer and use its VNC protocol. Supply the IP address and port (e.g. 192.168.0.2:1) and enter the password you chose above. You should see the R-Pi GUI screen without hogging the TV.

Making a disk image

In my last post I glossed over the process of creating a disk image. This is not too hard, but it is not what most computer users might expect. In the world of desktop or laptop PCs if you want to install the operating system from scratch you probably now start with a CD, DVD or a USB stick, boot from that media and it will prompt you to create the disk image on the hard disk that the computer normally boots from. Raspberry Pi is different.

I normally use Linux - usually Ubuntu which is a derivative of Debian Linux. Debian Linux is available for R-Pi, so that was my first choice. R-Pi loads from an SD card, so the operating system needs to be installed on an SD card. This needs to be 2GB at least, but that would not leave much free space for any files that need to be added, so I use a 8GB SD card. I decided to use the command line tools in Linux which give a step by step view of the process.

The disk images for a few different set ups have been created and they can be found on the downloads page of the Raspberry Pi web site. From there I downloaded the Debian "squeeze" image which was debian6-19-04-2012.zip when I looked. Also published there is the SHA-1 hash for the .zip file. To check that the file you have downloaded is intact run

sha1sum <filename>

and compare the resulting hash against the hash on the web site. If they match the downloaded file should be fine.

Windows gives every logical disk drive a drive letter, Linux doesn't. When a removable drive is inserted it gets mounted as part of the directory structure, in Ubuntu's case as a folder in the /media folder. The name is logical, but not always obvious. The command df displays the file systems available, run it without the SD card inserted and then with the SD card inserted to see the name of the device. If there are multiple partitions already on the SD card df shows them with a number at the end. In my case the SD card was device /media/sdb (ignoring the numbers).

I then took the .img file from out of the zip file that was downloaded, placed it into the raspi folder under my home and knowing what the SD device is called I could copy it to the SD card with the command:

sudo dd bs=1M if=~/raspi/debian6-19-04-2012.img of=/dev/sdb

It is important that I got the device name right. If I had copied the image file over an existing disk partition, it would have been wiped out of course. If you do this, be careful.

After a few minutes the command finished and I had partitions on the SD card, but if I use that card in R-Pi it would only see a 2GB disk, and ignore the remaining 6GB. So now I needed to extend one partition to fill the remaining space on  the card. I started by using parted to see what the partitions on the card looked like. You need to have the card inserted in the card reader (or SD slot) but not mounted. I ran

sudo parted /dev/sdb

which starts parted. I use the unit chs (cylinders, heads, sectors) view and then print which showed three partitions, a FAT boot partition, which I left alone, the main ext4 partition which was nearly 2GB and a swap partition. I wanted to extend the main ext4 partition so first I needed to move the swap partition to the end of the space to make room. The exact numbers I used would vary on different cards as the size of storage varies from card type to card type. The command I had to issue was move 3 118223,0,0. This moved the third  partition (the swap) to the end of the available space. Now the ext4 partition needed to be resized, so two commands to remove and recreate the partition are needed. This does not delete the data, just alters some pointers that a later step could use. The commands were rm 2 followed by mkpart primary 1232,0,0 118222,3,31 which used the whole space for the ext4 partition. The start point was the same as before but the end point was just before the newly moved swap partition. I quit parted. To check the partition was sound I ran

sudo e2fsck -f /dev/sdb2

This prompted me to make lost+found and I accepted that.  The last step was to actually extend the partition with

sudo resize2fs /dev/sdb2

When this had run I had an SD card with a 7.7GB ext4 partition with over 6GB free space to save any data on.

When I inserted a card made like this into R-Pi the first time it booted it did some one-time changes and had to be restarted but then it worked well.

Making all of these changes on the command line will seem alien to Windows users who have been coaxed away from typing commands, but it makes more sense the more you do it. Describing, repeating and supporting command line style working is easier and often opens up more options. I do think the visualisation of a GUI interface would be easier to use for the moving of the SD card partitions, and of course they are available.

Monday 28 May 2012

The wait is over

I ordered a Raspberry Pi shortly after they became available and it has arrived less than three months later.

If you don't know what I'm talking about then you might like to take a look at the Raspberry Pi FAQ page, but Raspberry Pi is a small, self-contained computer that runs an ARM processor and costs less than £30 delivered. When I opened the envelope there was a tee shirt (which I'd forgotten about) and a small box about the size of a smart phone which contained a circuit board wrapped in anti-static plastic.

It has sockets and connectors all over it, around the edges are a mini-usb socket to power it, two USB sockets for USB things like a keyboard, a socket for an SD card, an ethernet RJ45 socket, an HDMI socket to connect it to a TV or monitor, a composite socket which would be an alternative way to use an older TV as a display and a 3.5mm jack for sound. On the top are general purpose IO (GPIO) pins. These are interesting, but not just yet.

I had created a boot disk by downloading a disk image from the Raspberry Pi web site. I had plenty of time while I was waiting. The disk image gets written to an SD card which is how the Pi boots. Once it has booted it is possible to use extra drives connected to USB, which can be extended with a USB hub. The disk image is a Debian distribution of Linux, specifically created and compiled to run on the ARM processor.

I plugged in the SD card, a USB keyboard, an HDMI cable to the TV and then plugged a phone charger for power. The TV leapt into life and a series of text scrolled by. Instead of the expected login prompt I got a Kernel Panic. After a second go with the same result I unplugged the keyboard and the Pi booted, but I couldn't login without a keyboard. When I plugged it in the Pi crashed. Mike McLennan pointed me, via twitter, to the list of keyboards that work and don't and sure enough mine was in the problem list.

I bought a cheap keyboard from Tesco today, and now all is well. Now I need to look at what is there and what I can do with it.