Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to mount UBI image using mtdram kernel module on linux/debian
if [ ! -d files ]; then
# Make the content
echo "Generating root content.."
mkdir files
echo "$(date)" > files/date.txt
echo "Another content" > files/other.txt
#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
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
if [ ! -f ubi.img ]; then
echo "Something wrong.."
exit 1
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."
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"
echo ":> Mount failed"
if [ ! -f ]; then
cat <<END >
# 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
chmod +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.