lvm基本应用,扩展及缩减实现
LVM
LVM是 Logical Volume Manager (逻辑卷管理) 的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。
如图LVM模型
由四个磁盘分区组成一个很大的空间(逻辑集合),然后在这些空间上划分一些逻辑分区,当一个逻辑分区的空间不够用的时候,可以从剩余空间上划分一些空间给空间不够的分区使用。
LVM基本术语
-
物理存储介质(PhysicalStorageMedia)
-
物理卷(Physical Volume,PV)
-
卷组(Volume Group,VG)
-
逻辑卷(Logical Volume,LV)
-
物理块(Physical Extent,PE)
图1所示LVM抽象模型,展示了PV、VG、LV三者之间关系:
创建一个LVM系统,一般需要以下步骤 centOS 7 环境
1、创建分区
使用分区工具(如:fdisk等)创建LVM分区,方法和创建其他一般分区的方式是一样的,区别仅仅是LVM的分区类型为8e
1 [root@centos7 ~]# fdisk /dev/sda 2 Welcome to fdisk (util-linux 2.23.2). 3 4 Changes will remain in memory only, until you decide to write them. 5 Be careful before using the write command. 6 7 8 Command (m for help): p //查看现有分区,我这磁盘分区已满,创建的扩展分区 9 10 Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors 11 Units = sectors of 1 * 512 = 512 bytes 12 Sector size (logical/physical): 512 bytes / 512 bytes 13 I/O size (minimum/optimal): 512 bytes / 512 bytes 14 Disk label type: dos 15 Disk identifier: 0x0009fa69 16 17 Device Boot Start End Blocks Id System 18 /dev/sda1 * 2048 2099199 1048576 83 Linux 19 /dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris 20 /dev/sda3 10487808 41943039 15727616 83 Linux 21 /dev/sda4 41943040 209715199 83886080 5 Extended 22 23 Command (m for help): n 24 All primary partitions are in use 25 Adding logical partition 5 26 First sector (41945088-209715199, default 41945088): 27 Using default value 41945088 28 Last sector, +sectors or +size{K,M,G} (41945088-209715199, default 209715199): +10G 29 Partition 5 of type Linux and of size 10 GiB is set 30 31 Command (m for help): n 32 All primary partitions are in use 33 Adding logical partition 6 34 First sector (62918656-209715199, default 62918656): 35 Using default value 62918656 36 Last sector, +sectors or +size{K,M,G} (62918656-209715199, default 209715199): +10G 37 Partition 6 of type Linux and of size 10 GiB is set 38 39 Command (m for help): n 40 All primary partitions are in use 41 Adding logical partition 7 42 First sector (83892224-209715199, default 83892224): 43 Using default value 83892224 44 Last sector, +sectors or +size{K,M,G} (83892224-209715199, default 209715199): +10G 45 Partition 7 of type Linux and of size 10 GiB is set 46 47 Command (m for help): t //修改分区类型 48 Partition number (1-7, default 7): 5 49 Hex code (type L to list all codes): 8e //LVM的指定格式,分区的类型为8e 50 Changed type of partition 'Linux' to 'Linux LVM' 51 52 Command (m for help): t 53 Partition number (1-7, default 7): 6 54 Hex code (type L to list all codes): 8e 55 Changed type of partition 'Linux' to 'Linux LVM' 56 57 Command (m for help): t 58 Partition number (1-7, default 7): 7 59 Hex code (type L to list all codes): 8e 60 Changed type of partition 'Linux' to 'Linux LVM' 61 62 Command (m for help): p 63 64 Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors 65 Units = sectors of 1 * 512 = 512 bytes 66 Sector size (logical/physical): 512 bytes / 512 bytes 67 I/O size (minimum/optimal): 512 bytes / 512 bytes 68 Disk label type: dos 69 Disk identifier: 0x0009fa69 70 71 Device Boot Start End Blocks Id System 72 /dev/sda1 * 2048 2099199 1048576 83 Linux 73 /dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris 74 /dev/sda3 10487808 41943039 15727616 83 Linux 75 /dev/sda4 41943040 209715199 83886080 5 Extended 76 /dev/sda5 41945088 62916607 10485760 8e Linux LVM 77 /dev/sda6 62918656 83890175 10485760 8e Linux LVM 78 /dev/sda7 83892224 104863743 10485760 8e Linux LVM 79 80 Command (m for help): w 81 The partition table has been altered! 82 83 Calling ioctl() to re-read partition table. 84 85 WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 86 The kernel still uses the old table. The new table will be used at 87 the next reboot or after you run partprobe(8) or kpartx(8) 88 Syncing disks.
重新读取分区列表,使刚创建的分区生效
1 [root@centos7 ~]# partx -a /dev/sda 2 partx: /dev/sda: error adding partitions 1-3 3 [root@centos7 ~]# partx -a /dev/sda 4 partx: /dev/sda: error adding partitions 1-7 5 [root@centos7 ~]# partx -a /dev/sda 6 partx: /dev/sda: error adding partitions 1-7
2、创建PV
1 [root@centos7 ~]# pvcreate /dev/sda5 2 Physical volume "/dev/sda5" successfully created. 3 [root@centos7 ~]# pvcreate /dev/sda6 4 Physical volume "/dev/sda6" successfully created. 5 [root@centos7 ~]# pvcreate /dev/sda7 6 Physical volume "/dev/sda7" successfully created. 7 [root@centos7 ~]#
display 查看以创建的pv
1 [root@centos7 ~]# pvdisplay 2 "/dev/sda5" is a new physical volume of "10.00 GiB" 3 --- NEW Physical volume --- 4 PV Name /dev/sda5 5 VG Name //还没有创建VG,加入VG 6 PV Size 10.00 GiB 7 Allocatable NO 8 PE Size 0 //还未加入vg 此处显示都是0 9 Total PE 0 10 Free PE 0 11 Allocated PE 0 12 PV UUID PWWEE1-2Kkz-Sc3k-HdJE-pX4a-eKeC-lad4O8 13 14 "/dev/sda7" is a new physical volume of "10.00 GiB" 15 --- NEW Physical volume --- 16 PV Name /dev/sda7 17 VG Name 18 PV Size 10.00 GiB 19 Allocatable NO 20 PE Size 0 21 Total PE 0 22 Free PE 0 23 Allocated PE 0 24 PV UUID cKxFTf-wFOM-ky1R-gFTQ-bYC8-dOG1-ijQZbT 25 26 "/dev/sda6" is a new physical volume of "10.00 GiB" 27 --- NEW Physical volume --- 28 PV Name /dev/sda6 29 VG Name 30 PV Size 10.00 GiB 31 Allocatable NO 32 PE Size 0 33 Total PE 0 34 Free PE 0 35 Allocated PE 0 36 PV UUID aVm3Q9-sqPf-hpiu-UwW5-13Se-gnuN-aHBaOP
3、创建VG
1 [root@centos7 ~]# vgdisplay 2 [root@centos7 ~]# vgcreate myvg /dev/sda5 /dev/sda6 //创建vg,vg名称myvg 3 Volume group "myvg" successfully created 4 [root@centos7 ~]# vgdisplay //查看已有的vg 5 --- Volume group --- 6 VG Name myvg 7 System ID 8 Format lvm2 9 Metadata Areas 2 10 Metadata Sequence No 1 11 VG Access read/write 12 VG Status resizable 13 MAX LV 0 14 Cur LV 0 15 Open LV 0 16 Max PV 0 17 Cur PV 2 18 Act PV 2 19 VG Size 19.99 GiB 20 PE Size 4.00 MiB 21 Total PE 5118 22 Alloc PE / Size 0 / 0 23 Free PE / Size 5118 / 19.99 GiB 24 VG UUID byg0hk-edrp-OXU6-L1qf-IcFA-uDAp-p9qzDi 25
创建完成VG后,才能从VG中划分一个LV
4、创建LV
1 [root@centos7 ~]# lvcreate -L +19G -n mylv myvg 2 Logical volume "mylv" created. 3 [root@centos7 ~]# lvdisplay 4 --- Logical volume --- 5 LV Path /dev/myvg/mylv 6 LV Name mylv 7 VG Name myvg 8 LV UUID 6Rcc2F-Ag79-f24h-q5AX-Tq0f-s7FI-cIXIFK 9 LV Write Access read/write 10 LV Creation host, time centos7, 2022-03-27 08:43:20 +0800 11 LV Status available 12 # open 0 13 LV Size 19.00 GiB 14 Current LE 4864 15 Segments 2 16 Allocation inherit 17 Read ahead sectors auto 18 - currently set to 8192 19 Block device 253:0
lvcreate -L +size(K,M,G)容量大小 -n NAME (lv的名称) myvg(表示从这个VG中划分LV)
5、LV格式化及挂载
1 [root@centos7 ~]# mke2fs -t ext4 /dev/myvg/mylv //格式化lv逻辑卷,格式化后才能储存数据 2 mke2fs 1.42.9 (28-Dec-2013) 3 Filesystem label= 4 OS type: Linux 5 Block size=4096 (log=2) 6 Fragment size=4096 (log=2) 7 Stride=0 blocks, Stripe width=0 blocks 8 1245184 inodes, 4980736 blocks 9 249036 blocks (5.00%) reserved for the super user 10 First data block=0 11 Maximum filesystem blocks=2153775104 12 152 block groups 13 32768 blocks per group, 32768 fragments per group 14 8192 inodes per group 15 Superblock backups stored on blocks: 16 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 17 4096000 18 19 Allocating group tables: done 20 Writing inode tables: done 21 Creating journal (32768 blocks): done 22 Writing superblocks and filesystem accounting information: done 23 24 [root@centos7 ~]# mkdir test 25 [root@centos7 ~]# mount /dev/myvg/mylv /root/test/ 26 [root@centos7 ~]# df -h 27 Filesystem Size Used Avail Use% Mounted on 28 devtmpfs 970M 0 970M 0% /dev 29 tmpfs 980M 0 980M 0% /dev/shm 30 tmpfs 980M 9.6M 971M 1% /run 31 tmpfs 980M 0 980M 0% /sys/fs/cgroup 32 /dev/sda3 15G 1.9G 14G 13% / 33 /dev/sda1 1014M 189M 826M 19% /boot 34 tmpfs 196M 0 196M 0% /run/user/0 35 /dev/mapper/myvg-mylv 19G 45M 18G 1% /root/test 36 [root@centos7 ~]#
开机自动挂载逻辑卷mylv
设置/etc/fstab文件
~]# vim /etc/fstab
1 # 2 # /etc/fstab 3 # Created by anaconda on Sat Jan 15 19:51:47 2022 4 # 5 # Accessible filesystems, by reference, are maintained under '/dev/disk' 6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 7 # 8 UUID=a73ff2c8-9f63-4b1c-a546-f5e172267fa5 / xfs defaults 0 0 9 UUID=067cc07f-f60f-4a2c-9ef3-4c95d2b4887d /boot xfs defaults 0 0 10 UUID=a5c9da83-e0f0-44af-a4d6-0de96e2f1a7a swap swap defaults 0 0 11 /dev/myvg/mylv /root/test ext4 defaults 0 0 12 ~ 13 ~ 14 ~ 15 ~ 16 ~ 17 ~ 18 ~ 19 ~ 20 ~ 21 ~ 22 ~ 23 ~ 24 ~ 25 ~ 26 ~
扩展逻辑卷
把先前创建的pv sda7,加入到VG myvg中
1 [root@centos7 ~]# vgextend myvg /dev/sda7 2 Volume group "myvg" successfully extended
查看pv sda7
总共2559个PE ,一个PE为4M ,2559*4=10236M≈9.9G 则为LV最大的扩展大小
1 [root@centos7 ~]# pvdisplay /dev/sda7 2 --- Physical volume --- 3 PV Name /dev/sda7 4 VG Name myvg 5 PV Size 10.00 GiB / not usable 4.00 MiB 6 Allocatable yes 7 PE Size 4.00 MiB 8 Total PE 2559 9 Free PE 2559 10 Allocated PE 0 11 PV UUID cKxFTf-wFOM-ky1R-gFTQ-bYC8-dOG1-ijQZbT
测试为myvg扩展2G空间
1 [root@centos7 ~]# lvextend -L +2G /dev/myvg/mylv //扩展2G空间 2 Size of logical volume myvg/mylv changed from 19.00 GiB (4864 extents) to 21.00 GiB (5376 extents). 3 Logical volume myvg/mylv successfully resized. 4 [root@centos7 ~]# resize2fs /dev/myvg/mylv //扩展空间生效 5 resize2fs 1.42.9 (28-Dec-2013) 6 Filesystem at /dev/myvg/mylv is mounted on /root/test; on-line resizing required 7 old_desc_blocks = 3, new_desc_blocks = 3 8 The filesystem on /dev/myvg/mylv is now 5505024 blocks long. 9 10 [root@centos7 ~]# df -h 11 Filesystem Size Used Avail Use% Mounted on 12 devtmpfs 970M 0 970M 0% /dev 13 tmpfs 980M 0 980M 0% /dev/shm 14 tmpfs 980M 9.6M 971M 1% /run 15 tmpfs 980M 0 980M 0% /sys/fs/cgroup 16 /dev/sda3 15G 1.9G 14G 13% / 17 /dev/sda1 1014M 189M 826M 19% /boot 18 tmpfs 196M 0 196M 0% /run/user/0 19 /dev/mapper/myvg-mylv 21G 45M 20G 1% /root/test 20 [root@centos7 ~]#
缩减逻辑卷(*慎重使用)
缩减逻辑卷后的空间,不能小于文件存储的空间。例如:逻辑卷21G,文件存储用了15G,缩减后的空间要大于15G。
1、取消逻辑卷挂载
1 [root@centos7 ~]# umount /dev/m 2 /dev/mapper/myvg-mylv /dev/mqueue 3 [root@centos7 ~]# umount /dev/mapper/myvg-mylv //取消逻辑券挂载
查看逻辑卷已经取消
1 [root@centos7 ~]# df -h
2 Filesystem Size Used Avail Use% Mounted on
3 devtmpfs 970M 0 970M 0% /dev
4 tmpfs 980M 0 980M 0% /dev/shm
5 tmpfs 980M 9.6M 971M 1% /run
6 tmpfs 980M 0 980M 0% /sys/fs/cgroup
7 /dev/sda3 15G 1.9G 14G 13% /
8 /dev/sda1 1014M 189M 826M 19% /boot
9 tmpfs 196M 0 196M 0% /run/user/0
2、缩减逻辑卷,重新挂载
1 [root@centos7 ~]# e2fsck -f /dev/myvg/mylv //做文件系统强制检测和修复 2 e2fsck 1.42.9 (28-Dec-2013) 3 Pass 1: Checking inodes, blocks, and sizes 4 Pass 2: Checking directory structure 5 Pass 3: Checking directory connectivity 6 Pass 4: Checking reference counts 7 Pass 5: Checking group summary information 8 /dev/myvg/mylv: 11/1376256 files (0.0% non-contiguous), 130412/5505024 blocks 9 [root@centos7 ~]# resize2fs /dev/myvg/mylv 19G //缩减逻辑边界为指定大小,原大小为21G,我缩减至19G 10 resize2fs 1.42.9 (28-Dec-2013) 11 Resizing the filesystem on /dev/myvg/mylv to 4980736 (4k) blocks. 12 The filesystem on /dev/myvg/mylv is now 4980736 blocks long. 13 14 [root@centos7 ~]# lvreduce -L 19G /dev/myvg/mylv //缩减逻辑卷指定大小19G。原大小21G 15 WARNING: Reducing active logical volume to 19.00 GiB. 16 THIS MAY DESTROY YOUR DATA (filesystem etc.) 17 Do you really want to reduce myvg/mylv? [y/n]: y 18 Size of logical volume myvg/mylv changed from 21.00 GiB (5376 extents) to 19.00 GiB (4864 extents). 19 Logical volume myvg/mylv successfully resized. 20 [root@centos7 ~]# mount /dev/myvg/mylv /root/test/ 21 [root@centos7 ~]# dh -f 22 -bash: dh: command not found 23 [root@centos7 ~]# df -h 24 Filesystem Size Used Avail Use% Mounted on 25 devtmpfs 970M 0 970M 0% /dev 26 tmpfs 980M 0 980M 0% /dev/shm 27 tmpfs 980M 9.6M 971M 1% /run 28 tmpfs 980M 0 980M 0% /sys/fs/cgroup 29 /dev/sda3 15G 1.9G 14G 13% / 30 /dev/sda1 1014M 189M 826M 19% /boot 31 tmpfs 196M 0 196M 0% /run/user/0 32 /dev/mapper/myvg-mylv 19G 44M 18G 1% /root/test //已经缩减2G,现有空间19G 33 [root@centos7 ~]#
参考文献:https://www.cnblogs.com/gaoyuechen/p/9110301.html#auto-id-2
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库