Search code examples
linuxnfs

How to simulate a hanging filesystem?


I'd like to verify an application's behavior when trying to read from a bad filesystem (most notably: a hung NFS mount where reads might just hang forever if left unchecked).

Is there an easy way to locally reproduce this behavior? Be great if I could just force-mount something that doesn't exist, or mount a local ram disk and corrupt it, or lock it from reads such that it hangs rather than erroring out, or something of the sort. It should be something I could do reproducibly as part of a small integration test.


Solution

    1. create a directory to export: mkdir /tmp/nfs
    2. configure the directory, by adding the line /tmp/nfs *(sync,sync,ro,no_subtree_check,fsid=0468884e-fdab-11e9-8ee6-4b007bc13e45) to /etc/exports
    3. sudo exportfs -ra
    4. make sure the NFS server is running.
    5. create the directory for the client to mount: mkdir /tmp/hang
    6. mount -t nfs -o vers=3 localhost:/tmp/nfs /tmp/hang
    7. run ls /tmp/hang - everything is fine.
    8. shut down the NFS server.
    9. run ls /tmp/hang - it will hang.
    10. run the NFS server again.
    11. the ls command will release.

    An alternative to shutting down the NFS server is to block the client via iptables. This has the advantage that you can set up multiple network interfaces, mount multiple clients via different interfaces, and then selectively block clients by blocking network interfaces with iptables.