逻辑卷的扩展和收缩

LVM(logical volume)的一个重要的功能就是可以随意扩展和收缩分区的大小。下面简单模拟这两种情形。

1. 扩展逻辑卷

当前vg大小
# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 2 0 0 wz--n- 19.94g 19.94g

创建一个逻辑卷
# lvcreate -n lv01 -L 5G -i 2 vg01
Using default stripesize 64.00 KiB
Logical volume "lv01" created

创建文件系统并挂载该分区
# mkfs.ext4 /dev/mapper/vg01-lv01
......
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
# mkdir /mnt/lv01
# mount /dev/mapper/vg01-lv01 /mnt/lv01/

生成一些测试文件
# dd if=/dev/zero of=/mnt/lv01/test.data bs=1M count=3000
3000+0 records in
3000+0 records out
3145728000 bytes (3.1 GB) copied, 6.47174 s, 486 MB/s

下面对这个逻辑卷进行在线扩展
# lvextend -l +60 /dev/mapper/vg01-lv01
Using stripesize of last segment 64.00 KiB
Extending logical volume lv01 to 6.88 GiB
Logical volume lv01 successfully resized

再扩展ext4文件系统
# resize2fs /dev/mapper/vg01-lv01
......
Performing an on-line resize of /dev/mapper/vg01-lv01 to 1802240 (4k) blocks.
The filesystem on /dev/mapper/vg01-lv01 is now 1802240 blocks long.

经过df查看,该文件系统已经被成功在线扩展。
# df -h /dev/mapper/vg01-lv01
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg01-lv01
6.8G 3.1G 3.4G 48% /mnt/lv01

2. 收缩逻辑卷

为了保证数据的完整性,收缩逻辑卷需要将逻辑卷分区umount
先计算一下测试文件的校验和
# md5sum test.data
4322d422199eaccfbd36feb3da955b16 test.data

查看当前分区被哪些进程占用
# lsof /mnt/lv01
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 3191 root cwd DIR 252,3 4096 2 /mnt/lv01

然后需要手工停掉这些进程,或者使用fuser -k /mnt/lv01直接杀掉这些进程。
# fuser -k /mnt/lv01
/mnt/lv01: 3191c
# umount /mnt/lv01

执行文件系统校验
# e2fsck -f /dev/mapper/vg01-lv01
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/vg01-lv01: 12/450560 files (0.0% non-contiguous), 831934/1802240 blocks

将文件系统缩小到6.6G
# resize2fs /dev/mapper/vg01-lv01 6758M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lv01 to 1730048 (4k) blocks.
The filesystem on /dev/mapper/vg01-lv01 is now 1730048 blocks long.

将逻辑卷缩小到6.6G
# lvreduce -L 6758M /dev/mapper/vg01-lv01
Rounding size to boundary between physical extents: 6.62 GiB
WARNING: Reducing active logical volume to 6.62 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
Reducing logical volume lv01 to 6.62 GiB
Logical volume lv01 successfully resized

挂载分区并验证
[root@db02 mapper]# mount /dev/mapper/vg01-lv01 /mnt/lv01/
[root@db02 mapper]# md5sum /mnt/lv01/test.data
4322d422199eaccfbd36feb3da955b16 /mnt/lv01/test.data

Posted in Linux.