Dealing with Disk Bloat and "Cross-Device Link" Errors on Ubuntu
As a developer working on a Linux system with segmented partitions, you eventually hit a wall where the system refuses to install software, citing an invalid cross-device link. This guide covers how to diagnose partition bottlenecks, fix Snap-related errors, and automate system maintenance.
1. The Problem: The “Small Root” Trap
Modern Linux distributions like Ubuntu often suggest a partition layout that separates the Root (/) from the Home (/home) directory.
- Root (
/): Stores the OS, system logs, and application runtimes (Snaps/Flatpaks). - Home (
/home): Stores your personal files and projects.
The invalid cross-device link error occurs when a service (usually snapd) tries to create a hard link between two files that live on different physical partitions. In Linux, a hard link cannot point to a file on a different filesystem.
Furthermore, high-resource tools like Ollama (AI models) or Snaps can quickly fill a 40–50GB root partition, causing system deadlocks.
2. Phase One: The Deep Diagnosis
Before deleting anything, you must identify where the “weight” is. Use these commands to see the difference between physical disk space and directory usage.
Check Physical Partitions
lsblk -f
Check Directory Usage (Ignoring virtual mounts)
sudo du -xh / | sort -rh | head -n 10
The -x flag is critical—it prevents the command from wandering into your giant /home partition, keeping the focus on the struggling Root drive.
3. Phase Two: Relocating “Heavy” Data
If a specific tool (like Ollama) is eating several gigabytes on your Root partition, you can move it to your larger Home partition and use a Symbolic Link to “trick” the system into thinking it’s still in the original spot.
Example: Moving Ollama Models
# 1. Stop the service
sudo systemctl stop ollama
# 2. Move data to the larger partition
sudo mv /usr/local/lib/ollama /home/wga/Nextcloud/ollama_data
# 3. Create the Symbolic Link
sudo ln -s /home/wga/Nextcloud/ollama_data /usr/local/lib/ollama
# 4. Restart service
sudo systemctl start ollama
4. Phase Three: Aggressive Snap & APT Cleanup
Snap stores multiple revisions of every app, and APT stores every .deb file it has ever downloaded. This is pure bloat for a local PC.
Purge Snap Cache & Disabled Revisions
# Remove the cache that causes linking errors
sudo rm -rf /var/lib/snapd/cache/*
# Loop through and remove all 'disabled' (old) versions of snaps
snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do
sudo snap remove "$snapname" --revision="$revision"
done
Clean APT Archives
sudo apt autoremove -y
sudo apt clean
5. Phase Four: Automation with Systemd
To prevent the “mess” from returning, we can automate the cleanup using a Systemd Timer. We set these to “Idle” priority so they never interrupt your coding sessions.
The Maintenance Script (~/.local/bin/system-cleanup.sh)
#!/bin/bash
/usr/bin/apt-get autoremove -y
/usr/bin/apt-get clean
/usr/bin/journalctl --vacuum-time=3d
The Systemd Service (/etc/systemd/system/monthly-cleanup.service)
Set Nice=19 and IOSchedulingClass=idle to ensure the process only runs when the CPU and Disk are not busy with your work.
The Systemd Timer (/etc/systemd/system/monthly-cleanup.timer)
[Timer]
OnCalendar=monthly
RandomizedDelaySec=7200
Persistent=true
Summary of Best Practices
- Prefer APT: Native packages share libraries and save space.
- Flatpak over Snap: If you must use a containerized app, use
flatpak install --userto keep it on your large/homepartition. - Symbolic Links are Power: Use them to redirect any folder that grows too large for your Root drive.