Raspberry PI by default has only one memory available – SD card which also holds kernel itself. Unless you are using large SD, eventually you are going to run out of space -especially if you are dealing with media files or playing with camera module. The easiest and cheapest way of expanding memory is to us USB Flash drive.
If you are accessing PI from terminal screen, then probably one of built in USB ports are free and you can attach drive directly to it, otherwise use USB HUB – better with external power option.
When you load graphical interface with startx command – USB drives are detected and mounted automatically. But we want to run PI without graphical interface and access from terminal, so we need to mount USB and other devices manually.
I am not pro at Linux, so I’m going to place step by step guide how to mount and use drive, so next time I could use it as quick guide.
First of all as we already heard somewhere – everything in Linux is a file. This actually makes things pretty simple. So once you plug your USB drive to port system detects it and adds to device hierarchy folder /dev. You can take a look around by typing ls /dev command. Detected USB drives here are named as sda1, where a is is a letter indicating drive number and number 1 shows partition. If you would attach drive to another USB port you would get sdb1. So normally device can be accessed by using path /dev/sda1.
Detected device cannot be used as storage. First it has to be mounted to mounting point. For this we need to create a folder in /mnt directory which would be used to access disk.To create a mounting point just create new directory in /mnt folder using mkdir command. Use any name you preffer, just be sure there are no spaces and special characters:
cd /mnt sudo mkdir 4gusbdrive
sudo mkdir /mnt/4gusbdrive
Now when we have mount point, we can mount detected drive /dev/sda1 to /mnt/4gusbdrive using mount command:
sudo mount /dev/sda1 /mnt/4gusbdrive
Now you can go to this directory and do what ever you want – create, edit, run apps. Run command:
to enter the drive
run ls command to see file list here and so on.
If you wish to unplug USB drive, it is better to run umount command for safe removal:
sudo umount /dev/sda1
If you are planning to use USB drive every time you start up Raspberry Pi. Then you can mount drive at boot. For this you need to edit /etc/fstab file.
To do so I run command:
sudo nano /etc/fstab
Here you can already see SD card partitions. All you need is to add your drive added to mounting point:
Flash drive is FAT formatted so we type in vfat – filesystem type so algorithm could interpret it correctly.
To see information about disk space type df command in console:
pi@raspberrypi /mnt/4gusbdrive $ df Filesystem 1K-blocks Used Available Use% Mounted on rootfs 3749072 2001884 1576512 56% / /dev/root 3749072 2001884 1576512 56% / devtmpfs 183596 0 183596 0% /dev tmpfs 38372 224 38148 1% /run tmpfs 5120 0 5120 0% /run/lock tmpfs 76740 0 76740 0% /run/shm /dev/mmcblk0p1 57288 18552 38736 33% /boot /dev/sda1 3924932 405276 3519656 11% /mnt/4gusbdrive
You can see your disk space information – used and awailable.
At the end lets take couple images with camera and store to flash directory images. First create new directory in usbflash drive:
sudo mkdir /mnt/4gusbdrive/images
Then take an image:
sudo raspistill -o /mnt/4gusbdrive/images/image1.jpg
Images file will be placed in images folder. As you can see that mounter drive requires super user (sudo) privileges for writing file. This is because we mounted drive and by default it is set to root user and root group instead of user pi and group pi. For this wee need to mount drive with proper user and group like this:
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/4gusbdrive
Then you can write, store photos without using sudo.