CentOS系统下从/home分区扩容/root分区的方法
/root Partition Using Space from /home on CentOS Alright, let's walk through this step by step—this method assumes both /root and /home are part of an LVM volume group (the default setup for most CentOS installations). If they're on separate physical disks, this won't work, but let's stick to the typical scenario.
Critical Pre-Requisite: Backup Your /home Data
Before we touch anything, backup all data in /home—this is non-negotiable. You can use tar to create a compressed backup:
tar -czf /tmp/home_backup.tar.gz /home
Or, if you have external storage, use rsync to copy files over:
rsync -av /home/ /mnt/external_drive/home_backup/
Step 1: Unmount the /home Partition
First, switch to the root user if you aren't already:
su - root
Then unmount /home. If you get a "device is busy" error, it means a user is logged into their home directory. You can force terminate processes using /home with:
fuser -km /home
Or reboot into single-user mode (safer for busy systems) and then run:
umount /home
Step 2: Shrink the /home Logical Volume
First, check the file system for errors (this is mandatory before resizing):
e2fsck -f /dev/mapper/centos-home
(Note: Replace centos-home with your actual logical volume name—find it with lvdisplay)
Next, shrink the file system itself. Let's say we want to leave 20GB for /home—adjust the size to fit your needs:
resize2fs /dev/mapper/centos-home 20G
(If your /home uses XFS (common on CentOS 7+), you can't shrink XFS. Skip to the alternative note at the bottom.)
Now shrink the logical volume to match the resized file system:
lvreduce -L 20G /dev/mapper/centos-home
Step 3: Expand the /root Partition
Now we have free space in the volume group. Let's allocate all of it to /root:
First, expand the logical volume:
lvextend -l +100%FREE /dev/mapper/centos-root
(Again, replace centos-root with your actual logical volume name if needed)
Then expand the file system to fill the new logical volume space:
- For ext4 file systems:
resize2fs /dev/mapper/centos-root
- For XFS file systems (CentOS 7+ default):
xfs_growfs /dev/mapper/centos-root
Step 4: Remount /home and Restore Data
Remount the /home partition:
mount /home
Then restore your backup data. If you used tar:
tar -xzf /tmp/home_backup.tar.gz -C /
If you used rsync:
rsync -av /mnt/external_drive/home_backup/ /home/
Step 5: Verify the Changes
Check the new partition sizes with:
df -h
You should see /root has more space, and /home is now the size you specified.
Alternative for XFS /home Partitions
Since XFS doesn't support shrinking, you'll need to delete the /home logical volume entirely:
- Backup
/homedata as before. - Unmount
/home. - Delete the logical volume:
lvremove /dev/mapper/centos-home
- Expand
/rootusing all free space (follow Step 3 above). - Create a new
/homelogical volume (e.g., 20GB):
lvcreate -L 20G -n home centos
- Format it with XFS:
mkfs.xfs /dev/mapper/centos-home
- Mount
/homeand restore your data.
内容的提问来源于stack exchange,提问作者krishna

