Ubuntu – How to set disk IO priority automatically

diskprocess-priority

If I use some IO intensive application (like Virtualbox, Steam data verification or even dd) my system becomes almost unresponsive. Despite all being started with normal priority, they make the desktop very sluggish and delay a lot the startup of other applications.

I know I can use something like ionice to change the priority to idle for instance, but in the case of Virtualbox which spawns many processes it becomes difficult.

Can I give some type of hints to the system, so when launching some specific applications they are started with the least IO priority?

Best Answer

Note: This solution only works with the cfq scheduler, as explained here. You should check which one is used by issuing

cat /sys/block/sda/queue/scheduler

and change it if necessary.

If I understood correctly, you want some ionice setting to be applied every time you start a command. You can do it this way:

echo 'ionice -c 3 /usr/bin/VirtualBox "$@"' > VirtualBox
chmod a+x VirtualBox
sudo mv VirtualBox /usr/local/bin/

First command creates a file VirtualBox with the single-quoted text as its content. This should have the full path to the program, because if one only specify VirtualBox it can interpret it as the file we just created and create a "loop".

Second command makes it executable, and third moves this new file in a folder which is in front of the "system" bin folder /usr/bin, so our new file gets executed instead of the original. The "$@" part contains all the arguments the command was invoked with, so they get forwarded to the real VirtualBox command.

According to answer ("yes") to this Super User question Do children processes inherit ionice priorities from their parents? How do you check the IO priority of a running process?, it should be enough to ionice the parent process, like this:

ionice -c 3 VirtualBox

From ionice man page, following I/O scheduling class values are available:

  • 0 for none
  • 1 for realtime
  • 2 for best-effort
  • 3 for idle (used in example above)

A number or class name can be used.

Also, -n level option is applicable for realtime and best-effort classes, with 0-7 as valid data (priority levels).