April 13

Android: Mount an image file

Sometimes you may wish to put multiple files into a single image file.
This makes moving around a large number of files more efficient.

Eg. I use an image file to hold an entire html version of technical references.
Rather than adding over 28,000 files to my Android phones file system,
I can put a single file (virtual file system) on my phone that houses all of these files.
When needed, I simply mount the single file as a drive and have full access to my technical guides.

To use this script, your Android device will need to be rooted with busybox installed.
*** I can not guarantee this will work on all devices.  It is working fine on my HTC phone.

How to:
1. On your phones SDCARD create a folder named “mnt”
mkdir /sdcard/mnt

2. Create a folder within with name of you image file.  Do not include the .img extension.
mkdir /sdcard/mnt/yourimagefilename

3. Create another folder with a name of your choice and put your .img file into this folder.
mkdir /sdcard/images

4. Create an empty file in this image folder called img.sh, copy and save the script below into this file.
touch img.sh
Use your favorite editor to paste in the script.

5. From a terminal session go to /sdcard/images
cd /sdcard/images

6. Type: su

7. Type: sh img.sh
If the script does not run type: chmod 777 img.sh and try running it again.

8. You will be prompted for the name of your image file. Enter the name without the .img extension.

9. Your image should now be mounted.  To check:
cd /sdcard /mnt/yourimagefilename

10.  To unmount your image type: sh /sdcard/images/uimg.sh

 

Copy and paste everything below this line:

echo “Before running this script create a directory for your image in /sdcard/mnt”
echo -n “Enter the image file name (Do not include .img): ”
read userdata
##########################################
#Check for root                          #
##########################################
perm=$(id|cut -b 5)
if [ “$perm” != “0” ];then echo “This script requires root! Type: su”; exit; fi
mount -o remount,rw /dev/block/mmcblk0p5 /system
##########################################
#Set up veriables                        #
##########################################
export kit=$(dirname $0)
export bin=/system/bin
export mnt=/sdcard/mnt/$userdata
if [[ ! -d $mnt ]]; then mkdir $mnt; fi
export PATH=$bin:/usr/bin:/usr/local/bin:/usr/sbin:/bin:/usr/local/sbin:/usr/games:$PATH
##########################################
#Set up loop device                      #
##########################################
if [ -b /dev/block/loop250 ]; then
echo “Loop device exists”
else
busybox mknod /dev/block/loop250 b 7 250
fi
busybox losetup /dev/block/loop250 $kit/$userdata”.img”
##########################################
#Mount all required partitions           #
##########################################
busybox mount -t ext2 /dev/block/loop250 $mnt
##########################################
#Create a script file to unmount the image          #
##########################################
echo “umount /sdcard/mnt/”$userdata > uimg.sh
echo “losetup -d /dev/block/loop250” >> uimg.sh

#By: nighthawk

 


Copyright 2021. All rights reserved.

Posted April 13, 2012 by Timothy Conrad in category "Android

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B