How to do it...

To create a 1 GB ext4 filesystem in a file, follow these steps:

  1. Use dd to create a 1 GB file:
        $ dd if=/dev/zero of=loobackfile.img bs=1G count=1
        1024+0 records in
        1024+0 records out
        1073741824 bytes (1.1 GB) copied, 37.3155 s, 28.8 MB/s

 The size of the created file exceeds 1 GB because the hard disk is a block device, and hence, storage must be allocated by integral multiples of blocks size.

  1. Format the 1 GB file to ext4 using the mkfs command:
        $ mkfs.ext4 loopbackfile.img
  1. Check the file type with the file command:
        $ file loobackfile.img
        loobackfile.img: Linux rev 1.0 ext4 filesystem data,   
        UUID=c9d56c42-   
        f8e6-4cbd-aeab-369d5056660a (extents) (large files) (huge files)
  1. Create a mount point and mount the loopback file with mkdir and mount:
        # mkdir /mnt/loopback
        # mount -o loop loopbackfile.img /mnt/loopback

The -o loop option is used to mount loopback filesystems.

This is a short method that attaches the loopback filesystem to a device chosen by the operating system named something similar to /dev/loop1 or /dev/loop2.

  1. To specify a specific loopback device, run the following command:
        # losetup /dev/loop1 loopbackfile.img
        # mount /dev/loop1 /mnt/loopback
  1. To umount (unmount), use the following syntax:
        # umount mount_point

Consider this example:

        # umount /mnt/loopback
  1. We can also use the device file path as an argument to the umount command:
        # umount /dev/loop1
Note that the mount and umount commands should be executed as a root user, since it is a privileged command.