如何在运行中的Linux服务器上通过缩减/home分区来扩容/分区
Hey there, I see you're stuck with a nearly full root partition while /home has tons of unused space—let's work through this fix step by step. First, let's confirm your current disk setup from the output you shared:
[root@myserver ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 63G 0 63G 0% /dev tmpfs 63G 5.2M 63G 1% /dev/shm tmpfs 63G 4.0G 59G 7% /run tmpfs 63G 0 63G 0% /sys/fs/cgroup /dev/mapper/almalinux-root 70G 43G 28G 61% / none 63G 6.5M 63G 1% /var/lve/dbgovernor-shm /dev/nvme0n1p2 1014M 352M 663M 35% /boot /dev/nvme0n1p1 599M 5.8M 594M 1% /boot/efi /dev/mapper/almalinux-home 1.8T 185G 1.6T 11% /home [root@myserver ~]#
Perfect, your /home has 1.6T free space, so we can safely shrink it to give more breathing room to the root partition. Here's how to do it without breaking anything (promise, just follow along carefully):
Backup /home data first (this is non-negotiable!)
Partition tweaks carry risk, so don't skip this. Create a compressed backup to a temporary location (or better, external storage if you have it):tar -czf /tmp/home_backup.tar.gz /homeIf you prefer rsync for incremental backups, use:
rsync -av /home /path/to/external/drive/home_backupUnmount the /home partition
First, make sure no users or processes are using /home. Check for active processes with:lsof /homeAsk users to log out or kill those processes gracefully. If you can't do that, force unmount (use this only as a last resort):
fuser -km /home umount /homeRun
df -hagain to confirm /home is no longer listed.Shrink the /home logical volume
First, check and repair the filesystem to avoid corruption:e2fsck -f /dev/mapper/almalinux-homeNow shrink the filesystem itself. Let's leave 500G for /home (since you only use 185G, this gives you plenty of spare space and frees up ~1.3T for root):
resize2fs /dev/mapper/almalinux-home 500GThen shrink the logical volume to match the filesystem size:
lvreduce -L 500G /dev/mapper/almalinux-homeYou'll get a warning—type
yto confirm if you're confident in your setup.Expand the root logical volume
Allocate all the freed-up space to your root partition with:lvextend -l +100%FREE /dev/mapper/almalinux-rootIf you want to add a specific amount instead (e.g., 400G), use:
lvextend -L +400G /dev/mapper/almalinux-rootResize the root filesystem to use the new space
Finally, expand the filesystem to utilize the newly allocated space:resize2fs /dev/mapper/almalinux-rootRemount /home and verify changes
Mount your /home partition back:mount /homeRun
df -hone last time—you should see your root partition has way more free space, and /home is at the size you set.
Quick heads-up for XFS users
If your filesystems are XFS instead of ext4, XFS doesn't support shrinking. In that case, you'll need to: backup /home → delete the /home logical volume → expand the root volume → create a new /home logical volume → format it → restore your data.
Also, to prevent this from happening again, set up automatic log rotation for Apache with logrotate—it'll archive old logs and keep your root partition from filling up unexpectedly.
备注:内容来源于stack exchange,提问作者MinHeap

