Rooot.net

  • Increase font size
  • Default font size
  • Decrease font size
Living in a wooden world.
Home Geek stuff Openwrt Mount an external USB media as root fs on OpenWRT

Mount an external USB media as root fs on OpenWRT

The more packages you install on your OpenWRT, the more space you need. If your router has an USB port, here is a simple way to replace the JFFS flash partition with an external USB stick or hard drive.

 

Install packages

First, install needed modules and a few programs.

~$ opkg install kmod-scsi-core kmod-usb-core kmod-usb2 kmod-usb-storage
~$ opkg install cfdisk e2fsprogs swap-utils

Plug your USB key and check kernel messages. It should give you the path to your USB drive.

~$ dmesg
usb.c: USB disconnect on device 01:03.2-2 address 2
hub.c: new USB device 01:03.2-1, assigned address 3
scsi1 : SCSI emulation for USB Mass Storage devices
Vendor: VBTM Model: Store 'n' Go Rev: 1.04
Type: Direct-Access ANSI SCSI revision: 02
Attached scsi removable disk sdb at scsi1, channel 0, id 0, lun 0
SCSI device sdb: 1001472 512-byte hdwr sectors (513 MB)
sdb: Write Protect is off
/dev/scsi/host1/bus0/target0/lun0: p1 p2 p3
WARNING: USB Mass Storage data integrity not assured
USB Mass Storage device found at 3

Prepare USB device

Now partition & format your usb stick as follows.

~$ cfdisk /dev/scsi/host1/bus0/target0/lun0/disc
cfdisk (util-linux-ng 2.13.0.1)

Disk Drive: /dev/scsi/host1/bus0/target0/lun0/disc
Size: 512753664 bytes, 512 MB
Heads: 16 Sectors per Track: 62 Cylinders: 1009

NameFlags Part Type FS Type [Label] Size (MB)
-----------------------------------------------------------------------------
disc1 Primary Linux ext3 [storage] 463.21
disc2 Primary Linux swap / Solaris 49.27

Format the first linux ext3 partition.

~$ mke2fs -j /dev/scsi/host1/bus0/target0/lun0/part1
mke2fs 1.40.11 (17-June-2008)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
113344 inodes, 452320 blocks
22616 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
56 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

And mount it

~$ mkdir -p /mnt/root
~$ mount /dev/discs/disc0/part1 /mnt/root

We may also need some swap just in case the router runs out of memory

~$ mkswap /dev/discs/disc0/part2
~$ swapon /dev/discs/disc0/part2

Copy all fs into new mounted partition.

~$ tar cvO -C / bin/ etc/ root/ lib/ sbin/ usr/ www/ var/ | tar x -C /mnt/root
~$ mkdir -p /mnt/root/tmp && mkdir -p /mnt/root/dev && mkdir -p /mnt/root/proc && mkdir -p /mnt/root/jffs && mkdir -p /mnt/root/mnt/openwrt && mkdir -p /mnt/root/sys

Use the new partition as root partition

Here comes the interesting part. Create a new script in init.d to mount the usb drive at startup.

~$ cat > /etc/init.d/mountusb << EOF
#!/bin/sh /etc/rc.common
EXTERNAL_PARTITION=/dev/discs/disc0/part1

START=06

start () {
echo "loading USB and ext3 modules"

for module in usbcore usb-ohci uhci ehci-hcd scsi_mod sd_mod usb-storage jbd ext3; do
insmod $module
done
sleep 2s

[ "$FAILSAFE" != "true" ] && [ -b $EXTERNAL_PARTITION ] && {
mount $EXTERNAL_PARTITION /mnt/root
[ -x /mnt/root/sbin/init ] && {
mount -o move /proc /mnt/root/proc && \
pivot_root /mnt/root /mnt/root/mnt/openwrt && {
mount -o move /mnt/openwrt/dev /dev
mount -o move /mnt/openwrt/tmp /tmp
}
}
}
}
EOF
~$ chmod +x /etc/init.d/mountusb
~$ cd /etc/rc.d/ && ln -s ../init.d/mountusb S06mountusb

This script uses pivot_root to make /mnt/root the new / and will move the current root folder to /mnt/root/mnt/openwrt. The other mount commands will make sure the /proc, /dev & /tmp are updated.

Now reboot your router "et voilà"! You're ready to fill all this space with new packages & stuff.

Sources:
http://mpd.wikia.com/wiki/OpenWRT_FullInstall
https://forum.openwrt.org/viewtopic.php?id=21377

Comments
Add New Search
barshad  - Which version/build/revision of openwrt you are us   |::ffff:119.154..xxx |2011-06-17 18:28:44
Hi,
Your guide is nice, but i stuck at ~$ cfdisk
/dev/scsi/host1/bus0/target0/lun0/disc
I cannot see scsi in /dev/

can you
please help ?
Anonymous   |::ffff:88.178.1.xxx |2011-06-17 18:40:49
Hi, I'm running Kamikaze 8.09.2 (r18961). What's the output of dmesg when you
plug your usb drive?
Catfish  - device is probably different     |::ffff:58.96.32.xxx |2011-07-02 05:50:46
i have found my device is usually something like /dev/sda and not the long scsi
device names in this guide. if you are able to already mount your usb device
manually, use the device name you use to mount it. mine is /dev/sda1 so i mount
like:

