明日复明日,明日何其多。
我生待明日,万事成蹉跎。

还是老外的靠谱!openwrt用U盘扩容

csdn收到一个,用他的代码最后报错,可能是老版不兼容了。收到衣蛾最新的,不过是国外的。很不错。

# opkg update
# opkg install block-mount kmod-fs-ext4 kmod-usb-storage e2fsprogs kmod-usb-ohci kmod-usb-uhci fdisk ntfs-3g
opkg install kmod-sdhci kmod-sdhci-mt7620

USB device preparation
We can manipulate the USB device we intend to use either on a separate machine, or directly on the LEDE system, using fdisk. For the sake of this tutorial we will choose the second option and create a single partition which will use all the space available on the USB device.

First we connect the USB to our device. To verify it is recognized by the kernel we can examine the last lines of the output produced by the d

mesg command. We should observe a result similar to the following:

# dmesg | tail
[   91.701565] usb-storage 1-1.1:1.0: USB Mass Storage device detected
[   91.708962] scsi host2: usb-storage 1-1.1:1.0
[   92.714770] scsi 2:0:0:0: Direct-Access     Kingston DataTraveler 2.0 1.00 PQ: 0 ANSI: 2
[   92.726372] sd 2:0:0:0: [sda] 1994752 512-byte logical blocks: (1.02 GB/974 MiB)
[   92.734814] sd 2:0:0:0: [sda] Write Protect is off
[   92.739691] sd 2:0:0:0: [sda] Mode Sense: 23 00 00 00
[   92.745685] sd 2:0:0:0: [sda] No Caching mode page found
[   92.751147] sd 2:0:0:0: [sda] Assuming drive cache: write through
[   92.851061]  sda: sda1
[   92.858827] sd 2:0:0:0: [sda] Attached SCSI removable disk

Our device has been recognizes as sda. To partition it, we launch fdisk and pass the USB device path as the utility argument:

# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

The first thing we want to do is to create a new DOS partition table on the device, therefore we enter o as command, and press enter:

Command (m for help): o
Created a new DOS disklabel with disk identifier 0xd67f57f9.


Next, we want to add a new partition. We use the n command to perform the operation. We will be asked want type of partition we want to create: here we want a primary partition. We will also asked to enter the partition number and the partition first and the last sector. In all three cases we can just press enter and accept the defaults.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-1994751, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-1994751, default 1994751):

Created a new partition 1 of type 'Linux' and of size 973 MiB.

The changes we performed to the device are not effective yet. To confirm them we must use the wcommand:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now that our device is partitioned, we must create a filesystem.

Filesystem creation

The next step consists into creating an ext4 filesystem on the partition we created in the previous step. We just have to launch the mkfs.ext4 command and pass the path of the partition as argument:

# mkfs.ext4 /dev/sda1
mke2fs 1.43.3 (04-Sep-2016)
Creating filesystem with 249088 4k blocks and 62336 inodes
Filesystem UUID: 42109b6a-759a-48ba-a7b9-1508d0973131
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

We take note of the filesystem UUID (42109b6a-759a-48ba-a7b9-1508d0973131): we need it to reference the fileystem in the next step.

Fstab setup

In this step we modify the system fstab file, which in our LEDE system is /etc/config/fstab. Inside the file, we append the following section:

config 'mount'
        option  target  '/overlay'
        option  uuid    '42109b6a-759a-48ba-a7b9-1508d0973131'
        option  enabled '1'

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.



The filesystem identified by UUID, which is the one we created on our USB device, will be mounted on /overlay, so it will be used as the system storage.

Copying the content of the system storage on the usb device

In order for our setup to work we must copy the content of the current system storage on the USB device. We first mount the ext4 filesystem on /mnt:

# mount /dev/sda1 /mnt

Than, we copy the content on it:

# cp -a /overlay/. /mnt

In the example above, we used the cp command with two options -a option: it is the short version of --archive, and it is used to preserve the attribute of the copied files.

Reboot the system

At this point our setup should be complete. For the changes to become effective we need to reboot the system. We can turn the device off and on again from the physical switch, or we can issue the following command (the terminal will probably freeze once the device is turned off):

# reboot

Once the system is rebooted, to verify the additional space is used, we can login again into our router, and run the df command passing /overlay as argument. Here we also used the -hoption in order to obtain human-readable sizes:

# df -h /overlay
Filesystem                Size      Used Available Use% Mounted on
/dev/sda1               941.7M      5.2M    871.9M   1% /overlay

As expected, we can see that /dev/sda1 is the filesystem mounted on /overlay: the size is 941.7M: only 5.2M are in use, which is approximately the 1% of the available space.

Back to stock

Returning to the stock system configuration is pretty simple, only few steps must be performed. The first thing we have to do is to identify the system partition originally mounted on /overlay. To do so, we must take a look at the /proc/mtd file:

# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00020000 00010000 "u-boot"
mtd1: 001333cc 00010000 "kernel"
mtd2: 0069cc34 00010000 "rootfs"
mtd3: 00460000 00010000 "rootfs_data"
mtd4: 00010000 00010000 "art"
mtd5: 007d0000 00010000 "firmware"


What interests us is the mtd file with the rootfs_data name, which in this case is mtd3. We must mount the corresponding block device, /dev/mtdblock3 on /mnt:

# mount -t jffs2 /dev/mtdblock3 /mnt

Notice that we used the -t option of the mount command to specify the filesystem type, jffs2 in this case (a filesystem designed specifically for flash memory devices).

Once the partition is mounted, we must revert the changes previously made in the fstab file. At this point the original file should be accessible as /mnt/upper/etc/config/fstab. We open it with our favorite text editor and either delete, comment or modify the section we previously added, from:

config 'mount'
        [...]
        option  enabled '1'

To:

config 'mount'
        [...]
        option  enabled '0'

Once we are done, we save the changes. Finally, we unmount the block device and reboot the system:

# umount /mnt && reboot

Conclusion

In this article we learned how to expand the storage space of a LEDE system using a simple USB device. LEDE is an open source OS which can be installed on a variety of routers; with this simple procedure we obtain more space for the system data and use it, for example, to install additional packages which would not fit on the usually small storage space available on routers. To know more about the LEDE project, please visit the LEDE documentation.

赞(0) 打赏
未经允许不得转载:致明日 » 还是老外的靠谱!openwrt用U盘扩容
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

坚持,总会看见蓝天和白云

联系我们你来了,你就是最棒的!

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