Mounting JFFS2 image files

To check and browse files containing a JFFS2 file-system you can mount the packed file. This shell-script automates the mounting and unmounting procedure.

This approach uses the mtd2block driver which represents a Memory Technology Device file-system such as JFFS2 as an ordinary block devices.
More information about MTDs on Linux.
Emulate an MTD using a block device

#!/bin/sh
JFFSIMG=$1 # jffs image
LOOP="/dev/loop1" # loop device
MP="/media/jffs2" # mount point
MTDBLOCK="/tmp/mtdblock0" # MTD device file
KVER="2.6"
BLKMTD="block2mtd"
UMNT=""
IMAGE_FILE=""

#Maximum IMAGE Size to mount in blocks
MAXIMAGE=67108864
IMG_SIZE=""


#check argument count > 1 and for unmounting
[ $# -lt 1 ] && echo -e "Usage: \n$0 file.jffs2 \nor:\n$0 file.jffs2 umount" && exit 1

echo "$@" | grep unmount >/dev/null 2>&1 
 [ $? -eq 0 ] && UMNT=1 
 if [ $# -gt 1 -a x"$2"x = x"unmount"x ]; then
   UMNT=1 
 else 
 IMAGE_FILE=$1
fi

uname -r | egrep '^2\.6' >/dev/null 2>&1
if [ $? -ne 0 ]; then
  KVER="2.4"
  BLKMTD=blkmtd
fi 

########### Mounting ########### 
if [ x"${UMNT}"x = x""x ]; then

  #chek img IMAGE_FILE & FILE_SIZE 
  IMG_SIZE=$(stat -c%s "$IMAGE_FILE") || exit 1 
  if [ $IMG_SIZE -ge $MAXIMAGE ]; then
      echo "Image $IMAGE_FILE bigger than $MAXIMAGE < $IMG_SIZE. Can't mount it!"
      exit 1
  fi

  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
   if [ x"${KVER}"x = x"2.4"x ]; then
     modprobe ${BLKMTD} device=${LOOP} || exit 1
   else
     modprobe ${BLKMTD} || exit 1
     echo "${LOOP}" > /sys/module/block2mtd/parameters/block2mtd
   fi
   sleep 1
   modprobe jffs2
   [ ! -d ${MP} ] && mkdir -p ${MP}
   mount -t jffs2 ${MTDBLOCK} ${MP} || exit 1
   echo "$IMAGE_FILE mounted at $MP"
 else
########### Un-mounting ########### 
   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 

Note: This scripts originates from https://wiki.maemo.org/Modifying_the_root_image#Mounting_the_JFFS2_image .

Geschrieben in Linux | Kommentare aus

Kommentarfunktion ist deaktiviert

Kommentare sind deaktiviert.