nbdkit is a pluggable NBD server and you can write plugins in C or several other scripting languages. But not shell script – until now. Shell script turns out to be a reasonably nice language for this:
case "$1" in get_size) stat -L -c '%s' $f || exit 1 ;; pread) dd iflag=skip_bytes,count_bytes skip=$4 count=$3 if=$f || exit 1 ;; pwrite) dd oflag=seek_bytes conv=notrunc seek=$4 of=$f || exit 1 ;;
Now that coreutils util-linux provides the fallocate program we can even implement efficient trim and zeroing operations.