1、管理PV物理卷
- pvs #显示PV的简要信息
- pvdisplay #显示所有PV的属性
- pvcreate /dev/sdf1 #创建PV
- pvremove #删除PV
- pvmove #移动PV中的数据
2、管理VG卷组
- vgs #显示vg的信息
- vgdisplay #显示VG的详细信息
- vgcreate myvg /dev/sdf1 #将sdf1这个PV创建了一个·vg,加-s可指定PE大小
- vgremove #删除VG
- vgreduce #从VG中移除PV
- vgextend #将pv添加到VG中
3、管理LV逻辑卷
- lvs #显示lv的信息
- lvdisplay #显示lv的详细信息
- lvcreate -L 5G -n mylv myvg #-L创建5G空间,-n命名为mylv,-l可以指定分配多少PE给lv
- lvcreate -l 100%FREE -n mylv myvg #分配所有vg空间给lv
- lvremove #删除lv
- lvreduce #减少LV的容量
- lvextend #增加LV的容量
- lvresize #改变LV的容量
4、格式化LV为文件系统
- mkfs.ext4 /dev/myvg/mylv #格式化为ext4
- mkfs.xfs /dev/myvg/mylv #格式化为xfs格式
- mount /dev/myvg/mylv /home/test #挂载
5、EXT系列文件系统
5.1、LVM磁盘扩容操作(VG还有空间的情况,无空间需要将其他磁盘格式化成PV,在加入需要扩容的VG组里面)
- umount /dev/myvg/mylv #卸载设备
- lvextend/lvresize -L +5G /dev/myvg/mylv #按照容量大小添加5G
- lvextend/lvresize -l +200 /dev/myvg/mylv #按照PE大小添加200PE,每个PE默认4M
- resize2fs /dev/myvg/mylv 20G #20G是你希望扩大到的空间大小(EXT系列文件系统扩容)
5.2、LVM磁盘收缩操作
- umount /dev/myvg/mylv #卸载设备
- e2fsck -f /dev/myvg/mylv #磁盘检查
- resize2fs /dev/myvg/mylv 10G #收缩文件系统为10G(EXT)
- vgreduce myvg /dev/sdf2 #从VG移除空闲PV
- pvremove /dev/sdf2 #移除该PV
6、XFS系列文件系统(XFS文件系统只能扩容,无法收缩)
6.1 LVM磁盘扩容操作
- mount /dev/myvg/mylv /home/test #挂载
- lvextend -L +10G /dev/myvg/mylv #扩容10G空间
- xfs_growfs /home/test #xfs可以在挂载的情况下扩容。
6.2 vg容量不足可以将其他PV加入到VG中,然后再扩容
- vgextend myvg /dev/sde1 #pv sde1加入到myvg
- lvextend -L +10G /dev/myvg/mylv #再扩容10G空间
- xfs_growfs /home/test #文件系统扩容
7、EXT4系列文件扩容与缩小案例
- 创建分区sdf1
- 创建pv,vg,lv
[root@localhost scripts]# pvcreate /dev/sdf1 Physical volume "/dev/sdf1" successfully created. [root@localhost scripts]# pvs PV VG Fmt Attr PSize PFree /dev/sdc3 centos lvm2 a-- 10.91t 0 /dev/sdf1 lvm2 --- 10.00g 10.00g [root@localhost scripts]# vgcreate myvg /dev/sdf1 Volume group "myvg" successfully created [root@localhost scripts]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 10.91t 0 myvg 1 0 0 wz--n- <10.00g <10.00g [root@localhost scripts]# lvcreate -L 5G -n mylv myvg WARNING: ext4 signature detected on /dev/myvg/mylv at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/myvg/mylv. Logical volume "mylv" created. [root@localhost scripts]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 10.86t root centos -wi-ao---- 50.00g swap centos -wi-ao---- 4.00g mylv myvg -wi-a----- 5.00g
-
格式化lv为ext4文件系统
[root@localhost scripts]# mkfs.ext4 /dev/myvg/mylv 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 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
- 挂载,mount /dev/myvg/mylv /home/test/
- 查看
[root@localhost scripts]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 378G 0 378G 0% /dev tmpfs 378G 100K 378G 1% /dev/shm tmpfs 378G 20M 378G 1% /run tmpfs 378G 0 378G 0% /sys/fs/cgroup /dev/mapper/centos-root 50G 8.9G 42G 18% / /dev/sdc2 1014M 220M 795M 22% /boot /dev/sdc1 200M 12M 189M 6% /boot/efi /dev/mapper/centos-home 11T 197M 11T 1% /home tmpfs 76G 48K 76G 1% /run/user/0 /dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /home/test
-
lv逻辑卷扩容,在vg还有容量的情况,扩展2G,在vg容量不足时可以先在其他磁盘创建PV,再加入到刚刚创建的myvg,再进行扩容操作。
[root@localhost scripts]# umount /home/test [root@localhost scripts]# lvextend -L +2G /dev/myvg/mylv Size of logical volume myvg/mylv changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents). Logical volume myvg/mylv successfully resized. [root@localhost scripts]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 10.86t root centos -wi-ao---- 50.00g swap centos -wi-ao---- 4.00g mylv myvg -wi-a----- 7.00g
-
上一步只是扩容的lv,现在扩容文件系统,查看已经有7G空间了
[root@localhost scripts]# e2fsck -f /dev/myvg/mylv 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/myvg/mylv: 11/327680 files (0.0% non-contiguous), 58462/1310720 blocks [root@localhost scripts]# resize2fs /dev/myvg/mylv resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv is now 1835008 blocks long. [root@localhost scripts]# mount /dev/myvg/mylv /home/test/ [root@localhost scripts]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 378G 0 378G 0% /dev tmpfs 378G 100K 378G 1% /dev/shm tmpfs 378G 20M 378G 1% /run tmpfs 378G 0 378G 0% /sys/fs/cgroup /dev/mapper/centos-root 50G 8.9G 42G 18% / /dev/sdc2 1014M 220M 795M 22% /boot /dev/sdc1 200M 12M 189M 6% /boot/efi /dev/mapper/centos-home 11T 197M 11T 1% /home tmpfs 76G 48K 76G 1% /run/user/0 /dev/mapper/myvg-mylv 6.8G 23M 6.4G 1% /home/test
-
缩小容量为5G,先缩小文件系统,再缩小lv,缩小文件系统数据丢失风险大
[root@localhost scripts]# e2fsck -f /dev/myvg/mylv 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/myvg/mylv: 11/458752 files (0.0% non-contiguous), 67327/1835008 blocks [root@localhost scripts]# resize2fs /dev/myvg/mylv 5G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv to 1310720 (4k) blocks. The filesystem on /dev/myvg/mylv is now 1310720 blocks long. [root@localhost scripts]# lvreduce -L -2G /dev/myvg/mylv WARNING: Reducing active logical volume to 5.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylv? [y/n]: y Size of logical volume myvg/mylv changed from 7.00 GiB (1792 extents) to 5.00 GiB (1280 extents). Logical volume myvg/mylv successfully resized. [root@localhost scripts]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 10.86t root centos -wi-ao---- 50.00g swap centos -wi-ao---- 4.00g mylv myvg -wi-a----- 5.00g
8、XFS文件系统扩容案例
- fdisk /dev/sdf,创建sdf2
- 创建pv,vg,lv,文件系统,挂载
[root@localhost scripts]# pvcreate /dev/sdf2 Physical volume "/dev/sdf2" successfully created. [root@localhost scripts]# vgcreate myvg1 /dev/sdf2 Volume group "myvg1" successfully created [root@localhost scripts]# lvcreate -L +5G -n mylv1 myvg1 Logical volume "mylv1" created. [root@localhost scripts]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 10.86t root centos -wi-ao---- 50.00g swap centos -wi-ao---- 4.00g mylv myvg -wi-a----- 5.00g mylv1 myvg1 -wi-a----- 5.00g [root@localhost scripts]# mkfs.xfs /dev/myvg1/mylv1 meta-data=/dev/myvg1/mylv1 isize=512 agcount=4, agsize=327680 blks = sectsz=4096 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=1310720, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=4096 sunit=1 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost scripts]# mount /dev/myvg1/mylv1 /home/test/ [root@localhost scripts]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 378G 0 378G 0% /dev tmpfs 378G 100K 378G 1% /dev/shm tmpfs 378G 20M 378G 1% /run tmpfs 378G 0 378G 0% /sys/fs/cgroup /dev/mapper/centos-root 50G 8.9G 42G 18% / /dev/sdc2 1014M 220M 795M 22% /boot /dev/sdc1 200M 12M 189M 6% /boot/efi /dev/mapper/centos-home 11T 197M 11T 1% /home tmpfs 76G 48K 76G 1% /run/user/0 /dev/mapper/myvg1-mylv1 5.0G 33M 5.0G 1% /home/test
-
XFS文件系统扩容,先扩容lv,再扩容文件系统,xfs文件系统可以不卸载扩容
[root@localhost scripts]# lvextend -L +2G /dev/myvg1/mylv1 Size of logical volume myvg1/mylv1 changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents). Logical volume myvg1/mylv1 successfully resized. [root@localhost scripts]# xfs_growfs /home/test/ meta-data=/dev/mapper/myvg1-mylv1 isize=512 agcount=4, agsize=327680 blks = sectsz=4096 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=1310720, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=4096 sunit=1 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 1310720 to 1835008 [root@localhost scripts]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 378G 0 378G 0% /dev tmpfs 378G 100K 378G 1% /dev/shm tmpfs 378G 20M 378G 1% /run tmpfs 378G 0 378G 0% /sys/fs/cgroup /dev/mapper/centos-root 50G 8.9G 42G 18% / /dev/sdc2 1014M 220M 795M 22% /boot /dev/sdc1 200M 12M 189M 6% /boot/efi /dev/mapper/centos-home 11T 197M 11T 1% /home tmpfs 76G 48K 76G 1% /run/user/0 /dev/mapper/myvg1-mylv1 7.0G 33M 7.0G 1% /home/test
xfs文件系统一般不能缩小容量操作,除非重新格式化,数据会丢失无意义。