18

I have a program that may be dying when it runs out of disk space writing a certain file, I am not sure if this is the case.

I'd like to run it and see, but my test server is not going to run out of space any time soon. Is there any way I could mock this behavior? It doesn't look like there is any way to set a folder/file size limit in Ubuntu, and getting user quotas set up is going to be a process ( due to getting permissions )

Is there a common way of testing this situation?

I'm running Ubuntu 12.04

2
  • 1
    Do you have spare USB memory sticks handy? Would be easy to fill up one of those.
    – JJJ
    Commented Apr 16, 2013 at 18:16
  • 3
    Why not just setup a partition that is really small?
    – user195488
    Commented Apr 16, 2013 at 18:16

5 Answers 5

37
  1. Create a file of the size you want (here 10MB)

    dd if=/dev/zero of=/home/qdii/test bs=1024 count=10000

  2. Make a loopback device out of this file

    losetup -f /home/qdii/test

  3. Format that device in the file system you want

    mkfs.ext4 /dev/loopXXX

  4. Mount it wherever you want (/mnt/test should exist)

    sudo mount /dev/loopXXX /mnt/test

  5. Copy your program on that partition and test

    cp /path/my/program /mnt/test && cd /mnt/test && ./program


Substitute /dev/loopXXX with the loop device losetup created, find out with losetup -a .

When done, don't forget to:

  • unmount with sudo umount /mnt/test .
  • clean up loop devices after use, with losetup -D /dev/loopXXX
  • remove the file.
3
  • 4
    With -D option losetup will try to detach all loop devices, which are widely used (at least on Ubuntu), so I'd prefer sudo losetup -d /dev/loopXX to detach selected device.
    – ololobus
    Commented Mar 11, 2019 at 12:32
  • 1
    Something that should be said: the losetup at least must be done as root. Otherwise one gets an error "losetup: cannot find an unused loop device".
    – vinc17
    Commented Aug 4, 2021 at 9:15
  • @vinc17 in my case the error was 'failed to set up loop device: No such file or directory'. I also had to use sudo for the third step.
    – Dan M.
    Commented Apr 5, 2022 at 12:44
14

Just use /dev/full, it will raise the ENOSPC error when you try to write to it:

$ echo "Hello world" > /dev/full
bash: echo: write error: No space left on device
4

Another possibility would be to reduce the appropriate limit with setrlimit(2) syscall with RLIMIT_FSIZE or with the bash ulimit builtin (using -f). Then write(2) would fail with EFBIG

And you could also set some quotas on some appropriate file system, so write(2) fails with EDQOT.

If you want the real ENOSPC error to write(2) you probably need a loopback file system as answered by qdii.

BTW, I don't really know how to "emulate" the EIO error (maybe with some FUSE filesystem?).

Many programs handle write(2) errors (and nearly all should). But I don't know many programs which handle very differently the various errors possible with write(2). Most programs handle all write(2) errors the same way.

However, you might need to handle EINTR and EWOULDBLOCK errors differently: these are recoverable errors, and you usually redo the write(2) at some later time.

1
  • No, not better but different. And your solution gives the real ENOSPC error .... Commented Apr 16, 2013 at 18:48
0

If you're making POSIX filesystem calls you can use libfiu to inject ENOSPC errors into calls.

0

A good way to recreate the scenario is to mount a directory with limited space. Let's say that your program will be executed on /data. To limit the storage there to 100MB (customize as needed):

  1. sudo su
  2. mkdir /media/images/
  3. dd if=/dev/zero of=/media/images/data.img bs=512K count=200 #100MB
  4. mkfs.ext4 /media/images/data.img
  5. mkdir /data
  6. mount -o loop /media/images/data.img /data

But knock your socks off if you want even less space

  • dd if=/dev/zero of=/media/images/data.img bs=1M count=1 #1MB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.