3

I have an HFS+ volume where I need to be able to reserve large amounts of space on it quickly, upwards of 100 GB. The recommended command to use, mkfile -n, does create large files but the -n flag seems to be ignored for HFS+ (though not APFS), as it still takes a long time (i.e. several minutes) to finish and the resultant file is still zeroed out. I also tried installing truncate via MacPorts and using that, but it suffers from the same issue.

Is there any other way to do this that just instantly creates a large a file with an arbitrary size? Or am I perhaps running up against a limit of HFS+?

4
  • dd if=/dev/zero of=zero bs=$((1024*1024)) count=$((100*1024)) might work, but if you need to have the file written to a HDD, any method will be as slow/fast as the drive itself.
    – nohillside
    Commented Sep 3, 2023 at 13:38
  • 3
    Also, can you be more specific about what you mean with "quickly" and "takes a long time"?
    – nohillside
    Commented Sep 3, 2023 at 13:39
  • Can you clarify whether you are trying to reserve large amounts of space by creating a file or are you trying to create a large sparse file? The former will require initializing the space by writing to the file. The latter does not actually reserve large amounts of space. The former will require noticeably more time than the latter. Note that (J)HFS+ does not support sparse files and APFS does. Commented Sep 4, 2023 at 20:07
  • @DavidAnderson It doesn't matter to me. I just need a file that takes up a set amount of space on disk, however that's accomplished, and I'm hoping there's a way to do it on HFS+ volumes that will not take longer than a few seconds to complete, even for files that are tens or hundreds of gigabytes.
    – Bri Bri
    Commented Sep 6, 2023 at 14:36

2 Answers 2

3

Evidently, (J)HFS+ file systems do not allow for sparse files. However, the macOS operating system does allow for sparse images. A sparse image can be a single file (.sparseimage) or a singe directory (.sparsebundle), where the image is stored in many files within the directory. An entire drive with an APFS Volume (MyAPFS) containing a large sparse file (Myfile) can be created as a sparse image. Examples are given below.

As a singe (J)HFS+ file (MyImage.sparseimage):

hdiutil create -type SPARSE -size 101g -layout GPTSPUD -fs APFS -volname MyAPFS MyImage
hdiutil attach MyImage.sparseimage
mkfile -n 100g /Volumes/MyAPFS/MyFile

As a single (J)HFS+ directory (MyImage.sparsebundle):

hdiutil create -type SPARSEBUNDLE -size 101g -layout GPTSPUD -fs APFS -volname MyAPFS MyImage
hdiutil attach MyImage.sparsebundle
mkfile -n 100g /Volumes/MyAPFS/MyFile
6
  • The second form does not reserve any disk space. Try du -hs t tt. The second form creates a sparse file.
    – Brad Lanam
    Commented Sep 4, 2023 at 3:45
  • @BradLanam: Are you using a JHFS+ volume or an APFS volume? The OP specified the file must created on a HFS+ volume. (I assume the OP meant JHFS+.) I updated my answer to show that du -hs t tt shows no difference. Commented Sep 4, 2023 at 5:13
  • That was APFS. Interesting that HFS doesn't create a sparse file. The first method seems to be the best for making sure that the space is allocated without regards to the filesystem (works on Linux ext4 also). It should also be noted that mkfile -n also creates a sparse file (noted in its manual page).
    – Brad Lanam
    Commented Sep 4, 2023 at 14:20
  • @DavidAnderson This approach didn't work for me. While dd does "finish" and report back very quickly, its process takes much longer to actually terminate cleanly. And creating a 10 GB file took 10x as long as a 1 GB file. See here where I time two different dd commands: pastebin.com/uUCW18jz I think the issue may simply be that HFS+ / JHFS+ does not support sparse files, and so the entire large file needs to be written to disk. There may not be any way to do what I'm after. Do you see the same sort of behavior and timing if you make a 10 GB file? (assuming you have the space?)
    – Bri Bri
    Commented Sep 4, 2023 at 14:34
  • @BriBri A sparse file is not going to reserve space. mkfile -n 2000g xy works fine on APFS (and I don't have 2t of space free). You will either have to work with the slower speed, or reserve the space ahead of time. Then your process can grab some already reserved space.
    – Brad Lanam
    Commented Sep 5, 2023 at 13:27
0

It looks like this is not possible with HFS+, and requires APFS. Specifically it requires a file system that can create sparse files.

I'll leave the question open though in case I'm wrong and someone has a better answer.

1
  • You can hdiutil to create a sparse file as a block special file or character special file. However, your question does not contain any information about how the file is to be used. Therefore, I do not know if a block or character special file would be of any use. Commented Sep 6, 2023 at 18:14

You must log in to answer this question.

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