Wednesday, November 9, 2011

Mount jffs2 image in your development system

Devices with embedded linux quite often have root file-system stored in flash-memory. And quite often it is whether jffs2 or yaffs2. For partitions of small size (say < 100-200 Mb) one may easily use jffs2, while for larger partitions it is recommended to use yaffs2 because of its better scalability (smaller RAM requirements and better performance).
Another big advantage of jffs2 over yaffs2 is its native support in mainline linux kernel. For sure yaffs2 patch could be easily applied to your kernel tree it is always very nice to have a feature in mainline.

So basically you prepare proper file-system image and put it on MTD-device of your embedded board.
And sometimes you need to get some data from prepared file-system image or even modify it on your host development machine.

I found a nice bash script on Maemo Wiki http://wiki.maemo.org/Modifying_the_root_image#Shell_script_to_mount.2Funmount_JFFS2_using_block_device_emulating_MTD
But since I have kernel of version 3.0 on my machine I decided to remove parts for 2.4 kernel (corresponding branches were used for 3.0 kernel as well because of improper tests that were not written for 3.0 obviously). And also I added automatic pick-up of an empty loop device.
So here's a very helpful script for mounting jffs2 image:

#!/bin/sh
JFFSIMG=$1 # jffs image
LOOP=$(losetup --find) # loop device
MP="/media/jffs2" # mount point
MTDBLOCK="/tmp/mtdblock0" # MTD device file
BLKMTD="block2mtd"
UMNT=""
 
echo "$0" | grep unmount_ >/dev/null 2>&1
[ $? -eq 0 ] && UMNT=1
if [ $# -gt 1 -a x"$2"x = x"unmount"x ]; then
  UMNT=1
fi
  
if [ x"${UMNT}"x = x""x ]; then
  if [ ! -b ${MTDBLOCK} ] ; then
    mknod ${MTDBLOCK} b 31 0 || exit 1
  fi
  lsmod | grep loop >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    modprobe loop
    [ $? -ne 0 ] && echo "loopback loading failed" && exit 1
    sleep 1
  fi
  losetup ${LOOP} ${JFFSIMG} || exit 1
  sleep 1
  modprobe mtdblock
  modprobe ${BLKMTD} || exit 1
  echo "${LOOP}" > /sys/module/block2mtd/parameters/block2mtd
  sleep 1
  modprobe jffs2
  [ ! -d ${MP} ] && mkdir -p ${MP}
  mount -t jffs2 ${MTDBLOCK} ${MP} || exit 1
else
  umount ${MP}
  if [ $? -ne 0 ]; then
    echo "Cannot unmount JFFS2 at $MP" && exit 1
  fi
  modprobe -r jffs2
  modprobe -r ${BLKMTD}
  modprobe -r mtdblock
  sleep 1
  losetup -d ${LOOP}
fi

3 comments:

  1. Thanks! This saved my day! I got the original but it didn't work cause the kernel checks I guess.

    ReplyDelete
  2. I wanted to thank you for this great read. Your blog is one of the finest blogs . Thanks for posting this informative article.app development company in bhopal

    ReplyDelete
  3. It is a very nice blog. Really it is a very international destination post. Thanks for sharing this post.
    Website development company in Bangladesh

    ReplyDelete