All posts
Android, Blog, iOS

/dev/full on OS X

Karol Wrótniak

Karol Wrótniak

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:

  1. Create RAM disk with minimal size to store filesystem.
  2. Format it (create filesystem).
  3. Mount it.
  4. Create the file that will act as a /dev/full.
  5. Fill rest of the RAM drive with dummy content.

Solution

Here is the implementation of steps from above in a bash script:

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:

It can be optionally symlinked to /dev/fullsudo 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.

References

  1. hdiutil(1) manual
  2. diskutil(8) manual
  3. full(4) manual
  4. zero(4) manual
  5. touch(1) manual
  6. errrno(3) manual

Newsletter

The post is created by Droids On Roids Team Member.
We would love to take care of your business.

Leave comment