UBIFS FAQ and HOWTO
Table of contents
- May UBIFS be used on MLC NAND flash?
- How to mount UBIFS?
- How to find out LEB size, sub-page size, etc?
- How to create an UBIFS image?
- May an empty UBI volume be mounted?
- How to use UBIFS with nandsim?
- What is the purpose of --max-leb-cnt mkfs.ubifs option?
May UBIFS be used on MLC NAND flash?
Yes, it should run fine. Let's consider the specific aspects of MLC NAND flashes:
- MLC NAND flashes are more "faulty" than SLC, so they use stronger ECC codes which occupy whole OOB area; this is not a problem for UBI/UBIFS, because neither UBIFS nor UBI use OOB area;
- when the data is written to an eraseblock, it has to be written sequentially, from the beginning of the eraseblock to the end of it; this is also not a problem because it is exactly what UBI and UBIFS do (see also this section);
- MLC flashes have rather short eraseblock life-cycle of just few thousand of erase cycles; and this is not be a problem, because UBI uses deterministic wear-leveling algorithm (see this section).
How to mount UBIFS?
UBIFS mounts UBI volumes, not UBI devices, not MTD devices. There are no
block device nodes corresponding to UBI volumes and UBIFS uses device-less
mount, just like procfs or sysfs. The volume to
mount is specified using ubiX_Y or ubiX:NAME
syntax, where
X- UBI device number;Y- UBI volume number;NAME- UBI volume name.
For example,
$ mount -t ubifs ubi0_0 /mnt/ubifs
mounts volume 0 on UBI device 0 to /mnt/ubifs, and
$ mount -t ubifs ubi0:rootfs /mnt/ubifs
mounts "rootfs" volume of UBI device 0 to /mnt/ubifs ("rootfs"
is volume name). This method of specifying UBI volume is more preferable
because it does not depend on volume number.
Note, if X is not specified, UBIFS uses 0, i.e.,
"ubi0:rootfs" and "ubi:rootfs" are
equivalent.
Some environments like busybox are confused by the ":" delimiter (e.g.,
ubi:rootfs) and "!" may be used instead (e.g.,
ubi!rootfs).
The following is an example of the kernel boot arguments to attach
mtd0 to UBI and mount volume "rootfs":
ubi.mtd=0 root=ubi0:rootfs rootfstype=ubifs
Please, see this section for information about how to create UBI devices and this section for information about how to create UBI volumes.
How to create an UBIFS image?
Creating UBIFS images might be a little trickier than creating JFFS2 images. First of all, you have to understand that UBIFS works on top of UBI which works on top or MTD which basically represents your raw flash. This means, that if you need to create an image which would be flashed to the raw flash, you should first create an UBIFS image, than UBI image. In other words, the process has 2 steps.
However, as described here,
UBI has a volume update facility and there is an
ubiupdatevol utility
which for this. So you may update UBI volumes straight form your
running system as well. In this case, you only need UBIFS image, and you do not
have to make the UBI image, i.e., the process has only 1 step in this case.
So, there are 2 utilities:
mkfs.ubifswhich creates UBIFS images;ubinizewhich creates UBI images.
And depending on the needs you use either mkfs.ubifs or
mkfs.ubifs plus ubinize. Choose the former if you are
going to upload the update UBIFS image on your target and then update the UBI
volume using ubiupdatevol. Choose the latter if you are going to
flash the image to raw flash, e.g., at the factory.
The UBI and UBIFS images depend on parameters of the flash they are going to be used on. Namely, you have to know the following characteristics of the flash before creating images:
- flash size;
- physical eraseblock size;
- minimum flash input/output unit size;
- for NAND flashes, sub-page size;
- logical eraseblock size.
This section explains the above parameters and gives some hints about how to find them out.
And optionally, you should decide which compression algorithm you would want
to use for this file-system. UBIFS supports zlib and LZO (default) at the
moment. Generally, zlib compresses better, but it is slower on both compression
and decompression. So this is a trade-off between space savings and speed. The
best idea is to try both and choose the one which is more appropriate for you.
But if flash space is not a big issue, it is recommended to use LZO.
Alternatively, the compression may be switched off. See "-x"
option of the mkfs.ubifs utility.
There are other advanced file-system and UBI characteristics which may be altered with the tools. Use them only if you understand what they do.
The below example demonstrates how to create an UBI/UBIFS image for a 256MiB SLC OneNAND flash chip with 128KiB physical eraseblocks, 2048 bytes NAND pages, and which supports 4 sub-pages (this means that it allows to do 4x512 bytes writes to the same NAND page, which is quite typical for SLC flashes). The resulting image will have one UBI volume storing UBIFS file-system.
$ mkfs.ubifs -r root-fs -m 2048 -e 129024 -c 2047 -o ubifs.img $ ubinize -o ubi.img -m 2048 -p 128KiB -s 512 ubinize.cfg
where ubinize.cfg contains:
$ cat ubinize.cfg [ubifs] mode=ubi image=ubifs.img vol_id=0 vol_size=200MiB vol_type=dynamic vol_name=rootfs vol_alignment=1 vol_flags=autoresize
Some comments about what the options mean:
-r root-fs: tellsmkfs.ubifsto create an UBIFS image which would have identical contents as the localroot-fsdirectory has;-m 2048: tellsmkfs.ubifsthat the minimum input/output unit size of the flash this UBIFS image is created for is 2048 bytes;-e 129024: logical eraseblock size of the UBI volume this image is created for;-c 2047: specifies maximum file-system size in logical eraseblocks; this means that it will be possible to use the resulting file-system on volumes up to this size (less or equivalent); so in this particular case, the resulting FS may be put on volumes up to about 251MiB (129024 multiplied by 2047);-p 128KiB: tellsubinizethat physical eraseblock size of the flash chip the UBI image is created for is 128KiB (128 * 1024 bytes);-s 512: tellsubinizethat the flash supports sub-pages and sub-page size is 512 bytes;ubinizewill take this into account and put the VID header to the same NAND page as the EC header.
The ubinize utility requires volumes description file which has
standard .ini file syntax. One UBI image may contain many UBI
volumes with different characteristics and it is difficult to invent a nice
command-line interface for specifying those characteristics. This is why the
utility requires the volumes description file.
The example configuration file tells ubinize to create an UBI
image which has a singe 200MiB dynamic volume with ID 0, name "rootfs", and
alignment 1. The configuration file also sets the "autoresize" volume flag,
which means that the volume will be automatically enlarged by UBIFS to have
the maximum possible size when it runs for the first time. See
here for more information about what
is the auto-resize trick.
Run ubinize -h and mkfs.ubifs -h for more
information and for more possibilities to tweak images.
Here is one more example for a 32MiB NOR flash with 128KiB physical eraseblock size.
$ mkfs.ubifs -r root-fs -m 1 -e 130944 -c 255 -o ubifs.img $ ubinize -o ubi.img -m 1 -p 128KiB ubinize.cfg
where ubinize.cfg contains:
$ cat ubinize.cfg [ubifs] mode=ubi image=ubifs.img vol_id=0 vol_size=30MiB vol_type=dynamic vol_name=rootfs vol_alignment=1 vol_flags=autoresize
And one more example for a 512MiB MLC NAND flash with 128KiB physical eraseblock size, 2048 bytes NAND page size and no sub-page write support.
$ mkfs.ubifs -r root-fs -m 2048 -e 126976 -c 4095 -o ubifs.img $ ubinize -o ubi.img -m 2048 -p 128KiB ubinize.cfg
where ubinize.cfg contains:
$ cat ubinize.cfg [ubifs] mode=ubi image=ubifs.img vol_id=0 vol_size=450MiB vol_type=dynamic vol_name=rootfs vol_alignment=1 vol_flags=autoresize
How to find out LEB size, sub-page size, etc?
When creating UBI/UBIFS images it is necessary to know:
- physical eraseblock size;
- minimum input/output unit size;
- sub-page size;
- logical eraseblock size.
Physical eraseblock (PEB) size should probably be found out in the flash manual.
Minimum I/O unit size is NAND page size in case of NAND flash and should also be found out in the flash manual (it is 512, 2048 or 4096 at some modern NANDs). For NOR flashes it is 1, unless you have a (rare) ECC-NOR flash which may have 16 byte minimal I/O unit size.
Sub-page size is relevant only for some NAND flashes which allow several (usually 2 or 4) writes to the same NAND page. For example, many SLC NAND flashes have this. UBI utilizes this feature if it is available to waste less flash space. Typically, sub-page size if 256 in case of 512 bytes NAND page and 512 in case of 2048 bytes NAND pages. MLC NAND flashes typically have no sub-pages.
Note, sub-page is an MTD term, but this is also referred to as "NOP" which stands for "number of partial programs". So NOP1 NAND flashes have no sub-pages (or you may treat this as sub-page size is equivalent to NAND page size), NOP 2 NAND flashes have 2 sub-pages (half a NAND page each), NOP 4 flashes have 4 sub-pages (quarter of a NAND page each).
Logical eraseblock (LEB) size is basically determined by the above 3 parameters. Here are the most typical configurations:
- NOR flash with 1 byte min. I/O unit size: LEB size is PEB size minus 128;
- NAND flash with 512 byte NAND page and 265 byte sub-page: LEB size is PEB size minus 512;
- NAND flash with 2048 byte NAND page and 512 byte sub-page: LEB size is PEB size minus 2048;
- NAND flash with 2048 byte NAND page and no sub-page: LEB size is PEB size minus 4096.
Please, refer this section for additional information.
Unfortunately, MTD does not expose information via sysfs and it is a bit
tricky to find the above characteristics out for an existing MTD device (one
would need to use an ioctl). Moreover, the sub-page size is not
exposed to the user-space at all (just because nobody implemented this).
The easiest way to find this out is to attach your MTD device to UBI and glance to the syslog/dmesg output. The newest UBI prints something like this:
UBI: physical eraseblock size: 131072 bytes (128 KiB) UBI: logical eraseblock size: 129024 bytes UBI: smallest flash I/O unit: 2048 UBI: sub-page size: 512
Note, if sup-page size was not printed, it does not exist. Older UBI implementation do not print sub-page size, but they print VID header offset, which is by default equivalent to sub-page size:
UBI: VID header offset: 512 (aligned 512)
Is it OK to mount empty UBI volumes?
Yes, it is OK to mount empty UBI volumes, i.e. the volumes which contain only 0xFF bytes. In this case UBIFS formats the media automatically with default parameters (journal size, compression, etc). But generally, this feature should have limited use, and a proper UBIFS image should preferably be flashed created (see this section).
Note, UBI has similar property and it automatically formats the flash media
if it is empty (see here). So if
there is an mtd0 M`TD device, the following will work:
# Wipe the MTD device out. Note, we could use flash_eraseall, but we do not # want to loose erase counters ubiformat /dev/mtd0 # Load UBI module modprobe ubi # Attach mtd0 to UBI - UBI will detect that the MTD device is # empty and automatically format it. This command will also create # UBI device 0 and udev should create /dev/ubi0 node ubiattach /dev/ubi_ctrl -m 0 # Create an UBI volume - the created volume will be empty ubimkvol /dev/ubi0 -N test_volume -s 10MiB # Mount UBIFS - it will automatically format the empty volume mount -t ubifs ubi0:test_volume /mnt/ubifs
It is also possible to wipe out an existing UBIFS volume represented
by /dev/ubi0_0 using the following command:
ubiupdatevol /dev/ubi0_0 -t
How to use UBIFS with nandsim?
The same way as with any MTD device. Here is an example of how to load
nandsim, create an UBI volume and mount it.
# Create an 256MiB emulated NAND flash
modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa \
third_id_byte=0x00 fourth_id_byte=0x15
# MTD is not LDM-enabled and udev does not create device
# MTD device nodes automatically, so create /dev/mtd0
mknod /dev/mtd0 c 90 0
# Load UBI module and attach mtd0
modprobe ubi mtd=0
# Create a 200MiB UBI volume with name "ubifs-vol"
ubimkvol /dev/ubi0 -N ubifs-vol -s 200MiB
# Mount UBIFS
mount -t ubifs ubi0:ubifs-vol /mnt/ubifs
For more information about nandsim see here.
What is the purpose of --max-leb-cnt mkfs.ubifs option?
It is a form of specifying file-system size. But instead of specifying the
exact file-system size, this option defines the maximum file-system
size (more strictly, maximum UBI volume size). For example, if you use
--max-leb-cnt=200 mkfs.ubifs option, than it will be
possible to put the resulting image to smaller UBI volume and mount it. But if
the image is put to a larger UBI volume, the file-system will anyway take only
first 200 LEBs, and the rest of the volume will be wasted.
Note, the --max-leb-cnt option does not affect the size of the
resulting image file, which depends only on the amount of data in the
file-system. mkfs.ubifs just writes the --max-leb-cnt
value to the file-system superblocks.
This feature is quite handy on NAND flashes, because they have random amount of initial bad eraseblocks (marked as bad in production). This means, that different devices may have slightly different volume sizes (especially if the UBI auto-resize feature is used). So you may specify the maximum possible volume size and this will guarantee that the image will work on all devices, irrespectively on the amount of initial bad eraseblocks.
Fundamentally, mkfs.ubifs has to know file-system size because
UBIFS maintains and stores per-LEB information (like amount of dirty and free
space in each LEB) in so-called LPT area on the media. So obviously, the size of
this area depends on the total amount of LEBs, i.e. on the volume size. Note,
various characteristics of the LPT B-tree depend on the LPT area size, e.g., we
use less bits in LPT tree keys of smaller LPT area. So do not use unnecessarily
large --max-leb-cnt value to achieve better performance.