Friday, 1 June 2012

Raspberry Pi: automatically mount usb as hard drive

I'd like to configure my raspberry PI to auto configure itself on the USB memory stick that I boot it up with. Its USB memory stick will essentially be the 'hard disk' for the device storing various applications and dependant on which stick is found the raspberry will load and run in a particular way.

 Stage 1 Finding the memory stick and mounting

All attached usb devices can be listed by:

lsusb or lsusb -v.

To mount we need to be root.  For my image ( to change to root I need to issue:

sudo su -

After you have plugged the USB key in there are several ways in which you can get the device id (eg /dev/sda1), the most common is probably via dmesg.  However if you are running udev (which my release of Rasberry Pi is) you can reference the device by several aliases id, label, path, uuid you can list the aliases under each by: ls /dev/disk/by-

To allow copying of the hard disk I intent to label the usbkey so to replace the key I can label a new one with the same label.

My list of currently inserted keys is found by:

ls /dev/disk/by-label


The first thing I will do is change the label of my disk from USB2 to something else: 'PI1'

to do this I find the true device ID for my disk:

ls -l /dev/disk/by-label/PI_1
>lrwxrwxrwx 1 root root 10 Apr 17 14:15 /dev/disk/by-label/PI_1 -> ../../sda1

ie the device label is a symbolic link (shown by the '->') to two directories above '../../' named sda1

I issue the command mlabel -i /dev/sda1 ::PI1 to change my label

now I can add a mount command in the /etc/fstab file:

/dev/disk/by-label/PI1  /mnt/usbkey vfat defaults 0 0

If I were to want another PI version I could follow the procedure as above (using a different label eg PI2).  Then also add this to the /etc/fstabs file to mount in the same place:

/dev/disk/by-label/PI2 /mnt/usbkey vfat defaults 0 0

Whichever key inserted will be mounted to the /mnt/usbkey directory

1 comment:

  1. Hey,
    if you want to automatically monut any usb storage to your pi you can use this script: