UBI FAQ and HOWTO

Table of contents

  1. How to enable UBI?
  2. How to attach an MTD device?
  3. How to create/delete UBI volumes?
  4. How to run JFFS2 on top of an UBI volume?
  5. Can I run ext2 on top of UBI?
  6. Do I have to format my empty flash before running UBI on top of it?
  7. How to erase flash and preserve erase counters?
  8. How to create UBI images?
  9. How to find out min. I/O unit size, sub-page size, and LEB size?
  10. How to find out LEB size?
  11. How to flash UBI images and preserve erase counters?
  12. Can UBI logical eraseblocks be written randomly?
  13. Why UBI does not use OOB area of NAND flashes?
  14. Is UBI tolerant of power failures?
  15. May UBI be used on MLC flash?
  16. Why does ubiattach on a freshly formatted device fail with "Invalid argument"?
  17. What is sub-page?
  18. I get "ubi_io_write: error -5 while writing 512 bytes to PEB 5:512"
  19. I get "no VID header found at PEB 7923, only 0xFF bytes"
  20. How to force UBI to ignore sub-pages?
  21. How to implement UBI flasher?
  22. What does the "ubi_bgt0d" thread do?
  23. How do I speed up UBI initialization
  24. How to debug UBI?

How to enable UBI?

In the Linux configuration menu, go to "Device Drivers" -> "Memory Technology Devices (MTD)" -> "UBI - Unsorted block images", and mark the "Enable UBI" check-box. UBI may be either compiled into the kernel or be built as a kernel module.

How to attach an MTD device?

Each MTD device has a name and a number, which you may find out by examining the /proc/mtd file. The preferable way to attach MTD devices is to attach them by name, not by number, because MTD device numbers may change if you change the layout of your flash, while the names will supposedly stay the same.

If UBI is compiled as a kernel module, it is enough to specify the MTD device name or number to attach in the module arguments, e.g.

$ modprobe ubi mtd=rootfs
$ modprobe ubi mtd=0

loads the UBI kernel module and attaches MTD device named "rootfs" or with number 3 (mtd3). And

$ modprobe ubi mtd=config mtd=rootfs
$ modprobe ubi mtd=3 mtd=5

command loads UBI kernel module and attaches MTD devices named "config" and "rootfs", or mtd3 and mtd5.

If UBI is compiled into the kernel, the mtd device to attach may be specified in the ubi.mtd=kernel boot parameter, e.g.,

ubi.mtd=rootfs
ubi.mtd=0

command makes UBI attach MTD device named "rootfs" or mtd3 when the kernel is booting. And

ubi.mtd=config ubi.mtd=rootfs
ubi.mtd=3 ubi.mtd=5

command makes UBI attach MTD devices named "config" and "rootfs", or mtd3 and mtd5.

And finally, MTD devices may be attached or detached at any time with the ubiattach and ubidetach utilities; For example,

$ ubiattach /dev/ubi_ctrl -m 3

attaches mtd3. But these utilities will work with kernel versions starting from version 2.6.25. And someone should update them and teach to accept MTD device names, not only MTD device numbers.

How to create/delete UBI volumes?

Use the ubimkvol and ubirmvol utilities. For example, the below command creates an 128MiB volume on UBI device 0:

$ ubimkvol /dev/ubi0 -N rootfs -s 128MiB

and the following command removes it:

$ ubirmvol /dev/ubi0 -n 0

For additional information, use ubimkvol -h and ubirmvol -h.

How to run JFFS2 on top of an UBI volume?

There is an additional driver called gluebi which can emulate fake MTD devices for each UBI volume and JFFS2 can be used with these emulated MTD devices. Enable gluebi in the UBI configuration menu (the "MTD devices emulation driver (gluebi)" option).

Note, pre-2.6.31 kernels did not have a separate gluebi driver and it was built into the UBI driver. The corresponding UBI configuration menu option was "Emulate MTD devices"

Can I run ext2 on top of UBI?

UBI is not a block device emulation layer, it is not an FTL. Neither ext2 nor other "traditional" file systems can be run on top of an UBI device. Please, read the big red note and overview documentation sections to realize why.

But it is much easier to implement FTL on top of UBI than on top of MTD, because UBI takes care about many flash complexities and makes it possible to concentrate on on upper-level issues rather then solving flash media problems like wear-leveling, bit-flips, bad eraseblocks, etc.

This e-mail describes an idea of a simple FTL layer on top of UBI.

Do I have to format my empty flash before running UBI on top of it?

Ideally, yes, but not necessarily.

Roughly speaking, "UBI-formatted MTD device" has each PEB erased and containing a valid EC header. However, it is not always possible because many a lot of existing software is not UBI-aware and cannot preserve erase counters. Often all it can do is to wipe out the flash. This is why UBI can to deal with empty flash perfectly well - it just automatically formats it.

So the answer is no, you do not have to. For example, if you wipe out the flash and try to attach it to UBI - it will work. UBI will just automatically format the flash. The formatting process takes time, though.

But keep in mind that every time you erase the flash, you lose erase counters, so you lose wearing information. Doing this over and over again may wear out some eraseblocks. This is especially dangerous on MLC NAND flashes which have very low eraseblock life-cycle. Try to use UBI-aware utilities and flashing programs.

How to erase flash and preserve erase counters?

Use the ubiformat utility. Example:

$ ubiformat /dev/mtd0
ubiformat: mtd0 (NAND), size 536870912 bytes (512.0 MiB), 131072 eraseblocks of
131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 4095 -- 100 % complete
ubiformat: 4094 eraseblocks have valid erase counter, mean value is 104
ubiformat: bad eraseblocks: 13, 666
ubiformat: formatting eraseblock 4095 -- 100 % complete

Note! If you use an old kernel, which is older than 2.6.30 and where MTD does not support sysfs, you have to pass "-s" argument to ubiformat.

This section has some hints for those who implement a custom UBI flasher.

How to create UBI images?

UBI images may be created using the ubinize utility. This utility takes the configuration file on input and generates an UBI image on output. The input configuration file describes all UBI volumes which the resulting UBI image has to contain. The configuration file has the ini-file syntax. Here is an example:

$ cat config.ini
[configuration-data-volume]
mode=ubi
image=config_data.img
vol_id=0
vol_size=512KiB
vol_type=static
vol_name=configuration

[rootfs-volume]
mode=ubi
image=rootfs.img
vol_id=1
vol_size=220MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize

$ ./ubinize -o ubi.img -p 128KiB -m 512 -s 256 config.ini

The config.ini file tells UBIFS to create 2 volumes:

So in the above example, ubinize basically reads 3 input files:

Users often wonder why ubinize needs a configuration file. The answer is that one UBI image may contain many UBI volumes with different characteristics and it is difficult to invent a nice command-line interface for specifying all those characteristics. Thus a configuration file is used. But feel free sending a patch which teaches ubinize working without the configuration file if there is only one volume.

Note, UBI reserves physical flash space for volumes. Namely, UBI reserves a physical eraseblock for each logical eraseblock. The size if LEB 130560 bytes in our example (found out by running ubinize with -v option), which means that the configuration volume will have 5 LEBs ([512 * 1024] / 130560 rounded up) and the root file-system volume will have at least 1767 LEBs. This means that the MTD device the ubi.img is going to be flashed to has to have at least 1772 physical eraseblocks, which is about 221MiB. But because of the UBI overhead (see this section), the MTD device actually has to be at least 225MiB in size. Of course it may be larger, in which case the "rootfs" volume will be re-sized and take the rest of the flash space (because of the auto-resize flag).

Also, the config_data.img and rootfs.img input files do not have to be 512KiB and 220MiB respectively, but may be smaller if they contain less data. In this case the resulting ubi.img file will also be smaller than 221MiB. All the ubinize utility does is it takes the image files, splits them to LEB-sized chunks, forms PEB data by adding UBI headers to these LEB chunks, and writes the result to the output file. It also writes the volume table (2 physical eraseblocks). Thus, ubi.img file size will be small if the input volume images are small. And ubinize does not do any further padding.

Please, use the Use the ubiformat utility to flash the resulting UBI image. Also, you can find detailed description of how UBI-aware flashing programs should work in this section.

How to find out min. I/O unit size, sub-page size, and LEB size?

If you run kernel version 2.6.30 or later, the easiest way to find out all these parameters is to run the mtdinfo tool, which reports them. Of course, the tool has to be run on the target.

Otherwise, first find out the physical eraseblock (PEB) size from the flash manual, or from the /proc/mtd file on the target.

Refer this and this sections for some hints about what the min. I/O unit and sub-page sizes may be for you.

The LEB size is defined by:

Please, read this section for information about how LEB size is calculated (it is denoted by O there).

How to flash UBI images and preserve erase counters?

Use the ubiformat utility. Example:

$ ubiformat /dev/mtd0 -f ubi.img
ubiformat: mtd0 (NAND), size 536870912 bytes (512.0 MiB), 131072 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 4095 -- 100 % complete
ubiformat: 4094 eraseblocks have valid erase counter, mean value is 105
ubiformat: bad eraseblocks: 13, 666
ubiformat: flashing eraseblock 50 -- 100 % complete
ubiformat: formatting eraseblock 4095 -- 100 % complete

Note, this section has describes how UBI-aware flasher program has to work.

Important!: even if you do not care about erasecounters, do not use dd for flashing UBI images to NAND flashes, because dd does handle bad eraseblocks.

Important!: even if you do not care about erasecounters, do not use nandwrite for flashing UBI images containing UBIFS file-system to NAND flashes, read here for more information.

Can UBI logical eraseblocks be written randomly?

No, the flash chip restrictions have to be taken into account. This is because UBI logical eraseblocks (LEB) are mapped to physical eraseblocks (PEB), and an LEB write operation is essentially a write to the corresponding PEB plus a small offset. The important flash restrictions are:

  1. all writes have to be aligned to the min. I/O unit size and be multiple of min. I/O unit size;
  2. sometimes it is prohibited to write more then once to the same PEB offset (e.g., in case of NAND flash);
  3. many NAND flashes (specifically, MLC NAND flashes) require NAND pages to be written sequentially from the beginning of the physical eraseblock, to the end of the physical eraseblock; for example, it is prohibited to first write to offset 2048, then to offset 0; once offset 2048 has been written to, it is possible to write only to further offsets.

Even if the flash chip is devoid of the last restriction, UBI anyway requires logical eraseblocks to be written sequentially from the beginning to the end. This is because UBI calculates data CRC when moving logical eraseblocks to other physical eraseblocks (see here for more information), so a write an offset which is less than the furthest written data offset causes CRC errors;

Why UBI does not use OOB area of NAND flashes?

Because many flashes (e.g., NOR) do not have OOB and UBI was designed to be generic. Also, modern MLC NAND flashes use whole OOB area for the ECC checksum, so there is no room for application data.

But of course, things could be optimized for SLC NAND flashes if UBI used the space available in the OOB area. This is not implemented, but one could probably do this.

Is UBI tolerant of power failures?

Yes, UBI is designed to be tolerant of power failures and unclean reboots.

May UBI be used on MLC flash?

Yes, it may, as long as the flash is supported by the MTD layer. UBI does not use OOB and it requires data to be written sequentially (see here). UBI guarantees that the difference between maximum and minimum erase-counters is withing certain threshold, which is 4096 by default. Since MLC flashes have quite low eraseblock life-cycle (about 1000-10000, unlike 100000-1000000 for SLC NAND and NOR flashes), the threshold has to be set to a lower value (e.g., 256). This may be done via the Linux kernel configuration menu.

Note, unlike UBI, JFFS2 uses random wear-leveling algorithm, which is in fact not completely random, because JFFS2 makes it more probable to garbage collect eraseblocks with more dirty data. This means that JFFS2 is not really appropriate for MLC flashes. However, it is possible to use JFFS2 file-system on top of UBI (see this section) to improve wear-leveling.

Why does ubiattach on a freshly formatted device fail with "Invalid argument"?

On NAND devices that support sub-page accesses, ubiformat may choose a different location for the VID header to the kernel UBI driver This can result in the following error when attaching to a UBI device:

$ ubiformat /dev/mtd0
ubiformat: mtd0 (NAND), size 260046848 bytes (248.0 MiB), 131072 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
[...]
$ ubiattach /dev/ubi_ctrl -m 0
ubiattach: error!: cannot attach mtd0
           error 22 (Invalid argument)

and in dmesg you will see:

UBI error: validate_ec_hdr: bad VID header offset 2048, expected 512
UBI error: validate_ec_hdr: bad EC header
UBI error: ubi_io_read_ec_hdr: validation failed for PEB 0

This happens because ubiformat assumes the flash does not support sub-pages, because the kernel does not expose sub-page information to user-space (which should be fixed when sysfs support is added to MTD). However, the kernel UBI driver assumes sub-pages are supported and sub-page size is 512 bytes in our example. To fix this, you should override the default sub-page size that ubiformat uses to what the kernel expects using the -s option of ubiformat. For example, if you see the error above in dmesg, you can tell ubiformat to assume 512-byte sub-page by executing:

$ ubiformat /dev/mtd0 -s 512

Or you may pass "-O 512" would have the same effect as "-s 512" - the VID header would be put at offset 512.

Alternately, you may wish to actually attach to the UBI device by forcing VID header offset to be 2048 bytes. In other words, you may ask UBI to avoid using sub-pages. This is not recommended since this will require more storage overhead, but may be useful if your NAND driver incorrectly reports that it can handle sub-page accesses when it should not. To do this with ubiattach, use:

$ ubiattach /dev/ubi_ctrl -m 0 -O 2048

or on the kernel command-line, pass:

ubi.mtd=0,2048

What is sub-page?

Please, refer this section.

I get "ubi_io_write: error -5 while writing 512 bytes to PEB 5:512"

If you have a 2048 bytes per NAND page device, and have CONFIG_MTD_NAND_VERIFY_WRITE enabled in your kernel, you will need to turn it off. The code does not currently (as of 2.6.26) perform verification of sub-page writes correctly. As UBI is one of the few users of sub-page writes, not much else seems to be affected by this bug.

I get "no VID header found at PEB 7923, only 0xFF bytes"

The messages mean that UBI could not find VID header in the eraseblock, but the header supposed to be there. This probably means some corruption.

However, if you have UBI "build" debugging messages enabled, (CONFIG_MTD_UBI_DEBUG_MSG_BLD=y), you may see a lot of these messages and they are harmless. They are just debugging messages in this case.

How to force UBI to ignore sub-pages?"

If your NAND flash supports sub-pages, UBI will use them. But sometimes you may want to ask UBI to ignore sub-pages, for example if you have an UBI image which was made for a similar NAND flash, but without sub-pages, or if you just want to disable sub-pages for testing purposes. You may do this by forcing UBI to read/write the VID header from/to the beginning of the second page, instead of the second sub-page (which is the default behavior). And UBI uses sub-pages only for VID headers, so this is enough.

If you attach MTD devices using the ubiattach tool - just use the --vid-hdr-offset option and specify the second page offset. For example, if you have 2048-byte NAND pages, use --vid-hdr-offset 2048. All the below examples also assume 2048-byte NAND pages.

If you do not use ubiattach and instead specify the MTD device to attach using kernel boot parameters (see here), then you may put VID header offset after the MTD device name or number. For example:

$ ubi.mtd=rootfs,2048
$ ubi.mtd=3,2048

will attach MTD device named "rootfs" (or MTD device number 3) and force UBI to read/write VID headers from/to offset 2048, which is the second NAND page in the eraseblock.

Similarly, if you use module load parameters, try

$ modprobe ubi mtd=rootfs,2048
$ modprobe ubi mtd=3,2048

And of course, if you have an MTD partition which is already formatted so that sub-pages are used, which means all VID headers sit at offsets 512 (second sub-page of a 2048-byte NAND page), you cannot force UBI to stop using sub-pages (it will fail). You may do this only if you are attaching an empty MTD partition, or if the flash is already formatted so that all VID headers are at offset 2048. This means that if you use or ubinize, you should avoid using the -s parameter.

How to implement UBI flasher?

Please, read here

What does the "ubi_bgt0d" thread do?

The UBI back-ground thread is a per-UBI device thread which has "ubi_bgtXd" name, where "X" is the UBI device number. For example, "ubi_bgt0d" is a background thread corresponding to UBI device 0.

