Saturday, 19 January 2013

USB Hard drive

Finally, I got to do something useful with the Pi.  This is one of two useful uses so far :)   For Christmas I got a weather station from 'Maplin' with a USB interface, and it was relatively straightforward to get the Pi pulling data from this and publishing it in a web page using pywws.  I found relatively little on the web about running the Pi from a hard drive, so these notes are my reference if I need to rebuild this. Most of the info can be gleaned from the thread here, though it's a little fragmented and somewhat incomplete -

- It is not possible to 'boot' the Pi from a hard drive - it needs to boot from the SD card, but it can 'run' from a USB attached hard disk. - I was worried that a 24x7 system would quickly wear the SD card, especially as the weather station software processes data and writes to files every minute or so.  This, and improved storage - perhaps as a backup server was my driver for using the external hard disk.  (NB - in fact, the weatherstation died after 4 days and I'm currently waiting on a replacement, but at least I have the logging / publishing framework in place).

 - Experiment with several 2.5" external USB disks show that the Pi doesn't provide enough power to run them without external power supply, but I had a spare 320Gb 3.5" external drive with it's own power which I've now dedicated to this.

- Step1 was to install the system from an image onto the SD card, and get it running and configured with all the software I wanted (primarily pywws and lighttpd), get networking etc configured to my satisfaction.  Now, make a backup of the working SD card before proceeding!

2. - Plug the hard drive into the Pi and create the partitions I needed using fdisk - In my case, I just created a single large partition which will later be the 'root' of the Pi, and a 256Mb swap partition:-

root@weatherpi:/etc/lighttpd# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 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: 0xfa24485f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   624617471   312307712   83  Linux
/dev/sda2       624617472   625141759      262144   82  Linux swap / Solaris

3. - mkswap /dev/sda2
4. - mkfs.ext4 /dev/sda1
5. - mkdir /media/sda1
6. - mount /dev/sda1 /media/sda1
7. - cd /
8. - rsync -avxS / /media/sda1 --exclude /media/sda1
9. - cd /media/sda1/etc
10. - update the path of root in fstab to be /dev/sda1 instead of /dev/mmcblk0p2.
11. - cd /boot
12. - edit cmdline.txt and change the root= to /dev/sda1 from /dev/mmcblk0p2.
13. - Cross fingers and reboot.....  I was lucky, and the Pi booted first time and I could access so I didn't have to faff with connecting to a monitor / keyboard etc.

Check that it's actually all configured the way I expect with the huge root filesystem:-

root@weatherpi:/etc/lighttpd# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          294G  1.7G  277G   1% /
/dev/root       294G  1.7G  277G   1% /
devtmpfs        117M     0  117M   0% /dev
tmpfs            24M  220K   24M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            47M     0   47M   0% /run/shm
/dev/mmcblk0p1   56M   17M   40M  30% /boot

All looks good. - My SD card should live a long time as all the 'wear' activity is now on the hard drive.

14. - Now we've got it all booted, remember about the swap partition.....  edit fstab and add in the swap:-

    /dev/sda2       none            swap    sw                 0      0

Now, 'swapon -a'
'free' should now show the swap:-

pi@weatherpi /etc $ free
             total       used       free     shared    buffers     cached
Mem:        237868      53164     184704          0       6808      28580
-/+ buffers/cache:      17776     220092
Swap:       262140          0     262140

Great - final reboot to check this all works OK then I'm happy.

15. - I should also have disabled the auto generation of a 'swap file' now that we have a proper swap partition. - sudo update-rc.d dphys-swapfile disable  Might also remove /var/swap having done swapoff /var/swap. - The auto swapfile generation was running prior to this and it's not necessary.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.