CodeTheRobot

Ubuntu Linux for Robotics · Lesson 15 of 96

Disk management

lsblk, df, du, partitions, filesystems and finding what fills a disk.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain disk management in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A disk is divided into partitions; each holds a filesystem (ext4 on Ubuntu). lsblk shows the devices (sda, nvme0n1) and their partitions, df -h shows how full each mounted filesystem is, and du -sh folder shows the size of a folder. sudo fdisk -l and lsblk -f give more detail.

Robots run out of disk space through logs and recordings (ros2 bag files, ~/.ros/log). Check df -h regularly and find large folders with du. Never run a partitioning or formatting command until you have confirmed the device name twice: choosing the wrong disk destroys its data.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the disk management commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

lsblk -f
# Show devices, partitions, filesystems and mount points.
df -h
# Show used and free space of each mounted filesystem.
du -sh ~/.ros/log
# Size of the ROS log folder.
du -h --max-depth=1 ~ | sort -h | tail
# The ten biggest folders in your home directory.
sudo fdisk -l /dev/sda
# Read the partition table of a disk (read-only).
sudo journalctl --vacuum-size=200M
# Shrink the system journal to 200 MB.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Disk management. Expected result: 1048576

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

f=$(mktemp)
# TODO: write exactly 1048576 zero bytes into the file using head -c and /dev/zero
stat -c %s "$f"

Expected output

1048576
Stuck? Show the full solution (try it yourself first)
f=$(mktemp)
head -c 1048576 /dev/zero > "$f"
stat -c %s "$f"

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Find the three largest folders in your home directory, record the disk usage before and after deleting old ROS logs, and write down the device name and filesystem of every partition on your computer.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.