1、创建一个物理卷
[root@docker tmp]# pvcreate /dev/sda{5..6}
Physical volume "/dev/sda5" successfully created
Physical volume "/dev/sda6" successfully created
[root@docker tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 lvm2 --- g g
/dev/sda6 lvm2 --- g g
[root@docker tmp]# pvscan
PV /dev/sda5 lvm2 [ GiB]
PV /dev/sda6 lvm2 [ GiB]
Total: [ GiB] / in use: [ ] / in no VG: [ GiB]
2、创建一个卷组
[[email protected] tmp]# vgcreate vg0 /dev/sda{5..6}
Volume group "vg0" successfully created
[[email protected] tmp]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 wz--n- g g
[[email protected] tmp]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "vg0" using metadata type lvm2
3、创建一个逻辑卷
[root@docker tmp]# lvcreate -L +1G -nlv0 /dev/vg0
Logical volume "lv0" created
[root@docker tmp]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv vg -wi-a----- g
[root@docker tmp]# lvscan
ACTIVE '/dev/vg0/lv0' [ GiB] inherit
4、格式化逻辑卷
[[email protected] tmp]# mkfs.ext4 /dev/vg0/lv0
mke2fs (-May-)
文件系统标签=
操作系统:Linux
块大小= (log=)
分块大小= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (%) reserved for the super user
第一个数据块=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , ,
正在写入inode表: 完成
Creating journal ( blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.
5、挂载逻辑卷
[root@docker ~]# mount /dev/vg0/lv0 /luyun
[root@docker ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 48G 4.5G 42G 10% /
tmpfs tmpfs M K M % /dev/shm
/dev/mapper/vg0-lv0 ext4 M M M % /luyun
6、扩展逻辑卷大小
[[email protected] ~]# lvextend -L +500M /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from GiB ( extents) to GiB ( extents).
Logical volume lv0 successfully resized
[[email protected] ~]# resize2fs /dev/vg0/lv0
resize2fs (-May-)
Filesystem at /dev/vg0/lv0 is mounted on /luyun; on-line resizing required
old desc_blocks = , new_desc_blocks =
Performing an on-line resize of /dev/vg0/lv0 to (k) blocks.
The filesystem on /dev/vg0/lv0 is now blocks long.
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 1.49g
7、压缩逻辑卷为800M
[[email protected] ~]# umount /luyun
[[email protected] ~]# fsck /dev/vg0/lv0 #检查逻辑卷里的数据
fsck from util-linux-ng
e2fsck (-May-)
/dev/mapper/vg0-lv0: clean, / files, / blocks
[[email protected] ~]# e2fsck -f /dev/vg0/lv0 #检查文件系统大小
e2fsck (-May-)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第步: 检查目录连接性
Pass : Checking reference counts
第步: 检查簇概要信息
/dev/vg0/lv0: / files (% non-contiguous), / blocks
[[email protected] ~]# resize2fs /dev/vg0/lv0 800M #把文件系统减小为800M
resize2fs (-May-)
Resizing the filesystem on /dev/vg0/lv0 to (k) blocks.
The filesystem on /dev/vg0/lv0 is now blocks long.
[[email protected] ~]# lvresize -L 800M /dev/vg0/lv0 #把逻辑卷减小为800M
WARNING: Reducing active logical volume to MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from GiB ( extents) to MiB ( extents).
Logical volume lv0 successfully resized
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 800.00m
注意:+M:在原有的大小上再加上M ; M:不管原来有多大,最后的大小就是M
8、当卷组空间分完后,须重新添加个物理分区,把物理分区做成物理卷加入卷组
fdisk /dev/sda #创建一个/dev/sda7
partx -a /dev/sda
pvcreate /dev/sda7
vgextend vg0 /dev/sda7 这样卷组的空间就会增大
9、删除逻辑卷、卷组、物理卷
、先手动卸载挂载点,然后把/etc/fstab中的开机自动挂载删除,再重新加载挂载表mount -a,
、lvremove /dev/vg0/* 卸载/dev/vg0/下所有的逻辑卷
、vgremove vg0 卸载卷组vg0 ;
、pvremove /dev/sdb{12..14} 卸载物理卷
10、自动挂载逻辑卷脚本
device=`fdisk -l 2> /dev/null | grep "Disk /dev/" | sort | uniq -c | sort -nr | head -1 | awk '{print $3}' | awk -F: '{print $1}'`
pvcreate $device
vgcreate vg1 $device
vgfree=`vgdisplay | grep "Free" | awk '{print $5}'`
lvcreate -l $vgfree -n lv1 vg1
mkfs.ext4 -j /dev/vg1/lv1
tune2fs -c 0 -i 0 /dev/vg1/lv1
echo "/dev/vg1/lv1 /home ext4 defaults 0 0 " >> /etc/fstab
mount -a