Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs

Raspberry Pi – mounting USB drive

Raspberry PI, by default, has only one memory available – an SD card that also holds the kernel itself. Unless you are using a large SD, eventually, you will run out of space -especially if you are dealing with media files or playing with the camera module. The easiest and cheapest way of expanding memory is to use a USB Flash drive.

raspberry_pi_usb_drive

If you are accessing PI from the terminal screen, then probably one of the built-in USB ports is free, and you can attach a drive directly to it; otherwise, use USB HUB – better with an external power option.

When you load the graphical interface with the start command – USB drives are detected and mounted automatically. But we want to run PI without a graphical interface and access from the terminal, so we need to mount USB and other devices manually.

I am not a pro at Linux, so I’m going to place a step-by-step guide on how to mount and use the drive, so next time I could use it as a 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 into the port system, detect it and add it to the devise hierarchy folder /dev. You can take a look around by typing the ls /dev command. Detected USB drives here are named as sda1, where a letter indicating drive number and number 1 shows partition. If you attached the drive to another USB port, you would get sdb1. So normally device can be accessed by using path /dev/sda1.

The detected device cannot be used as storage. First, it has to be mounted to the mounting point. For this, we need to create a folder in the /mnt directory, which would be used to access the disk. To create a mounting point, just create a new directory in the /mnt folder using mkdir command. Use any name you prefer; just be sure there are no spaces and special characters:

cd /mnt
sudo mkdir 4gusbdrive

or just

sudo mkdir /mnt/4gusbdrive

Now when we have mount point, we can mount detected drive /dev/sda1 to /mnt/4gusbdrive using the 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:

cd /mnt/4gusbdrive

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 a 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.

In the end, let’s take a couple of images with a camera and store them to flash directory images. First, create a new directory on a USB flash drive:

sudo mkdir /mnt/4gusbdrive/images

Then take an image:

sudo raspistill -o /mnt/4gusbdrive/images/image1.jpg

Images file will be placed in the images folder as you can see that mounter drive requires superuser (sudo) privileges for writing files. 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.

Leave a Reply