什么是LVM
LVM是Logical Volume Manager的简写。其是Linux环境下对磁盘分区进行管理的一种机制。LVM由Heinz Mauelshagen在Linux 2.4内核上实现。其主要作用是在不损坏磁盘数据的情况下对磁盘空间进行增加,缩减。LVM的做法是将几块物理硬盘通过软件的方式组合成一块空间相对大的Volume Group,简称VG,然后将这块大磁盘通过Physical Extend组成可以使用的分区Logical Volume,简称LV。将LV格式化之后挂载既能使用
LVM的几个名词介绍
PV:Physical Volume,物理卷
PE:Physical Extend,物理块
VG:Volume Group,卷组
LV:Logical Volume,逻辑卷
LVM常用命令介绍
PV常用命令:
pvcreate [device]:创建PV
pvs:显示pv的简要信息
pvdisplay /dev/sd[abcd]#:显示PV的详细信息。如果不指定设备,则显示所有PV详细信息
pvremove /dev/sd[abcd]#:删除PV
VG常用命令:
vgcreate [–s #[mMgGtT]] VG_NAME PhysicalDevicePath…:创建VG
-s:指定PE的大小,PE默认大小是4MB
vgs [VG_NAME]:显示简要VG信息
vgdisplay [VG_NAME]:显示指定VG的详细信息,不指定VG则默认显示全部VG的详细信息
LV常用命令
lvcreate –L [+]#[mMgGtT] –n LV_NAME VG_NAME:创建LV
-L:指定LV的大小
-n:指定LV的名字
lvs [LV_NAME]:显示制指定v的简要信息,不指定lv默认显示全部lv的简要信息
lvdisplay [LV_NAME]:显示指定lv的详细信息,不指定lv默认显示全部lv的详细信息
lvextend -L [+]#[mMgGtT] LV_NAME:扩展LV到指定大小
lvreduce -L [+]#[mMgGtT] LV_NAME:缩减LV到指定大小
lvremove /dev/VG_NAME/LV_NAME::删除LV
LV的抽象表示
示例:LV的创建,扩展和缩减。此处以一块120G的硬盘为例,其设备文件是/dev/sda
LV的创建
1. 使用fdisk内建命令创建分区。注意,分区完毕后要将分区文件的System ID 改为8e: Linux LVM。此处以/dev/sda5, /dev/sda6,/dev/sda7作为LVM示例
[root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c744b Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 89120767 44047360 8e Linux LVM /dev/sda3 89120768 110092287 10485760 83 Linux /dev/sda4 110092288 251658239 70782976 5 Extended /dev/sda5 110094336 120580095 5242880 8e Linux LVM /dev/sda6 120582144 141553663 10485760 8e Linux LVM /dev/sda7 141555712 173012991 15728640 8e Linux LVM
2. 创建pv
[root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 42.00g 4.00m [root@localhost ~]# pvcreate /dev/{sda5,sda6,sda7} Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created Physical volume "/dev/sda7" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 42.00g 4.00m /dev/sda5 lvm2 --- 10.00g 10.00g /dev/sda6 lvm2 --- 10.00g 10.00g /dev/sda7 lvm2 --- 5.00g 5.00g
3. 创建VG,指定PE大小为16MB
[root@localhost ~]# vgcreate -s 16M myvg /dev/sda5 /dev/sda6 /dev/sda7 Volume group "myvg" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 42.00g 4.00m myvg 3 0 0 wz--n- 24.95g 24.95g [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 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 3 Act PV 3 VG Size 24.95 GiB PE Size 16.00 MiB Total PE 1597 Alloc PE / Size 0 / 0 Free PE / Size 1597 / 24.95 GiB VG UUID b2OW0g-eCXR-0oxI-Z8Ft-Jemk-zXON-fnpSb1
4. 创建LV
[root@localhost ~]# lvcreate -L 10G -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 ~]# lvdisplay /dev/myvg/mylv --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID 669CDR-JifV-bCou-1QfU-q7VH-0b07-orShqW LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-07 14:33:17 +0800 LV Status available # open 0 LV Size 10.00 GiB Current LE 640 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2
5. 格式化LV文件系统
[root@localhost ~]# mke2fs -t ext3 /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 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2684354560 80 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, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# blkid /dev/myvg/mylv /dev/myvg/mylv: UUID="4eb1b6ce-3777-4c69-9be1-522ca486d936" SEC_TYPE="ext2" TYPE="ext3"
6. Mount LV并测试
[root@localhost ~]# mount /dev/myvg/mylv /test_lv/ [root@localhost ~]# cp /var/log/*.log /test_lv/ [root@localhost ~]# ls /test_lv boot.log lost+found wpa_supplicant.log Xorg.0.log [root@localhost ~]# umount /testlv umount: /testlv: mountpoint not found [root@localhost ~]# umount /dev/myvg/mylv [root@localhost ~]# mount /dev/myvg//mylv /users [root@localhost ~]# ls /users boot.log lost+found wpa_supplicant.log Xorg.0.log
LVM的扩展
-
扩展LV
[root@localhost ~]# lvextend -L 15G /dev/myvg/mylv Size of logical volume myvg/mylv changed from 10.00 GiB (640 extents) to 15.00 GiB (960 extents). Logical volume mylv successfully resized.
2. 扩展文件系统,填充LV的空间
[root@localhost ~]# resize2fs /dev/myvg/mylv 15G resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/myvg/mylv is now 3932160 blocks long. [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 20G 3.7G 17G 19% / devtmpfs 480M 0 480M 0% /dev tmpfs 495M 152K 495M 1% /dev/shm tmpfs 495M 7.2M 488M 2% /run tmpfs 495M 0 495M 0% /sys/fs/cgroup /dev/mapper/centos-home 20G 36M 20G 1% /home /dev/sda1 497M 159M 339M 32% /boot tmpfs 99M 12K 99M 1% /run/user/1000 /dev/sr0 4.1G 4.1G 0 100% /run/media/max/CentOS 7 x86_64 /dev/mapper/myvg-mylv 15G 26M 14G 1% /users
Lvm的缩减:先缩减文件空间,再缩减物理空间
-
卸载LV
[root@localhost ~]# umount /dev/myvg/mylv
2. 强制检测文件系统
[root@localhost ~]# 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: 14/983040 files (7.1% non-contiguous), 100917/3932160 blocks
3. 缩减文件系统空间到7G
[root@localhost ~]# resize2fs /dev/myvg/mylv 7G 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.
4. 缩减LV空间至7G
[root@localhost ~]# lvs /dev/myvg/mylv LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 15.00g [root@localhost ~]# lvreduce -L 7G mylv Path required for Logical Volume "mylv". Please provide a volume group name Run `lvreduce --help' for more information. [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv WARNING: Reducing active logical volume to 7.00 GiB [root@localhost ~]# lvs /dev/myvg/mylv LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 7.00g
5. 挂载并测试
[root@localhost ~]# mount /dev/myvg/mylv /users [root@localhost ~]# ls /users boot.log lost+found wpa_supplicant.log Xorg.0.log [root@localhost ~]# cat /users/Xorg.0.log [ 30.065] X.Org X Server 1.17.2 Release Date: 2015-06-16
原创文章,作者:mxb93,如若转载,请注明出处:http://www.178linux.com/44303