Resize Root Partition & Add Volume (No Data Loss)

by SLV Team 50 views
How to Resize Root Partition and Add Another Logical Volume Without Data Loss

Hey guys! Today, we're diving into a super important topic: how to resize your root partition (/) and add another logical volume without losing any of your precious data. This is especially useful if you're running Ubuntu 16.04 (or similar) with LVM (Logical Volume Management) and find yourself running out of space on your root partition. Don't worry; we'll walk through it step-by-step. Let's get started!

Understanding the Setup

Before we jump into the commands, let's quickly understand the typical setup you might have if you installed Ubuntu with the "Erase Disk and install Ubuntu" option and selected "Use LVM with the new Ubuntu installation". Usually, you'll see something like this:

  • /dev/sda1: Mounted as /boot/efi (the EFI system partition).
  • /dev/sda2: A Linux partition that contains the LVM.

Inside /dev/sda2, LVM creates a Volume Group (VG), and within that, you'll typically have:

  • A root Logical Volume (LV) mounted as /.
  • A swap LV.

The goal here is to reduce the size of the root LV and use the freed-up space to create a new LV. Remember, backups are crucial before making any changes to partitions. It's like having a safety net – you hope you don't need it, but you'll be glad it's there if things go south. Use tools like rsync or Clonezilla to back up your important data. You can back up to an external hard drive or a network share to ensure your data is safe. Consider creating a full system image so you can restore your system to its previous state if necessary. Better safe than sorry, right?

Step-by-Step Guide to Resizing and Adding a Logical Volume

Step 1: Boot from a Live CD/USB

You can't modify partitions that are currently in use. So, the first thing you need to do is boot your system from a Live CD or USB. This gives you a working environment without mounting your hard drive's root partition. This ensures that the filesystem is not in use and can be safely modified. You can use the same Ubuntu installation media you used to install the system initially. Just boot from it and select "Try Ubuntu" to get a live environment.

Step 2: Identify Your Logical Volumes and Volume Group

Open a terminal in the live environment. You'll need to identify your Volume Group (VG) and Logical Volumes (LVs). Use the following commands:

lsblk

This command lists all block devices. Identify the LVM partition (probably /dev/sda2) and note the names of your Logical Volumes (LVs) and Volume Group (VG). The output will show you the device names, sizes, and mount points. Look for the / mount point to identify your root LV. The VG name is essential for subsequent commands.

vgs

This command displays information about Volume Groups. It shows the VG name, its size, and how much free space is available. This is useful to confirm the VG name and see the total size of the VG.

lvs

This command lists all Logical Volumes. It shows the LV names, their sizes, and which VG they belong to. Pay close attention to the root LV (the one mounted at /) and its size. This command is crucial for verifying the LV's path, which you'll need in later steps. You should see entries for the root LV and the swap LV.

Step 3: Resize the Root Logical Volume

Now, let's resize the root LV. Before resizing, it's a good idea to check the filesystem for errors.

e2fsck -f /dev/mapper/<your_vg_name>-root

Replace <your_vg_name> with your actual Volume Group name. This command checks the filesystem for errors and fixes them if possible. The -f option forces a check even if the filesystem is marked as clean. It's like giving your filesystem a health check before surgery.

Next, resize the filesystem. Let's say you want to reduce the root LV by 50GB. First, unmount the root logical volume using the command:

umount /dev/mapper/<your_vg_name>-root

Now, reduce the size of the filesystem:

resize2fs /dev/mapper/<your_vg_name>-root <new_size>

Replace <your_vg_name> with your actual Volume Group name and <new_size> with the new size you want for your root partition (e.g., 150G). This command resizes the filesystem to the specified size. Make sure to specify the size with a unit (e.g., G for gigabytes, M for megabytes). After resizing the filesystem, you can reduce the size of the logical volume:

lvreduce -L <new_size> /dev/mapper/<your_vg_name>-root

Again, replace <your_vg_name> with your actual Volume Group name and <new_size> with the same new size you used in the resize2fs command. This command reduces the size of the logical volume. It's important to use the same size as the filesystem to avoid data loss. For example:

lvreduce -L 150G /dev/mapper/ubuntu--vg-root

Important: Make sure the new size is large enough to accommodate your current data. Use the df -h / command on your running system (before booting from the live CD) to see how much space you're currently using on your root partition.

Step 4: Create a New Logical Volume

Now that you've freed up space in the Volume Group, you can create a new Logical Volume. Let's say you want to create a new LV called "data" that uses all the available free space.

lvcreate -n data -l +100%FREE <your_vg_name>

Replace <your_vg_name> with your actual Volume Group name. This command creates a new LV named "data" using all the remaining free space in the Volume Group. The -n option specifies the name of the LV, and the -l option specifies the size. +100%FREE tells LVM to use all available free space. Alternatively, you can specify a size:

lvcreate -n data -L 50G <your_vg_name>

This creates a new LV named "data" with a size of 50GB. Once the LV is created, you need to create a filesystem on it:

mkfs.ext4 /dev/mapper/<your_vg_name>-data

This command creates an ext4 filesystem on the new LV. You can choose a different filesystem if you prefer (e.g., mkfs.xfs).

Step 5: Mount the New Logical Volume

Now that you've created the new LV and formatted it, you need to mount it. First, create a mount point on your system:

mount /dev/mapper/<your_vg_name>-root /mnt
mkdir /mnt/data

Then, mount the new LV to the mount point:

mount /dev/mapper/<your_vg_name>-data /mnt/data

To make the mount permanent, add an entry to /etc/fstab. First, find the UUID of the new LV:

blkid /dev/mapper/<your_vg_name>-data

This command displays the UUID of the LV. Copy the UUID and add the following line to /etc/fstab:

UUID=<your_uuid> /data ext4 defaults 0 2

Replace <your_uuid> with the actual UUID you copied. Save the file and exit. Now, unmount everything:

umount /mnt/data
umount /mnt

Step 6: Reboot and Verify

Reboot your system. After rebooting, verify that the new LV is mounted correctly and that your root partition has the new size.

df -h

This command shows the disk space usage. Check the sizes and mount points of your partitions to ensure everything is as expected. You should see the new LV mounted at /data (or whatever mount point you chose) and the reduced size of your root partition.

Troubleshooting