挂载USB存储设备

设若你的OP固件已经包含对 USB设备的基本支持, 现在,你就要在你的路由器上插上USB存储设备了 (U盘,USB移动硬盘,等等)吧 。 这个HOWTO是告诉你如何挂载优盘等USB设备的。 你懂得的,任何存储设备在使用前均需要进行分区格式化才能正常用于存储数据的。 磁盘分区磁盘格式化 通过一种 文件格式系统 类似于 ext3格式,在存入 文件 数据前。

准备工作

先决条件

  1. 确保在刷了OpenWrt固件的设备上,确保该设备带USB接口 基本的USB支持
  2. 插上已经分区和格式化过了的存储设备 (例如移动硬盘等) ; 设备本身和它的分区,应立即作为 Device files 位于 /dev/目录下的一个 设备文件s ,举例说明,比如 dev/sda, /dev/sda1, /dev/sda2,等等依次类推。
我们建议 不要使用 NTFS 作为文件系统,即便可以使用,在Linux下性能恐怕也会很低下。
我们建议使用 ext2ext3, 在 kmod-fs-ext4EXT4模块中都已向下兼容支持

假如你打算通过Samba共享你存储在USB设备里的文件, 或者通过UPnP等分享你的文档, 你最好使用ext3或ext4格式,因为Ext2格式默认不支持超过4GB的文件。 为了在EXT3和EXT4格式下获得更好的性能,特别是USB储存设备,建议在使用mkfs.ext4格式化的时候加上"-O ^has_journal"参数以关闭日志功能。在EXT4文件系统下,使用"-O ^hasjournal,extent"参数格式化可以提高读取大文件的性能。

必需的Packages包

因为 r26314block-extrootblock-hotplug 合并到了 block-mount中, 这意味着一旦你选择了 block-mount,那么 extroot 和 hotplug 也会一并安装。
你也可以安装block-mount 即使你没有选择块设备包,但当你真正想挂在块设备的时候你必须安装驱动(块设备包)。
同时也要安装你要使用的文件系统。(比如:ext4)
  • kmod-usb-storage USB大容量存储设备的内核支持
  • kmod-usb-storage-extras 可选 Kernel support for some more drivers, such as for SmartMedia card readers
  • block-mount optional & recommended Scripts used to mount and check block devices (filesystems and swap) required to configure with /etc/config/fstab
    • blkid Command-line utility to locate/print block device attributes
    • swap-utils This package contains a collection of tools for managing swap space: mkswap, swapon and swapoff
  • kmod-fs-???? the filesystem you formated your partition in

可用的文件系统

As of r26645 the modules for ext2 and ext3 (kmod-fs-ext2 and kmod-fs-ext3) are no longer available. You can use the module for ext4 to mount ext2/ext3 as well.

OpenWrt-Forum: Benchmarks with different filesystems

Available file systems and their packages can be listed with opkg update; opkg list "kmod-fs*".

名称 描述
kmod-fs-autofs4 AutoFS自动挂载指定的文件系统的版本4的内核级支持模块
kmod-fs-btrfs Btrfs格式的内核级支持模块
kmod-fs-cifs CIFS 内核级支持模块
kmod-fs-exportfs Kernel module for exportfs. 需要配合其他一些模块。
kmod-fs-ext4 Ext4格式的内核级支持模块,向下兼容ext2, ext3
kmod-fs-hfs HFS 格式的内核级支持模块
kmod-fs-hfsplus HFS+ 格式的内核级支持模块
kmod-fs-isofs ISO9660格式的内核级支持模块
kmod-fs-minix MINIX file system 格式的内核级支持模块
kmod-fs-msdos MSDOS 格式的内核级支持模块
kmod-fs-nfs NFS 格式的内核级支持模块
kmod-fs-nfs-common 普通的NFS 的格式模块
kmod-fs-nfsd NFS 格式的内核级支持的模块
kmod-fs-ntfs NTFS 格式的仅读取(即不可写)的内核级支持模块
kmod-fs-reiserfs ReiserFS 格式的内核级支持的模块
kmod-fs-udf UDF 格式的内核级支持的模块
kmod-fs-vfat VFAT 格式的内核级支持的模块
kmod-fs-xfs XFS 格式的内核级支持的模块
You are not required to use NTFS or FAT in order to access the disk from Microsoft Windows! See http://wiki.ubuntuusers.de/Linux-Partitionen_unter_Windows on HowTo access linux filesystems from Windows. Thus native Linux file systems are the first choice when working with Linux. Don't bother with NTFS, VFAT and the like. They all have some quirks we could do without.

