top of page

How to Add and Format a Drive on Linux and Expand and Extend the Volume

Feb 12

8 min read

0

0

0

How to Add and Format a Drive on Linux and Expand and Extend the Volume

Adding a new drive to your Linux system can provide much-needed storage space for your growing data. This blog post will guide you through the process of adding and formatting a new drive in Linux, covering various aspects, including partitioning schemes, filesystems, and tools like fdisk, parted, and LVM. We'll also discuss how to expand and extend volumes to utilize the new storage effectively.

Before You Begin

Before making any changes to your system's storage, backing up your important data is crucial. Disk operations, even when performed correctly, carry a risk of data loss. Several backup tools are available in Linux, each with its own strengths:

  • rsync: A versatile command-line tool for copying and synchronizing files and directories. It can be used to create full or incremental backups, saving only the changes since the last backup1.

  • dd: Another command-line tool that creates a bit-by-bit copy of a disk or partition. This is useful for creating exact replicas of your storage, but it can be slow for large drives1.

  • Deja Dup: A user-friendly graphical backup tool that comes with GNOME desktops. It simplifies the backup process and offers features like encryption and remote backups1.

  • Timeshift: A tool that creates system snapshots, allowing you to restore your system to a previous state. This is useful for recovering from system errors or unwanted changes1.

Choose the backup tool that best suits your needs and ensure you have a reliable backup before proceeding with the following steps.

Adding a New Drive

Start by physically installing the new drive in your system. This process depends on your computer's hardware, so consult your system's documentation if needed. Once the drive is installed, boot your Linux system.

To confirm that the BIOS and Linux recognize the new drive, use the following command in the terminal:


Bash



sudo lshw -C disk

This command lists all disks connected to your system, including the new one. Note the drive's path (e.g., /dev/sdb)2.

You can also use the lsblk command to list block devices and their information:


Bash



sudo lsblk

This command provides a clear overview of your storage devices, including their size, type, and mount points3.

Partitioning Schemes

Before formatting the drive, you need to decide on a partitioning scheme. Two common schemes are MBR (Master Boot Record) and GPT (GUID Partition Table).

MBR is an older scheme with limitations:

  • Supports a maximum of four primary partitions.

  • Can only handle disks up to 2 TB in size.

  • Limited error detection capabilities4.

GPT is a newer scheme that overcomes these limitations:

  • Supports a large number of partitions (typically around 128).

  • Can handle disks larger than 2 TB.

  • Offers robust error detection with CRC checks4.

For most modern systems with larger drives, GPT is the recommended partitioning scheme.

Partitioning the Disk

Now, let's partition the new drive. We'll use parted, a powerful partitioning tool, for this task.

  1. Launch parted in the terminal with the following command, replacing /dev/sdb with the path to your new drive:Bashsudo parted /dev/sdb

  2. Create a new GPT partition table:(parted) mklabel gpt

  3. Set the unit to terabytes (TB):(parted) unit tb

  4. Create a primary partition that uses the entire disk:(parted) mkpartPartition name? ? primaryFile system type? ? ext4Start? 0%End? 100%

  5. Verify the partition setup:(parted) print

  6. Save the changes and exit parted:(parted) quit

These steps create a single partition (/dev/sdb1) on your new drive2.

Formatting the Drive

With the partition created, it's time to format it with a filesystem. Linux supports various filesystems, each with its own advantages and disadvantages:

  • Ext4: A mature and stable filesystem that is the default choice for many Linux distributions. It offers good performance for general use and supports journaling for data integrity. However, it may not be the best choice for very large files or high-performance environments5.

  • XFS: A high-performance filesystem that excels at handling large files and directories. It's often used for servers and databases due to its scalability and efficient parallel I/O operations. However, it can have performance issues with many small files and might be more complex to manage5. It's worth noting that XFS can have limitations and potential issues with very large storage systems6.

  • Btrfs: A modern filesystem with advanced features like snapshots, subvolumes, and integrated RAID support. It's suitable for use cases where data integrity and flexibility are crucial. However, it's less mature than Ext4 and might have stability issues in certain scenarios5.

  • ZFS: A powerful filesystem that combines file system and volume management capabilities. It offers excellent data integrity features, including checksumming and self-healing. However, it requires substantial memory for optimal performance and has licensing restrictions that might conflict with some Linux distributions5.

  • FAT32: An older filesystem that is compatible with both Linux and Windows. It's suitable for external drives or situations where cross-platform compatibility is essential. However, it has limitations in terms of file size (maximum 4 GB) and volume size (maximum 2 TB) and lacks journaling for data protection5.

