1.LVM简介:
LVM(Logical Volume Manager) 逻辑卷管理器:
可以动态调整磁盘容量,提高磁盘管理灵活性。
注意:绝大多数分区可以基于LVM创建,但是 /boot 挂载分区不能基于LVM创建。
LVM管理组成成份:
PV(物理卷):物理卷可以由整个硬盘也可以是独立分区转化而成。物理卷包括了许多默认大小为4MB的PE(Physical Extent)基本单元。
PE(物理拓展):lvm设备的最小存储单元。
VG(物理卷组):卷组由一个或多个物理卷组成的整体
LV( 逻辑卷):从卷组中抽出一部分空间,可以建立文件系统;直接使用的设备,可以增大缩减并保持原有的数据不变
2.创建LV逻辑卷
fdisk -l
fdisk /dev/vdb
[root@server ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe4cfbfc1
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 83 Linux
/dev/vdb3 1230848 2254847 512000 83 Linux
/dev/vdb4 2254848 20971519 9358336 5 Extended
/dev/vdb5 2256896 2461695 102400 83 Linux
/dev/vdb6 2463744 2668543 102400 83 Linux
/dev/vdb7 2670592 3694591 512000 83 Linux
[root@server ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Partition number (1-7, default 7): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe4cfbfc1
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 83 Linux
/dev/vdb3 1230848 2254847 512000 83 Linux
/dev/vdb4 2254848 20971519 9358336 5 Extended
/dev/vdb5 2256896 2461695 102400 83 Linux
/dev/vdb6 2463744 2668543 102400 8e Linux LVM
/dev/vdb7 2670592 3694591 512000 8e Linux LVM
[root@server ~]# pvcreate /dev/vdb6 ##创建PV
[root@server ~]# vgcreate vg0 /dev/vdb6 ##创建VG
[root@server ~]# lvcreate -L 20M -n lv0 vg0 ##创建LV
[root@server ~]# mkfs.xfs /dev/vg0/lv0 ##格式化建立文件系统
[root@server ~]# pvcreate /dev/vdb6
Physical volume "/dev/vdb6" successfully created
[root@server ~]# vgcreate vg0 /dev/vdb6
Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 20M -n lv0 vg0
Logical volume "lv0" created
[root@server ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=256 agcount=1, agsize=5120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=5120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server ~]# rm -rf /mnt/*
[root@server ~]# mount /dev/vg0/lv0 /mnt/
[root@server ~]# df -H /mnt/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 18M 1.2M 17M 7% /mnt
为了更方便的观察LVM的管理情况,我们采用监控命令:
watch -n 1 'pvs;echo =======;vgs;echo =======;lvs;echo =======;df -h /mnt/'
2.LV逻辑卷扩展
[root@server ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd5528270
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 821247 307200 83 Linux
/dev/vdb3 821248 1845247 512000 83 Linux
/dev/vdb4 1845248 20971519 9563136 5 Extended
/dev/vdb5 1847296 2871295 512000 83 Linux
/dev/vdb6 2873344 3078143 102400 8e Linux LVM
/dev/vdb7 3080192 3489791 204800 8e Linux LVM
[root@server ~]# pvcreate /dev/vdb6 ##新建pv物理卷/dev/vdb6
Physical volume "/dev/vdb6" successfully created
[root@server ~]# pvcreate /dev/vdb7 ##新建pv物理卷/dev/vdb7
Physical volume "/dev/vdb7" successfully created
[root@server ~]# vgcreate vg0 /dev/vdb6 ##新建物理卷组vg0
Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 20M -n lv0 vg0 ##在物理卷组vg0中新建20M的逻辑卷lv0
Logical volume "lv0" created
[root@server ~]# mkfs.xfs /dev/vg0/lv0 ##格式化,建立文件系统
meta-data=/dev/vg0/lv0 isize=256 agcount=1, agsize=5120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=5120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server ~]# mount /dev/vg0/lv0 /mnt ##挂载逻辑卷lv0到/mnt/
[root@server ~]# df ##查看挂载情况
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3154804 7319096 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12844 472076 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-lv0 17068 1088 15980 7% /mnt
##从现有的逻辑分区中扩展
[root@server ~]# vgextend vg0 /dev/vdb7 ##扩展vg0
Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 290M /dev/vg0/lv0 ##扩展lv
Rounding size to boundary between physical extents: 292.00 MiB
Extending logical volume lv0 to 292.00 MiB
Logical volume lv0 successfully resized
[root@server ~]# xfs_growfs /dev/vg0/lv0 ##扩展文件系统
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=1, agsize=5120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=5120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5120 to 74752
[root@server ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
All primary partitions are in use
Adding logical partition 8
First sector (3491840-20971519, default 3491840):
Using default value 3491840
Last sector, +sectors or +size{K,M,G} (3491840-20971519, default 20971519): +1G
Partition 8 of type Linux and of size 1 GiB is set
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@server ~]# partprobe ##同步分区表
##新建分区并扩展
[root@server ~]# fdisk /dev/vdb ##新建分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Partition number (1-8, default 8): 8
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8) ##有这个提示说明需要同步分区表
Syncing disks.
[root@server ~]# partprobe ##同步分区表
[root@server ~]# pvcreate /dev/vdb8 ##新建pv物理卷/vdb8
Physical volume "/dev/vdb8" successfully created
[root@server ~]# vgextend vg0 /dev/vdb8 ##将/vdb8添加到物理卷组vg0中
Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 1200M /dev/vg0/lv0 ##扩展逻辑卷lv0
Extending logical volume lv0 to 1.17 GiB
Logical volume lv0 successfully resized
[root@server ~]# xfs_growfs /dev/vg0/lv0 ##扩展xfs文件系统
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=15, agsize=5120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=74752, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 74752 to 307200
[root@server ~]# umount /mnt/ ##卸载/mnt
[root@server ~]# mkfs.ext4 /dev/vg0/lv0 ##更改逻辑卷文件类型(xfs不可缩减,ext4可缩减)
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
76800 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7680 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@server ~]# mount /dev/vg0/lv0 /mnt/ ##再次挂载lv0逻辑卷
[root@server ~]# df ##查看挂载情况
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3159240 7314660 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 140 484780 1% /dev/shm
tmpfs 484920 12848 472072 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-lv0 1176704 3600 1095280 1% /mnt
[root@server ~]# lvextend -L 1250M /dev/vg0/lv0 ##扩展lv0逻辑卷
Rounding size to boundary between physical extents: 1.22 GiB
Extending logical volume lv0 to 1.22 GiB
Logical volume lv0 successfully resized
[root@server ~]# resize2fs /dev/vg0/lv0
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 320512 blocks long.
[root@server ~]# mkfs.ext4 /dev/vg0/lv0 ##重建文件系统
mke2fs 1.42.9 (28-Dec-2013)
/dev/vg0/lv0 is mounted; will not make a filesystem here!
[root@server ~]# lvextend -L 1300M /dev/vg0/lv0 ## 扩展lv0逻辑卷
Extending logical volume lv0 to 1.27 GiB
Logical volume lv0 successfully resized
[root@server ~]# resize2fs /dev/vg0/lv0
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 332800 blocks long.
[root@server ~]# umount /mnt/ ##卸载/mnt下挂载的逻辑卷
3.LV逻辑卷缩减
[root@server ~]# e2fsck -f /dev/vg0/lv0 ##检测逻辑卷
e2fsck 1.42.9 (28-Dec-2013)
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/lv0: 11/84480 files (0.0% non-contiguous), 14406/332800 blocks
[root@server ~]# resize2fs /dev/vg0/lv0 1000M ##缩减文件系统
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 256000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 256000 blocks long.
[root@server ~]# mount /dev/vg0/lv0 /mnt/ ##挂载逻辑卷
[root@server ~]# lvreduce -L 1000 /dev/vg0/lv0 ##缩减逻辑卷至1000M
WARNING: Reducing active and open logical volume to 1000.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 1000.00 MiB
Logical volume lv0 successfully resized
[root@server ~]# pvmove /dev/vdb7 /dev/vdb8 ##将/vdb7上的数据转移到vdb8上,以使vdb7空闲
/dev/vdb7: Moved: 12.2%
/dev/vdb7: Moved: 100.0%
[root@server ~]# vgreduce vg0 /dev/vdb7 ##从物理卷组中移除物理卷/vdb7
Removed "/dev/vdb7" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb7 ##移除/vdb7物理卷
Labels on physical volume "/dev/vdb7" successfully wiped
4.LV逻辑卷快照
[root@server ~]# cd /mnt/
[root@server mnt]# rm -rf * ##清空/mnt
[root@server mnt]# ls
[root@server mnt]# touch file{1..9} ##新建文件
[root@server mnt]# ls
file1 file2 file3 file4 file5 file6 file7 file8 file9
[root@server mnt]# cd ~
[root@server ~]# umount /mnt/ ##卸载/mnt
[root@server ~]# cd /mnt/
[root@server mnt]# ls ##查看/mnt
[root@server mnt]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0 ##创建50M的lv0的快照
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0_snap" created
[root@server mnt]# mount /dev/vg0/lv0_snap /mnt/ ##将lv0的快照挂载到/mnt下
[root@server mnt]# ls
[root@server mnt]# cd -
/root
[root@server ~]# cd - ##重新进入文件目录
/mnt
[root@server mnt]# ls ##可以查看到文件存在
file1 file2 file3 file4 file5 file6 file7 file8 file9
[root@server mnt]# rm -rf *
[root@server mnt]# ls
[root@server mnt]# cd -
/root
[root@server ~]# umount /mnt/
[root@server ~]# lvremove /dev/vg0/lv0_snap ##移除
Do you really want to remove active logical volume lv0_snap? [y/n]: y
Logical volume "lv0_snap" successfully removed
[root@server ~]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0 ##重建快照
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0_snap" created
[root@server ~]# mount /dev/vg0/lv0_snap /mnt/
[root@server ~]# cd -
/mnt
[root@server mnt]# ls
file1 file2 file3 file4 file5 file6 file7 file8 file9
5.移除LV逻辑卷
[root@server ~]# umount /mnt ##卸载
[root@server ~]# lvremove /dev/vg0/lv0 ##移除逻辑卷lv0
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0" successfully removed
[root@server ~]# vgremove vg0 ##移除物理卷组vg0
Volume group "vg0" successfully removed
[root@server ~]# pvremove /dev/vdb6 ##移除物理卷vdb6
Labels on physical volume "/dev/vdb6" successfully wiped
[root@server ~]# pvremove /dev/vdb8 ##移除物理卷vdb8
Labels on physical volume "/dev/vdb8" successfully wiped
[root@server ~]# fdisk /dev/vdb ##删除分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1-8, default 8): 8
Partition 8 is deleted
Command (m for help): d
Partition number (1-7, default 7): 7
Partition 7 is deleted
Command (m for help): d
Partition number (1-6, default 6): 6
Partition 6 is deleted
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# fdisk -l ##查看分区情况
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd5528270
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 821247 307200 83 Linux
/dev/vdb3 821248 1845247 512000 83 Linux
/dev/vdb4 1845248 20971519 9563136 5 Extended
/dev/vdb5 1847296 2871295 512000 83 Linux
[root@server ~]# partprobe ##同步分区表
完成以上操作就可以移除LV逻辑卷