In this post, I will show you how to properly extend Linux root volume after installing a new hard disk and avoid and data loss.
Expanding the root volume on a Linux system is a common task, especially when your server is running out of space and you need to add additional storage. In this guide, I will walk you through the steps to extend the root volume after adding a new disk to your Linux system.
Prerequisites
Before proceeding, ensure that you have:
- Added a new disk to your Linux system.
- Root or sudo privileges to execute the necessary commands.
Step 1: Identify the New Disk
First, let's identify the new disk that was added to the system using the "lsblk" command:
lsblk{codeBox}
This command will display a list of all available block devices, helping you to identify the new disk (e.g., "/dev/sdb").
Step 2: Prepare the Disk for Use
To start using the new disk, you need to initialize it as a physical volume using "pvcreate":
sudo pvcreate /dev/sdb{codeBox}
This command prepares "/dev/sdb" for use by the Logical Volume Manager (LVM).
Step 3: Verify Volume Group Information
Next, check the current status of your volume groups with "vgdisplay":
sudo vgdisplay{codeBox}
This command provides an overview of the volume groups on your system, including the free physical extents available.
Step 4: Extend the Volume Group
Now, extend your existing volume group (replace "ubuntu-vg" with your volume group name) to include the new physical volume:
sudo vgextend ubuntu-vg /dev/sdb{codeBox}
This command adds the new disk to your volume group, making its space available for use.
Step 5: Extend the Logical Volume
With the volume group extended, you can now expand the logical volume that corresponds to your root file system:
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv{codeBox}
This command extends the logical volume to use all the free space available in the volume group. (replace "ubuntu--vg-ubuntu--lv" with your logical volume name, you can identify the name by using the command "df -h").
Step 6: Resize the File System
Finally, resize the file system to occupy the newly available space on the logical volume:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv{codeBox}
This command adjusts the file system size to match the newly extended logical volume.
During the process of extending a logical volume, you might encounter the following error when trying to resize the filesystem with "resize2fs":
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-home
Couldn't find valid filesystem superblock.
This error occurs because resize2fs is used for resizing "ext4" filesystems, but if your filesystem is "xfs", you'll need to use a different command.{alertError}
To resize an "xfs" filesystem, use the "xfs_growfs" command with the mount point instead of the device path:
sudo xfs_growfs /home{codeBox}
This command will properly resize the "xfs" filesystem to utilize the newly allocated space in the logical volume.
Step 7: Verify the Changes
To confirm that the root volume has been successfully extended, check the disk usage with the "df -h" command:
df -h{codeBox}
You should now see that the root volume has increased in size, reflecting the addition of the new disk space.
If you are working in a virtual environment and have extended the current disk size from the virtual machine settings instead of adding a new hard disk, you can start directly from (Step 5).{alertInfo}
Always remember to back up your data before performing any disk operations to avoid accidental data loss.{alertWarning}
Conclusion
By following these steps, you've successfully extended your Linux root volume after adding a new disk. This process is crucial for managing storage efficiently on your Linux systems, ensuring that you have enough space to accommodate growing data needs.
Feel free to adjust any specific details, such as volume group names, based on your environment. This method is applicable to most Linux distributions that use LVM for managing disk partitions.