How to resize the root directory under Linux

  • 2020-05-30 21:44:56
  • OfStack

Purpose 1.

When using CentOS6.3 version of Linux system, it was found that the space of the root directory (/) was not enough, while the other directory Spaces had a lot of free space, so this article was mainly adjusted for the existing space. First, let's check the space allocation of the system:


[root@CentOS-78 /]# df -h 
Filesystem      Size Used Avail Use% Mounted on 
/dev/mapper/vg_centos-lv_root 
            50G  14G  34G 30% / 
tmpfs         1.9G   0 1.9G  0% /dev/shm 
/dev/sda1       485M  37M 423M  8% /boot 
/dev/mapper/vg_centos-lv_home 
           404G 670M 382G  1% /home 

The detailed steps below add 100G from the vg_centos-lv_home partition to the/vg_centos-lv_root partition.

2. Detailed steps

1. Uninstall the vg_centos-lv_home partition


[root@CentOS-78 /]# umount /home 

If you use the df command at this point, you will notice that the /home directory is no longer visible, as shown below:


[root@CentOS-78 /]# df -h 
Filesystem      Size Used Avail Use% Mounted on 
/dev/mapper/vg_centos-lv_root 
            50G  14G  34G 30% / 
tmpfs         1.9G   0 1.9G  0% /dev/shm 
/dev/sda1       485M  37M 423M  8% /boot 

2. Reset the size of vg_home-lv_home


[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G 
resize2fs 1.41.12 (17-May-2010) 
Please run 'e2fsck -f /dev/mapper/vg_centos-lv_home' first. 

This step did not succeed in setting the size of vg_home-lv_home. We were prompted to run the following command first. The operation is as follows:


[root@CentOS-78 /]# e2fsck -f /dev/mapper/vg_centos-lv_home 
e2fsck 1.41.12 (17-May-2010) 
Pass 1: Checking inodes, blocks, and sizes 
Pass 2: Checking directory structure 
Pass 3: Checking directory connectivity 
Pass 4: Checking reference counts 
Pass 5: Checking group summary information 
/dev/mapper/vg_centos-lv_home: 1386/26836992 files (0.9% non-contiguous), 1855856/107344896 blocks 

Reset the size of vg_home-lv_home:


[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G 
resize2fs 1.41.12 (17-May-2010) 
Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 73924608 (4k) blocks. 
Begin pass 2 (max = 43) 
Relocating blocks       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Begin pass 3 (max = 3276) 
Scanning inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Begin pass 4 (max = 266) 
Updating inode references   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
The filesystem on /dev/mapper/vg_centos-lv_home is now 73924608 blocks long. 

Check for success:


[root@CentOS-78 /]# mount /home 
[root@CentOS-78 /]# 
[root@CentOS-78 /]# df -h 
Filesystem      Size Used Avail Use% Mounted on 
/dev/mapper/vg_centos-lv_root 
            50G  14G  34G 30% / 
tmpfs         1.9G   0 1.9G  0% /dev/shm 
/dev/sda1       485M  37M 423M  8% /boot 
/dev/mapper/vg_centos-lv_home 
           278G 663M 263G  1% /home 
[root@CentOS-78 /]# 

Reduce the lv_home logical partition to the specified size


[root@CentOS-78 /]# lvreduce -L 282G /dev/mapper/vg_centos-lv_home 
 WARNING: Reducing active and open logical volume to 282.00 GiB 
 THIS MAY DESTROY YOUR DATA (filesystem etc.) 
Do you really want to reduce lv_home? [y/n]: y 
 Reducing logical volume lv_home to 282.00 GiB 
 Logical volume lv_home successfully resized 
[root@CentOS-78 /]# 

I think this command is to take the reduced 100G space to the common area of the system, which can be loaded and utilized by other partitions.

Query volume group information:


[root@CentOS-78 /]# vgdisplay 
 --- Volume group --- 
 VG Name        vg_centos 
 System ID 
 Format        lvm2 
 Metadata Areas    1 
 Metadata Sequence No 5 
 VG Access       read/write 
 VG Status       resizable 
 MAX LV        0 
 Cur LV        3 
 Open LV        3 
 Max PV        0 
 Cur PV        1 
 Act PV        1 
 VG Size        465.27 GiB 
 PE Size        4.00 MiB 
 Total PE       119109 
 Alloc PE / Size    86472 / 337.78 GiB 
 Free PE / Size    32637 / 127.49 GiB 
 VG UUID        1k4ooN-RFV9-uyf1-uMYf-aERG-YaGs-ZNoSD6 

Free PE/Size should specify the space now available for allocation.

4. Increase the size of the vg_centos-lv_root partition

Add the available space to the vg_centos-lv_root partition:


[root@CentOS-78 /]# lvextend -L +127.40G /dev/mapper/vg_centos-lv_root 
 Rounding up size to full physical extent 127.40 GiB 
 Extending logical volume lv_root to 177.40 GiB 
 Logical volume lv_root successfully resized 
[root@CentOS-78 /]# 

Resize the partition:


[root@CentOS-78 /]# umount /home 
0

5. Check the partition size again


[root@CentOS-78 /]# umount /home 
1

We found that the space of the vg_centos-lv_root partition has been increased by 125G, which is more than the space reduction of lv_home partition by 25G mainly because we added all available space of the system to the lv_root partition.

3. Problems encountered

1. Failed to uninstall /home directory

You can execute the following fuser command and then umount:


[root@CentOS-78 /]# umount /home 
2

2. After setting the size of lv_home, it is found that the partition cannot be seen with df command when mount is partitioned again

3. When setting the size of lv_root, do not use all the space of Free PE/Size. It is very likely that there will be insufficient space of Free PE.


Related articles: