I'm trying to create a btrfs file system with a single volume from a directory 'root', over mtd and nandsim.

With a few token files and directories in my root directory, and successfully created and mounted the filesystem:

sudo modprobe mtdblock
sudo flash_erase /dev/mtd0 0 0
sudo mkfs.btrfs -m single -s 2048 -r root /dev/mtdblock0

All is well in the world. Now, I add in the actual contents of my root directory: a few metadata files, and just under 128k binary files at 2k each. When I try the same approach again, mkfs.btrfs fails with "ERROR: unable to zero the output file".

In the source code, the offending method at line 407 fails if either call to pwrite64() fails. I can't see why this would fail, unless the system call has some limit on the overall size it will allow?

That said, my device is only 256MB, on a system with plenty of RAM and disk space -- it seems unlikely.

Could anyone please point me in the right direction? Have I missed some key step?

If it matters, I'm using btrfs-progs v4.15.1, on bionic 18.04 kernel 4.15.0-99-generic

  • 1
    Have you given the option --shrink a try ? From the description that could help : Shrink the filesystem to its minimal size, only works with --rootdir option. If the destination is a regular file, this option will also truncate the file to the minimal size. Otherwise it will reduce the filesystem available space. Extra space will not be usable unless the filesystem is mounted and resized using btrfs filesystem resize. Commented Jun 15, 2020 at 9:48
  • 1
    shrink does not seem to have made a difference. I scaled back the number of files in my root directory to 90%, 80%, .. 20% of the nominal capacity, and I'm still seeing the same error both with and without the shrink option
    – Onofog
    Commented Jun 16, 2020 at 15:38
  • Line 407 of the v4.15.1 source is not a pwrite call. There are two pwrite calls in zero_output_file. Exactly which one is it?
    – crass
    Commented Jul 23, 2023 at 0:45

1 Answer 1


I ran into the same error when attempting to prefill a BTRFS file system using the -r or --rootdir option. In my case, the issue was that the rootdir had files such that when all their sizes were added was larger than the block device I was trying to write the BTRFS filesystem to. When using the rootdir option, mkfs.btrfs adds up all the sizes of the files in rootdir and then writes a zero byte at that location in the block device/file given. When I device is given and that write is beyond the end of the device and error will occur and the described error message "ERROR: unable to zero the output file" is displayed. This is generally not a problem give a file, instead of a block device, is given because the file will be resized to the size of the location of the largest write position. In this case the error message could be drastically improved and here's a github issue for this.

So, I would do du -hs --apparent-size root and verify that that number is less than the size of /dev/mtdblock0.

Also, your flash_erase invocation seems off as it takes a start block and number of blocks to erase. You have number of blocks to erase set at 0.

You must log in to answer this question.

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