Convert your 4TB USB HDD into a NAS in 5 minutes with Raspberry

With a built-in WIFI module, true Gigabit Ethernet ports and multiple USB3 ports, the raspberry was turning to be a real candidate.

Image for post
Image for post

Pre-requisites

I’m using a Raspberry 4 Model B for this project, but feel free to use older versions but your mileage might vary. I will be using the raspberry in headless mode, so checkout my previous articles on how to setup the raspberry pi incase you haven’t.

Also, if you want to know what other optimizations to work on after the installation, check out my article on 10 tweaks.

Hardware

  • Raspberry PI
  • Rasbian Buster
  • External USB HDD

Setup static IP

So, I actually was stuck here for quite a while. I wasn’t sure which approach to use, but I finally decided to use the dhcpcd.conf approach.

For the uninitiated there are 2 different ways to setup network interfaces. The first is to use the file /etc/network/interface and in the case of rasbian, the other approach is using the dhcpcd.conf . I chose to go with dhcpcd.conf approach since it was the recommended approach. See some links in the reference section.

Once logged in, open up /etc/dhcpcd.conf and append to the end of the file with the following contents. Uncomment the ip6_address if you want to use ipv6. Here I’m using the wlan0 interface. Change the bolded content as needed.

interface wlan0
static ip_address=192.168.1.20/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=192.168.1.2
static domain_name_servers=192.168.1.2 8.8.8.8

Once, done, reboot to try and login with SSH again with the new IP address.

Format diskdrive

So, here is the part where it is a little tricky. Incase you were curious as to why I used a specific disk size of 4TB for this article, I found out the hard way that ext4 only supports up to a maximum size of 2TB. I didn’t want to create 2 separate partitions and I really wanted ext4 to work as opposed to using something less native to linux like exFAT or NTFS.

Feel free to use whichever format works for you and skip this section if you know what you’re doing. If you want me to write about exFAT or NTFS, leave me a private message!

Firstly, install gdisk.

$ sudo apt-get install gdisk

Next, with your usb drive plugged in, let’s get the device name.

$ sudo fdisk -l

Your device name should look something like /dev/sda1 .

Device Start End Sectors Size Type
/dev/sda1 40 409639 409600 200M EFI System
/dev/sda2 409640 7813707735 7813298096 3.7T Apple HFS/HFS+

So, in the example above, I only have one drive connected but it has 2 partitions. So I would like to fully format everything into just one partition. /dev/sda is the drive and /dev/sda1 is the first partition and /dev/sda2 is the second partition.

Let’s use gdisk to create the partition.

$ sudo gdisk

This will show the following outputs, and it is waiting for you to key in the device name. Here, I will key in /dev/sda since it is the drive that I am going to create partitions on, change it to the one that is yours.

GPT fdisk (gdisk) version 1.0.3Type device filename, or press <Enter> to exit: /dev/sda

Next, it will show you the next step

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.Command (? for help):

Here, if you type ? you will bring up the help screen.

b back up GPT data to a file
c change a partition’s name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition’s type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu

Here are the list of commands we can use. In this tutorial, we are only going to use p which will show us which partitions there are, then will then used to delete all the partitions one by one.

Next, with this clean slate, we will then add a new partition with n and use all the defaults.

Finally, remember in the above step, we were just defining what goes into each step, now we will need to write them onto the disk with w. With this if everything went well you should see the following screen.

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

Before we proceed, let’s just check that we got everything right.

$ sudo gdisk -l /dev/sda

This should show something like the below. Note that if you use fdisk you might see a GPT PMBR size mismatch error.

GPT fdisk (gdisk) version 1.0.3Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 7813969920 sectors, 3.6 TiB
Model: My Passport 25E2
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 630203FC-911C-4767-A6BE-13BCF1A94C7C
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 7813969886
Partitions will be aligned on 8-sector boundaries
Total free space is 6 sectors (3.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 40 7813969886 3.6 TiB 8300 Linux filesystem

Finally, let’s add a file extension. Replace stuffs with the name of your choice.

sudo mkfs.ext4 /dev/sda1 -L stuffs

Mounting the drive

Once all the formatting is completed, let’s proceed to mount the drive. To mount the drive manually, we must first create a place so that the drive can be mounted on. Let’s create a folder in /mnt.

$ sudo mkdir /mnt/stuffs

Next, to manually mount it.

$ sudo mount /dev/sda1 /mnt/stuffs

If you now do a df -H you should be able to see the size of the mounted drive.

Filesystem Size Used Avail Use% Mounted on
/dev/root 63G 1.4G 59G 3% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.1G 0 2.1G 0% /dev/shm
tmpfs 2.1G 8.9M 2.1G 1% /run
tmpfs 5.3M 4.1k 5.3M 1% /run/lock
tmpfs 2.1G 0 2.1G 0% /sys/fs/cgroup
/dev/mmcblk0p1 265M 56M 209M 22% /boot
/dev/sda1 4.0T 93M 3.8T 1% /mnt/stuffs
tmpfs 410M 0 410M 0% /run/user/1000

But wait, this mounted drive will not be there when we reboot the next time. So, we’ll use fstab to add the mount points. Within /etc/fstab add this last line. Replace the bolded parts with your own.

LABEL=stuffs /mnt/stuffs ext4 defaults 0 2

Setup SMB

First, install the samba application files.

$ sudo apt-get install samba samba-common-bin

Next, backup the configuration file

$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

Before we proceed, let’s make the following assumptions, we are going to create a secured network drive that only a group of people with the password can login and share the files.

Create a new group with,

$ sudo addgroup smbgrp

Next, create a new user, let’s call actor and add it to that group

$ sudo useradd actor -G smbgrp

We want to add a samba password for this user so,

$ sudo smbpasswd -a actor

Finally, let’s change the rights of the folder and ownership of the mounted drive.

$ sudo chmod -R 0770 /mnt/stuffs$ sudo chown root:smbgrp /mnt/stuffs

Now, open the /etc/samba/smb.conf file and add this to the end of the file

[stuffs]path = /mnt/stuffs
valid users = @smbgrp
browsable = yes
writable = yes
read only = no
inherit permissions = yes

Finally, restart the service

$ sudo service smbd restart

You should be able to now connect to the folder with the login credentials you created earlier.

What Next?

Now that you have completed setting up the base file sharing service, you can start to consider adding build out your raspberry pi with cool features.

Alternatively, look at how to use Raspberry Pi with Plex as a media streaming server. This way you can stream videos to any device anywhere!

Reference

An avid web developer constantly looking for new web technologies to dabble in, more information can be found on bit.ly/jayden-chua

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store