Building A Linux Filesystem From An Ordinary File

First, create a 20MB file (you could use any size for your FS) by executing the following command:

# dd if=/dev/zero of=disk-image count=40960
40960+0 records in
40960+0 records out
20971520 bytes (21 MB) copied, 0.225793 s, 92.9 MB/s

Next, to format this as an ext3 filesystem, you just execute the following command:
# /sbin/mkfs -t ext3 -q disk-image
disk-image is not a block special device.
Proceed anyway? (y,n)

Next, you need to create a directory that will serve as a mount point for the loopback device.
# mkdir fs

Now mount the this filesystem (file)
# mount -o loop=/dev/loop0 disk-image fs

Check if your FS is mounted
# mount
/dev/hda2 on / type ext3 (rw,noatime,acl,user_xattr)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
debugfs on /sys/kernel/debug type debugfs (rw)
udev on /dev type tmpfs (rw)
devpts on /dev/pts type devpts (rw,mode=0620,gid=5)
/dev/hda5 on /mnt/D type vfat (rw)
/dev/hda1 on /mnt/C type ntfs (rw)
/dev/hda7 on /cache type ext3 (rw,noatime)
/root/disk-image on /root/fs type ext3 (rw,loop=/dev/loop0)

Now, move to the mounted directory and create some directory and files on this new FS which we have created using file.
# ll
total 14
drwx—— 2 root root 12288 Apr 25 23:30 lost+found
drwxr-xr-x 2 root root 1024 Apr 25 23:33 vij
drwxr-xr-x 2 root root 1024 Apr 25 23:33 vijay


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s