How to manage disk usage in Ubuntu?

Hello,
I have been running under ubuntu 14.04 trusty and 18.04 bionic.
What I wonder is about:

  • how to stop all process in case hdd capacity is almost full and create space for system files

  • if possible, to prevent filesystem to write anything into hdd when there is such a risk

  • if there is any script that can manage/control the disk usage and protect the system.

  • if possible to fix via recovery mode when disk usage is at 100%

Many thanks
Boris

Let me attempt to answer these one at a time:

  1. how to stop all process in case hdd capacity is almost full and create space for system files? Not sure if you truly mean "all processes" as that would shut down the whole system. Or how you would intend to create space. If you are wanting to allocate more space from somewhere or if you want to remove unneeded files.

  2. if possible, to prevent filesystem to write anything into hdd when there is such a risk? There is already such a threshold called "reserved space". You can actually fill a disk to 100% and see 0MB available and yet there is there is some reserved space to keep the system online. This can be adjusted, but its pretty standard to have in every Linux distro.

  3. if there is any script that can manage/control the disk usage and protect the system? It's possible, but again, not sure if you are wanting to allocate more space from another resource or get rid of unneeded content. The reserved space I just mentioned may suffice for this problem as well.

  4. if possible to fix via recovery mode when disk usage is at 100%? Yes, but as I also mentioned, SSH should still work at 100% because of the reserved space.

Dear Azrael,
Thanks for your return.
Regarding remark 2, yes. That was what I needed.
Maybe to define a target directory name and empty it in case df -H is above %95-%98 etc would be good. I do not demand more space, just deletion of consumed part section is okay. Regarding remark 4, ssh did not work in my case, neither recovery/rescue mode . Now I have a clean vps

Kind regards
Boris

Sorry, but i might be a bit slow: if you have some data which you don't need at all (in this "target directory") and it should be deleted automatically when the "disc" (i suppose what you mean is "filesystem") is 95% or 98% full - why can't that be deleted at first without getting the filesystem that full in first place?

Data is either nececssary or not: if it is, get a bigger disk and/or add a disk, make the filesystem bigger and avoid a full filesystem that way. If it is not necessary and you are prepared to throw it away at certain circumstance you can throw it away in first place, no?

Btw., you should nowadays ALWAYS use LVM (logical volume manager) and at most have the /boot and (if you use UEFI) the UEFI-system-partition as real partitions. Everything else (including the / FS) should come from a logical volume. It is easy to resize that logical volumes and resize the filesystems on them, minimising the administration overhead.

I use Linux on my desktop and my laptop and the following schema has worked best for me:

/dev/sda1 512MB mounted at /boot (for grub, the boot menu and the kernel images)
Everything else in a VG:

  • 32GB swap (unecessary but Linux acts funny with suspend-to-RAM or suspend-to-disk when the paging space is smaller than the RAM and this is how much my laptop has installed)
  • 20GB mounted at / (for the main OS installation)
  • 20GB mounted at /altroot (this is for a secondary OS installation, comes in handy if for some reason the main installation goes to the fritz)
  • 10GB mounted at /home (user data, resize according to necessity)
  • 50GB mounted at /opt/images (all my virtual machines disk files go there, i use lots of virtual machines for test purposes but most only need barebone OS installations, therefore not much space necessary)

/ and /altroot are mounted vice versa: if i start the OS from what is normally /altroot it will be mounted at / and what is normally / will be mounted at /altroot instead. Everything else is mounted on its normal place. This way i have two separate OS installations and if one breaks i can still boot from the other and repair or even replace it. This has already saved my behind as the update from Xubuntu 17 to 18 left me with an unbootable system for no apparent reason.

I hope this helps.

bakunin

3 Likes

Dear Bakunin,
Thank you so much for your detailed explanation.
I will try to test the same method at my end.
Much appreciated

Kind regards
Boris