Файловые система
Пожалуйста, прочтите также про → flash.layout. Существует 2 типа flash(флэш)-памяти: NOR flash и NAND flash. Кроме того вам обязательно надо прочитать mtd
.
mini_fo
The (mini fanout overlay file system) – перенаправляет модифицированные операции в записываемое пространство названное “каталог хранения”, оставляет оригинальные данные в основном каталоге незатронутыми. При считывании, файловая система смешивает модифицированные данными и оригинальные так, чтобы только наиболее новая версия была показана.
- была заменена overlayfs, смотрите dev
overlayfs
SquashFS
SquashFS только для чтения сжатая файловая система. Когда доступна gzip, OpenWrt использует LZMA для сжатия. Поскольку файловая система SquashFS только для чтения, она не нужна для ?выравнивания? данных, то это позволяет ей упаковывать лучше, таким образом файлы занимают меньше пространства чем JFFS2 (20-30% лучше чем JFFS2)!
- + занимают наиболее меньше места, чем это возможно
- + позволяют выполнения FailSafe для восстановления, так как нельзя записывать в нее
- - только для чтения
- - waste space, since each time a file contained on it is modified, actually a copy of it is being copied to the second (JFFS2) partition
JFFS2
JFFS2 is a writable compressed filesystem with journaling and wear leveling using LZMA for the compression.
- + is writable, has journaling and wear leveling
- + is cool
- - is compressed, so a program (
opkg
in particularly) cannot know in advance how much space a package will occupy
UBIFS
YAFFS2
CramFS
CramFS is not utilized by OpenWrt, but the DV4210 (Livebox) uses it.
ext2
- + a program (
opkg
in particularly) knows how much space is left! - + good ol' veteran FOSS file system
- - no journaling
- - no wear leveling
- - no transparent compression
tmpfs
/tmp
resides on a tmpfs-partition and/var
is a symlink to it;/dev
resides on a little tmpfs partition of its own- + no wear leveling
- - volatile (doesn't survive a reboot)
Implementation in OpenWrt
As you can see in the article flash.layout OpenWrt uses a combination of both filesystem, SquashFS and JFFS2, at the same time! In addition, the Kernel is on neither of both filesystems! In the generation process of the firmware (see imagebuilder) the Kernel image is first packed with LZMA, then the obtained file is packed with gzip and then this file will be written onto the raw flash without being part of any filesystem!
Boot process
System bootup is as follows: →process.boot
- kernel boots from SquashFS and runs
/etc/preinit
/etc/preinit
runs/sbin/mount_root
mount_root
mounts the JFFS2 partition (/jffs
) and combines it with the SquashFS partition (/rom
) to create a new virtual root filesystem (/
)- bootup continues with
/sbin/init
Explanation
Both SquashFS and JFFS2 are compressed filesystems using LZMA for the compression. SquashFS is a read only filesystem while JFFS2 is a writable filesystem with journaling and wear leveling.
Our job when writing the firmware is to put as much common functionality on SquashFS while not wasting space with unwanted features. Additional features can always be installed onto JFFS2 by the user. The use of mini_fo
means that the filesystem is presented as one large writable filesystem to the user with no visible boundary between SquashFS and JFFS2 -- files are simply copied to JFFS2 when they're written.
It's not all without side effects however -
The fact that we pack things so tightly in flash means that if the firmware ever changes, the size and location of the JFFS2 partition also changes, potentially wiping out a large chunk of JFFS2 data and corrupting the filesystem. To deal with this, we've implemented a policy that after each reflash the JFFS2 data is reformatted. The trick to doing that is a special value, 0xdeadc0de
; when this value appears in a JFFS2 partition, everything from that point to the end of the partition is wiped. So, hidden at the end of the firmware images, is the value 0xdeadcode, positioned such that it becomes the start of the JFFS2 partition.
The fact that we use a combination of compressed and partially read only filesystems also has an interesting effect on package management:
In particular, you need to be careful what packages you update. While opkg
is more than happy to install an updated package on JFFS2, it's unable to remove the original package from SquashFS; the end result is that you slowly start using more and more space until the JFFS2 partition is filled. The opkg util really has no idea how much space is available on the JFFS2 partition since it's compressed, and so it will blindly keep going until the opkg system crashes -- at that point you have so little space you probably can't even use opkg to remove anything.
Explanation 2
On many embedded targets that use NAND flash for the root filesystem, OpenWrt implements a clever trick to get the most out of the limited flash memory capacity while retaining flexibility for the end-user:
Basically, during the image creation, all of the rootfs contents is packed up in a SquashFS filesystem -- a highly efficient filesystem with compression support. There's one important detail about it though: it is a read-only filesystem. To overcome this limitation OpenWrt uses the remaining portion of the NAND rootfs partition to store an additional read/write jffs2 filesystem which is “overlayed” on top of the rootfs (that is, allowing to read unchanged files from the SquashFS but storing all the modifications made to the jffs2 part).
This design has another important advantage for the end-user: even when the read/write partition is in total mess, he can always boot to the failsafe mode (which mounts only the squashfs part) and proceed from there.
Technical Details
The kernel boot process involves discovering of NAND partitions (what is a “NAND partition”?) and it can be done by various target-dependent means:
- some bootloaders store a partition table at a known location
- some pass the partition layout via kernel command line
- some targets require specifying the kernel command line at the compile time (thus overriding the one provided by the bootloader).
Either way, if there is a partition named rootfs
and MTD_ROOTFS_ROOT_DEV
kernel config option is set to yes
, this partition is automatically used for the root filesystem.
After that, if MTD_ROOTFS_SPLIT
is enabled, the kernel adjusts the rootfs
partition size to the minimum required by the particular SquashFS image and automatically adds rootfs_data
to the list of the available mtd partitions setting its beginning to the first appropriate address after the SquashFS end and size to the remainder of the original rootfs
partition. The resulting list is stored in RAM only, so no partition table of any kind gets actually modified.
For more details please refer to the actual patch at: https://dev.openwrt.org/browser/trunk/target/linux/generic/patches-2.6.37/065-rootfs_split.patch
For overlaying a special mini_fo
filesystem is used, the README
is available from the sources at
https://dev.openwrt.org/browser/trunk/target/linux/generic/patches-2.6.37/209-mini_fo.patch
: Please feel free to merge Explanation 1 with Explanation 2 |
Can we switch the filesystem to be entirely JFFS2?
Note:
: It is possible to contain the entire root filesystem on a JFFS2-Partition only, instead of a combination of both.
The advantage is that changes to included files no longer leaves behind an old copy on the read only filesystem. So you could end up saving space.
The disadvantage of this would be, that you have no failsafe any longer and also, JFFS2 takes significantly more space then SquashFS.
Yes, it's technically possible, but a bit of a mess to actually pull off. The firmware has to be loaded as a trx file, which means that you have to put the JFFS2 data inside of the trx. But, as I said above, the trx has a checksum, meaning that if you ever change that data, you invalidate the checksum. The solution is that you install with the JFFS2 data contained within the trx, and then change the trx-boundaries at runtime. The end result is a single JFFS2 partition for the root filesystem. Why someone would want to do it is beyond me; it takes more space, and while it would allow you to upgrade the contents of the filesystem you would still be unable to replace the kernel (outside of the filesystem), meaning that a seamless upgrade between releases is still not possible! Having SquashFS gives you a failsafe mechanism where you can always ignore the JFFS2 partition and boot directly off SquashFS, or restore files to their original SquashFS versions.
I used to have a trick where I could convert a SquashFS install to a JFFS2 install at runtime by copying all the data onto the SquashFS partition and changing the partition boundaries. I never really had much use for the util -- not to mention it required a rather large flash to store both SquashFS and JFFS2 copies of the root during transition -- so support for it was dropped.
Notes
Example pictures: on formated partition / how data is stored (and addressed on ext3)
- how data is stored and addressed by ext2:
- how data is stored and addressed by ext3:
- how data is stored and addressed by SquashFS:
- how data is stored and addressed by JFFS2: