DEV Community

Hedy
Hedy

Posted on

How do you expand the file system after installing Raspberry Pi OS?

When you install Raspberry Pi OS (formerly Raspbian) on an SD card, the file system may not use the entire available space on the card. To utilize the full capacity of the SD card, you need to expand the file system. Below are the steps to do this:

Image description

1. Using raspi-config (Recommended)
The easiest way to expand the file system is by using the raspi-config tool, which is included with Raspberry Pi OS.

Steps:

  1. Open Terminal:

Launch the terminal on your Raspberry Pi.

  1. Run raspi-config:
bash
sudo raspi-config
Enter fullscreen mode Exit fullscreen mode
  1. Select "Advanced Options":

Use the arrow keys to navigate to "Advanced Options" and press Enter.

  1. Choose "Expand Filesystem":

Select "Expand Filesystem" and press Enter.

  1. Confirm and Reboot:
  • The tool will resize the file system to use the entire SD card.
  • Reboot the Raspberry Pi when prompted:
bash
sudo reboot
Enter fullscreen mode Exit fullscreen mode

2. Manually Resizing the File System
If raspi-config is not available or you prefer manual control, you can resize the file system using command-line tools.

Steps:

  1. Check Current Disk Usage:

Use the df command to check the current disk usage:

bash
df -h
Enter fullscreen mode Exit fullscreen mode

Note the size of the root partition (/dev/root).

  1. Start fdisk:

Use fdisk to view and modify the partition table:

bash
sudo fdisk /dev/mmcblk0
Enter fullscreen mode Exit fullscreen mode
  1. Delete and Recreate the Root Partition:
  • Press p to print the partition table.
  • Note the start sector of the root partition (usually /dev/mmcblk0p2).
  • Press d to delete the root partition.
  • Press 2 to select the second partition (root partition).
  • Press n to create a new partition.
  • Use the same start sector as before.
  • For the end sector, press Enter to use the default (maximum available space).
  • Press w to write the changes and exit.
  1. Reboot the Raspberry Pi:

Reboot to apply the changes:

bash
sudo reboot
Enter fullscreen mode Exit fullscreen mode
  1. Resize the File System:

After rebooting, resize the file system using resize2fs:

bash
sudo resize2fs /dev/mmcblk0p2
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Resize:

Check the disk usage again to confirm the file system has been expanded:

bash
df -h
Enter fullscreen mode Exit fullscreen mode

3. Using GParted (Graphical Tool)
If you prefer a graphical interface, you can use GParted to resize the file system.

Steps:

  1. Install GParted:

Install GParted on your Raspberry Pi:

bash
sudo apt update
sudo apt install gparted
Enter fullscreen mode Exit fullscreen mode
  1. Launch GParted:

Open GParted from the menu or run:

bash
sudo gparted
Enter fullscreen mode Exit fullscreen mode
  1. Resize the Root Partition:
  • Select the root partition (/dev/mmcblk0p2).
  • Right-click and choose "Resize/Move".
  • Drag the partition to fill the available space.
  • Click "Apply" to execute the changes.
  1. Reboot the Raspberry Pi:

Reboot to apply the changes:

bash
sudo reboot
Enter fullscreen mode Exit fullscreen mode

4. Automated Script for Advanced Users

For advanced users, you can create a script to automate the resizing process.

Example Script:

bash
#!/bin/bash
# Resize the root partition to fill the SD card

# Resize the partition
sudo parted /dev/mmcblk0 -- resizepart 2 100%

# Resize the file system
sudo resize2fs /dev/mmcblk0p2

# Reboot to apply changes
sudo reboot
Enter fullscreen mode Exit fullscreen mode

Steps:

  1. Save the script as resize_fs.sh.

  2. Make the script executable:

bash
chmod +x resize_fs.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run the script:
bash
sudo ./resize_fs.sh
Enter fullscreen mode Exit fullscreen mode

Summary

Expanding the file system on a Raspberry Pi ensures that you can use the full capacity of your SD card. The easiest method is using raspi-config, but you can also manually resize the file system using fdisk and resize2fs or use a graphical tool like GParted. Always back up your data before making changes to the file system.

Top comments (0)