Skip to main content

Adding a USB drive to a router with OpenWrt

Contents

Today’s solution is a bit unusual - my network drive (NAS) recently stopped working, and to sort it out I need to do some digging in it. In the meantime, I would like to use the USB 3.0 port built into my WRT3200ACM router with OpenWrt software to share a USB drive on the network.

For this purpose, I use a 2TB WD My Book Essential (3.5” with external power) with a USB 3.0 port. I formatted the drive in exFat (from the computer I started with) for compatibility with both Windows and Mac. When I want to disconnect it from the network, I will always have access to the data by connecting the USB cable directly to the computer.

So here we go…

Most steps are performed from the terminal.

In this post I will focus on how to add a drive to a router. In a separate post (link at the end) I will describe how to share this drive on a local network.

Installing USB Drive Support

First, we update the packages.

opkg update

Next we add support for USB devices.

opkg install kmod-usb-storage kmod-usb-storage-uas

In my case I need to add exFat support as well. Depending on which file system you choose, you need to install it according to this guide.

opkg install kmod-fs-exfat libblkid

For OpenWrt 23.05 (only) we need the libblkid1 package

In addition, we will need tools to check if our USB device is working properly. We install them using the following command:

opkg install usbutils

Now we can connect our drive.

Jeżeli wszystko wykonaliśmy prawidłowo, wykonując poniższą komendę, zobaczymy nasz dysk na liście urządzeń podłączonych do USB.

lsusb -t

We should see something like this:

root@OpenWrt:~# lsusb -t
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 4, If 0, Class=Mass Storage, Driver=usb-storage, 5000M

or in case of supported protocol UASP:

root@OpenWrt:~# lsusb -t
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 4, If 0, Class=Mass Storage, Driver=uas, 5000M

Checking disk visibility

We check whether our disk and partitions are visible in the system.

ls -l /dev/sd*

We should see something like this:

root@OpenWrt:~# ls -l /dev/sd*
brw-------    1 root     root        8,   0 May  1 21:20 /dev/sda
brw-------    1 root     root        8,   1 May  1 21:20 /dev/sda1

In the next step, we will install an additional tool that we will use to mount the partition.

opkg install block-mount

We execute the command:

block info | grep "/dev/sd"

We should see something like this:

root@OpenWrt:~# block info | grep "/dev/sd"
/dev/sda1: UUID="5EAC-6F27" LABEL="2TB" TYPE="exfat"

Mounting the disk

We prepare the configuration file:

block detect | uci import fstab

Enabling automatic mounting based on the generated configuration file:

uci set fstab.@mount[-1].enabled='1'
uci commit fstab

Montujemy nasz dysk:

block mount

(Optional) If we want to make sure that automatic disk mounting works at system startup, we execute:

reboot

We check if our disk is visible using:

df

We should see something like this:

root@OpenWrt:/mnt# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1            195341.     10368 1953408896   0% /mnt/sda1

And:

uci show fstab

We should see something like this:

root@OpenWrt:~# uci show fstab
fstab.@mount[0]=mount
fstab.@mount[0].target='/mnt/sda1'
fstab.@mount[0].uuid='5EAC-6F27'
fstab.@mount[0].enabled='1'

If you have any problems, make sure that the @mount[0].enabled field is set to 1.

If we have any data on the disk, we can see it:

ls -l /mnt/sda1

We make sure that everything works properly by executing the command:

block info

We should see something like this:

root@OpenWrt:~# block info
/dev/sda1: UUID="5EAC-6F27" LABEL="2TB" MOUNT="/mnt/sda1" TYPE="exfat"

We finish it all with the command:

service fstab boot

Disk Sleep - SpinDown (Optional)

My WD caddy has built-in software that suspends/puts the drive motor to sleep after a period of time. The drive is still accessible, but when not in use, it won’t spin up unnecessarily. When the drive is needed, it will spin up again.

If we do not have the software option, then we can use an additional solution that will allow us to program the disk directly to do the same thing.

To do this, we install hdparm:

opkg update
opkg install hdparm

Assuming (from the above steps) that our disk is available as /dev/sda1, the following command sets the time after which the disk will go into suspend mode. We set this time as a multiple of 5 seconds. A minute is the value of 12, if we want to set 15 minutes, we need the value of 180.

hdparm -S 180 /dev/sda1

The above option is a hardware option. It introduces changes to the disk settings, so that they are preserved even when we connect our disk to another device / computer. Unfortunately, it does not always work. Some enclosures refuse to accept the above command, returning an error. In this case, there is an additional software solution called hd-idle, which can be integrated with our router administration panel from a web browser.

The hd-idle package, after a specified period of time (defined in the administration panel), sends the spindown-now command to turn off disk spin-up and enter standby mode.

We install it using the command:

opkg install luci-app-hd-idle

Then we go to our web control panel, Services > HDD idle, where we set whether this software option should be enabled, for which disk and the time.

OpenWrt Service hdd idle

OpenWrt hdd idle

I recommend first checking whether the hardware option works (whether built-in or programmed), as it does not require any additional packages that must be enabled on our router, and only then choose a software solution.


And that would be it, when it comes to adding a disk, we can move on to sharing our disk on the local network. However, I described this step in a separate entry: Sharing a USB drive over the network from an OpenWrt router


Credits1

Share on Threads
Share on Bluesky
Share on Linkedin
Share via WhatsApp
Share via Email

Comments & Reactions

Categories