最近刚刚装好了一个系统,但是因为没有分好区,导致home分区过大,所以想把home分区的一大半移动到根分区里面。
1、先说一下我的环境。
安装的是centos6版本的系统,使用的默认文件系统是ext4格式的。
2、查看当前分区的大小
[root@centos6-chaofeng2 src]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 50G 5.4G 42G 12% / tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda2 ext4 477M 39M 413M 9% /boot /dev/sda1 vfat 200M 264K 200M 1% /boot/efi /dev/mapper/VolGroup-lv_home ext4 52G 24M 52G 2% /home
可以看出home分区的是相当大的。但是现在我不想使用这么大的home分区,因此我需要把一大部分size移动到根分区下。接下来我们看一下操作步骤:
3、首先是卸载home分区
[root@centos6-chaofeng2 src]# mount /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda2 on /boot type ext4 (rw) /dev/sda1 on /boot/efi type vfat (rw,umask=0077,shortname=winnt) /dev/mapper/VolGroup-lv_home on /home type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) [root@centos6-chaofeng2 src]# umount /home
mount命令可以查看当前系统设备的挂载信息。
4、强制进行磁盘检测。可以检测坏块等。
[root@centos6-chaofeng2 src]# e2fsck -f /dev/mapper/VolGroup-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/VolGroup-lv_home: 11/3497984 files (0.0% non-contiguous), 265577/13973504 blocks
5、先使用针对ext文件系统的工具调整大小。resize2fs命令专门用来管理ext4文件系统的size大小的工具
[root@centos6-chaofeng2 src]# resize2fs /dev/mapper/VolGroup-lv_home 2000M resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 512000 (4k) blocks. The filesystem on /dev/mapper/VolGroup-lv_home is now 512000 blocks long.
原来是50G的大小,现在调整为2G大小就够了。
注意:调整后的size大小必须大于原来占用整个home分区的所有文件的总大小,否则,整个home分区的数据全部丢失。
6、接着使用针对lvm管理的磁盘调整大小
[root@centos6-chaofeng2 src]# lvreduce -L 2000M /dev/mapper/VolGroup-lv_home WARNING: Reducing active logical volume to 1.95 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce VolGroup/lv_home? [y/n]: y Size of logical volume VolGroup/lv_home changed from 53.30 GiB (13646 extents) to 1.95 GiB (500 extents). Logical volume lv_home successfully resized.
7、重新挂载home分区。
[root@centos6-chaofeng2 src]# mount /dev/mapper/VolGroup-lv_home /home
8、我们将home分区从50G缩减到2G的大小。那么此时缩减出来的48G的空间怎么查看呢?
[root@centos6-chaofeng2 dev]# vgdisplay --- Volume group --- VG Name VolGroup 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 111.10 GiB PE Size 4.00 MiB Total PE 28442 Alloc PE / Size 15296 / 59.75 GiB Free PE / Size 13146 / 51.35 GiB VG UUID R2SyTs-XBry-ULRF-Qnfz-MCS8-p8CW-Cp675N
这一行的”Free PE/Size“就表示缩减掉的那部分空间。
9、现在我们把这部分空间加到根分区上。
[root@centos6-chaofeng2 dev]# lvextend -L +51.3G /dev/mapper/VolGroup-lv_root Rounding size to boundary between physical extents: 51.30 GiB. Size of logical volume VolGroup/lv_root changed from 50.00 GiB (12800 extents) to 101.30 GiB (25933 extents). Logical volume lv_root successfully resized.
10、接着使用resize2fs命令调整ext4的文件系统的size大小
[root@centos6-chaofeng2 dev]# resize2fs /dev/mapper/VolGroup-lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required old desc_blocks = 4, new_desc_blocks = 7 Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 26555392 (4k) blocks. The filesystem on /dev/mapper/VolGroup-lv_root is now 26555392 blocks long.
11、到此基本就已经完成磁盘扩容了,此时再来看看各个分区大小
[root@centos6-chaofeng2 dev]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 100G 5.4G 90G 6% / tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda2 ext4 477M 39M 413M 9% /boot /dev/sda1 vfat 200M 264K 200M 1% /boot/efi /dev/mapper/VolGroup-lv_home ext4 1.8G 24M 1.7G 2% /home
磁盘大小基本就调整完毕了。