This post explains how to add a FAT32 partition to Raspbian on a Raspberry Pi. This is really useful if you want to be able to add files to your Pi SD card (e.g. media files) directly from a PC or Mac.
Before we start
This post assumes that you already have a working Raspbian OS installed on the SD card, that you have SSH or direct access to the shell on your Pi, and you are comfortable using the command line. There is a risk that you’ll wipe your Pi if you do things wrong so make sure you understand each step!
It also assumes that there is some free space at the end of your SD card which will normally be the case if you have installed Raspbian directly from the downloaded image using a block level file copy (e.g. dd) onto an SD card that is larger than 2GB.
First boot your Pi into the shell or open a terminal window, and install dosfstools:
sudo apt-get install dosfstools
List the storage devices:
sudo fdisk -l
The SD card will appear as /dev/mmcblk0 with a series of partitions labelled p0, p1 etc.
Run cfdisk with the SD card as a target device. cfdisk is a menu based partition manager:
sudo cfdisk /dev/mmcblk0
Using the keyboard create a new primary partition in the free space, and change the type to FAT32 which is option 0B (W95 FAT32). Select Write to save the changes, then select Quit.
The partition table is now updated but not visible to the pi so easiest thing is to reboot. When the pi has restarted we can verify the partition map with fdisk:
sudo fdisk -l
Note the number of the new partition, it will be pN where N is a number.
Now we need to put a filesystem in place, in this case we use mkfs.msdos to create a FAT filesystem, remember to replace the N with the number of the new empty partition:
sudo mkfs.msdos /dev/mmcblk0pN
Now create a mount point, e.g. /mnt/storage and in this case change its permissions so the pi user/group owns it
sudo mkdir /mnt/storage sudo chown pi:pi /mnt/storage
Now edit /etc/fstab with your favourite editor (e.g. nano or vim) and add a new line. The following example mounts partition /dev/mmcblk0p3 at /mnt/storage with a vfat filesystem owned by user id 1000 (pi) and gid 1000 (pi), allows all users to mount/umount it, read write permissions, noatime, executable, and umask to allow full write permissions for all users. The spaces you see are tabs, this is important.
/dev/mmcblk0p3 /mnt/storage vfat auto,rw,user,users,exec,noatime,uid=1000,gid=1000,umask=000 0 0
You can now mount/un-mount the filesystem and it should all work, at this point do a restart for the win!
The last thing would be to label the partition so that when you insert the SD card into a PC or Mac it doesn’t just shown up as “Untitled”. Normally we’d use the tools e2label or mlabel to label a partition but I couldn’t get it to work, so if anyone knows a way then please leave a comment below. In the end I just mounted the new partition on Mac OSX and used the Disk Utility to label it.
Hope this is helpful to someone!