LVM, 逻辑卷管理(Logical Volume Manager),是Linux下系统对磁盘分区进行管理的一种机制。
逻辑卷创建整体步骤:
- 创建物理分区, 在物理分区弄好物理卷
- 物理卷形成一个卷组
- 在卷组的基础上划分逻辑卷
- 格式化
- 挂载
准备磁盘分区
fdisk /dev/sdb
n创建3个分区,分别为1G
t改变分区类型为8e
[root@centos01 ~]# fdisk /dev/sdb # 分区
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 0x8720953d.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
...
# 创建三个后输入w保存分区退出,结果如下
[root@centos01 ~]# fdisk -l
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x8720953d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b933a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 8800255 4194304 82 Linux swap / Solaris
/dev/sda3 8800256 62914559 27057152 83 Linux
准备物理卷
需要安装物理卷工具 yum install -y lvm2
知道命令,不知道工具叫什么时搜索对应的工具方式 yum provides "/*/pvcreate"
pvcreate /dev/sdb1 # 如果出现Device /dev/sdb1 not found (or ignored by filtering).提示,说明没创建/dev/sdb1文件,使用partprobe命令即可生成
[root@centos01 ~]# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@centos01 ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@centos01 ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[root@centos01 ~]# pvdisplay #查看当前的物理卷,pvs命令可以查看简单物理卷信息
"/dev/sdb3" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID C5ebe2-eTk6-S6o2-1EC4-ZfnN-OCaq-nMinQ0
"/dev/sdb1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xgf99Z-OJK6-wumF-sUNu-MbTu-8fqj-qViNxN
"/dev/sdb2" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID vxsNFj-DpeA-Mhot-sp6z-QAAY-EOCx-jlo4UL
[root@centos01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g
/dev/sdb3 lvm2 --- 1.00g 1.00g
创建卷组
[root@centos01 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created
创建逻辑卷
[root@centos01 ~]# lvcreate -L 100M -n lv1 vg1
Logical volume "lv1" created.
格式化
[root@centos01 ~]# mkfs.ext4 /dev/vg1/lv1
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
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
挂载
[root@centos01 ~]# mount /dev/vg1/lv1 /mnt/
[root@centos01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 26G 1002M 25G 4% /
devtmpfs 489M 0 489M 0% /dev
tmpfs 494M 0 494M 0% /dev/shm
tmpfs 494M 6.7M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 197M 75M 123M 38% /boot
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt
# 结果中/dev/mapper/vg1-lv1与/dev/vg1/lv1指向同一个文件
[root@centos01 ~]# ls -l /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 Sep 28 10:07 /dev/vg1/lv1 -> ../dm-0
[root@centos01 ~]# ls -l /dev/mapper/vg1-lv1
lrwxrwxrwx. 1 root root 7 Sep 28 10:07 /dev/mapper/vg1-lv1 -> ../dm-0
扩容逻辑卷
lvresize -L 200M /dev/vg1/lv1 # 重新设置卷大小
[root@centos01 ~]# umount /mnt
[root@centos01 ~]# lvresize -L 200M /dev/vg1/lv1
New size (50 extents) matches existing size (50 extents).
e2fsck -f /dev/vg1/lv1 # 检查磁盘错误(ext4执行)
[root@centos01 ~]# e2fsck -f /dev/vg1/lv1
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/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks
resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行)
[root@centos01 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
缩减逻辑卷(xfs不支持)
要先umount
e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext)
[root@centos01 ~]# e2fsck -f /dev/vg1/lv1
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/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11887/204800 blocks
resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext)
[root@centos01 ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
[root@centos01 ~]# lvresize -L 100M /dev/vg1/lv1
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv1 successfully resized.
xfs扩容逻辑卷
[root@centos01 ~]# mkfs.xfs -f /dev/vg1/lv1 # 重新格式化为xfs
meta-data=/dev/vg1/lv1 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, 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
mount /dev/vg1/lv1 /mnt/
[root@centos01 ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, 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
扩展卷组
[root@centos01 ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 75 / 300.00 MiB
Free PE / Size 435 / <1.70 GiB
VG UUID wVCjMR-btBV-3oHG-zT8T-9tZF-6tRv-rSHilv
[root@centos01 ~]# vgextend vg1 /dev/sdb3
Volume group "vg1" successfully extended
[root@centos01 ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size <2.99 GiB
PE Size 4.00 MiB
Total PE 765
Alloc PE / Size 75 / 300.00 MiB
Free PE / Size 690 / <2.70 GiB
VG UUID wVCjMR-btBV-3oHG-zT8T-9tZF-6tRv-rSHilv