RSS

Tag Archives: ext2

TP-Link TL-WR1043ND – Openwrt – Ch05_USB_Storage

0) Briefing

    • What next
      • My USB thumb drive will be partition in 3 parts. I will be using ext4 for data storage. You may use FAT16/FAT32/NTFS, but you cannot have your drive as rootfs_data ( /overlay )
      • Install USB storage kernel module kmod-usb-storage ( 82 Byte in total )
        • Dependency : kmod-scsi-core
      • Install block-mount( 132 byte in total )
        • Dependency : libuuid libblkid blkid swap-utils
      • Install libext2fs( 168 Byte in total )
        • Dependency : libpathread libcom_err
      • install ext4 file system kernel module kmod-fs-ext4 ( 188 Byte in total )
        • Dependency : kmod-lib-crc16
      • (Optional) Install file system utilities e2fsprogs ( 312 Byte in size )
      • Setup a USB thumb drive to be part of the root memory

——————————————————————————–

1) Install USB storage softwares

  • Open up your favorite terminal and login into the router
    ssh root@192.168.1.1
  • These commands will update database:
    opkg update
  • Install all these softwares (580 Byte in size):
    opkg install kmod-usb-storage block-mount libext2fs kmod-fs-ext4
  • (Optional) If you want to format you USB thumb drive via OpenWRT, you should install:
    opkg install e2fsprogs

——————————————————————————–

2) Prepare your USB thumb drive

  • I am using a 8GB thumb drive, and will be chopped into 3 partitions:
    1. Root file system data ( /overlay )
    2. Personal data storage ( /mnt/shares )
    3. Swap

2.1) Partitioning

  • Plug in your external drive into your computer and see if it is present:
    dmesg  | tail -20
  • You should able to see something like USB storage in those lines, but you should be looking at:

    sd 0:0:0:0: [sda] 15124992 512-byte logical blocks: (7.74 GB/7.21 GiB)

    • Your drive is recognized as /dev/sda
    • if you have more than one drive, then it may be /dev/sdb
  • Use fdisk to partition your drive:
    fdisk /dev/sda
  • My partitions on my 8GB thumb drive:
    1. sda1 – 6.2GB – ext4 – Use for personal data ( /mnt/shares )
    2. sda2 – 504MB – ext4 – Root files system data ( /overlay )
    3. sda3 – The rest 300MB+ – Swap
  • Then you should format your drive:
     mkfs.ext4 -O ^has_journal,extent /dev/sda1
     mkfs.ext4 -O ^has_journal,extent /dev/sda2
     mkswap /dev/sda3

2.2) Mounting

  • Now plug in your USB external storage to the router
  • Create a location for you share folder and mount it
    mkdir /mnt/shares && mount /dev/sda1 /mnt/shares
  • To mount your swap partition:
    swapon /dev/sda3
  • To config your drives mount automatically at boot, first you have to find out the UUID of each partition. UUID is used is because Linux system cannot guaranty it will give the same sda or sdb or sdc to a drive. So when you write down the config file to mount sda1 it may not work because your drive may become sdb1 when next boot. Although it is very rare to be happened, now Linux system use UUID. UUID only change when you format your drive.To find out the UUID of your drive:
    blkid
  • Edit this file to tell the system what to mount:
    vi /etc/config/fstab
  • Here is my /etc/config/fstab

    config global automount
    option from_fstab 1
    option anon_mount 1

    config global autoswap
    option from_fstab 1
    option anon_swap 0

    config mount
    option target           /mnt/shares
    option uuid             “e2bbca31-8df9-447e-affd-70c927e59847”
    #option device          /dev/sda1
    option fstype           ext2
    option options          rw,sync
    option enabled          1
    option enabled_fsck     0

    config swap
    option uuid             “5fb9edfd-23f0-48c4-8a96-f2887b469329”
    #option device          /dev/sda2
    option enabled          1

  • When you are done, restart fstab
    /etc/init.d/fstab restart
  • Check are you drives properly mounted:
    df

——————————————————————————–

3) rootfs on external storage ( extroot )

In Linux file system, Root ( / ) is the mother of all folders and files. Every folder and file structure start building from there, just like C: drive in MS Windows.

Additionally, Linux link a USB external storage to a folder when it is plug in, so all other softwares in Linux will think it is just another normal folder rather than something special. They call this Mounting to a folder.

In OpenWRT, there are two main folders. One is /rom, which hold all pre-installed softwares. The other is /overlay, which hold every other data and settings, which included softwares that you manually installed.

What are we going to do now is to ask the system to mount our external drive to /overlay at boot. Imagine the original /overlay like a bin, you put things in it. Then what are we doing next is to put a plastic bag over the bin, which the bag represent the external drive. People still think they put thing into the bin, but actually the bag store everything, and when the bag is remove, it will expose the original stuffs inside the bin.

3.1) Partitioning and formating

  • If you have not partition and format your USB external storage, just follow the Step 2.1 above

3.2) Config to mount your external storage at boot

  • We need to visit the /etc/config/fstab file again:
    vi /etc/config/fstab
  • Add the following data:

    config mount
    option target            /overlay
    option uuid             “a60731b0-c056-4771-993e-c8f8338cc21f”
    #option device          /dev/sda2
    option fstype            ext4
    option options            rw,sync
    option enabled            1
    option enabled_fsck            0

3.3) Copy original /overlay data to external storage

  • As my Bin and Bag explanation above, if you put your new bag inside the bin, people will think it is a empty bin, however, the original stuffs inside the bin is needed. So we need to duplicate them to our bag.
  • First if your drive is not mount, mount it, in my case is sda2:
    mkdir /mnt/sda2 && mount /dev/sda2 /mnt/sda2
  • Then copy the original data to the external storage:
    tar -C /overlay -cvf - . | tar -C /mnt/sda2 -xf -
  • Unmount the external drive then reboot:
    umount /mnt/sda2 && rm -rf /mnt/sda2

——————————————————————————–

4) Quick tips

  • If you do not want to edit /etc/config/fstab, you can edit /etc/fstab:

    /dev/sda2    /overlay    ext3    rw,sync    0    0
    /dev/sda1    /mnt/shares    ext2    rw,sync    0    0
    /dev/sda3    none    swap    sw    0    0

——————————————————————————–

Appendix : References

 
5 Comments

Posted by on May 8, 2013 in Networking, Router

 

Tags: , , , , , , , , , , , , ,

 
%d bloggers like this: