I would like to create a 100GB file to use as a file-system. The file will be stored on an encrypted volume, so the file/file-system itself does not need to be (further) encrypted. Searching has revealed many different methods of doing this. Some use losetup, some don't. Is the following an acceptable method?

$ dd if=/dev/zero of=~/theFile.img bs=1M count=10240
$ mkfs.ext4 ~/theFile.img   # will prompt about block special device
$ sudo mount -o loop ~/theFile.img /media/mountPoint
  • 5
    Besides this creating a 10GB File instead of 100GB, thanks alot, changing the count to 102400 fulfilled my needs :)
    – user94215
    Commented Dec 8, 2014 at 13:08
  • 2
    You may want to use truncate -s 10240M ~/theFile.img to just allocate the file size without writing zeros to it. Just saves you some time :)
    – debuglevel
    Commented Jan 11, 2022 at 17:28

2 Answers 2

  1. BS and COUNT should be lowercase:

    dd if=/dev/zero of=~/theFile.img bs=1M count=10240

  2. you need to make the /media/MountPoint directory if it doesn't already exist:

    sudo mkdir -p /media/MountPoint

Apart from those two things, what you have there should work.

It's usually better to be explicit than rely on implicit behaviour, so you might want to change the mount line to:

sudo mount -t ext4 -o loop ~/theFile.img /media/mountPoint

  • 2
    Thanks for the corrections. It was the M in 1M that had to be uppercase. Don't know why I put bs and count in uppercase too. Thanks for the improved mount.
    – SabreWolfy
    Commented Jul 25, 2012 at 9:34
  • I've accepted a suggested edit to my question to correct the uppercase mistake and to explicitly specify a loop device in the mount.
    – SabreWolfy
    Commented Jul 25, 2012 at 9:38

Except for the capital letters in the dd options and not including the loop option in mount, I find this perfectly valid. mkfs is warning you that is not using a block device to make sure that you know what you're doing.

Anyway, at the end, you will be using a loop device:

$ dd if=/dev/zero of=~/theFile.img bs=1M count=10
$ /sbin/mkfs.ext4 theFile.img
$ sudo mount -o loop theFile.img /mnt/tmp/
$ df -h

/dev/loop0            9,7M  1,1M  8,1M  12% /mnt/tmp
  • 1
    The -o loop may or may not be required...depends on the version of mount. My current version doesn't require it, but I remember having to specify it in the past. Still, like i said - it's better to be explicit than implicit in scripts, serves as documentation of WTF Was I Thinking When I Did That?
    – cas
    Commented Jul 25, 2012 at 9:39
  • Agreed. (Note: I'm using mount 2.17.2-9 from the current Debian Stable)
    – rpet
    Commented Jul 25, 2012 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .