Mount a file or RAM Disk


There are many times when you would want to create a file and mount it as a filesystem, this is great for testings and also moving whole mounts between servers using something like scp.

First you’ll need create the loop file, to do this follow the belew command to create a loop file



dd if=/dev/zero of=[OutputFile] bs=1048576 count=[Size]

Substituting the following values with your own

  • [OutputFile] the filename for the loop file
  • [Size] the size of the loop file in MB, this will be how much space is available in the new mount.
  • Now you’ll need to create a filesystem in the loop file, for this example I choose ext2 however you can change this to suit your need.

     mkfs.ext2 [OutputFile] 

    Substituting the following values with your own

  • [OutputFile] the filename for the loop file
  • Next you can mount your new loop file to a folder

    mkdir [MountPoint]
    
    sudo mount [OutputFile] [MountPoint] 

    Substituting the following values with your own

  • [OutputFile] the filename for the loop file
  • [MountPoint] the folder where you want to mount your new loop file
  • Now you’ll be able to run

    df -h

    to show how much space is left in your new mount, it will look something like this

    /dev/loop0      9.7M   92K  9.1M   1% /home/me/mountpoint



    RamDisk
    You may also want to create a RamDisk, this is similar to the above loop file however instead of using a file on the existing disk we can mount some space from memory. You would want to do this to provide a extremely low latency and fast mount to store files.

    mkdir [MountPoint]
    
    sudo mount -t tmpfs -o size=[Size]m tmpfs [MountPoint]

    Substituting the following values with your own

  • [MountPoint] the folder where you want to mount your new loop file
  • [Size] the size of the loop file in MB, this will be how much space is available in the new mount.
  • Comments are closed.