从CentOS7默认安装的/home中转移空间到根目录/ - LVM操作简明教程
# lvdisplay //查看逻辑卷情况,默认三个,root、home和交换空间swap
[html] view plain copy- --- Logical volume ---
- LV Path /dev/centos/swap
- LV Name swap
- VG Name centos
- LV UUID FGrPuO-2T02-1OfG-0dcz-oVTy-qcsh-IxEmd4
- LV Write Access read/write
- LV Creation host, time localhost, 2015-11-11 04:46:00 +0800
- LV Status available
- # open 2
- LV Size 7.75 GiB
- Current LE 1984
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:1
- --- Logical volume ---
- LV Path /dev/centos/home
- LV Name home
- VG Name centos
- LV UUID x7Oh0t-6R1x-ey7G-G5OX-PnBO-VI0C-kcRuvO
- LV Write Access read/write
- LV Creation host, time localhost, 2015-11-11 04:46:01 +0800
- LV Status available
- # open 1
- LV Size 873.21 GiB
- Current LE 223541
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:2
- --- Logical volume ---
- LV Path /dev/centos/root
- LV Name root
- VG Name centos
- LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs
- LV Write Access read/write
- LV Creation host, time localhost, 2015-11-11 04:46:04 +0800
- LV Status available
- # open 1
- LV Size 50.00 GiB
- Current LE 12800
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:0
1. 备份/home中的用户数据
[root@localhost /]# mkdir /backup && mv /home/* /backup
[root@localhost /]# ls /home/
2. 卸载这个/home并删除逻辑卷home
# umount /home
# df -h //查看磁盘情况
[html] view plain copy- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/centos-root 50G 17G 34G 33% /
- devtmpfs 3.9G 0 3.9G 0% /dev
- tmpfs 3.9G 84K 3.9G 1% /dev/shm
- tmpfs 3.9G 9.0M 3.9G 1% /run
- tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
- /dev/sda1 494M 133M 362M 27% /boot
# lvremove /dev/centos/home //删除逻辑卷home
[html] view plain copy- Do you really want to remove active logical volume home? [y/n]: y
- Logical volume "home" successfully removed
# vgdisplay //查看卷组可用空间
[html] view plain copy- --- Volume group ---
- VG Name centos
- System ID
- Format lvm2
- Metadata Areas 1
- Metadata Sequence No 7
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 2
- Open LV 2
- Max PV 0
- Cur PV 1
- Act PV 1
- VG Size 931.02 GiB
- PE Size 4.00 MiB
- Total PE 238341
- Alloc PE / Size 14784 / 57.75 GiB
- Free PE / Size 223557 / 873.27 GiB
- VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
这里注意,Free PE / Size 中显示的空间为卷组的空闲空间873G,之前为64M,也就是刚才删除的逻辑卷home所占用的空间,我们就从这个当中拿出一部分比如50G作为home
3. 新建一个卷home,fdisk格式化为8e格式,文件系统还是搞为xfs(同样挂载到/home)
# lvcreate -L 50G -n home centos //L表示大小,默认单位为M;n表示卷名;这里的centos是CentOS7安装系统的时候就默认建立好的卷组名
[html] view plain copy- WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y
- Wiping xfs signature on /dev/centos/home.
- Logical volume "home" created.
# lvdisplay //查看逻辑卷home
[html] view plain copy- --- Logical volume ---
- LV Path /dev/centos/home
- LV Name home
- VG Name centos
- LV UUID b5lLlv-FQPY-dT43-SmzV-sSLA-4QqP-gdQqAP
- LV Write Access read/write
- LV Creation host, time localhost, 2015-11-13 14:05:06 +0800
- LV Status available
- # open 0
- LV Size 50.00 GiB
- Current LE 12800
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:2
# vgdisplay //再次查看卷组空间大小
[html] view plain copy- --- Volume group ---
- VG Name centos
- System ID
- Format lvm2
- Metadata Areas 1
- Metadata Sequence No 8
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 3
- Open LV 2
- Max PV 0
- Cur PV 1
- Act PV 1
- VG Size 931.02 GiB
- PE Size 4.00 MiB
- Total PE 238341
- Alloc PE / Size 27584 / 107.75 GiB
- Free PE / Size 210757 / 823.27 GiB
- VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
[# vgchange -ay centos //可选步骤:激活卷组centos,使得这个新建的home逻辑卷生效(用vgchange而不用lvchange)]
[html] view plain copy- 3 logical volume(s) in volume group "centos" now active
# mkfs -t xfs /dev/centos/home //在新建的逻辑卷home上建立xfs文件系统
[html] view plain copy- meta-data=/dev/centos/home isize=256 agcount=4, agsize=3276800 blks
- = sectsz=4096 attr=2, projid32bit=1
- = crc=0 finobt=0
- data = bsize=4096 blocks=13107200, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=0
- log =internal log bsize=4096 blocks=6400, version=2
- = sectsz=4096 sunit=1 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
# mount /dev/centos/home /home/ //把这个新逻辑卷home挂到之前的文件夹/home中去,直接重启用fstab来挂载也行
# df -h //现在来查看磁盘使用情况
[html] view plain copy- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/centos-root 50G 17G 34G 33% /
- devtmpfs 3.9G 0 3.9G 0% /dev
- tmpfs 3.9G 84K 3.9G 1% /dev/shm
- tmpfs 3.9G 9.0M 3.9G 1% /run
- tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
- /dev/sda1 494M 133M 362M 27% /boot
- /dev/mapper/centos-home 50G 33M 50G 1% /home
# mv /backup/* /home/ //再把之前拷出来的东西拷回新建的/home中,拷贝完成就可以直接用这个普通用户来桌面登录系统了,不用重启
4. 最后再把释放出来多余的空间分配给root卷并xfs_growfs扩展文件系统
# lvextend -L +823G /dev/centos/root //把剩下的823G现在分配给root卷,剩下那点渣渣空间让它闲着;+号表示在原来的基础上额外增加,不要加好则设定为具体额度
- Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 873.00 GiB (223488 extents).
- Logical volume root successfully resized
# lvdisplay //查看逻辑卷和卷组情况,这下root的空间就多达到873G,之前的是50G
[html] view plain copy- --- Logical volume ---
- LV Path /dev/centos/root
- LV Name root
- VG Name centos
- LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs
- LV Write Access read/write
- LV Creation host, time localhost, 2015-11-11 04:46:04 +0800
- LV Status available
- # open 1
- LV Size 873.00 GiB
- Current LE 223488
- Segments 2
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:0
[# vgchange -ay centos //可选步骤:再次激活下卷组centos]
# df -h //不使用xfs_growfs扩展文件系统,磁盘是不认得多的空间的
[html] view plain copy- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/centos-root 50G 4.8G 46G 10% /
- devtmpfs 3.9G 0 3.9G 0% /dev
- tmpfs 3.9G 140K 3.9G 1% /dev/shm
- tmpfs 3.9G 9.0M 3.9G 1% /run
- tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
- /dev/sda1 494M 133M 362M 27% /boot
- /dev/mapper/centos-home 50G 12G 39G 23% /home
# xfs_growfs /dev/centos/root //扩展root卷
[html] view plain copy- meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=3276800 blks
- = sectsz=4096 attr=2, projid32bit=1
- = crc=0 finobt=0
- data = bsize=4096 blocks=13107200, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=0
- log =internal bsize=4096 blocks=6400, version=2
- = sectsz=4096 sunit=1 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
- data blocks changed from 13107200 to 228851712
# df -h //再看root大小已经生效,800多G了
[html] view plain copy- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/centos-root 873G 4.8G 869G 1% /
- devtmpfs 3.9G 0 3.9G 0% /dev
- tmpfs 3.9G 140K 3.9G 1% /dev/shm
- tmpfs 3.9G 9.0M 3.9G 1% /run
- tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
- /dev/sda1 494M 133M 362M 27% /boot
- /dev/mapper/centos-home 50G 12G 39G 23% /home
六、命令汇总
简单总结下,就是下面几条命令:
[html] view plain copy- # mkdir /backup
- # mv /home/* /backup/
- # umount /home
- # lvremove /dev/centos/home
- # lvcreate -L 50G -n home cents
- # mkfs -t xfs /dev/centos/home
- # mv /backup/* /home/
- # lvextend -L +xxxG /dev/centos/root
- # xfs_growfs root
- # rm -rf /backup