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 18-01-08@17:14

    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 19-01-08@12:28

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

    Good work!

  3. The Bomb Of Adsense Junk Makes Blog Suck – Walker News 23-01-08@23:57

    […] 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 06-07-08@15:11

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

  5. Mastaus 23-10-08@23:48

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

  6. Scooter 06-11-08@22:38

    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 06-11-08@22:46

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

  8. palak 10-02-09@05:19

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

  9. Walker 10-02-09@22:01

    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 17-02-09@22:12

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

  11. Michael Hampton 19-07-09@20:26

    All that losetup junk is wholly unnecessary.

    Just do:

    mount -o loop myimage.img /mnt/whatever

    The loopback device is automagically handled.

  12. tubs 31-10-09@16:06

    how do you create a disk file.

  13. Andrei 27-02-10@23:54

    Actually i found the losetup junk very useful :)

    I didn’t go too much into the file systems specifics however I needed to mount an ext4 partition clone (created with partclone) and if the partition was being restored as a plain image file on hdd the automagic of the loopback device wouldn’t work on that file.

    However, if I would restore the partition clone on a loopback device created as shown above by Walker, I could then mount it and restore access to my precious files :)

    Thank you, Walker! This was a really useful tip for me

  14. Jonny Waterlake 09-04-10@22:28

    This is awesome! Thanks!

  15. paul 25-04-10@02:20

    I have trouble visualizing what a loopback actually is and why it’s called that. I’m very new to linux and have trouble picturing this idea in my mind.

  16. toro 09-07-10@08:01

    In electronics, the term loopback is generally used to describe methods of routing electronic signals or digital data streams, from their originating device quickly back to the same source without processing or modification.

    In Unix-like operating systems, a loop device, is a pseudo-device that makes a file accessible as a block device. Block devices correspond to devices through which the system moves data in the form of blocks. These device nodes often represent addressable devices such as CD-ROM drives.

    The two concepts are similar, so it was not a big stretch of the imagination for Linux to also use the term loopback originally used in electronics.

  17. David C. 31-07-10@00:09

    That’s a great explanation, toro –

    In order to picture it in your mind, paul, the situation is fairly analogous to owning a large, old house.

    You decide that the entire house is too large and, considering modern lifestyles, you can reduce the amount of house needed to make a home. (Or for whatever reason.)

    You begin segregating one side of the house from the other (closing off doors, rerouting HVAC and electrical, water, sewer, etc.) until you have created a duplex. Each side of the house has its own utilities, entrances, parking, etc. allowing the former entire house to now function as two complete houses.

    Using a loopback filesystem, you segregate off a portion of your original filesystem so that you wind up with two (or more) filesystems available on your one physical hard drive. No extra room is created – the original hard drive still has the same overall capacity – but the essential functionality of a file storage system is now doubled.

    I hope this helps!

    /David C.

  18. David C. 31-07-10@00:13

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

    Someone or some process has a file open on the device . . .

  19. David C. 31-07-10@00:23

    Mastaus wrote:
    “Is it possible to do the same in Windows XP or Vista ? and how ?”

    Yes it is possible. Right-click your My Computer (or simply Computer in Vista/W7) icon and choose Manage from the pop-up menu.

    Go to Storage -> Disk Management where you can do all kinds of scary things to your disks, including creating a new partition. Right-click the (C:) portion of Disk 0 and choose to Shrink Volume. Once that process has completed (first you have to make some decisions, etc.), right-click the newly vacated space and choose to create a new partition. The system will assign a drive letter and you can now use it by accessing that drive letter in Windows Explorer. Otherwise, it’s just like using a loop-back file system on Linux.

    I hope this helps –

    /David C.

  20. Bluechip 26-09-10@09:03

    If you want the virtual-fs remounted automatically at boot time …add this to the botto of /etc/fstab :
    /virtualfs /mnt/vfs ext3 loop,auto 0 0
    …obviously change the fliename[1], mountpoint[6] and filesystem[5] as you require :)

    You can test this by un-mounting the current system :
    umount /mnt/vfs
    …and remounting it with the “mount all” command (as used during boot):
    mount -a

    …Thnaks for the great tutorial :)

  21. Padam 26-09-10@23:17

    I have created a ntfs image file in Linux . I am able to mount it successfully in linux and add and delete data. But i dont know how can i use this image file in wondows ?

    Thanks for help

  22. Klepto 11-11-10@02:01

    @Dave C

    What you are describing is creating another partition on a disk, this is NOT the same as a loopback filesystem.

  23. CocoChristopher 14-07-11@09:19

    I have found the free IMDISK driver will allow using a filesystem within a host file on Windows.

    It can be found here:

  24. Archie 19-01-12@19:48

    I get the concept of loopback file system now after reading everyones comments. The one question I have is whether this can be achieved on a file system where there is limited disk space, e.g. an embedded system with limited resources. From what I understand here, you have a file system, you then create a file and turn it into another file system. I then need to copy the original file system into this pseudo file system structure in order to trick some application software into thinking its running on a different file system. Do I need twice the disk space of the first file system?

  25. Andrzej 18-12-14@20:04

    Instead of dd you can use truncate:

    truncate -s 30M /virtualfs

2018  •  Privacy Policy