Creating a JFFS2 image from a directory structure


Sometimes creating customized rootfs-images via OpenEmbedded is complicated. When changes are minor it is also possible to create a jffs2-image from any directory. Also when using jffs2 partitions in production systems it is advisable to create the image from scratch, because space requirements will be optimized.


You need the following tools:

  • mtd-tools
  • linux development system with nfs


Installation of the needed tools

On Debian and derived distributions just enter

apt-get install mtd-tools

with root rights.

Generating an jffs2-image from a complete directory structure

You have to have all the files you need in a directory. If you for example have a customized rootfs on a sd-card and you now want to create a jffs2-image to store it into flash and boot from it, just mount the sd-card on your development system and create the image hereof. The example given here in this how-to creates an image from the Angstrom tarball distributed with Stamp9g20, store the created image in flash and booting the newly created image. When creating a rootfs, root permissions may be necessary:

mkfs.jffs2 --little-endian --eraseblock=0x20000 -n --pad -d ./jffs2-test -o jffs2-root.img

This creates a jffs2-image image called jffs2-root with the contents of the directory jffs2-test. If you need the root squashing feature (making all files owned by root as described in the Linux guide) add the option --squash. Do not use this feature, if the contents already have the correct ownership.
Next we flash this image to a mtd-partition on our board, here mtd5 on the Stamp9G20:

mount -o nolock,tcp /mnt/net
flash_eraseall /dev/mtd5
Erasing 128 Kibyte @ 1fd60000 -- 100 % complete.
nandwrite /dev/mtd5 /mnt/net/jffs2-root.img
Writing data to block 137 at offset 0x1120000

Done. If you have a NOR-Flash based system use flashcp instead of nandwrite. Now reboot, interrupt booting with u-boot, type run flashboot to boot your newly created image on Stamp9G20 or PortuxG20.
You can also just mount the new partition in your running system: mount -t jffs2 /dev/mtdblock5 /some_mountpoint.