Thursday, October 4, 2012

How to create, format and mount .img files

Now since I am doing some designs for the PirateBox in the future it will be easier if I would make custom image files that could replace the original one. This would make life easier as there would be no need to mount, backup and change the original image file. 

I am experimenting with some html5 videos in my custom PB design just to see how the server and router handles the "pressure". Also after reading some threads on reddit I am keen to try some browser games on my PB, but these will need a larger container than the default img (20MB). So here is a short note on how to handle these img files.
(Also sorry for the colours, it is just to illustrate parts of the code.)

Creating & formatting img files in Ubuntu

1.   Open terminal and type
dd if=/dev/zero of=file.img bs=1M count=50
This will create a file called "file.img" in your home directory with a size of 50 MB.

What is what?
if=/dev/zero => input file, but since we want an empty container (no input file), we use the "universal" term for "nothing" /dev/zero
of=file.img => output file, file name & location
count=50 => Size of the file in MB

2.   After this image needs to be formatted in some file format (ext4, fat32, ntfs and so on; ext3 for the PirateBox). Now you can either use gparted if you want  some nice GUI or you can use mkfs command:
mkfs ext3 -F file.img
(For a quick and easy way to do this, you can use my script imgKreator from GitHub.)

Mounting the img file

I have tried mounting the PirateBox img file with different software, but that didn't work. So after some help through chat and forums here is the command to mount the img file:

sudo mkdir -p /tmp/mount_tmp/ && sudo mount -o loop,rw,sync piratebox/pbIMG_ws.img /tmp/mount_tmp
What is what?
/tmp/mount_tmp/ => Create the folder where the img will be mounted
piratebox/pbIMG_ws.img => Route to PB image file
/tmp/mount_tmp => Mount location

I personally had issues trying to edit anything in the folder so I am running a mounting script with the second line being:
sudo chmod -R ug+rw /tmp/mount_tmp
Which just gives read+write permission to the folder.(Although this worked for me before, when creating a new file container use sudo chmod 777 otherwise the PB will not recognize the files and although connection to the network can be made, no website will load). After this editing is simple.

To unmount:
sudo unmount piratebox/pbIMG_ws.img

PirateBox: If interested here is a script that will allow you to quickly mount and unmount pbIM_ws.img file that contains the www folder and the UI/html files.


  1. I'm new to linux and openWRT, but I know enough to navigate directories from the command line, copy files and so on. What exactly is an image file good for? Are you "mounting" it so that you can, for instance, download an img file and put it on a PB, and thus have an entire directory structure, web pages and so forth already preconfigured? So it's sort of like a fake storage device or whatever that's actually an IMG file?

    By the way, I really like the look of your Pirate Box. I have another router on order and will try yours out when I get it. Right now I'm afraid of breaking the one I just built.

  2. Hello Damon,
    yes you could say that such a raw image is just like a "fake storage". DD creates an image of all the input files that can be files, folders or enitre partitions (backup function), these are then "merged" into an image that is a single file, but contains the entire file strucure of the say folder you have put in it. The contents of this image cannot be accessed directly, but you have to mount it manuall first (like you would mount an external drive or USB drive, but the OS does it automatically for you upon connecting).
    After mounting the img file, the contained file strucure appears where you mounted it and you can access files and documents in it.
    You can read more about dd here for example:

    And yes you though right, the whole idea behind this is that you could have an unlimited number of images on your PirateBox each having different web designs etc. in them. Swapping them out is just as easy as naming them, so for exampple:
    The file that the PirateBox will use is USB/piratebox/pbIMG_ws.img. If you have 1 other image file you can just call it pbIMG_ws_alternative.img and leave it next to the other file. Swap the names around and you can use instantly the contents of the other image file.

    This is the next step I am going to implement in my design yo you can just download an *.img file and use that without having to touch the original one.

    I am happy you like my design. If you want to test it out, you don't even have to touch your box, only the pendrive! Simply make a backup of the USB/piratebox/pbIMG_ws.img file either on your hard drive or just in the same folder and call it say USB/piratebox/pbIMG_ws_backup.img. You can then mount the original pbIMG_ws.img file and put my Design files in the www folder (original img files is backed up). Dismount the file, plug in the USB to the router and its good to go. If you feel like changing back to the original site, just swap the names back.