SoFunction
Updated on 2025-05-19

How to effectively manage mirrored disk space in Ubuntu system

introduction

On Ubuntu servers, managing mirrored disk space is a critical task in system maintenance, especially when using virtualization technologies (such as Docker, LVM, KVM, etc.). Rationally managing disk space can effectively prevent insufficient disk space and improve system stability and performance. This article will introduce you to how to effectively manage mirrored disk space in Ubuntu system, covering disk partitioning, file system optimization, space cleaning and other aspects.

1. Check disk usage

Before performing disk space management, you must first understand the current disk usage. You can use some basic commands to view the status of the disk.

1. Use the df command

The df command is used to display the overall usage of disk space. Common commands are as follows:

df -h

-h: Display the results in a human-readable format, automatically selecting the appropriate unit (such as GB or MB).

Output example:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk
├─sda1   8:1    0   50G  0 part /
sdb      8:16   0  100G  0 disk
└─sdb1   8:17   0  100G  0 part /data

explain:

Size: Total disk size.

Used: Used space.

Avail: Available space.

Use%: Disk usage rate.

2. Use the du command to view directory usage

The du(Disk Usage) command is used to view disk space usage for a specified directory and its subdirectories. For example:

du -sh /var/log

-s: Summary of the total usage of all files and subdirectories in the directory.

-h: Display output in a human-readable way.

This way, you can view the disk usage of individual directories to make better decisions about cleaning or optimization.

2. Disk partition and mount

Reasonably partitioning and mounting disks on Ubuntu can effectively manage disk space and avoid system failures caused by insufficient space for a certain partition.

1. Use lsblk to view the disk structure

The lsblk command displays the structure of all disks and partitions, including mount points.

lsblk

Output example:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk
├─sda1   8:1    0   50G  0 part /
sdb      8:16   0  100G  0 disk
└─sdb1   8:17   0  100G  0 part /data

2. Determine the disk partition

The disk partitioning can be done on Ubuntu via the fdisk or parted tool. For example, use fdisk to create a new partition:

sudo fdisk /dev/sdb

Select the partition type and size according to the prompts to create a new partition. After creation is complete, you can use the mkfs command to format the disk:

sudo mkfs.ext4 /dev/sdb1

3. Mount the disk partition

After creating and formatting the partition, you can mount it to the specified directory by modifying the /etc/fstab file. First, create the mount directory:

sudo mkdir /mnt/data

Then, mount the partition to the directory through the mount command:

sudo mount /dev/sdb1 /mnt/data

In order to make the partition automatically mount after the system restarts, you can add the following line to the /etc/fstab file:

/dev/sdb1 /mnt/data ext4 defaults 0 2

3. Manage mirror disk space

Management of mirror disk space is especially important for systems using virtualization or container technologies such as Docker. Here are some common ways to manage mirrors and container disk space.

1. Clean up Docker images and containers

Docker images, containers, and volumes can take up a lot of disk space. Unused images and containers can be cleaned with the following command.

Delete unused images

View all images stored on the current system:

docker images

Delete a mirror:

docker rmi

Delete all unused images:

docker image prune -a

Delete the stopped container

View all containers, including the stopped containers:

docker ps -a

Delete the stopped container:

docker rm

Delete all stopped containers:

docker container prune

Delete unused volumes

Docker volumes are used to store data, and unused volumes also take up disk space. Unused volumes can be deleted by the following command:

docker volume prune

2. Clean up the APT cache

In Ubuntu systems, the APT package management tool caches downloaded package files, which may take up a lot of disk space over time. The APT cache can be cleaned regularly to free up space.

Clean up local cache

Use the following command to clean up the downloaded package cache:

sudo apt clean

This command deletes all cache files in the /var/cache/apt/archives directory, freeing up space.

Clean up packages that you no longer need

Use the following command to delete package files that are no longer needed:

sudo apt autoremove

3. Use ncdu tool to analyze disk space

ncdu(NCurses Disk Usage) is a command line tool that helps you quickly analyze disk usage and find the files and directories that take up the most disk space.

Install ncdu:

sudo apt install ncdu

Then run:

sudo ncdu /

ncdu scans the entire file system and displays disk usage, allowing you to interactively view and clean disk space.

4. Monitor disk space usage

On Ubuntu servers, monitoring disk space usage can help administrators detect the risk of insufficient disk space in a timely manner and take appropriate measures.

1. Use the watch command to regularly view disk space

The watch command allows you to execute commands every once in a while and display the output results. Use the following command to view disk usage periodically:

watch -n 60 df -h

This command refreshes disk space usage every 60 seconds.

2. Set disk space alert

You can use the cron timing task to periodically check disk space and send alerts. For example, you can set up a cron task to check disk usage daily, and send an email to notify the administrator if disk usage exceeds a certain threshold.

Edit cron file:

crontab -e

Add the following tasks:

0 8 * * * df -h | awk '$5 > 80 {print $0}' | mail -s "Disk space warning" admin@

This task checks disk space usage at 8 o'clock every day, and sends an email alert if the usage rate of a certain partition exceeds 80%.

Disk space management is critical to Ubuntu servers, especially when deploying mirrored storage, virtualization, or containerized deployments. By reasonably partitioning, mounting, cleaning unnecessary files, checking disk space regularly, and setting space alerts, you can effectively avoid the problem of insufficient disk space and ensure the stable operation of the system.

The above is the detailed content on how to effectively manage mirror disk space in Ubuntu system. For more information about Ubuntu mirror disk space management, please pay attention to my other related articles!