Testing Out of Disk Space in Linux

testing out of disk space in linux

  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.

Easiest way to simulate no free disk space situation?

I bet you could also create your own .dmg file with file system of size ... say 2Mb and write to it. If this works, then it is super-easy for testing - you just mount it and switch the path for testing. If the dmg is small enough, you could probably even upload it to the source control.

How can I simulate a disk full error in a Windows environment?

You could try writing to a full floppy disk.

Edit:

In light of your edited question, you could set up a network share with no disk space quota and write to that. The error will then be produced regardless of the logged on user or machine.

Get available space on ALL devices on server

df has --output to select the fields to be printed. This can then be further processed with grep and so:

tasks:
- name: Disk usage from device
shell: df -h --output=source,pcent | grep '^/dev
register: devicespace
- debug:
msg: "{{ devicespace.stdout_lines }}"

Check free disk space for current partition in bash

Yes:

df -k .

for the current directory.

df -k /some/dir

if you want to check a specific directory.

You might also want to check out the stat(1) command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:

$ echo $(($(stat -f --format="%a*%S" .)))

Execute a command which will check if the disk space on somepartion is greater than 1 KB, return -1 else return 0

This may do:

df | awk 'END {print ($4<1024?"-1":"0")}'
0

You can change the number to any that fits your need.

END is used to get last line, instead of tail


To get it into an exit/return code do:

(exit $(df | awk 'END {print ($4<1024?"-1":"0")}')); echo "$?"

PS exit -1 will give 255

kill a user's program if disk usage exceeds 98% in linux

Instead of lurking, adding up space usage over and over (which requires repeated scans of your entire home directory hierarchy) and killing all your processes, you should just set yourself up with a disk quota. If your programs get out of hand, the OS will choke their access to the disk and you won't have to do a thing.

Or if it's a particular program writing to a single file that gets out of hand, you can use ulimit -f to cap the size of the files it can create. (See help ulimit at the bash prompt.)



Related Topics



Leave a reply



Submit