Key Insight: Choosing the right filesystem depends on your specific needs. For general use, Ext4 is a solid choice. If you need high performance for large files, XFS is a good option. Btrfs offers advanced features, while ZFS prioritizes data integrity7.

For this example, we'll format the partition with Ext4. Use the following command, replacing /dev/sdb1 with the path to your partition:


Bash



sudo mkfs -t ext4 /dev/sdb1

This command will create an Ext4 filesystem on the partition2.

Mounting the Drive

To access the formatted drive, you need to mount it to a directory. This directory acts as an entry point to the files and folders on the drive3.

  1. Create a mount point directory:Bashsudo mkdir /media/newdrive

  2. Mount the partition to the mount point:Bashsudo mount /dev/sdb1 /media/newdrive

Now you can access the new drive through the /media/newdrive directory.

Mount Options

When mounting a drive, you can use various options to control its behavior. Some common options include:

  • ro: Mounts the filesystem in read-only mode, preventing any modifications.

  • rw: Mounts the filesystem in read-write mode (default).

  • noatime: Disables the updating of access times for files, which can improve performance.

  • nosuid: Prevents the execution of setuid and setgid bits, enhancing security.

  • nodev: Disables the interpretation of device files, further improving security5.

These options can be specified in the mount command or in the /etc/fstab file.

Making the Mount Permanent

By default, the drive will be unmounted when you reboot your system. To make the mount permanent, you need to add an entry to the /etc/fstab file.

  1. Open /etc/fstab in a text editor with root privileges:Bashsudo vim /etc/fstab

  2. Add the following line to the end of the file, replacing /dev/sdb1 and /media/newdrive with your actual partition path and mount point:/dev/sdb1 /media/newdrive ext4 defaults 0 2

  3. Save the file and exit the editor.

Now, the drive will be automatically mounted every time you boot your system2.

Considerations for Partitioning

While partitioning offers flexibility in organizing your storage, it's essential to be aware of potential drawbacks:

  • Increased administration overhead: Managing multiple partitions can be more complex than having a single large partition.

  • Potential for wasted space: If you misjudge your storage needs, you might end up with one partition having excess space while another runs out.

  • Increased risk of disk full incidents: With multiple partitions, it's easier to fill up one partition while others have free space.

  • Difficulty in shrinking partitions: Reallocating space between partitions can be challenging, especially when dealing with non-LVM volumes8.

Carefully consider these factors when deciding on your partitioning strategy.

Expanding and Extending Volumes

When your existing storage space runs low, you can expand or extend volumes to utilize the new drive. Linux offers different approaches for this, each with its own advantages and considerations.

Expanding and Extending a Volume with LVM

Logical Volume Management (LVM) provides a flexible way to manage disk space. It allows you to create logical volumes that can span multiple physical disks and resize them dynamically without data loss.

Key Insight: LVM offers greater flexibility for managing disk space, especially when dealing with multiple disks or dynamic resizing needs9.

Here's how to expand an existing LVM volume:

  1. Identify the logical volume you want to extend (e.g., /dev/mapper/vg_mygroup-lv_root).

  2. Add the new physical volume (your new drive) to the volume group:Bashsudo vgextend vg_mygroup /dev/sdb1

  3. Extend the logical volume to use all available space in the volume group:Bashsudo lvextend -l +100%FREE /dev/mapper/vg_mygroup-lv_root

  4. Resize the filesystem to match the new logical volume size. If the filesystem is Ext4, use resize2fs:Bashsudo resize2fs /dev/mapper/vg_mygroup-lv_rootIf the filesystem is XFS, use xfs_growfs: 10Bashsudo xfs_growfs /dev/mapper/vg_mygroup-lv_root

These steps will expand the logical volume and the filesystem to utilize the space on the new drive9.

Expanding and Extending a Volume with fdisk

If you're not using LVM, you can still expand a partition using fdisk. However, this process is more complex and requires careful attention to avoid data loss.

Warning: This method involves deleting and recreating partitions, which can be risky. Ensure you have a backup before proceeding.

