Walker News

Create Linux Loopback File System On Disk File

Powerful, free-of-charge, open-source Linux operating system.Can you create a Linux file system without using partition DIRECTLY – sort of file system within file system management?

The answer is certainly YES!

With a loopback device in Linux (a feature that’s not natively available in Windows Vista and its predecessors) one can easily create a Linux loopback file system on a regular disk file, and not directly using a disk partition!
How to create a Linux loopback file system with a regular disk file?
To complete this Linux tricks, you need to login with a root user ID for all the steps given below
  1. Type dd if=/dev/zero of=/virtualfs bs=1024 count=30720 to create a 30MB disk file (zero-filled) called virtualfs in the root (/) directory
     
  2. Type losetup /dev/loop0 to confirm that the current system is not using any loopback devices. Replace /dev/loop0 with /dev/loop1, /dev/loop2, etc, until a free Linux loopback device is found. In this case, let’s assume that /dev/loop0 is free for usage
     
  3. Tpye losetup /dev/loop0 /virtualfs to attach the first Linux loopback device (/dev/loop0) with regular disk file (/virtualfs) created in step 1
     
  4. Type echo $? to confirm the previous step is completed successfully without error – a zero will be returned to indicate success. Alternative, type losetup /dev/loop0 to confirm
     
  5. Type mkfs -t ext3 -m 1 -v /dev/loop0 to create a Linux EXT3 file system with 1% reserved block count on the loopback device that’s currently associated with a regular disk file. Hence, we are creating a file system within file system, or creating a file system (mkfs) without using a disk partition directly
     
  6. Type mkdir /mnt/vfs to create a directory (as mount point) in /mnt
     
  7. Type mount -t ext3 /dev/loop0 /mnt/vfs to mount the loopback device (regular disk file) to /mnt/vfs as a “regular” Linux EXT3 file system! Now, all the Linux file system-related commands can be act on this unusual Linux file system. For example, you can type df -h to confirm its “disk usage”, type tune2fs -l /dev/loop0 to print its file system settings, create / remove files or directories, etc.
     
  8. To un-mount the loopback file system, type umount /mnt/vfs follow with losetup -d /dev/loop0 to effectively remove the loopback file system and release loopback device subsequently.


Custom Search
  1. Guillaume January 18th, 2008 5:14 PM

    That’s a good tip !

    It is really helpful but I notice that there is a fastest way to find the first unused Linux loopback device :

    losetup -f

  2. Walker January 19th, 2008 12:28 PM

    Hi Guillaume, thank you for reminding me this command option.

    Good work!

  3. The Bomb Of Adsense Junk Makes Blog Suck – Walker News January 23rd, 2008 11:57 PM

    [...] useful; some readers also really kind to share more info on the topic that I’ve made – e.g. Guillaume, Johnny, Brian, CityShaman, Mroids…(Thank you!) However, the no-so-good WalkerNews.net [...]

  4. How To Read ISO Image File In Linux – Walker News July 6th, 2008 3:11 PM

    [...] build in loopback device that allows user to easily mount and access CD image files. No complicated installation or [...]

  5. Mastaus October 23rd, 2008 11:48 PM

    Is it possible to do the same in Windows XP or Vista ? and how ?

  6. Scooter November 6th, 2008 10:38 PM

    Anyone know why sometimes, even after a umount of the mounted file, losetup -d /dev/loop returns “LOOP_CLR_FD: Device or resource busy” ?

  7. Scooter November 6th, 2008 10:46 PM

    Just to avoid any confusion, my loop device is 2, I just typo’d above. /dev/loop2.

  8. palak February 10th, 2009 5:19 AM

    how can i retain the data in my loopback files even after reboot?

  9. Walker February 10th, 2009 10:01 PM

    I don’t think the data in loopback file will lost after reboot, unless you’ve recreated the loopback file (especial step 1 to step 6) after a reboot.

  10. palak February 17th, 2009 10:12 PM

    where is the data of loopback files stored after reboot..??
    is it in some image file?

  11. Michael Hampton July 19th, 2009 8:26 PM

    All that losetup junk is wholly unnecessary.

    Just do:

    mount -o loop myimage.img /mnt/whatever

    The loopback device is automagically handled.

  12. tubs October 31st, 2009 4:06 PM

    how do you create a disk file.