• Augmenter la taille
  • Taille par défaut
  • Diminuer la taille
Come to the darkside, we have cookies!!!
Home Geek stuff Openwrt Monter un lecteur USB en tant que partition root sur OpenWRT

Monter un lecteur USB en tant que partition root sur OpenWRT

Plus vous installez de packages sur votre OpenWRT, plus il vous faut de l'espace de stockage. Si votre routeur dispose d'un port USB, voici un moyen simple de remplacer la partition flash JFFS du routeur avec une clé ou un disque dur USB.

Installation des packages

Tout d'abord, installez les modules et programmes nécessaires.

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

Branchez votre lecteur USB et observez les messages du noyau. Ils devraient vous donner le chemin de votre lecteur USB.

~$ 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

Preparer le périphérique USB

Il faut maintenant partitionner et formater le disque USB comme ceci :

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

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.

et le monter

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

Nous pourrions avoir besoin de swap pour éviter que le routeur ne soit à court de mémoire.

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

Copiez maintenant tout le système de fichier dans la nouvelle 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

Utiliser la nouvelle partition en tant que partition racine

Voici la partie la plus intéressante. Il faut créer un nouveau script dans init.d pour monter le périphérique USB au démarrage.

~$ cat > /etc/init.d/mountusb << EOF
#!/bin/sh /etc/rc.common


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
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
~$ chmod +x /etc/init.d/mountusb
~$ cd /etc/rc.d/ && ln -s ../init.d/mountusb S06mountusb

Le script utilise pivot_root pour faire de /mnt/root le nouveau / et va déplacer la racine courante dans /mnt/root/mnt/openwrt. Les autres commandes mount s'assurent que les partitions /proc, /dev et /tmp sont mises à jour.

Il ne reste qu'à rebooter le routeur et voilà ! Il n'y a plus qu'à remplir ce nouvel espace de stockage de packages et autre fichiers.


Ajouter un nouveau Rechercher
barshad  - Which version/build/revision of openwrt you are us   | |2011-06-17 18:28:44
Your guide is nice, but i stuck at ~$ cfdisk
I cannot see scsi in /dev/

can you
please help ?
Anonyme   | |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     | |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

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     | |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

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?
Anonyme   | |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

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

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

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
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

Hope this helps.
Catfish  - mountusb script is not running or is exiting early     | |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:

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
#!/bin/sh /etc/rc.common


echo "loading USB and ext3 modules"

Anonyme  - re: mountusb script is not running or is exiting e   | |2011-08-24 09:36:23
Catfish a écrit:

/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.
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   | |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 !

Ta!   | |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?   | |2013-09-01 15:03:23
Will this work on the current versions of openWRT?
BTW, is this
opkg cfdisk e2fsprogs swap-utils

opkg install cfdisk e2fsprogs swap-utils
neo73  - Thanks   | |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
Ecrire un commentaire
[b] [i] [u] [url] [quote] [code] [img] 
Saisissez le code que vous voyez.

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