/dev/full on OS X
Background
How to test program behaviour when there is no disk space left? On Linux, there is a special device /dev/full
. Writing to it always causes ENOSPC
an error. Even if it is not present it can be created using mknod
. Unfortunately, such a device is not available on OS X.
Workaround
How to deal with it? Well, ENOSPC
means No space left on device, so the straightforward solution is to have a file on disk with no space left. Such file will behave just like /dev/full. How to get such a disk? Luckily it can be created without using the physical medium. A RAM drive can be used for that purpose. So to sum up there are few steps needed:
- Create RAM disk with minimal size to store filesystem.
- Format it (create filesystem).
- Mount it.
- Create the file that will act as a /dev/full.
- Fill rest of the RAM drive with dummy content.
Solution
Here is the implementation of steps from above in a bash script:
1 2 3 4 5 6 7 | #!/usr/bin/env bash VOLUME=FULL_DISK DEVICE=$(hdiutil attach -nomount ram://64) diskutil erasevolume MS-DOS ${VOLUME} ${DEVICE} touch "/Volumes/${VOLUME}/full" cat /dev/zero > "/Volumes/${VOLUME}/space" |
OS X provides handy tools for creating and formatting disk so its implementation is pretty easy. Firstly (line #4) we create new RAM disk with 64 sectors (minimum number to hold filesystem). -nomount option has to be used since the freshly createdRAM disk is empty and could not be mounted yet.
Then using diskutil we create FAT12 (MS-DOS) filesystem on the disk. It will be mounted automatically. FAT12 has been chosen because it is one of the simplest that are supported by OSX thus requires a smaller number of sectors than eg. HFS+.
Finally, we create empty file acting as /dev/full and fill rest of the disk with NULL bytes. Et voilà, we have /Volumes/FULL_DISK/full
which works just like /dev/full
:
1 2 | $ echo > /Volumes/FULL_DISK/full bash: echo: write error: No space left on device |
It can be optionally symlinked to /dev/full: sudo ln -s /Volumes/FULL_DISK/full /dev/full
To dispose of ramdisk use: hdiutil detach /Volumes/FULL_DISK/
This will unmount RAM drive and will free allocated memory.
Caveats
Such “emulated” device provides only ENOSPC simulation. It does not support other features available natively in Linux. Namely, seeks will not always succeed and reads will not provide NULL bytes.
Are you willing to try this out? Check the complete snippets in gist.
References
About the author
Ready to take your business to the next level with a digital product?
We'll be with you every step of the way, from idea to launch and beyond!