Since I usually run my images from my NFS server instead of from flash, I found myself quite often having to extract e2jffs2.img files. So, I found a script which did most of what I wanted and made some mods. I don't remember where I found the original script (was a couple of years ago...) otherwise, I would give the original author credit.
The first script mounts the e2jffs2.img file using a loop device and copies the filesystem to a local directory. Then copies the uImage file to the boot directory in the extracted filesystem. (My NFS boot scripts automatically look for the kernel there...)
Usage: ./extract_jffs2.sh e2jffs2.img Destination_Dir
(Full or relative paths can be used)
Example: Assuming you have unzipped/untarred the image to /home/user/download/image_x/ and you want to extract it to /data/alien/images/new_image:
./extract_jffs2.sh /home/user/download/image_x/enigma2/e2jffs2.img /data/alien/images/new_image
#!/bin/bash
## Script to extract e2jffs2.img filesystem using mtd kernel modules.
if [[ $# -lt 1 ]]
then
echo "Usage: $0 e2jffs2.img Destination_Dir"
echo "(Full or relative paths can be used)"
exit 1
fi
if [ '$(whoami)' != 'root' ]
then
echo "$0 must be run as root!"
exit 1
fi
if [[ ! -e $1 ]]
then
echo "$1 does not exist"
exit 1
fi
if [[ -d $2 ]]
then
echo "$2 already exists!!"
exit 1
else
mkdir $2
fi
if [[ ! -d $2 ]]
then
echo "$2 is not a valid extract directory"
exit 1
fi
# cleanup if necessary
umount /tmp/e2jffs2 &>/dev/null
umount /tmp/mtdblock0 &>/dev/null
modprobe -r jffs2 &>/dev/null
modprobe -r block2mtd &>/dev/null
modprobe -r mtdblock &>/dev/null
sleep 0.25
losetup -d /dev/loop1 &>/dev/null
sleep 0.25
modprobe loop || exit 1
losetup /dev/loop1 "$1" || exit 1
modprobe block2mtd || exit 1
modprobe jffs2 || exit 1
if [[ ! -e /tmp/mtdblock0 ]]
then
mknod /tmp/mtdblock0 b 31 0 || exit 1
fi
echo "/dev/loop1,128KiB" > /sys/module/block2mtd/parameters/block2mtd
if [[ ! -e /tmp/e2jffs2 ]]
then
mkdir /tmp/e2jffs2
fi
mount -t jffs2 -o ro /tmp/mtdblock0 /tmp/e2jffs2 || exit 1
(cd /tmp/e2jffs2; tar cf - .) | (cd "$2" ; tar xpf -) || exit 1
# cleanup if necessary
#umount /tmp/jffs2 &>/dev/null
#umount /tmp/mtdblock0 &>/dev/null
#modprobe -r jffs2 &>/dev/null
#modprobe -r block2mtd &>/dev/null
#modprobe -r mtdblock &>/dev/null
umount /tmp/e2jffs2
#umount /tmp/mtdblock0
modprobe -r jffs2
modprobe -r block2mtd
modprobe -r mtdblock
sleep 0.25
#losetup -d /dev/loop1 &>/dev/null
losetup -d /dev/loop1
sleep 0.25
rmdir /tmp/e2jffs2
rm /tmp/mtdblock0
cp $(dirname $1)/uImage $2/boot/
echo "Successfully extracted $1 to $2"
exit 0
Display More
Second script is for re-packing backup images. It mounts the backup e2jffs2.img file using a loop device and then uses mkfs.jffs2 to create a new image file.
Usage: ./repack_e2jffs2.sh e2jffs2.img
(Full or relative paths can be used)
Example: Assuming you have unzipped/untarred the backup image to /home/user/download/image_x/
It will rename the original e2jffs2.img file to e2jffs2.orig and create a new repacked e2jffs2.img file.
#!/bin/bash
## Script to repack e2jffs2.img filesystem using mtd kernel modules.
if [[ $# -lt 1 ]]
then
echo "Usage: $0 e2jffs2.img"
echo "(Full or relative paths can be used)"
exit 1
fi
if [ '$(whoami)' != 'root' ]
then
echo "$0 must be run as root!"
exit 1
fi
if [[ ! -e $1 ]]
then
echo "$1 does not exist"
exit 1
fi
# cleanup if necessary
umount /tmp/e2jffs2 &>/dev/null
umount /tmp/mtdblock0 &>/dev/null
modprobe -r jffs2 &>/dev/null
modprobe -r block2mtd &>/dev/null
modprobe -r mtdblock &>/dev/null
sleep 0.25
losetup -d /dev/loop1 &>/dev/null
sleep 0.25
modprobe loop || exit 1
losetup /dev/loop1 "$1" || exit 1
modprobe block2mtd || exit 1
modprobe jffs2 || exit 1
if [[ ! -e /tmp/mtdblock0 ]]
then
mknod /tmp/mtdblock0 b 31 0 || exit 1
fi
echo "/dev/loop1,128KiB" > /sys/module/block2mtd/parameters/block2mtd
if [[ ! -e /tmp/e2jffs2 ]]
then
mkdir /tmp/e2jffs2
fi
mount -t jffs2 -o ro /tmp/mtdblock0 /tmp/e2jffs2 || exit 1
mkfs.jffs2 --root=/tmp/e2jffs2/ --output=/tmp/mtd_root.bin --eraseblock=128KiB --pad --no-cleanmarkers || exit 1
sumtool --pad --eraseblock=128KiB --input=/tmp/mtd_root.bin --output=$(dirname $1)/mtd_root.sum.bin || exit 1
# cleanup if necessary
#umount /tmp/jffs2 &>/dev/null
#umount /tmp/mtdblock0 &>/dev/null
#modprobe -r jffs2 &>/dev/null
#modprobe -r block2mtd &>/dev/null
#modprobe -r mtdblock &>/dev/null
umount /tmp/e2jffs2
#umount /tmp/mtdblock0
modprobe -r jffs2
modprobe -r block2mtd
modprobe -r mtdblock
sleep 0.25
#losetup -d /dev/loop1 &>/dev/null
losetup -d /dev/loop1
sleep 0.25
rmdir /tmp/e2jffs2
rm /tmp/mtdblock0
rm /tmp/mtd_root.bin
mv $1 $1.orig && mv $(dirname $1)/mtd_root.sum.bin $1 || exit 1
echo "Successfully repacked $1"
exit 0
Display More
I use Debian and already had all tools installed, so I am not sure exactly which packages you will need. I know for sure mtd-utils is necessary. Of course it depends on which Linux distribution you are using.
You can try running the scripts with:
bash -x extract_e2jffs2.sh /home/user/download/image_x/enigma2/e2jffs2.img /data/alien/images/new_image
to see what is going on if there is a problem.
The scripts are not fool-proof and not much error checking is done, so take care with what you are doing...