Today, I wanted to connect an USB drive to my Raspberry Pi and it did not work as easy as expected to access it’s data. As a side note, it is a Raspberry Pi completely set up via terminal so there is no nice GUI where you can have a look what happens. I am by far a terminal expert so I googled around a lot during the process. This is what I found and worked for finally accessing the data on my Mac formatted USB stick.
For setting up the Raspberry Pi itself, I wrote another post for that, see: https://mic.st/blog/minimal-setup-of-raspberry-pi-zero-w-with-ssh-over-wifi-connection/
Connect it
First just connect it and see if your Raspberry can see it at all. E.g. type lsub
which will give you something like this depending on your setup.
pi@raspberrypi:/ $ lsusb
Bus 002 Device 002: ID 0781:5591 SanDisk Corp. Ultra Flair
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Code language: Bash (bash)
The first one here looks pretty promising. Nice! So it’s there.
However, you still cannot just access it like you would do on your Mac or Windows machine. On Linux you have to “mount” it.
Depending on your setup this might have happened automagically then you are basically done already. Check by typing to your terminal and see if there is any folder that looks like your usb stick.
ls /media
If nothing is shown, no worries, it just means your device is not mounted yet. I also had to do it manually.
Check filesystem
It is important to know the file system of your USB drive, i.e. how you formatted it when preparing it on your Mac or Windows PC. You can check that by typing:
blkid
E.g. you will see something like this in your list:
<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-accent-color">/dev/sda2:</mark> UUID="xxxx-xxxx-xxx-xxxx-xxxx" BLOCK_SIZE="4096" LABEL="Untitled" TYPE="<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-accent-color">hfsplus</mark>" PARTUUID="1fd0f434-937a-48ec-971b-78c7f67a1a3f"
Code language: HTML, XML (xml)
The highlighted parts are the important pieces of information here. First is the path of the device you have to use when mounting and for the parameter TYPE
you can see the actual filesystem.
In my case it is hfsplus
which relates to “HFS+” you can select when formatting a device on Mac for example. By default, Raspberry Pi’s Linux does not seem to support HFS+, we will change that now.
Add HFS+ Support to Raspberry Pi
Luckily, we have a builtin package manager called apt-get on Linux which we can use for quickly adding HFS+ support.
Call the following in your Raspberry Pi’s terminal:
sudo apt-get update
sudo apt-get upgrade
Code language: JavaScript (javascript)
These just make sure everything is up to date.
Then add all the needed dependencies for HFS+ support with the following command:
sudo apt-get install hfsplus hfsutils hfsprogs gdisk
Code language: JavaScript (javascript)
Now we are ready to mount!
Mount your USB stick / device
First, create a new folder in that empty /media
we had a look into earlier:
sudo mkdir /media/myUSBStick
Of course you can name myUSBStick
however you like.
Second, mount it by using the following:
sudo mount -t hfsplus <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-accent-color">[add path you got via blkid]</mark> /media/hfsplus
Code language: JavaScript (javascript)
Make sure you add the correct path you got earlier when using blkid
, e.g. in my case it would be:
sudo mount -t hfsplus /dev/sda2 /media/hfsplus
Check if you can access your mounted USB stick by trying to list its contents:
ls /media/myUSBStick
In case you do have any content (folders or files) on your USB stick, you should see it now.