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:
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:
- Open Terminal:
Launch the terminal on your Raspberry Pi.
- Run raspi-config:
bash
sudo raspi-config
- Select "Advanced Options":
Use the arrow keys to navigate to "Advanced Options" and press Enter.
- Choose "Expand Filesystem":
Select "Expand Filesystem" and press Enter.
- 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
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:
- Check Current Disk Usage:
Use the df command to check the current disk usage:
bash
df -h
Note the size of the root partition (/dev/root).
- Start fdisk:
Use fdisk to view and modify the partition table:
bash
sudo fdisk /dev/mmcblk0
- 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.
- Reboot the Raspberry Pi:
Reboot to apply the changes:
bash
sudo reboot
- Resize the File System:
After rebooting, resize the file system using resize2fs:
bash
sudo resize2fs /dev/mmcblk0p2
- Verify the Resize:
Check the disk usage again to confirm the file system has been expanded:
bash
df -h
3. Using GParted (Graphical Tool)
If you prefer a graphical interface, you can use GParted to resize the file system.
Steps:
- Install GParted:
Install GParted on your Raspberry Pi:
bash
sudo apt update
sudo apt install gparted
- Launch GParted:
Open GParted from the menu or run:
bash
sudo gparted
- 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.
- Reboot the Raspberry Pi:
Reboot to apply the changes:
bash
sudo reboot
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
Steps:
Save the script as resize_fs.sh.
Make the script executable:
bash
chmod +x resize_fs.sh
- Run the script:
bash
sudo ./resize_fs.sh
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)