[1] 创建逻辑卷

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@hyperStor ~] # vgs //查看当前卷组
VG #PV #LV #SN Attr VSize VFree
vg0 2 2 0 wz--n- 698.38g 98.29g
[root@hyperStor ~] # lvcreate -L 2GB -n lv1 vg0 //在卷组vg0上建立2GB的逻辑卷lv1
Logical volume "lv1" created
[root@hyperStor ~] # lvs |grep lv1
lv1 vg0 -wi-a- 2.00g
[root@hyperStor ~] # mkfs.ext4 /dev/vg0/lv1 > /dev/null 2>& 1 //格式化lv1
[root@hyperStor ~] # mkdir /lv1
[root@hyperStor ~] # mount /dev/vg0/lv1 /lv1 //挂载lv1到/lv1目录下
[root@hyperStor ~] # df -H |grep lv1 //查看挂载状态
/dev/mapper/vg0-lv1 2.2G 71M 2.0G 4% /lv1
[root@hyperStor ~] # mount |grep lv1
/dev/mapper/vg0-lv1 on /lv1 type ext4 (rw)
[root@hyperStor ~] # dd if=/dev/zero of=/lv1/test1 bs=100M count=5 //想/lv1中写入500M数据
5+0 records in
5+0 records out
524288000 bytes (524 MB) copied, 1.92862 s, 272 MB /s
[root@hyperStor ~] # du -sh /lv1/* //查看文件状态
16K /lv1/lost +found
500M /lv1/test1

[2] lvextend命令扩容lv1到3GB

//将lv1扩到3GB,如果是从2GB到5GB,则为lvextend -L +3GB
[root@hyperStor ~]# lvextend  -L 3GB -n  /dev/vg0/lv1
  Extending logical volume lv1 to 3.00 GiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# lvs |grep lv1  //查看当前逻辑卷大小,已经为3GB
  lv1            vg0  -wi-ao   3.00g                           
[root@hyperStor ~]# df -H |grep lv1  //查看文件系统状态,发现文件系统仍没有扩容
/dev/mapper/vg0-lv1    2.2G   595M   1.5G  30% /lv1
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  //调整文件系统大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv1 is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv1 to 786432 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 786432 blocks long.
[root@hyperStor ~]# df -H |grep lv1   //重新查看文件系统大小,成功在线扩容到3GB
/dev/mapper/vg0-lv1    3.2G   595M   2.5G  20% /lv1
[root@hyperStor ~]# lvs |grep lv1
  lv1            vg0  -wi-ao   3.00g                           
[root@hyperStor ~]# du -sh /lv1/*     //查看数据是否完整
16K/lv1/lost+found
501M/lv1/test1

[3] lvreduce 缩容,不可“在线”操作 lv1:3GB-->1GB

[root@hyperStor ~]# umount /lv1/      //卸载文件系统
[root@hyperStor ~]# mount |grep lv1|wc -l   //查看是否成功卸载
0
[root@hyperStor ~]# lvs |grep lv1  //查看当前lv1 状态
  lv1            vg0  -wi-a-   3.00g                            
[root@hyperStor ~]# e2fsck -f /dev/vg0/lv1   //进行磁盘检查
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/vg0/lv1: 12/196608 files (0.0% non-contiguous), 157501/786432 blocks
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  1G  //调整文件系统
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv1 to 262144 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 262144 blocks long.
[root@hyperStor ~]# lvs |grep lv1    //查看lv1 大小,未变
  lv1            vg0  -wi-a-   3.00g                            
[root@hyperStor ~]# lvreduce -L 1GB /dev/vg0/lv1   //通过lvduce命令进行缩容
  WARNING: Reducing active logical volume to 1.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 1.00 GiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# lvs |grep lv1   //再次查看lv1大小,减少
  lv1            vg0  -wi-a-   1.00g                            
[root@hyperStor ~]# mount /dev/vg0/lv1  /lv1/   //重新挂载
[root@hyperStor ~]# df -H |grep lv1  //查看当前状态
/dev/mapper/vg0-lv1    1.1G   595M   410M  60% /lv1
[root@hyperStor ~]# du -sh /lv1/* //查看数据情况
16K/lv1/lost+found
501M/lv1/test1

注意:在进行扩容,缩容之后进行原有文件完整性校验的时候最好通过比较文件的md5值是否保持不变以判断操作对原有文件没有造成影响

如:

【1】扩容:两次/lv1/test1 的md5值均为d8b61b2c0025919d5321461045c8226f

[root@hyperStor ~]# md5sum   /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1
[root@hyperStor ~]# lvextend -L 2GB /dev/vg0/lv1
  Extending logical volume lv1 to 2.00 GiB
  Logical volume lv1 successfully resized
r[root@hyperStor ~]# resize2fs  /dev/vg0/lv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv1 is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv1 to 524288 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 524288 blocks long.
[root@hyperStor ~]# df -H |grep lv1
/dev/mapper/vg0-lv1    2.2G   595M   1.5G  30% /lv1
[root@hyperStor ~]# md5sum   /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1

【2】缩容

[root@hyperStor ~]# md5sum   /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1
[root@hyperStor ~]# umount /lv1/
[root@hyperStor ~]# e2fsck  -f /dev/vg0/lv1
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/vg0/lv1: 12/131072 files (0.0% non-contiguous), 153389/524288 blocks
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv1 to 204800 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 204800 blocks long.
[root@hyperStor ~]# lvreduce  -L 800M /dev/vg0/lv1
  WARNING: Reducing active logical volume to 800.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 800.00 MiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# mount  /dev/vg0/lv1  /lv1
[root@hyperStor ~]# md5sum   /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1

拓展:LVM 卷组“扩容/缩容”

上面说了如果对LVM 逻辑卷进行扩容以及缩容,这边说下如何对卷组进行所谓的扩容跟缩容,大家都知道卷组由物理卷组成,物理卷是卷组的最小组成单位,那么卷组的扩容跟缩容无非就是物理卷加入卷组以及移除卷组的过程。

vgextend vg0 new_pv //将新的pv设备加入vg0

vgreduce vg0 old_pv //将pv从vg0中删除,注意:是vgreduce 不是vgremove