NOTE: For some filesystems, like for VFAT, you need to additionally install language packages (codepages / charsets) to handle the filenames. If your mount fails, look in dmesg - a message like

FAT: codepage cp437 not found

means that you need NLS codepage 437, and a message like

FAT: IO charset iso8859-1 not found

means that you need NLS ISO 8859-1.

***''kmod-nls-base''** Kernel module for NLS (Native Language Support)

可用的NLS文件

Available NLS files and their packages can be listed with opkg update; opkg list "kmod-nls*".

名称 描述
kmod-nls-cp1250 Kernel module for NLS Codepage 1250 (Eastern Europe)
kmod-nls-cp1251 Kernel module for NLS Codepage 1251 (Russian)
kmod-nls-cp437 Kernel module for NLS Codepage 437 (United States, Canada)
kmod-nls-cp775 Kernel module for NLS Codepage 775 (Baltic Rim)
kmod-nls-cp850 Kernel module for NLS Codepage 850 (Europe)
kmod-nls-cp852 Kernel module for NLS Codepage 852 (Europe)
kmod-nls-cp866 Kernel module for NLS Codepage 866 (Cyrillic)
kmod-nls-iso8859-1 Kernel module for NLS ISO 8859-1 (Latin 1)
kmod-nls-iso8859-13 Kernel module for NLS ISO 8859-13 (Latin 7; Baltic)
kmod-nls-iso8859-15 Kernel module for NLS ISO 8859-15 (Latin 9)
kmod-nls-iso8859-2 Kernel module for NLS ISO 8859-2 (Latin 2)
kmod-nls-koi8r Kernel module for NLS KOI8-R (Russian)
kmod-nls-utf8 Kernel module for NLS UTF-8

Additionally, you may need Anybody with common sense will recommend using a native GNU/Linux filesystem like ext2 or ext3. Please see http://wiki.ubuntuusers.de/Linux-Partitionen_unter_Windows (german) on howto access such a partition when you connect the device directly to your PC which is running Miscrosoft Windows.

If, for whatever reasons, you insist to utilize NTFS as file system, you may fine Writable NTFS helpful.

配置

配置文件见: →/etc/config/fstab

There is nothing to configure! Read the manpage for mount. Pay special attention to the options! You can only mount a partition to an existing directory. 你可用 mkdir命令创建一个目录 ,举例如下:

mkdir -p /mnt/shares

手动挂载分区:

mount -t ext3 /dev/sda2 /mnt/shares -o rw,sync

要卸载:

umount /dev/sda2

要挂载一个交换分区:

swapon /dev/sda1

要卸载一个交换分区:

swapoff /dev/sda1

举例说明

opkg update
opkg install kmod-usb-storage block-mount block-hotplug kmod-fs-ext4
swapon /dev/sda1
mkdir -p /mnt/share
mount -t ext3 /dev/sda2 /mnt/share -o rw,sync

Start on boot

As of r26314 block-extroot and block-hotplug have been merged with block-mount. That means that once you select block-mount the scripts for extroot mounting and hotplug mounting
are installed. block-mount can be installed even if you have not selected a block device package, but if you want to actually mount a block device you must still install the drivers (block device package)
as well as the filesystem you want to use (e.g. ext4)

Install the packages block-mount and block-hotplug, they will take care of this and also enable automatic mount when device is plugged in. The configuration file is /etc/config/fstab.

Example settings in fstab for mounting to /mnt/share with automatic detection of the file system:

config 'mount' option 'device' '/dev/sda1' option 'options' 'rw,sync' option 'enabled_fsck' '0' option 'enabled' '1' option 'target' '/mnt/share'

UCI CLI commands:

/etc/init.d/fstab stop
uci add fstab mount
uci set fstab.@mount[-1].device=/dev/sda1
uci set fstab.@mount[-1].options=rw,sync
uci set fstab.@mount[-1].enabled_fsck=0
uci set fstab.@mount[-1].enabled=1
uci set fstab.@mount[-1].target=/mnt/share
uci commit fstab
/etc/init.d/fstab start

Delete with UCI CLI commands:

/etc/init.d/fstab stop
uci delete fstab.@mount[0]
uci commit fstab
/etc/init.d/fstab start

Rigging it up from OpenWrt

Actually there are packages in the OpenWrt repositories with which you can partition, format and check filesystems:

  • fdisk (for help with partitioning see TLDP)
  • e2fsprogs Contains the essential ext2 filesystem utilities e2fsck, mke2fs, debugfs, dumpe2fs and tune2fs, and also most of the other core ext2 filesystem utilities available. for help with formatting see TLDP

:!: Note: In order to perform a e2fsck on FIXME GiB partition, you will need FIXME MiB of RAM. The same is true for mke2fs. You can solve this by mounting a SWAP partition (or by using a device with enough RAM).

在root以外的挂载点安装OPKG包

一旦你成功挂载你的分区,你可能想在其上安装软件包。 参 opkg安装描述 页。

禁止未挂载好而写入

You may create an empty file to indicate that the disk is not plugged in so that you don't put files directly onto NAND by doing

touch /mnt/usbstorage/USB_DISK_NOT_PRESENT
chmod 444 /mnt/usbstorage -R

手册

mount the external drive:
(where sda1 is your drive, see fdisk -ls, and /mnt/hdd is the point where you wish to mount. Use md to make the directories)

ntfs-3g -o rw,noatime /dev/sda1 /mnt/hdd

Then make the following script:

nano /etc/init.d/hdd

And the contents:
(In this script, You can control other processes, like transmission, whitch uses Your external drive)

#!/bin/sh /etc/rc.common START=98 STOP=40 start() { echo -n "Mounting USB drive: " ntfs-3g -o rw,noatime /dev/sda1 /mnt/hdd swapon /mnt/hdd/swapfile mount -o loop /mnt/hdd/owrt_loop /mnt/loopdisk opkg-link mount /mnt/loopdisk apachectl start echo "Done." } stop() { echo -n "Umounting USB drive: " apachectl stop sync sync swapoff /mnt/hdd/swapfile umount -l /mnt/loopdisk umount -l /dev/sda1 echo "Done." } restart() { apachectl restart }

Enter the required permissions to it.

chmod a+x /etc/init.d/hdd

create a swapfile, then format and turned on.

dd if=/dev/zero of=/mnt/hdd/swapfile bs=1M count=64
mkswap /mnt/hdd/swapfile
swapon /mnt/hdd/swapfile

To see if work:

free

虚拟磁盘

现在,让我们做一个虚拟磁盘。您需要一个额外的Package软件包。命令如下:

 
opkg update
opkg install kmod-fs-ext3 kmod-loop e2fsprogs
insmod ext3
insmod loop

然后,创建,格式化该虚拟盘:

dd if=/dev/zero of=/mnt/hdd/owrt_loop bs=1M count=128
mke2fs -j -F -b 1024 /mnt/hdd/owrt_loop

为了能正常使用,插入到 OpenWrt中:

mkdir /mnt/loopdisk
mount -o loop /mnt/hdd/owrt_loop /mnt/loopdisk

现在我们的OpenWrt不仅仅是 xMB大小了, 而是 x+128 MB

Write in /etc/opkg.conf file into the following line

#installation.destinations

dest usb /mnt/loopdisk

安装package包命令如下:

opkg -dest usb install package

删除package包命令如下:

opkg -dest usb remove package

故障排除

确保是ext2或ext3的文件系统。

Notes

Back to top

zh-cn/doc/howto/usb.storage.txt · Last modified: 2012/02/12 05:04 by jsntyyl