About CentOS 7, divide part of the home directory into the root directory

1, Basic concepts

Cent0S 7 enables LVM2 (Logical Volume Manager) by default, and divides a hard disk of the machine into two areas sda1 and sda2, in which sda1 is mounted as a system disk / boot with a small amount of space; Sda2 acts as a physical volume and completely as a Logical Volume group VG(Volume Group) centos. In this Logical Volume group centos, three logical volumes LV (Logical Volume) root, home and swap are established and mounted to the root directory / and / home and swap respectively. The two partitions sda1 and sda2 have established file system XFS. As the default file system of red hat, file system XFS also has its consideration and has become the mainstream file system after ext3 and ext4.

Relationship of several concepts: M physical hard disks HD or partitions in physical hard disks are formed together into a logical volume group VG and storage pool, N logical volumes LV are created in the volume group VG, and a file system such as xfs is created in a logical volume Lv. The physical hard disk / partition and logical volume have the smallest basic addressing unit. The default size of CentOS7 is 4MB. They correspond one to one, similar to links or variable references. However, the relationship between the two is not always the same, because the physical hard disk may change and the logical volume will be adjusted automatically. When creating a volume group and a logical volume, the volume information VGDA (Volume Group Descriptor Area) is written at the beginning of the disk similar to creating a partition, which is used for identification. The advantage of logical volume is that it shields the physical underlying support and can be expanded and changed freely without worrying about the physical space limitations of hard disk or partition, so there will be no problems such as backing up / expanding partition and reformatting hard disk in order to expand partition size.

HD / partition -- through pvcreate - > PV -- through vgcreate (vgchange) / vgextend -- > VG -- through lvcreate / lvextend -- > LV -- through mkfs -- > FS -- through xfs_growfs, etc. -- > DF disk effective

However, the key point is that / home takes up too much space during the default installation of CentOS 7. Compared with the root directory, the root directory is much smaller (only 50G), and the OpenStack installation and storage are in the root directory. Uploading a few images may exhaust your root directory space. Unlike other file systems such as ext3, ext4 or ReiserFS, some commands (resize 2fs, resize_reiserfs) directly support reducing the size of the file system. xfs installed by default supports expanding but not reducing the space!


2, Step overview

Therefore, the following steps to be done are roughly as follows: (log in to the system directly as root user, local machine or ssh root in the past. If you use the current ordinary user, you will encounter some unnecessary trouble)