Key Insight: Using fdisk to modify partitions requires careful execution and backups, as incorrect usage can lead to data loss11.

Here's how to extend a partition with fdisk:

  1. Open fdisk with the path to your disk:Bashsudo fdisk /dev/sda

  2. Print the current partition table:(fdisk) p

  3. Delete the partition you want to extend:(fdisk) d

  4. Create a new partition with the same starting sector but a larger end sector:(fdisk) n

  5. Write the changes and exit fdisk:(fdisk) w

  6. Update the partition table in the kernel:Bashsudo partprobe

  7. Resize the filesystem:Bashsudo resize2fs /dev/sdaX

Replace /dev/sdaX with the path to your resized partition11.

Expanding and Extending a Volume with parted

Similar to fdisk, parted can also be used to resize partitions. Here's how:

  1. Start parted and select the disk:Bashsudo parted /dev/sda(parted) select /dev/sda

  2. Resize the partition:(parted) resizepart X YReplace X with the partition number and Y with the new end size.

  3. Print the partition table to verify the changes:(parted) print

  4. Quit parted:(parted) quit

  5. Resize the filesystem:Bashsudo resize2fs /dev/sdaXReplace /dev/sdaX with the path to your resized partition10.

Conclusion

Adding and formatting a new drive in Linux involves several steps, from physically installing the drive to partitioning, formatting, and mounting it. Choosing the right partitioning scheme (MBR or GPT) and filesystem (Ext4, XFS, Btrfs, ZFS, or FAT32) depends on your specific needs and system requirements.

LVM offers a more flexible approach to managing disk space, allowing for dynamic resizing and easier volume management. However, if you're not using LVM, tools like fdisk and parted can be used to expand partitions, but they require careful execution to avoid data loss.

Remember that backing up your data is essential before making any changes to your disk partitions. With proper planning and execution, you can successfully expand your storage capacity and enjoy the benefits of your new drive.

For further information, consult the man pages for the tools mentioned in this article (fdisk, parted, mkfs, resize2fs, xfs_growfs, vgextend, lvextend) or refer to the online documentation for your specific Linux distribution.

Works cited

1. What is a Hard Drive backup: Windows, Mac, Linux - ITAMG, accessed on February 12, 2025, https://www.itamg.com/data-storage/hard-drive/backup/

2. Setting up a new hard drive in Linux - Rob Allen, accessed on February 12, 2025, https://akrabat.com/setting-up-a-new-hard-drive-in-linux/

3. How do I add an additional hard drive? - Ask Ubuntu, accessed on February 12, 2025, https://askubuntu.com/questions/125257/how-do-i-add-an-additional-hard-drive

4. MBR vs GPT: Understanding Disk Partitioning Schemes - Codefinity, accessed on February 12, 2025, https://codefinity.com/blog/MBR-vs-GPT%3A-Understanding-Disk-Partitioning-Schemes

5. Linux File System: Types, Features, Limitations - phoenixNAP, accessed on February 12, 2025, https://phoenixnap.com/kb/linux-file-system

6. Change drive format to XFS : r/linux - Reddit, accessed on February 12, 2025, https://www.reddit.com/r/linux/comments/zv1tp/change_drive_format_to_xfs/

7. XFS vs. Ext4: Which Linux File System Is Better? | Pure Storage Blog, accessed on February 12, 2025, https://blog.purestorage.com/purely-educational/xfs-vs-ext4-which-linux-file-system-is-better/

8. What are the advantages and disadvantages of mounting various directories on separate partitions? - Ask Ubuntu, accessed on February 12, 2025, https://askubuntu.com/questions/516353/what-are-the-advantages-and-disadvantages-of-mounting-various-directories-on-sep

9. How to Extend Linux LVM Logical Volume - NetworkLessons.com, accessed on February 12, 2025, https://networklessons.com/miscellaneous/extend-lvm-partition

10. Extending a linux LVM partition after expanding its virtual hard drive using parted, accessed on February 12, 2025, https://davidrusseltrask.com/extending-a-linux-lvm-partition-after-expanding-its-virtual-hard-drive-using-parted/

11. Extend linux partition size without losing any data / techniques - Server Fault, accessed on February 12, 2025, https://serverfault.com/questions/994448/extend-linux-partition-size-without-losing-any-data-techniques


Feb 12

8 min read

0

0

0

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page