Thursday, 24 September 2015 22:03

Format / Partition a USB flash thumb drive on Asuswrt routers

Written by 
Rate this item
(7 votes)

How-To: Using a USB thumb drive with an Asuswrt Router

Format and partition it directly on the wrt linux router with these instructions

Running a modified version of the Asus firmware (Asuswrt-Merlin), you can plug in the USB flash drive directly into the router, ssh in, send a few commands and you're up and running.

I'm working on a few projects and I needed more space and flexibility than only using the JFFS partition on the router.  I will create 2 partitions in this example,  but this guide is easily adapted to meet any number of partitions and sizes.  You can also, locally from the command line, partition and format a USB drive following the same steps outlined below with any standard linux distribution, by eliminating the SSH sections and go straight to Locate your USB Drive in the system

You should also note that there are reported issues in using USB 3.0 drives with some of the Asus routers.  I have not tried it myself so unfortunatly I cannot confirm this to be true.  It is said to interfare with your 2.4 wifi channels.  I am using an Asus RT-N66U in this example, but other similar routers running Asuswrt custom firmware should work the same.

 

Enable SSH through your web browser interface

Log into your router with a web browser and navigate to Administration > System and scroll down to the SSH Daemon section and match the following sections.

  • Enable SSH = Yes
  • Allow SSH Port Forwarding = No
  • SSH Service port = 22
  • Allow SSH access from WLAN = no
  • Allow SSH password login = Yes
  • Enable SSH Brute Force Protection = Yes

Then click Apply at the bottom of the screen

Format USB Asus Router 01

 

SSH into the router using your command terminal or software like Putty

 I chose to use a console terminal ssh session.  SSH via the terminal with

ssh <username-on-router>@<router-ip-address> 

(you can add -p <port>) if you set the port to something other than the default of 22

example:

ssh This email address is being protected from spambots. You need JavaScript enabled to view it..1.1

*If you receive a message something about the authenticity of the host  "Are you sure you want to continue connecting (yes/no)?, type yes.  If you are on your own network, you can ignore the warning (That is beyond the scope of this article)

You should then receive a prompt for the password, type it in and you should be at the command prompt

<username>@<router-name>:/tmp/home/root#

 

Locate your USB Drive in the system

In the ssh terminal, type in

watch 'dmesg | tail -20'

and now plug in your USB stick into the back of the router (If you had already plugged it in, go ahead and remove it, and re-insert it).  You should see some changes to your terminal as you re-insert the thumb drive

Format USB Asus Router 02

Once you see something similar to this screen press Ctrl+c to exit the last command.

You're looking for the location of the USB flash drive sd[x], in our example it is sda.

 

Create new partitions on the USB drive

For my particular requirements, we are going to create 2 partitions for maximum flexibility and for some future projects, I'll be using an 8GB ninja USB flash drive in this example, it may also work with non-ninja thumb drives, but this has not been tested.

Format USB Asus Router Ninja

 

 

 

 

  • sda1 will be 2GB (mounted for entware)
  • sda2 we will leave the rest for data

You can modify the below commands to accommodate the desired partitions needed but in this example we will use sda1, which will be used for installing entwareEntware (modern alternative to optware) is a software repo solution for *wrt versions of firmware made specifically for routers.  It allows one to install a variety of different software packages directly onto the router.  Basic software such as nano, a simple to use text editing tool to use in place of vi, or more complex packages such as asterisk, a voip server for all of your telephony needs.  2GB is the suggested size for this partition.  sda2, will be used for everything else.

 

Now that we know where the USB drive is, we want to be sure to unmount all of the partitions, we can unmount all of the partitions of sda with this command

for n in /dev/sda* ; do umount -l $n ; done

You will receieve a message saying umount: can't forcibly umount /dev/sda: Invalid argument This is because the for loop command above, attemps to  unmount the device sda first, this can be ignored.

 

This should unmount any partitions, we can verify our drive sda no longer has any partitions mounted

df -h

If there were any partitions mounted for our drive sda, you would see /dev/sda1 pointed to a directory

 

We will use fdisk to create and modify the partitions.

To list out the current partitions

fdisk -l

To partition the disk, we need to interact with fdisk on-screen, to review all of the commands in order:

Execute fdisk on selected drive (again be sure to replace the [x] to your drive letter, (typically a)

fdisk /dev/sd[x]

d - we will delete the current partition

p - print the empty partition (confirm no other partitions exists now, we want it completely empty), If you have more partitions on sd[x], delete the rest of those partitions as well)

n - add a new partition

p - to make it a primary partition

1 - to state it's partition "1"

1 - to start at the first cylinder (default)

+2048M - create 2048M(B) in size = 2GB

n - add a new partition

p - to make it a primary partition

2 - to state it's partition "2"

765 - to start at the first available cylinder (default) (partition "1" is using 1 to 764)

Hit enter - for default value (may differ depending on the size of your USB drive) to make this partition size starting from end of partition "2" , to the rest of what is available on the drive

p - to print the new partition table

w - now we write the partition table

If at any point you make a mistake type q to quit, no changes will take effect until you issue the w (write) command

If you receive an error about re-reading the partition table, this can be ignored, but you will have to remove, and re-insert the USB drive now.  Here is a c&p from my session, my input commands are highlighted in yellow.

adminm@router:/tmp/home/root# fdisk /dev/sda
The number of cylinders for this disk is set to 3046.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): d
Selected partition 1
Command (m for help): p
Disk /dev/sda: 8178 MB, 8178892800 bytes
92 heads, 57 sectors/track, 3046 cylinders
Units = cylinders of 5244 * 512 = 2684928 bytes
  Device Boot      Start         End      Blocks  Id System
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-3046, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-3046, default 3046): +2048M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (765-3046, default 765): 765
Last cylinder or +size or +sizeM or +sizeK (765-3046, default 3046): <-- this is where I hit enter
Command (m for help): p
Disk /dev/sda: 8178 MB, 8178892800 bytes
92 heads, 57 sectors/track, 3046 cylinders
Units = cylinders of 5244 * 512 = 2684928 bytes
   Device Boot      Start         End      Blocks  Id System
/dev/sda1               1         764     2003179+ 83 Linux
/dev/sda2             765        3046     5983404  83 Linux
Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table
fdisk: WARNING: rereading partition table failed, kernel still uses old table: Device or resource busy
admin@router:/tmp/home/root# fdisk -l
Disk /dev/sda: 8178 MB, 8178892800 bytes
92 heads, 57 sectors/track, 3046 cylinders
Units = cylinders of 5244 * 512 = 2684928 bytes
   Device Boot      Start         End      Blocks  Id System
/dev/sda1               1         764     2003179+ 83 Linux
/dev/sda2             765        3046     5983404  83 Linux

 I found it's best to remove and re-insert the USB drive after the previous partitioning you did with fdisk.

 

Format the new partitions on the USB drive

Be sure to unmount the disks

For removable USB flash drives it is recommended that we use the Ext2 file system.  Using mkfs.ext2 we will now format each of our partitions

mkfs.ext2 /dev/sda1
mkfs.ext2 /dev/sda2

 

You should now have a USB flash drive with 2 partitions formatted as Ext2 format.  If you disconnect and re-insert the USB drive at this time, they should auto mount and you should be able to locate the partitions in /tmp/mnt/ (if your not using the router to format the drive, in another linux distribution this location "/tmp/mnt" will vary, you may use dmesg or df -h or mount commands to assist in finding the auto mount point)

ls -la /tmp/mnt

and you should see sd[x]1, sd[x]2, listed in the directory for you to use as needed.

 

Label the partitions

I failed to label the partitions (whoch you should) but you can run tune2fs to do that now

tune2fs -L <label> <partition>

example

tune2fs -L entware /dev/sda1
tune2fs -L data /dev/sda2

 

Troubleshooting

If you run into any issues when unmounting or formating or receiving messages stating the sda1 or sda2 is in use, you can try to remove and re-insert the USB drive, run a few commands

To attempt to force an unmount

umount -f /dev/sda1

To check the partition (needs to be unmounted)

e2fsck /dev/sda1 

you can also reboot the router without the drive inserted, and then re-insert the drive after the reboot

 

You can now disable SSH to the router if you desire to, (but I recommend leaving it on, if access from WLAN is turned off)

 

Read 29230 times Last modified on Monday, 25 January 2016 18:22
Algis Salys

Creator and owner of algissalys.com.  Linux enthusiast, electronics tinkerer, and likes to spend time in the workshop building and creating new projects.