mount /dev/sda1 /mnt/root

so i would use: cfdisk /dev/sda

note to use
just the device name, not the partition number when you run cfdisk (because you
are working on an entire device, not a single partition).
Catfish  - Trouble having this work     |::ffff:58.96.32.xxx |2011-07-02 11:14:42
I am trying to make my Netgear WGT634U load openwrt from the USB stick. The USB
drive is /dev/sda.

I partitioned the drive so Linux ext3 is sda1 and swap
sda2.

I have all the packages installed as listed above and can manually mount
the drive. I cannot make it mount the swap partition.

I have followed the
instructions from "Copy all fs into new mounted partition." onwards but
the device will not boot from the USB drive. It actually doesn't auto mount the
device on start up. I edited /etc/config/fstab with settings to automount the
drive but it doesn't.

I am unsure when the mountusb script runs, i assume at
runtime at some point but I can't see any output from it in the system logs via
the web interface. The only way I can see it in the log is if I add a command to
run it in /etc/rc.local

Is there a way to know when this is being started or if
it is running and failing?
Anonymous   |::ffff:82.216.1.xxx |2011-08-24 09:12:06
At startup, your linux system will execute all scripts found in /etc/rc.d/S*.
The mount script is usually named S20fstab which means the S06mountusb script
will be executed first (the numbers after S define the order of script
execution)

Replacing your root filesystem is not a common mount operation as
it require a pivot_root so you won\'t be able to do this with a new entry in
/etc/config/fstab.

The /etc/localrc script is executed at the very end of
system startup so at this point it is too late to replace the root
filesystem.

Few tips to track your issue:
First make sure your
/etc/init.d/mountusb script has execution permission.
Then change your script
so all commands get logged. For example you can change the insmod line
to:
insmod $module 2&>1 >> /tmp/usbmount_log
so you\'ll get all messages
from module loading in a log file. Do the same for all mount commands in the
script.

Hope this helps.
Catfish  - mountusb script is not running or is exiting early     |::ffff:58.96.32.xxx |2011-08-20 09:59:17
Hi there,

I added the logging commands like you suggested, but
/tmp/mountusb_log is not even being created.

/etc/init.d/mountusb is definitely
executable. My only question is, are we talking about /etc/init.d/ in the root
filesystem before the reboot or the root filesystem we create in the guide? (ie:
/mnt/root/etc/init.d)

I added some lines to echo a message that we are trying
to mount the filesystems but these messages do not show up in the startup
messages either. therefore, either mountusb is not running or is exiting before
it gets to those lines to echo the information.

The only lines before the echo
are:
#!/bin/sh /etc/rc.common
EXTERNAL_PARTITION=/dev/sda1

START =06

start()
{
echo "loading USB and ext3 modules"

etc.
Anonymous  - re: mountusb script is not running or is exiting e   |::ffff:82.216.1.xxx |2011-08-24 09:36:23
Catfish wrote:

/etc/init.d/mountusb is definitely executable. My only question is, are we
talking about /etc/init.d/ in the root filesystem before the reboot or
the root filesystem we create in the guide? (ie: /mnt/root/etc/init.d)


When booting your system looks for scripts in /etc/rc.d/. You should have
a symlink /etc/rc.d/S06mountusb pointing
to /etc/init.d/mountusb.

When running the pivot_root command the
/etc/rc.d will be replaced by the one in /mnt/root/etc/rc.d/.

This implies
all scripts running *after* S06mountusb will be read
from /mnt/root/etc/rc.d/. If you want to change a script being
executed prior to S06 you have to do it in /etc/rc.d/. If you want to
change a script executed after S06 (let's say S20fstab) you have to
edit the script in /mnt/root/etc/rc.d/.

There's no reason this script
can't be executed. Please double check your permissions & symlink.
I'm
seeing a space in "START =06", I hope it's a typo in
your message (would cause an error in shell script).
You may also read
other script in /etc/init.d. My OpenWRT version (Kamikaze
8.09.2 r18961) uses "START=06" to define the order of script
execution but things may be differents with other versions.
MrTea  - order of scripts   |::ffff:94.252.1.xxx |2012-01-08 22:00:12
neo73 & guys here thanks for this guide & infos

in my case (Backfire) had to
modify the script "START" & rc.d file to get it to start at a later
phase through the inits after the usb was ready ... then all worked very well
plenty of new flash space to install new apps packages !

Thanks
Ta!   |::ffff:145.58.1.xxx |2012-03-15 20:18:33
Thank you!
This is one of the best written tutorials if have sseen in a while!
You Realy saved my day.
Saurabh  - Mistake?   |117.208.89.xxx |2013-09-01 15:03:23
Will this work on the current versions of openWRT?
BTW, is this
Quote:
opkg cfdisk e2fsprogs swap-utils


actually:
opkg install cfdisk e2fsprogs swap-utils
neo73  - Thanks   |88.178.190.xxx |2013-12-01 13:18:56
Thank you for pointing out this mistake!
This method should still work on
newer versions of OpenWRT. You can also check alternative ways
to mount rootfs on external storage at http://wiki.openwrt.org/doc/howto/extroot.
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."