1. Back up the / home / user file. If there is nothing, ignore this step (why do you want this / home? Delete it and use root directly? Or keep it. It makes sense that it exists. Besides, don't use root only in the production environment)

2. umount /home unmount and lvremove delete the home logical volume to free its space. vgdisplay checks the available space of the volume group

3. lvcreate creates a new home volume, and mkfs establishes an xfs file system on it (allocate and mount to / home - restart without changing / etc/fstab) to copy back the contents before

(at this time, you can allocate the free space freely. You can create another logical volume, or use it directly after being idle, or you can go straight to the topic and take the fourth step below)

4. Release the previous home logical volume and allocate the remaining space after the new volume home, allocate lvextend to the root volume, and use the command xfs_growfs extends its file system space


3, Related commands

Look at the general commands

# pv

pvchange   pvck       pvcreate   pvdisplay  pvmove     pvremove   pvresize   pvs        pvscan
 

# vb

 
  1. vgcfgbackup vgchange vgconvert vgdisplay vgextend vgimportclone vgmknodes vgremove vgs
  2. vgsplit vgcfgrestore vgck vgcreate vgexport vgimport vgmerge vgreduce vgrename
  3. vgscan
# lv

 
  1. lvchange lvcreate lvextend lvmchange lvmdiskscan lvmetad lvmsar lvremove lvresize lvscan
  2. lvconvert lvdisplay lvm

4, Default installation

#df -h / / check the disk usage. The mounting format of the logical volume is / dev / mapper / volumegroupname logicalvolumename

 
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/mapper/centos-root 50G 4.8G 46G 10% /
  3. devtmpfs 3.9G 0 3.9G 0% /dev
  4. tmpfs 3.9G 140K 3.9G 1% /dev/shm
  5. tmpfs 3.9G 8.9M 3.9G 1% /run
  6. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  7. /dev/mapper/centos-home 873G 12G 862G 2% /home
  8. /dev/sda1 494M 133M 362M 27% /boot
# cat /etc/fstab
 
  1. /dev/mapper/centos-root / xfs defaults 0 0
  2. UUID=6abe266a-12fe-4361-9417-8d317bc25c8d /boot xfs defaults 0 0
  3. /dev/mapper/centos-home /home xfs defaults 0 0
  4. /dev/mapper/centos-swap swap swap defaults 0 0
#vgdisplay / / view the logical volume group

 
  1. --- Volume group ---
  2. VG Name centos
  3. System ID
  4. Format lvm2
  5. Metadata Areas 1
  6. Metadata Sequence No 6
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 3
  11. Open LV 3
  12. Max PV 0
  13. Cur PV 1
  14. Act PV 1
  15. VG Size 931.02 GiB
  16. PE Size 4.00 MiB
  17. Total PE 238341
  18. Alloc PE / Size 238325 / 930.96 GiB
  19. Free PE / Size 16 / 64.00 MiB
  20. VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk

#lvdisplay / / view the status of logical volumes. The default values are root, home, and swap

 
  1. --- Logical volume ---
  2. LV Path /dev/centos/swap
  3. LV Name swap
  4. VG Name centos
  5. LV UUID FGrPuO-2T02-1OfG-0dcz-oVTy-qcsh-IxEmd4
  6. LV Write Access read/write
  7. LV Creation host, time localhost, 2015-11-11 04:46:00 +0800
  8. LV Status available
  9. # open 2
  10. LV Size 7.75 GiB
  11. Current LE 1984
  12. Segments 1
  13. Allocation inherit
  14. Read ahead sectors auto
  15. - currently set to 256
  16. Block device 253:1
  17. --- Logical volume ---
  18. LV Path /dev/centos/home
  19. LV Name home
  20. VG Name centos
  21. LV UUID x7Oh0t-6R1x-ey7G-G5OX-PnBO-VI0C-kcRuvO
  22. LV Write Access read/write
  23. LV Creation host, time localhost, 2015-11-11 04:46:01 +0800
  24. LV Status available
  25. # open 1
  26. LV Size 873.21 GiB
  27. Current LE 223541
  28. Segments 1
  29. Allocation inherit
  30. Read ahead sectors auto
  31. - currently set to 256
  32. Block device 253:2
  33. --- Logical volume ---
  34. LV Path /dev/centos/root
  35. LV Name root
  36. VG Name centos
  37. LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs
  38. LV Write Access read/write
  39. LV Creation host, time localhost, 2015-11-11 04:46:04 +0800
  40. LV Status available
  41. # open 1
  42. LV Size 50.00 GiB
  43. Current LE 12800
  44. Segments 1
  45. Allocation inherit
  46. Read ahead sectors auto
  47. - currently set to 256
  48. Block device 253:0


5, Operation steps

1. Back up user data in / home

[root@localhost /]# mkdir /backup && mv /home/* /backup
[root@localhost /]# ls /home/

2. Unmount this / home and delete the logical volume home

# umount /home

#df -h / / check the disk status

 
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/mapper/centos-root 50G 17G 34G 33% /
  3. devtmpfs 3.9G 0 3.9G 0% /dev
  4. tmpfs 3.9G 84K 3.9G 1% /dev/shm
  5. tmpfs 3.9G 9.0M 3.9G 1% /run
  6. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  7. /dev/sda1 494M 133M 362M 27% /boot

#lvremove /dev/centos/home / / delete the logical volume home

 
  1. Do you really want to remove active logical volume home? [y/n]: y
  2. Logical volume "home" successfully removed

#vgdisplay / / view the free space of the volume group

 
  1. --- Volume group ---
  2. VG Name centos
  3. System ID
  4. Format lvm2
  5. Metadata Areas 1
  6. Metadata Sequence No 7
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 2
  11. Open LV 2
  12. Max PV 0
  13. Cur PV 1
  14. Act PV 1
  15. VG Size 931.02 GiB
  16. PE Size 4.00 MiB
  17. Total PE 238341
  18. Alloc PE / Size 14784 / 57.75 GiB
  19. Free PE / Size 223557 / 873.27 GiB
  20. VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
Note here that the space displayed in the Free PE / Size is 873G of the free space of the volume group, which was 64M before, that is, the space occupied by the logical volume home just deleted. We will take some of this, such as 50G, as home

3. Create a new volume home, format fdisk in 8e format, and make the file system xfs (also mount to / home)

#lvcreate -L 50G -n home centos //L indicates the size, and the default unit is M; n indicates volume name; CentOS here is the volume group name established by default when CentOS7 installs the system

 
  1. WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y
  2. Wiping xfs signature on /dev/centos/home.
  3. Logical volume "home" created.

#lvdisplay / / view the logical volume home

 
  1. --- Logical volume ---
  2. LV Path /dev/centos/home
  3. LV Name home
  4. VG Name centos
  5. LV UUID b5lLlv-FQPY-dT43-SmzV-sSLA-4QqP-gdQqAP
  6. LV Write Access read/write
  7. LV Creation host, time localhost, 2015-11-13 14:05:06 +0800
  8. LV Status available
  9. # open 0
  10. LV Size 50.00 GiB
  11. Current LE 12800
  12. Segments 1
  13. Allocation inherit
  14. Read ahead sectors auto
  15. - currently set to 256
  16. Block device 253:2
#vgdisplay / / check the volume group space again

 
  1. --- Volume group ---
  2. VG Name centos
  3. System ID
  4. Format lvm2
  5. Metadata Areas 1
  6. Metadata Sequence No 8
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 3
  11. Open LV 2
  12. Max PV 0
  13. Cur PV 1
  14. Act PV 1
  15. VG Size 931.02 GiB
  16. PE Size 4.00 MiB
  17. Total PE 238341
  18. Alloc PE / Size 27584 / 107.75 GiB
  19. Free PE / Size 210757 / 823.27 GiB
  20. VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk

[# vgchange -ay centos / / optional steps: activate the volume group centos to make the new home logical volume effective (use vgchange instead of lvchange)]

  3 logical volume(s) in volume group "centos" now active
 
#mkfs -t xfs /dev/centos/home / / create an xfs file system on the newly created logical volume home
 
  1. meta-data=/dev/centos/home isize=256 agcount=4, agsize=3276800 blks
  2. = sectsz=4096 attr=2, projid32bit=1
  3. = crc=0 finobt=0
  4. data = bsize=4096 blocks=13107200, imaxpct=25
  5. = sunit=0 swidth=0 blks
  6. naming =version 2 bsize=4096 ascii-ci=0 ftype=0
  7. log =internal log bsize=4096 blocks=6400, version=2
  8. = sectsz=4096 sunit=1 blks, lazy-count=1
  9. realtime =none extsz=4096 blocks=0, rtextents=0
#mount /dev/centos/home /home / / / hang the new logical volume home to the previous folder / home. Restart it directly and mount it with fstab

#df -h / / now let's check the disk usage

 
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/mapper/centos-root 50G 17G 34G 33% /
  3. devtmpfs 3.9G 0 3.9G 0% /dev
  4. tmpfs 3.9G 84K 3.9G 1% /dev/shm
  5. tmpfs 3.9G 9.0M 3.9G 1% /run
  6. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  7. /dev/sda1 494M 133M 362M 27% /boot
  8. /dev/mapper/centos-home 50G 33M 50G 1% /home
#mv /backup/* /home / / / copy the previously copied items back to the new / home. After copying, you can directly log in to the system with this ordinary user on the desktop without restarting

4. Finally, allocate the free space to the root volume and xfs_growfs extended file system
#lvextend -L +823G /dev/centos/root / / allocate the remaining 823G to the root volume, leaving a little space for it to idle+ No. indicates an additional increase on the original basis. If not, it will be set as a specific amount

 
  1. Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 873.00 GiB (223488 extents).
  2. Logical volume root successfully resized

#lvdisplay / / check the logical volume and volume group. In this case, the root space is 873G, compared with 50G before

 
  1. --- Logical volume ---
  2. LV Path /dev/centos/root
  3. LV Name root
  4. VG Name centos
  5. LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs
  6. LV Write Access read/write
  7. LV Creation host, time localhost, 2015-11-11 04:46:04 +0800
  8. LV Status available
  9. # open 1
  10. LV Size 873.00 GiB
  11. Current LE 223488
  12. Segments 2
  13. Allocation inherit
  14. Read ahead sectors auto
  15. - currently set to 256
  16. Block device 253:0
[# vgchange -ay centos / / optional steps: activate the lower volume group centos again]

#/ / do not use XFS - H_ Growfs extends the file system, and the disk does not recognize much space

 
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/mapper/centos-root 50G 4.8G 46G 10% /
  3. devtmpfs 3.9G 0 3.9G 0% /dev
  4. tmpfs 3.9G 140K 3.9G 1% /dev/shm
  5. tmpfs 3.9G 9.0M 3.9G 1% /run
  6. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  7. /dev/sda1 494M 133M 362M 27% /boot
  8. /dev/mapper/centos-home 50G 12G 39G 23% /home
# xfs_growfs /dev/centos/root / / expand the root volume

 
  1. meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=3276800 blks
  2. = sectsz=4096 attr=2, projid32bit=1
  3. = crc=0 finobt=0
  4. data = bsize=4096 blocks=13107200, imaxpct=25
  5. = sunit=0 swidth=0 blks
  6. naming =version 2 bsize=4096 ascii-ci=0 ftype=0
  7. log =internal bsize=4096 blocks=6400, version=2
  8. = sectsz=4096 sunit=1 blks, lazy-count=1
  9. realtime =none extsz=4096 blocks=0, rtextents=0
  10. data blocks changed from 13107200 to 228851712
#df -h / / the root size has taken effect. It's over 800 G

 
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/mapper/centos-root 873G 4.8G 869G 1% /
  3. devtmpfs 3.9G 0 3.9G 0% /dev
  4. tmpfs 3.9G 140K 3.9G 1% /dev/shm
  5. tmpfs 3.9G 9.0M 3.9G 1% /run
  6. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  7. /dev/sda1 494M 133M 362M 27% /boot
  8. /dev/mapper/centos-home 50G 12G 39G 23% /home


6, Summary command

To sum up, here are the following commands:

 
  1. # mkdir /backup
  2. # mv /home/* /backup/
  3. # umount /home
  4. # lvremove /dev/centos/home
  5. # lvcreate -L 50G -n home cents
  6. # mkfs -t xfs /dev/centos/home
  7. # mv /backup/* /home/
  8. # lvextend -L +xxxG /dev/centos/root
  9. # xfs_growfs root
  10. # rm -rf /backup

Tags: Linux

Posted by bharanikumarphp on Tue, 17 May 2022 14:45:00 +0300