Sunday, 8 May 2016

Another slice of PI

Today is not a good day, my old faithful Asus EEBox which has been running almost constantly since 2008 decided to curl up and die, it's been running as a file server all this time amongst other things. The onboard had drive was just used to boot the OS, all data is store on two external 5TB drives.

It was time to replace it and to see if a Raspberry Pi was up to the job. The Raspberry Pi I got was as part of a starter kit from PC World, it included a case, power supply, ethernet lead and micro USB lead.I downloaded Rapbian and wrote to the image to a card as shown on the website.

I plugged everything, got some lights on the Pis but nothing on the ethernet socket, it was time to move it to the TV, unplug my other Pi and see what was happening, to my surprise it booted up and it all worked, I logged in via ssh and ran sudo raspi-config, set it all up, rebooted and reconnect. I took it back to where it's going to be located and again, no ethernet lights. After much buggering round it looks like the supplied micro USB lead isn't up to the job, there's too much of a voltage drop, the PI was now alive. I connected the external drives and set up fstab to mount the drives automatically, set up some samba shares and an nfs share, rebooted and was unable to connect to the nfs shares, again some research continued and it seems that there's an issue on Raspbian which means that the rcpbind service doesn't start automatically.

I decided to get round this in a most unelegant way, I added some entries into the rc.local file with:

sudo nano /etc/rc.local

This lot got pasted in just before the "exit 0" bit at the bottom

service smbd stop

service nfs-kernel-server stop

mount -U b8828e80-d5c4-4747-a3f6-82eda807a34d /mnt/data

mount -U dc3e664f-27da-4e62-b1ba-f5a220a836a6 /mnt/backups/

rpcbind -w

service nfs-kernel-server restart

exportfs -rav
service smbd restart

Not all of it may be needed but it does work, I decided to mount my external drives here too, just in 
case one failed to mount on bootup, the pi isn't going to have a screen or keyboard connected and would hang at the boot screen.

You just need to make the file executable with:

sudo chmod +x /etc/rc.local

After that it all worked quite nicely and I'm managing to play some music stored on the pi and watch some videos at the same time.

Just in case you didn't know, you can install the required nfs files with:

sudo apt-get install nfs-common nfs-kernel-server

And Samba can be installed with:

sudo apt-get install samba samba-common-bin  

Following up on this as the external drives started to fill up I began to get problems with the little Pi running out of memory and crashing. Ok, it's not got a lot of memory but it worked well to start with so I decided to upgrade the firmware, setup the memory, remove the swapfile and move the OS to an external drive, in this case it's a 32GB USB drive.

Let's remove the swapfile first with the following:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

Next up, we need to update the firmware with

sudo apt-get install rpi-update

and then

sudo rpi-update

Once it's all done, reboot and let's sort the memory, this time we run the config utility

sudo raspi-config

Select advanced options

Then the memory split option

Change this to 16 and select OK

Then Finish and reboot

And now we come to moving the OS onto an external drive, as mentioned before this is a 32GB USB drive, plug it in and run

dmesg | tail

It should show the USB device at the end, in my case it identified it as /dev/sda

Time to wipe it and sort out the filesystem with:

sudo fdisk /dev/sda (or whatever your USB device shows)

 Then it's "d" to delete the partition, "n" for a new partition, followed by "p" for primary, then select the defaults for the next three questions and finally "w" to write the partition.

Now we format it with:

sudo mkfs.ext4 /dev/sd?1

And then we mount it to a temporary folder with

sudo mkdir /mnt/temp
sudo chmod 0777 /mnt/temp
sudo mount /dev/sda? /mnt/temp

Once that's done it's time to copy all the files over but first we need to install rsync:

sudo apt-get install rsync

sudo rsync -axv / /mnt/temp

It will take some time for this to complete, so go and have a coffee.

Now we need to tell the pi to boot the OS from the stick, the micro SD card needs to stay in as it will initially start from this and then load everything from the USB stick.

Let's backup the config file first with:

sudo cp /boot/cmdline.txt /boot/cmdline.orig

Next we'll edit with

sudo nano /boot/cmdline.txt

The original looks like this:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

It's all on one line, we need to change the root=/dev/... bit to read root=/dev/sda1 or whatever your USB stick shows, we also need to add a delay to the end with rootdelay=5, it should look like the one below in the end.

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait rootdelay=5

Save it and then we need to edit fstab with:

sudo nano /etc/fstab

The following entry needs to be added:

/dev/sda1    /   ext4    defaults,noatime  0       1

And the line referring to the sd card needs to be commented out, mine looks like this now

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
# /dev/mmcblk0p2  /               ext4    defaults,noatime  0       1

/dev/sda1    /   ext4    defaults,noatime  0       1

Save it and then it's time to reboot and it should all now be running off the USB stick with a bit more memory.

It's now running quite well with those big drives plugged in, every hour the data drive is backed up to the backup drive with a cron job and data is also sent to a remote location once a week, it really is a good little machine.

Many thanks to the Raspberry Pi forums for the help in this.