Expanding the Root Directory in Linux

I. Feature Description

When configuring Linux, insufficient space reserved for the root directory may cause programs to fail to start. While changing the data storage directory can resolve this issue, there are situations where such changes are not feasible due to various reasons. In such cases, expanding the root directory space becomes necessary.

II. Operation Steps

  1. First, use the df -h command to check the disk space usage in the system.
df -h

From the above image, it can be seen that the root directory has only 20G of space, with 5.6G currently in use. We will attempt to expand it to 36G.

  1. Using the lsblk command, we observe that the sda disk has 48G of space, but only 20G is allocated to the root directory. Next, we will allocate the remaining 16G to the root directory.

  2. Run the growpart /dev/sdb3 command to expand the sdb3 partition.

  3. Use the lvm command to expand the root directory.

  • Step 1: Enter the lvm interactive mode.

  • Step 2: Use the pvdisplay command to display all PVs (Physical Volumes). The Free PE (Free Physical Extents) value of 2815 indicates that expansion is possible.

  • Step 3: Use the vgdisplay command to display all VGs (Volume Groups).

  • Step 4: Use the lvdisplay command to display all LVs (Logical Volumes). Currently, it is 20G.

  • Step 5: Use the lvextend -l +2815 /dev/ubuntu-vg/ubuntu-lv command to expand the root directory. (lvextend -l: number of blocks, check the remaining PE count via vgdisplay; -r: recursive; -L: specify exact size)

  • Step 6: Check the LV again; the size has been updated.

  • Step 7: Exit lvm.

  1. Use the resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv command to expand the file system.

  2. Use the df -h command to verify that the root directory space has been expanded to 31G.

III. FAQ

Question 1: When using the resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv command in the final step, an error occurs: resize2fs: Bad magic number in super-block while trying to open /dev/mapper/ubuntu--vg-ubuntu--lv.

Answer 1: The XFS file system cannot be expanded using the resize2fs command. Use the xfs_growfs command instead for expansion.