Commands.page Logo

Set Minimum Disk Space Before aria2 Download Ubuntu

This guide explains how to ensure sufficient storage is available before initiating downloads with aria2 on Ubuntu. Although aria2 automatically checks space for known file sizes, you may need to enforce a specific free space buffer to prevent system issues. We will cover the built-in pre-allocation option and provide a simple bash script to set a custom minimum disk space threshold.

Install aria2 on Ubuntu

Before configuring disk space settings, ensure aria2 is installed on your system. Open your terminal and run the following command to install the package from the official repositories:

sudo apt update
sudo apt install aria2

Enable Strict Space Reservation

By default, aria2 checks if there is enough space for the file being downloaded if the file size is known. To enforce this check strictly before any data is written, use the --file-allocation option. Setting this to prealloc or falloc reserves the entire file size on the disk immediately.

Add the following line to your aria2 configuration file located at ~/.aria2/aria2.conf:

file-allocation=prealloc

Alternatively, you can pass it directly via the command line:

aria2c --file-allocation=prealloc [URL]

If the disk does not have enough space for the specific file, the download will fail immediately without consuming partial bandwidth.

Set a Custom Minimum Free Space Buffer

Aria2 does not have a native flag to require a specific amount of free space remaining on the drive (e.g., “Do not download if less than 1GB free”). To achieve this, you can use a simple bash wrapper script. This script checks available space before calling aria2.

Create a new file named safe-aria2.sh:

nano safe-aria2.sh

Paste the following content into the file. This example sets a minimum requirement of 1GB (1048576 KB):

#!/bin/bash

MIN_SPACE_KB=1048576
TARGET_DIR="${2:-.}"

AVAILABLE_SPACE=$(df -k "$TARGET_DIR" | tail -1 | awk '{print $4}')

if [ "$AVAILABLE_SPACE" -lt "$MIN_SPACE_KB" ]; then
    echo "Error: Insufficient disk space. Required: 1GB"
    exit 1
fi

aria2c "$@"

Make the script executable and move it to your bin directory:

chmod +x safe-aria2.sh
sudo mv safe-aria2.sh /usr/local/bin/safe-aria2

You can now use safe-aria2 instead of aria2c for your downloads. This ensures the download only starts if your defined minimum disk space is available.