create-swap-and-disk-partition-pi
Tutorial: Disk Partitioning Using the Terminal on a Raspberry Pi
Introduction
Disk partitioning is an essential task for managing storage on your Raspberry Pi. This tutorial will guide you through checking and managing swap partitions, as well as creating new disk partitions using the terminal.
Prerequisites
- A Raspberry Pi with Raspbian (Raspberry Pi OS) installed.
- Basic knowledge of using the terminal.
- A USB drive or additional storage device (optional).
Step 1: Managing Swap Partitions
1.1 Check for Existing Swap Partition
First, check if a swap partition already exists on your system:
swapon --show
If you see an output listing a swap file or partition, it means swap is already configured.
1.2 Create a Swap Partition
If no swap partition is found, you can create one. Follow these steps:
-
Allocate Disk Space for Swap:
Decide how much space you want to allocate for swap. For example, to allocate 1GB of swap space:
sudo fallocate -l 1G /swapfile -
Set the Correct Permissions:
sudo chmod 600 /swapfile -
Set Up the Swap Space:
sudo mkswap /swapfile -
Enable the Swap File:
sudo swapon /swapfile -
Verify the Swap File is Active:
swapon --show -
Make the Swap File Permanent:
Add the following line to
/etc/fstabto ensure the swap file is activated on boot:echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
1.3 Expand an Existing Swap Partition
If you need to expand the swap space:
-
Turn Off Swap:
sudo swapoff /swapfile -
Resize the Swap File:
For example, to resize to 2GB:
sudo fallocate -l 2G /swapfile -
Reconfigure Swap Space:
sudo mkswap /swapfile -
Re-enable the Swap File:
sudo swapon /swapfile -
Verify the Swap Size:
swapon --show
Step 2: Creating a Disk Partition
2.1 List Available Disks
First, list all available disks:
lsblk
Identify the disk you want to partition (e.g., /dev/sda).
2.2 Launch fdisk to Partition the Disk
-
Start
fdisk:sudo fdisk /dev/sda -
Create a New Partition:
- Type
nto create a new partition. - Choose the partition type (
pfor primary). - Select the partition number.
- Specify the start and end of the partition.
- Type
-
Write the Changes:
- Type
wto write the changes to the disk and exitfdisk.
- Type
2.3 Format the New Partition
Format the new partition with a file system (e.g., ext4):
sudo mkfs.ext4 /dev/sda1
2.4 Mount the New Partition
-
Create a Mount Point:
sudo mkdir -p /mnt/new_partition -
Mount the Partition:
sudo mount /dev/sda1 /mnt/new_partition -
Verify the Partition is Mounted:
df -h
2.5 Make the Mount Permanent
To ensure the partition mounts automatically at boot, add an entry to /etc/fstab:
echo '/dev/sda1 /mnt/new_partition ext4 defaults 0 2' | sudo tee -a /etc/fstab
Additional Content: Disk Usage and Maintenance
Check Disk Usage
You can check disk usage with the following command:
df -h
Check Disk Health
To check the health of your disks, use the smartctl tool (install if necessary):
sudo apt install smartmontools
sudo smartctl -a /dev/sda
Clean Up Disk Space
Free up disk space by removing unnecessary files:
sudo apt-get clean
sudo apt-get autoremove
Conclusion
By following this tutorial, you have learned how to manage swap partitions and create new disk partitions on your Raspberry Pi using the terminal. These skills will help you optimize and manage your storage effectively.
Additional Resources
Subscribe and Stay Updated
Make sure to subscribe to our channel for more tutorials and IT tips. Like, comment, and hit the notification bell to stay updated. Thank you for watching, and enjoy managing your Raspberry Pi storage!