LVM 详解
LVM, 即逻辑管理器, 它是 Linux 环境下对磁盘分区进行管理的一种机制; 它可以将多个物理分区整合在一起, 并且可以根据实际需要动态调整文件系统空间. 另外, 它还可以提供快照, 快照在刚创建的时候, 其内容是和原卷一模一样的, 只有数据在原卷中发生变化的时候, 或者快照自身数据改变时候, 快照会将被改动数据的原数据保存起来.
在学习 LVM 之前, 需要先知道几个概念, 物理卷, 卷组, 逻辑卷, 扩展逻辑卷.
1, 物理卷 (pv)
物理卷是组成 LVM 的最底层的元素, 即 Linux 上的物理分区.
2, 卷组 (vg)
将各个独立的 PV 组合起来形成的一个存储空间就称为 VG,VG 的大小就是整个 LVM 的大小
3, 逻辑卷 (lv)
可以被用户格式化, 挂载并提供数据存储的对象就是 LV.
4, 物理扩展块
PE 相当于 Linux 分区中的 block, 它是 LVM 的最小存储单位, 默认为 4M.
- [root@localhost ~]# fdisk /dev/sdh
- 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.
- Device does not contain a recognized partition table
- Building a new DOS disklabel with disk identifier 0x12aee9aa.
- Command (m for help): n
- Partition type:
- p primary (0 primary, 0 extended, 4 free)
- e extended
- Select (default p): p
- Partition number (1-4, default 1): 1
- First sector (2048-2097151, default 2048):
- Using default value 2048
- Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +500M
- Partition 1 of type Linux and of size 500 MiB is set
- Command (m for help): n
- Partition type:
- p primary (1 primary, 0 extended, 3 free)
- e extended
- Select (default p): p
- Partition number (2-4, default 2):
- First sector (1026048-2097151, default 1026048):
- Using default value 1026048
- Last sector, +sectors or +size{K,M,G} (1026048-2097151, default 2097151): +500M
- Partition 2 of type Linux and of size 500 MiB is set
- Command (m for help): t
- Partition number (1,2, default 2): 1
- 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,2, default 2): 2
- Hex code (type L to list all codes): 8e
- Changed type of partition 'Linux' to 'Linux LVM'
- Command (m for help): w
- The partition table has been altered!
- Calling ioctl() to re-read partition table.
- Syncing disks.
- pvcreate /dev/sdh1 /dev/sdh2
- [root@localhost ~]# pvcreate /dev/sdh1 /dev/sdh2
- Physical volume "/dev/sdh1" successfully created.
- Physical volume "/dev/sdh2" successfully created.
- [root@localhost ~]# pvs /dev/sdh1 /dev/sdh2
- PV VG Fmt Attr PSize PFree
- /dev/sdh1 lvm2 --- 500.00m 500.00m
- /dev/sdh2 lvm2 --- 500.00m 500.00m
- [root@localhost ~]# pvdisplay /dev/sdh1 /dev/sdh2
- "/dev/sdh2" is a new physical volume of "500.00 MiB"
- --- NEW Physical volume ---
- PV Name /dev/sdh2
- VG Name
- PV Size 500.00 MiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID DR3W4b-mHaC-YdnN-uYm9-rIZa-8uE8-VyapNS
- [root@localhost ~]# vgcreate -s 100M vgfhj /dev/sdh1 /dev/sdh2
- Volume group "vgfhj" successfully created
- [root@localhost ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- centos 1 3 0 wz--n- 38.00g 4.00m
- vgfhj 2 0 0 wz--n- 800.00m 800.00m
- [root@localhost ~]# vgdisplay
- --- Volume group ---
- VG Name vgfhj
- System ID
- Format lvm2
- Metadata Areas 2
- Metadata Sequence No 1
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 0
- Open LV 0
- Max PV 0
- Cur PV 2
- Act PV 2
- VG Size 800.00 MiB
- PE Size 100.00 MiB
- Total PE 8
- Alloc PE / Size 0 / 0
- Free PE / Size 8 / 800.00 MiB
- VG UUID Z5HO4t-MKmX-yhQQ-NcoR-rkr4-HK6X-Xhtrd8
- [root@localhost ~]# lvcreate -L 500M -n lvfhj vgfhj
- WARNING: ext4 signature detected on /dev/vgfhj/lvfhj at offset 1080. Wipe it? [y/n]: y
- Wiping ext4 signature on /dev/vgfhj/lvfhj.
- Logical volume "lvfhj" created.
- [root@localhost ~]# lvs
- LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
- app centos -wi-ao---- 20.00g
- root centos -wi-ao---- 10.00g
- swap centos -wi-ao---- 8.00g
- lvfhj vgfhj -wi-a----- 500.00m
- [root@localhost ~]# lvdisplay
- --- Logical volume ---
- LV Path /dev/vgfhj/lvfhj
- LV Name lvfhj
- VG Name vgfhj
- LV UUID 4KNkDz-3CbQ-oAy4-ze5z-9eOK-3nwI-2Hp4pB
- LV Write Access read/write
- LV Creation host, time localhost.localdomain, 2018-08-11 17:18:00 +0800
- LV Status available
- # open 0
- LV Size 500.00 MiB
- Current LE 5
- Segments 2
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:3
- [root@localhost ~]# mkfs -t ext4 /dev/vgfhj/lvfhj
- mke2fs 1.42.9 (28-Dec-2013)
- Filesystem label=
- OS type: Linux
- Block size=1024 (log=0)
- Fragment size=1024 (log=0)
- Stride=0 blocks, Stripe width=0 blocks
- 128016 inodes, 512000 blocks
- 25600 blocks (5.00%) reserved for the super user
- First data block=1
- Maximum filesystem blocks=34078720
- 63 block groups
- [root@localhost ~]# vgextend vgfhj /dev/sdi
- Volume group "vgfhj" successfully extended
- [root@localhost ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- centos 1 3 0 wz--n- 38.00g 4.00m
- vgfhj 3 1 0 wz--n- <1.76g <1.27g
- [root@localhost ~]# pvs
- PV VG Fmt Attr PSize PFree
- /dev/sda2 centos lvm2 a-- 38.00g 4.00m
- /dev/sdh1 vgfhj lvm2 a-- 400.00m 0
- /dev/sdh2 vgfhj lvm2 a-- 400.00m 300.00m
- /dev/sdi vgfhj lvm2 a-- 1000.00m 1000.00m
- [root@localhost ~]# pvmove /dev/sdh1
- /dev/sdh1: Moved: 0.00%
- /dev/sdh1: Moved: 100.00%
- [root@localhost ~]# vgreduce vgfhj /dev/sdh1
- Removed "/dev/sdh1" from volume group "vgfhj"
- [root@localhost ~]# lvextend -L +500M /dev/vgfhj/lvfhj
- Size of logical volume vgfhj/lvfhj changed from 500.00 MiB (5 extents) to 1000.00 MiB (10 extents).
- Logical volume vgfhj/lvfhj successfully resized.
- [root@localhost ~]# resize2fs /dev/vgfhj/lvfhj
- resize2fs 1.42.9 (28-Dec-2013)
- Resizing the filesystem on /dev/vgfhj/lvfhj to 1024000 (1k) blocks.
- The filesystem on /dev/vgfhj/lvfhj is now 1024000 blocks long.
- [root@localhost ~]# umount /dev/vgfhj/lvfhj
- [root@localhost ~]# e2fsck -f /dev/vgfhj/lvfhj
- 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/vgfhj/lvfhj: 11/254000 files (0.0% non-contiguous), 42817/1024000 blocks
- [root@localhost ~]# resize2fs /dev/vgfhj/lvfhj 910M
- resize2fs 1.42.9 (28-Dec-2013)
- Resizing the filesystem on /dev/vgfhj/lvfhj to 931840 (1k) blocks.
- The filesystem on /dev/vgfhj/lvfhj is now 931840 blocks long.
- lvreduce -L [-]SIZE /path/to/lv
- [root@localhost ~]# lvreduce -L 910M /dev/vgfhj/lvfhj
- Rounding size to boundary between physical extents: 1000.00 MiB.
- New size (10 extents) matches existing size (10 extents).
- [root@localhost ~]# lvremove /dev/vgfhj/lvfhj
- Do you really want to remove active logical volume vgfhj/lvfhj? [y/n]: y
- Logical volume "lvfhj" successfully removed
- [root@localhost ~]# vgremove vgfhj
- Volume group "vgfhj" successfully removed
- [root@localhost ~]# pvs
- PV VG Fmt Attr PSize PFree
- /dev/sda2 centos lvm2 a-- 38.00g 4.00m
- /dev/sdh1 lvm2 --- 500.00m 500.00m
- /dev/sdh2 lvm2 --- 500.00m 500.00m
- /dev/sdi lvm2 --- 1.00g 1.00g
- [root@localhost ~]# pvremove /dev/sdh1 /dev/sdh2 /dev/sdi
- Labels on physical volume "/dev/sdh1" successfully wiped.
- Labels on physical volume "/dev/sdh2" successfully wiped.
- Labels on physical volume "/dev/sdi" successfully wiped.
- LVM
来源: http://www.bubuko.com/infodetail-2723813.html