Managing storage on Linux Virtual Private Servers (VPS) requires that you know how to make partitions. If you are new to Linux or already using Linux, this guide will take you through the basics and then into the advanced. Partitioning lets you allocate space, utilize resources intelligently, and organize your server data as you like. We will start to partition on Linux with a tutorial about VPS configurations.
1. Introduction to Linux Partitions on VPS
Partitioning is dividing a disk into segments, allowing each section to act independently. On a VPS, partitioning helps you manage data better, isolate different parts of your system, and back up individual segments if needed.
Why Partitioning Matters on VPS
- Improved Data Management: Partitioning makes it easier to categorize data.
- Increased Security: Isolating system and user data reduces risks.
- Efficient Resource Allocation: By setting specific sizes for each partition, you gain control over disk space.
- Backup and Recovery: Specific partitions are easier to back up and restore.
Whether setting up a web server, database, or personal projects, partitioning gives structure to your server’s storage.
What are Partitions on Linux?
Root Partition (/): The root partition holds core system files and essential binaries, serving as the main directory for the Linux operating system. It is crucial for the system’s functionality as all other directories are mounted under it.
Home Partition (/home): This partition keeps user-specific files, like documents and settings, aside from system files, so when you update or reinstall your system, you don’t have to worry about your user data.
Swap Partition: The swap partition acts as virtual memory, using disk space to support RAM when it’s full. It helps manage memory-intensive processes, improving system performance and stability.
Boot Partition (/boot): This partition contains essential boot files, including the Linux kernel and bootloader. Separating it from other partitions safeguards critical files needed to start the OS.
EFI Partition (/boot/efi): This partition contains files it is needed for booting in UEFI mode on systems that use UEFI, usually formatted in FAT32. It is essential for UEFI modern compatibility.
These partitions improve organization, security, and performance by separating system, user, and critical data.
2. Getting Started with Disk Space
Key Terms to Know
- Primary Partition: A directly usable section of your disk (maximum of four per disk on traditional MBR).
- Extended Partition: A workaround that lets you create more than four partitions.
- Logical Partition: Sections within an extended partition are used to bypass the four-partition limit.
- File System: The structure for organizing files and directories within a partition.
Understanding these terms will help as we explore partitioning methods.
Check the Current Disk Layout
To view the current partitions on your VPS, use the following command:
lsblk
With this command you can decide which space to use by viewing a tree-like structure of your disk with this command.
3. Partitioning Tools in Linux
Several tools are available in Linux for creating and managing disk space. The most popular are:
- fdisk: A command-line tool for handling MBR partitions.
- parted: A powerful command-line utility suitable for both MBR and GPT partitions.
- GParted: A graphical partition editor, useful if you’re managing your VPS with a GUI.
Since the VPS environment is dependent on the command line, we’ll concentrate on the fdisk and the parted.
4. Basic Partitioning Using fdisk
Creating partitions on MBR-based disks is a simple task using the fdisk utility. Here’s a step-by-step guide to using fdisk:
Step 1: Open fdisk
Open fdisk on the disk you want to partition. For example, if your disk is /dev/sda, type:
sudo fdisk /dev/sda
Step 2: View Existing Partitions
Type p to view existing partitions on the disk.
Command (m for help): p
Step 3: Create a New Partition
To create a new partition, type n and follow the prompts.
- Partition Type: Choose between a primary or extended partition.
- Partition Number: Select a partition number.
- Starting Sector: Press Enter to use the default.
- Ending Sector: Define the partition size by entering the last sector.
Step 4: Write Changes to Disk
Once you’re satisfied with your changes, type w to write them to the disk.
Command (m for help): w
After this, reboot your system, if necessary, to apply the changes.
5. Advanced Partitioning with parted
For more complex partitioning, parted is a robust tool that supports both MBR and GPT disks.
Step 1: Launch parted
Begin by opening part on the target disk:
sudo parted /dev/sda
Step 2: Set the Partition Table Type
If the disk doesn’t have a partition table, create one using either msdos (MBR) or gpt:
(parted) mklabel gpt
Step 3: Create a New Partition
Creating a partition with parted involves specifying the size directly:
(parted) mkpart primary ext4 1MiB 500MiB
This example creates a primary partition with the ext4 file system, starting at 1MB and ending at 500MB.
Step 4: Verify Partitions
List the partitions with:
(parted) print
When done, Exit parted:
(parted) quit
6. Logical Volume Management (LVM)
LVM is an advanced partitioning method that adds flexibility. With LVM, you can resize partitions without rebooting or repartitioning.
Setting Up LVM
Initialize the Physical Volume:
sudo pvcreate /dev/sda1
- Create a Volume Group:
sudo vgcreate vg_name /dev/sda1
- Create a Logical Volume:
sudo lvcreate -L 5G -n lv_name vg_name
- Format the Volume:
sudo mkfs.ext4 /dev/vg_name/lv_name
- Mount the Volume:
sudo mount /dev/vg_name/lv_name /mnt
LVM is useful for environments where storage needs fluctuate or grow over time.
7. Mounting Partitions on Your VPS
Mounting connects a partition to a directory so you can access it.
Create a Mount Point:
sudo mkdir /mnt/my_partition
- Mount the Partition:
sudo mount /dev/sda1 /mnt/my_partition
- Verify the Mount:
Check with the lsblk command or by listing the contents of the mounted directory.
8. Formatting Partitions
A new partition must be formatted with a file system before use. Common file systems in Linux are ext4, xfs, and btrfs.
Format with mkfs
For ext4, format a partition using:
sudo mkfs.ext4 /dev/sda1
For xfs, use:
sudo mkfs.xfs /dev/sda1
Choose a file system based on your use case. ext4 is popular for its stability, while xfs performs well with large files.
9. Automating Partition Mounting with fstab
The fstab file controls automatic mounting on startup, making it a time-saver for VPS management.
Editing the fstab File
Open the fstab file with your favorite editor:
sudo nano /etc/fstab
- Add an entry for your partition:
/dev/sda1 /mnt/my_partition ext4 defaults 0 2
- Save and close the file.
Test by unmounting and remounting:
sudo umount /mnt/my_partition
sudo mount -a
fstab helps ensure that partitions load automatically, simplifying server restarts and reboots.
10. Troubleshooting Common Partition Issues
Partitioning issues can crop up, but most have straightforward solutions.
Issue 1: Disk Space Not Recognized
- Solution: Ensure the partition is correctly mounted by checking lsblk or df -h.
Issue 2: Unable to Mount a Partition
- Solution: Check the file system type and fstab entries.
Issue 3: Not Available on Reboot
- Solution: Verify the fstab entry for typos or missing fields.
Regularly verify your partition setup to avoid downtime or data loss.
11. Conclusion
Partitioning in Linux, particularly on VPS setups, is a powerful way to optimize storage and manage data. By learning both basic and advanced techniques, you can confidently structure your VPS to meet your specific needs. From using fdisk and parted for partitioning to configuring LVM and automating mounts with fstab, you have the tools to customize your VPS storage efficiently.
About the writer
Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.