The UBI background thread is doing background physical eraseblock erasure. This is an important optimization which greatly improves UBI I/O throughput (applications do not have to wait for erasure completion). For example, UBI unmap operation schedules physical eraseblocks for erasure.

The background thread also tortures faulty physical eraseblocks.

The UBI background thread also moves data from more worn-out physical eraseblocks to less worn out, i.e., performs wear-leveling. It also moves data from physical eraseblocks which have bit-flips. See the UBI overview section for some more information.

Note, UBI may work without the background thread, so the thread is just an optimization, although a very important one.

How do I speed up UBI initialization

As this section explains, UBI has scalability limitations imposed by its design, so there is no much you can do about it but without re-designing. Here are some ideas

How to debug UBI?

Use fake MTD device

When debugging UBI one doesn't have to use a real embedded platform with real flash. In many cases, it is easier to use a PC with an MTD device emulator and run UBI on top of this emulated MTD device. In fact, this is how most of the UBI development was done.

There are 3 MTD device emulators in Linux kernel available:

For example, to get a 32MiB fake NOR flash, run

$ modprobe mtdram total_size=32768

or to get a 64MiB fake NAND flash, run

$ modprobe nandsim second_id_byte=0x36

See here for more information about the NAND simulator.

To ensure that you have fake MTD devices, run "cat /proc/mtd". It should print something like

dev:    size   erasesize  name
mtd0: 02000000 00020000 "mtdram test device"
mtd1: 04000000 00004000 "NAND simulator partition"

The fake MTD devices may further be attached to UBI (see here).

Enable debugging

Enable UBI debugging support in the configuration menu (the "UBI debugging" check-box). When debugging is enabled, UBI prints more information about errors, and adds extra assertions in the code which may help to catch bugs.

Note, if you enable the UBI debugging option, UBIFS will not flood syslog with its messages. It will just do some light-weight self-checks, and it will be more verbose in case of errors. The overhead of having only debugging enabled is very low. But if you enable other UBI debugging options, the situation changes (see below).

In many cases, it is enough to just enable debugging. But sometimes it is also useful to enable extra-self checks, which make sure internal data structures are consistent and may catch the problem much earlier then it would have been noticed otherwise. Please, mark the "Extra self-checks" check-box to enable the self-checks. Self-checks make UBI considerably slower. For example, UBI attach time may become very long.

Debugging messages

Sometimes it is necessary to make UBI print about what it is doing. You may enable various UBI debugging messages in the "Additional UBI debugging messages configuration menu. When the messages are enabled, UBI prints a lot to the kernel ring buffer and this makes it slower. This section describes few tricks and techniques which might be useful when debugging with kernel messages.

The Linux kernel has internal ring buffer where all the debugging prints go. User-space applications like syslogd usually read data from the ring buffer, do further processing and the prints usually end up in the system log file. When the UBI debugging messages are enabled, it prints huge amount of messages. What happens is that the user-space processes are unable to fetch them from the ring buffer with this pace so most of the messages are just lost. Namely, they are just over-written with newer message (the buffer is a "ring"). There are 2 ways to gather all the messages:

  1. use serial console;
  2. use very large ring buffer.

The first method is usually appropriate when debugging on a small embedded platform connected to a PC via serial line. What you have to be aware of is that the messages are printed to the serial console synchronously, which means that the system is blocked and waiting for the print operation to be finished. So if there are many prints, the system speed becomes limited to the serial console baud rate. And obviously, it is recommended to use higher baud rates, e.g. 115200.

The UBI debugging messages have "debugging" level 7 and they are usually not printed to the console. You may use dmesg -n8 command to make all kernel messages to go to the console. Another possibility is to boot the kernel with ignore_loglevel option, in which makes the kernel print all messages to the console unconditionally.

The second method is more appropriate when debugging on a machine with a lot of RAM, for example on a desktop PC with a flash emulator. Just make your ring buffer large, e.g. 64MiB by booting the kernel with log_buf_len=64M option. This will most probably make the ring buffer fit enough messages to identify the problem. And because the messages are printed to RAM, this is way quicker than the first method.

Last updated: 13 Jan 2009, dedekind Valid XHTML 1.0! Valid CSS!