Creating a File System on a File Within Another File System

  1. Create an empty file of the required size:

    sudo dd if=/dev/zero of=/fsfile bs=1024 count=1000000

    The output of the previous command would be as follows:

    1000000+0 records in
    1000000+0 records out
    1024000000 bytes (1.0 GB) copied, 8.44173 s, 121 MB/s
  2. Create a file system on the file:

    sudo mkfs.ext4 -F /fsfile

    The output of the previous command would be as follows:

    mke2fs 1.44.6 (5-Mar-2019)
    Discarding device blocks: done                            
    Creating filesystem with 250000 4k blocks and 62592 inodes
    Filesystem UUID: 17ef1d96-c595-4f19-891b-112a56b54c82
    Superblock backups stored on blocks: 
    	32768, 98304, 163840, 229376
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    
  3. Mount the file as a file system by using a loop device:

    sudo mount -o loop /fsfile /mnt

    The file appears as a normal file system when you run the sudo mount command:

    ...
    /fsfile on /mnt type ext4 (rw,loop=/dev/loop0)
    sudo df -h
    Filesystem            Size  Used Avail Use% Mounted on
    ...
    /fsfile               962M   18M  896M   2% /mnt

    If required, create a permanent entry for the file system in /etc/fstab:

    /fsfile          /mnt      ext4    rw,loop     0 0