Revision as of 14:01, 13 April 2014 by Craig Peacock (Talk | contribs)

What is Unsorted Block Image (UBI)?

The Unsorted Block Image File System (UBIFS) is designed to operate on raw flash devices. It was first introduced into the 2.6.27 Linux Kernel in October 2008.

UBIFS works on an Unsorted Block Image (UBI) device which provides tracking of bad flash blocks and wear leveling. UBI works on top of MTD.

For further information see :

Obtaining details of the Flash Device

Before we can start building UBIFS and UBI images, we need to know details of the flash device fitted on the Seagate GoFlex Home. This can be achieved by running mtdinfo -a on our target device, i.e. the GoFlex Home.

Pay particular attention to the Eraseblock size, Sub-page size, Minimum input/output unit size and the number of eraseblocks for the partition where our UBIFS will reside.

# mtdinfo -a
Count of MTD devices:           3
Present MTD devices:            mtd0, mtd1, mtd2
Sysfs interface supported:      yes

mtd0
Name:                           uboot
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB
Amount of eraseblocks:          8 (1048576 bytes, 1024.0 KiB)
Minimum input/output unit size: 2048 bytes
Sub-page size:                  512 bytes
OOB size:                       64 bytes
Character device major/minor:   90:0
Bad blocks are allowed:         true
Device is writable:             true

mtd1
Name:                           uImage
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB
Amount of eraseblocks:          48 (6291456 bytes, 6.0 MiB)
Minimum input/output unit size: 2048 bytes
Sub-page size:                  512 bytes
OOB size:                       64 bytes
Character device major/minor:   90:2
Bad blocks are allowed:         true
Device is writable:             true

mtd2
Name:                           root
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB
Amount of eraseblocks:          1992 (261095424 bytes, 249.0 MiB)
Minimum input/output unit size: 2048 bytes
Sub-page size:                  512 bytes
OOB size:                       64 bytes
Character device major/minor:   90:4
Bad blocks are allowed:         true
Device is writable:             true

Building an UBIFS

Make a ZLIB compressed (-x zlib) UBI filesystem image with a minimum I/O unit size of 2048 bytes (-m 2048), a logical erase block (LEB) size of 131072 bytes and a maximum logical erase block count of 1992:

# mkfs.ubifs -v -x zlib -m 2048 -e 129024 -c 1992 -r rootfs/ rootfs.ubifs.img
mkfs.ubifs
	root:         rootfs/
	min_io_size:  2048
	leb_size:     129024
	max_leb_cnt:  1992
	output:       rootfs.ubifs.img
	jrn_size:     8388608
	reserved:     0
	compr:        zlib
	keyhash:      r5
	fanout:       8
	orph_lebs:    1
	space_fixup:  0
	super lebs:   1
	master lebs:  2
	log_lebs:     5
	lpt_lebs:     2
	orph_lebs:    1
	main_lebs:    459
	gc lebs:      1
	index lebs:   11
	leb_cnt:      470
	UUID:         57A8A50F-A285-4BB5-A671-651CB920EDDA
Success!

Building an UBI Image

Next, we build an UBI image containing the above filesystem as one volume. Create a configuration file called ubi.cfg containing the following :

[ubifs]
mode=ubi
image=rootfs.ubifs.img
vol_id=0
vol_size=200MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize
# ubinize -v -o rootfs.ubi.img -m 2048 -p 128KiB -s 512 -O 512 ubi.cfg
ubinize: LEB size:                  129024
ubinize: PEB size:                  131072
ubinize: min. I/O size:             2048
ubinize: sub-page size:             512
ubinize: VID offset:                512
ubinize: data offset:               2048
ubinize: UBI image sequence number: 1674295640
ubinize: loaded the ini-file "ubi.cfg"
ubinize: count of sections: 1

ubinize: parsing section "ubifs"
ubinize: mode=ubi, keep parsing
ubinize: volume type: dynamic
ubinize: volume ID: 0
ubinize: volume size: 209715200 bytes
ubinize: volume name: rootfs
ubinize: volume alignment: 1
ubinize: autoresize flags found
ubinize: adding volume 0
ubinize: writing volume 0
ubinize: image file: rootfs.ubifs.img

ubinize: writing layout volume
ubinize: done

Flashing

It is advisable not to use nandwrite to flash an UBI image. For more information, see here.

The recommended method is to use ubiformat as this not only preserves the erase counters used for wear leveling, it prevents ECC errors on some NAND flash devices.

ubiformat is not available from the bootloader, thus you need to undertake this task on a bootable GoFlex Home. I normally boot using root NFS to carry out this operation.

From the shell on the GoFlex Home:

# flash_erase /dev/mtd2 0 0 
# ubiformat /dev/mtd2 -s 512 -O 512 -f rootfs.ubi.img

Testing

You can test an newly flashed image prior to loading it as the root filesystem (i.e. while using root NFS).

First attach the desired MTD partition to UBI using the ubiattach command. Once this is successful, you can mount the UBIFS.

ubiattach -p /dev/mtd2
mount -t ubifs /dev/ubi0_0 /mnt/ubifs/

Boot Messages

Below is the kernel messages of a system booting from the above UBIFS :