17.6 Creating a File System on a File

To create a file system on a file within another file system:

  1. Create an empty file of the required size, for example:

    # dd if=/dev/zero of=/fsfile bs=1024 count=1000000
    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:

    # mkfs.ext4 -F /fsfile
    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    62592 inodes, 250000 blocks
    12500 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=260046848
    8 block groups
    32768 blocks per group, 32768 fragments per group
    7824 inodes per group
    Superblock backups stored on blocks: 
    	32768, 98304, 163840, 229376
    Writing inode tables: done                            
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    This filesystem will be automatically checked every 33 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
  3. Mount the file as a file system by using a loop device:

    # mount -o loop /fsfile /mnt

    The file appears as a normal file system:

    # mount
    /fsfile on /mnt type ext4 (rw,loop=/dev/loop0)
    # 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