Skip to content

Instantly share code, notes, and snippets.

@ndunks
Created July 19, 2018 03:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ndunks/b9eb428d2b7add0821405c1cd04fe8ad to your computer and use it in GitHub Desktop.
Save ndunks/b9eb428d2b7add0821405c1cd04fe8ad to your computer and use it in GitHub Desktop.
How to mount UBI image using mtdram kernel module on linux/debian
#!/bin/bash
if [ ! -d files ]; then
# Make the content
echo "Generating root content.."
mkdir files
echo "$(date)" > files/date.txt
echo "Another content" > files/other.txt
fi
#These config are based by Nand device that you will use!
echo ":> Creating ubifs volume test.img from 'files' directory.."
mkfs.ubifs -q -r files -m 1 -e 130944 -c 25 -o files.img
if [ ! -f ubinize.cfg ]; then
echo "Generating ubinize config.."
# Dont forget to check generated files if you edit this!
cat <<END > ubinize.cfg
[ubi_rfs]
mode=ubi
image=files.img
vol_id=0
vol_size=5MiB
vol_type=dynamic
vol_name=testfs
vol_flags=autoresize
END
fi
echo ":> Creating UBI Image as ubi.img contain 1 volume (files.img).."
ubinize -o ubi.img -m 1 -p 128KiB -s 1 ubinize.cfg
# -r root-fs: tells mkfs.ubifs to create an UBIFS image which would have identical contents as the local root-fs directory;
# -m 1: tells mkfs.ubifs that the minimum input/output unit size of the flash this UBIFS image is created for is 1 bytes (mtdram page in this case);
# -e 130944: logical eraseblock size of the UBI volume this image is created for;
# -c 2047: specifies maximum file-system size in logical eraseblocks; this means that it will be possible to use the resulting file-system on volumes up to this size (less or equivalent); so in this particular case, the resulting FS may be put on volumes up to about 2.5MiB (130944 multiplied by 20); See this section for more details.
# -p 128KiB: tells ubinize that physical eraseblock size of the flash chip the UBI image is created for is 128KiB (128 * 1024 bytes);
# -s 1: tells ubinize that the flash supports sub-pages and sub-page size is 1 bytes; ubinize will take this into account and put the VID header to the same NAND page as the EC header.
# Load UBI module
if [ ! -f files.img ]; then
echo "Something wrong.."
exit 1
fi
if [ ! -f ubi.img ]; then
echo "Something wrong.."
exit 1
fi
echo ":> Prepare the NAND Emulator for MTD devices.."
# Make 64MB MTD Device
sudo modprobe mtdram total_size=8192
# erase_size=128
if [ ! -c /dev/mtd0 ]; then
echo ":> Something wrong with nandsim kernel module, mtd block not created."
exit
fi
sudo modprobe ubi
sudo modprobe mtd
echo ":> Formating mtd0 and fill with ubi.img.."
sudo ubiformat /dev/mtd0 -f ubi.img
echo ":> Attach ubi to mtd0.. creating /dev/ubi0"
sudo ubiattach -p /dev/mtd0
echo ":> Mount /dev/ubi0_0"
[ -d mount ] || mkdir mount
sudo mount -t ubifs /dev/ubi0_0 ./mount
if [ "$?" -eq "0" ]; then
echo ":> Files should be mounted on $PWD/mount"
else
echo ":> Mount failed"
fi
if [ ! -f clean.sh ]; then
cat <<END > clean.sh
#!/bin/bash
# CLEAN or unmount script:
sudo umount ./mount
rmdir ./mount
sudo ubidetach -p /dev/mtd0
sudo modprobe -a -r ubifs ubi mtdram mtdblock mtd
# deleted if required
unlink files.img
unlink ubi.img
unlink ubinize.cfg
unlink clean.sh
END
chmod +x clean.sh
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment