Using Multiple USB Sticks (Flash Drives) on the Raspberry Pi
(Also applies to the exchange of a single stick between Pi's)
Contents
Raspbian Wheezy (Headless) "Methods"
Most of this section was written in the early days of Raspbian Wheezy, pre-B+ Pi's, when more than one USB stick required the use of a (powered) external hub and the desktop file-manager did not auto-detect and auto-mount USB file-storage devices.
General (Single USB drive) method
The approach discussed here is based upon that used for a "webserver" running upon a Debian/NSLU2 ("Slug") system as summarised in the screenshot below:
Some similarities can be seen in the above with the approach of http://elinux.org/RPi_Adding_USB_Drives, especially in the use of /etc/fstab:
neil@raspbian:~$ cat /etc/fstab proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 0 #/dev/mmcblk0p3 none swap sw 0 0 /dev/mmcblk0p4 /mnt/sdcard ext4 rw,defaults 0 0 /dev/sda1 /mnt/usbdisk ext3 rw,defaults 0 0 /dev/sda3 none swap sw 0 0
Both approaches assume that only one "external" usb drive exist and that they will always be associated which fixed /dev entries ie. /dev/sdb1 for the usb flash drive and /dev/sda1 for, what appears to be, a usb hard drive (hence the /dev/sda3 swap partion). When mounting the Slug's Gizmo flash drive via fstab, options other than rw,defaults were adopted:
auto implies mountable at boot time
users allows all users to mount/u(n)mount
conv=binary is an old vfat option (no longer required)
The uid and gid options set the user and group id's in an equivalent way to the manual mounting methods described in http://elinux.org/RPi_Adding_USB_Drives, and the dmask and fmask options set the directory and file access permissions.
(Further information on the above can be found by typing man mount or man fstab on the command-line)
Extended (Multiple USB drives) method
Where multiple usb drives are required the above approaches are limited because of their dependence upon fixed /dev entries. Unless they are always inserted into the same usb ports on the Pi (or any connected usb hub) a particular drive may not be associated with the same /dev entry as first found. Also, where several Pi's exist on a local network, or drives may be exchanged between Pi's, a scheme providing "common mount points" and/or "access control" may be desirable. The latter is the simplest to deal with first.
Three access controls are suggested:
- A named (existing) user may access all mounted usb drives
- A named "dummy" user may access all mounted usb drives (but may have other restrictions). Other users may be privy to its login credentials and, therefore, access all mounted usb drives.
- A number of users may access a particular, single, mounted usb drive
The above access controls will only apply when the usb drive is plugged into a suitably configured Raspberry Pi. Usb flash drives, unless encrypted (which is outside of the scope of this article), are usually intended to be easily read or written to.
is easily implemented - eg. if user "fred" exists, simply arrange for the drive to be mounted with uid=fred and gid=fred. Also "fred" should not belong to the sudo group and/or have an entry in the sudoers file.
is similar. Use the adduser command to create a new user eg. "woody" using an easily remembered password. By default "woody" will only belong to the group "woody" and will not exist in sudoers. Hence it is (relatively) safe to allow someone else to login to your Pi as "woody". This time arrange for the drive to be mounted with uid=woody and gid=woody.
is best combined with a "dummy" user (as above), and requires the creation of new groups (via the addgroup command) for each usb flash drive that may be mounted eg. "cruzer". Mount the drive with uid=woody and gid=cruzer (ie. the associated group name). To enable other users to access the drive add them to the group(s) via adduser eg. adduser trevor cruzer. The screenshot below illustrates this:
File and directory permissions have been set by the fmask and dmask options illustrated in the Debian/NSLU2 ("Slug") system screenshot above. This means that "woody", being the owner of the mount point, can access both the "Cruzer2Gb" and "DTmicro16Gb" flash drives and so can "trevor" since he is a member of the cruzer and dtmicro groups.
How do you ensure a unique mount reference? The answer is to use the unique UUID string associated with the physical device. Without any flash drive(s) present enter this command: ls -laF /dev/disk/by-uuid . You should see something like this ...
trevor@raspberrypi ~ $ ls -laF /dev/disk/by-uuid/ total 0 drwxr-xr-x 2 root root 120 Aug 25 18:56 ./ drwxr-xr-x 6 root root 120 Jan 1 1970 ../ lrwxrwxrwx 1 root root 15 Jan 1 1970 10b4c001-2137-4418-b29e-57b7d15a6cbc -> ../../mmcblk0p2 lrwxrwxrwx 1 root root 15 Jan 1 1970 A1B1-918F -> ../../mmcblk0p1
... which shows the UUID's associated with the partitions on the Pi's SD card. Now plug the flash drive into a usb port. Wait a few seconds to allow it to be detected etc. then re-enter ls -laF /dev/disk/by-uuid ...
trevor@raspberrypi ~ $ ls -laF /dev/disk/by-uuid/ total 0 drwxr-xr-x 2 root root 120 Aug 25 18:56 ./ drwxr-xr-x 6 root root 120 Jan 1 1970 ../ lrwxrwxrwx 1 root root 15 Jan 1 1970 10b4c001-2137-4418-b29e-57b7d15a6cbc -> ../../mmcblk0p2 lrwxrwxrwx 1 root root 15 Jan 1 1970 A1B1-918F -> ../../mmcblk0p1 lrwxrwxrwx 1 root root 10 Jan 1 1970 A838-48E2 -> ../../sda1
Similarily, plugging in a second flash drive should result in something like ...
trevor@raspberrypi ~ $ ls -laF /dev/disk/by-uuid/ total 0 drwxr-xr-x 2 root root 120 Aug 25 18:56 ./ drwxr-xr-x 6 root root 120 Jan 1 1970 ../ lrwxrwxrwx 1 root root 15 Jan 1 1970 10b4c001-2137-4418-b29e-57b7d15a6cbc -> ../../mmcblk0p2 lrwxrwxrwx 1 root root 15 Jan 1 1970 A1B1-918F -> ../../mmcblk0p1 lrwxrwxrwx 1 root root 10 Aug 25 18:56 A3BF-8779 -> ../../sdb1 lrwxrwxrwx 1 root root 10 Jan 1 1970 A838-48E2 -> ../../sda1
For the examples above the first drive was the "Cruzer2Gb", which had the (physical) UUID of "A838-48E2" and was linked to /dev/sda1. Subsequently, the second drive, the "DTmicro16Gb", with UUID of "A3BF-8779" was linked to /dev/sdb1. (Had the drives been plugged in the other way around their links would have been swapped too). This means that, now we know the UUID(s), /etc/fstab may be edited (via sudo nano /etc/fstab) to include additional mount points as illustrated below:
trevor@raspberrypi ~ $ cat /etc/fstab proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 0 /dev/mmcblk0p2 / ext4 defaults,noatime 0 0 UUID=A838-48E2 /media/Cruzer2Gb vfat auto,users,rw,flush,utf8=1,uid=woody,gid=cruzer,dmask=002,fmask=113 0 0 UUID=A3BF-8779 /media/DTmicro16Gb vfat auto,users,rw,flush,utf8=1,uid=woody,gid=dtmicro,dmask=002,fmask=113 0 0 # a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that
A similar edit of /etc/fstab on (an)other Raspberry Pi('s) should ensure that a particular usb flash drive will always mount to a known /media sub-directory.
Other examples:
trevor@raspberrypi ~ $ ls -laF /dev/disk/by-uuid/ total 0 drwxr-xr-x 2 root root 120 Aug 26 16:05 ./ drwxr-xr-x 6 root root 120 Jan 1 1970 ../ lrwxrwxrwx 1 root root 15 Jan 1 1970 10b4c001-2137-4418-b29e-57b7d15a6cbc -> ../../mmcblk0p2 lrwxrwxrwx 1 root root 15 Jan 1 1970 A1B1-918F -> ../../mmcblk0p1 lrwxrwxrwx 1 root root 10 Jan 1 1970 A838-48E2 -> ../../sda1 lrwxrwxrwx 1 root root 10 Aug 26 16:05 d997a754-3eeb-443c-896b-404cacff08f3 -> ../../sdb1
Linux LKG8A6BB5 2.6.26-2-ixp4xx #1 Thu Nov 25 07:32:43 UTC 2010 armv5tel The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Aug 27 15:03:00 2012 from blueelf.local crochet@LKG8A6BB5:~$ id uid=1001(crochet) gid=1001(crochet) groups=1001(crochet),1002(webber),1003(gizmo) crochet@LKG8A6BB5:~$ cat /etc/fstab # /etc/fstab: static file system information. # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc defaults 0 0 /dev/sda2 / ext3 errors=remount-ro 0 1 /dev/sda1 /boot ext2 defaults 0 2 /dev/sda5 none swap sw 0 0 /dev/sdb1 /media/Gizmo1Gb vfat auto,users,rw,conv=binary,uid=1001,gid=1003,dmask=002,fmask=113 0 0 crochet@LKG8A6BB5:~$ ls -laF /dev/disk/by-uuid/ total 0 drwxr-xr-x 2 root root 100 Aug 27 15:00 ./ drwxr-xr-x 6 root root 120 Aug 27 15:00 ../ lrwxrwxrwx 1 root root 10 Aug 27 15:00 0b1f22c2-66dd-4ff4-9f50-9070c03bbd25 -> ../../sda2 lrwxrwxrwx 1 root root 10 Aug 27 15:00 1d3fefe3-a29a-4341-826a-8563b7566ebf -> ../../sda1 lrwxrwxrwx 1 root root 10 Aug 27 15:00 2820-0FE9 -> ../../sdb1
Raspbian Wheezy/Jessie (Desktop/Headless) Methods
Later (circa B+) Wheezy and the Jessie desktop's file manager supports auto-detect and auto-mount of USB file-storage for the current user logged in. However for headless systems requiring multiple user access to a "semi-permanent" USB stick (or sticks) it is possible to de-activate the auto-mount process and use similar methods to the above to provide controlled shared access.
By-passing Auto-mount
Auto-mounted Device |
|
Auto-mount Disabled |
|
|
Create USB-stick user and group, adding other user(s), eg. pi, to the group and edit fstab appropriately:
pi@raspiP2BJamesA:~ $ cat /etc/fstab proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 /dev/mmcblk0p2 / ext4 defaults,noatime 0 1 UUID=38D6-E887 /media/V732Gb vfat auto,users,rw,flush,utf8=1,uid=v7owner,gid=v7user,dmask=002,fmask=113 0 0 # a swapfile is not a swap partition, no line here # use dphys-swapfile swap[on|off] for that pi@raspiP2BJamesA:~ $ id uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi),1001(v7user)
USB stick is now mounted to a well-defined place with the required group access etc. |
|
|