Saturday, April 9, 2011

Create a simple linux disk image file

Let's consider the next problem. We have a Linux system installed on a computer or a virtual machine and the Linux partitions size is just around of 10 GB. We need an important disk space to compile a huge code source line Android for example, or a Linux distribution. 
The Linux system occupy around of 6 GB and 3-4 GB of free space is not enough for our needs. So to create a new partition of 10 GB would be great. But don't have empty space to create this partition. If the system is installed on a Virtual machine, we can create a new Virtual Hard Disk.
But we have a lot of free disk space on a external or internal hard disk formatted on NTFS for example. We can mount NTFS partitions on Linux but we can't use its to builds sources because usually the build system need a Linux partition. 
So, the best solution in our case is to create a Linux disk image file and format it using a Linux file system like ext3 or ext3.
First we have to create a file having the size of new partition and filled with 0:

dd if=/dev/zero of=./build_partition.img bs=1MiB count=200

The disk.img file of 200 MB size should be created.

Next we have to build the ext3 Linux file system on the created file:

mkfs.ext3 ./build_partition.img

Now we can mount the new partition:

sudo mount -oloop ./build_partition.img ./mnt/build_partition/

Or unmount the partition:

sudo umount /dev/loop0

No comments:

Post a Comment