企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。比普通的分区管理(没有LVm)具有灵活性,可也改变挂载点的大小. ![](https://box.kancloud.cn/5024d255db3987d6abb56a5ca93a98c6_359x297.png) 在进行下一步之前,我们说明一下pv、vg、lv的关系。 一个硬盘f分区组成一个pv(物理卷) 一个或者多个pv组成一个vg(卷组) 一个vg可以划分出多个lv(逻辑卷) ![](https://box.kancloud.cn/613a551769cd96e12854dcf73e922212_448x367.png) ## 1. 创建分区 ### 1.1 查看磁盘信息 ~~~ root@ubuntu01:~# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x000a2636 Device Boot Start End Blocks Id System /dev/sda1 * 2048 37847039 18922496 83 Linux /dev/sda2 37849086 41940991 2045953 5 Extended /dev/sda5 37849088 41940991 2045952 82 Linux swap / Solaris Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table ~~~ /dev/sdb整个盘都没有进行过分区 ### 1.2 分区 ~~~ root@ubuntu01:~# fdisk /dev/sdb Command (m for help): n #输入n创建分区 Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (1-4, default 2): 2 # 分区号,前边已经分过一个了 First sector (1026048-20971519, default 1026048): Using default value 1026048 Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): 1G Value out of range. Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): +1G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. root@ubuntu01:~# fdisk -l # 此时sdb设备有两个分区 Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x000a2636 Device Boot Start End Blocks Id System /dev/sda1 * 2048 37847039 18922496 83 Linux /dev/sda2 37849086 41940991 2045953 5 Extended /dev/sda5 37849088 41940991 2045952 82 Linux swap / Solaris Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0xc00d75b4 Device Boot Start End Blocks Id System /dev/sdb1 2048 1026047 512000 83 Linux /dev/sdb2 1026048 3123199 1048576 83 Linux ~~~ ### 1.3 创建lvm(逻辑卷) ~~~ root@ubuntu01:~# fdisk /dev/sdb Command (m for help): t # 修改分区编码(8e为逻辑分区) Partition number (1-4): 2 # 分区编号 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. ~~~ 此时sdb2分区的系统类别变成了 Linux LVM ~~~ root@ubuntu01:~# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x000a2636 Device Boot Start End Blocks Id System /dev/sda1 * 2048 37847039 18922496 83 Linux /dev/sda2 37849086 41940991 2045953 5 Extended /dev/sda5 37849088 41940991 2045952 82 Linux swap / Solaris Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0xc00d75b4 Device Boot Start End Blocks Id System /dev/sdb1 2048 1026047 512000 83 Linux /dev/sdb2 1026048 3123199 1048576 8e Linux LVM ~~~ ### 1.4 整合lvm,创建vg(卷组) 1. 创建pv(物理卷) ~~~ root@ubuntu01:~# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created root@ubuntu01:~# root@ubuntu01:~# pvdisplay "/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 nnMYmf-1xnO-LwyN-mYd5-3NTC-GOm2-lhfwmk ~~~ 2. 创建卷组 `vgcreate +组名 + PV Name ` ~~~ root@ubuntu01:~# vgcreate vgroup /dev/sdb2 # 根据pv:/dev/sdb2 创建vgroup组 Volume group "vgroup" successfully created root@ubuntu01:~# root@ubuntu01:~# root@ubuntu01:~# vgdisplay --- Volume group --- VG Name vgroup System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 1020.00 MiB PE Size 4.00 MiB Total PE 255 Alloc PE / Size 0 / 0 Free PE / Size 255 / 1020.00 MiB VG UUID EBCApt-lI68-OG4H-o6Yh-oGCB-1UQY-xphKJu ~~~ 4. 创建lvm 从卷组中分配出lvm逻辑卷 `lvcreate -L 【size】 -n 【lvm名】 【组名】` ~~~ root@ubuntu01:~# lvcreate -L 500M -n lvm1 vgroup Logical volume "lvm1" created ~~~ 5.格式化 ~~~ root@ubuntu01:~# mkfs -t ext4 /dev/vgroup/lvm1 mke2fs 1.42.9 (4-Feb-2014) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 128016 inodes, 512000 blocks 25600 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67633152 63 block groups 8192 blocks per group, 8192 fragments per group 2032 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done ~~~ 此时查看外部设备多了 /dev/mapper/vgroup-lvm1 在/dev/mapper/目录下(感觉映射的意思),vgroup-lvm1 (组名-lvm名) ~~~ root@ubuntu01:~# fdisk -l Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0xc00d75b4 Device Boot Start End Blocks Id System /dev/sdb1 2048 1026047 512000 83 Linux /dev/sdb2 1026048 3123199 1048576 8e Linux LVM Disk /dev/mapper/vgroup-lvm1: 524 MB, 524288000 bytes 255 heads, 63 sectors/track, 63 cylinders, total 1024000 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 identifier: 0x00000000 Disk /dev/mapper/vgroup-lvm1 doesn't contain a valid partition table ~~~ 6. 挂载 ~~~ root@ubuntu01:~# mount /dev/vgroup/lvm1 /lvmdata root@ubuntu01:~# root@ubuntu01:~# df -h Filesystem Size Used Avail Use% Mounted on udev 966M 4.0K 966M 1% /dev tmpfs 196M 532K 195M 1% /run /dev/sda1 18G 3.0G 14G 18% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 977M 0 977M 0% /run/shm none 100M 0 100M 0% /run/user /dev/mapper/vgroup-lvm1 477M 2.3M 445M 1% /lvmdata ~~~ 7. 修改配置文件 ~~~ root@ubuntu01:~# vim /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> # / was on /dev/sda1 during installation UUID=39960f15-013d-4b48-a437-002fd8e57a28 / ext4 errors=remount-ro 0 1 # swap was on /dev/sda5 during installation UUID=12c20ef4-19f1-4721-aa45-2da6db2826a7 none swap sw 0 0 /dev/vgroup/lvm1 /lvmdata ext4 errors=remount-ro 0 0 ~~~ 新形成的/dev/vgroup/lvm1 /dev/组名/lvm名 ## 2. LVM大小修改 ### 2.1 ext2/ext3/ext4文件系统 ext2/ext3/ext4文件系统的调整命令是resize2fs(增大和减小都支持) ~~~ lvextend -L 120G /dev/mapper/centos-home //增大至120G lvextend -L +20G /dev/mapper/centos-home //增加20G lvreduce -L 50G /dev/mapper/centos-home //减小至50G lvreduce -L -8G /dev/mapper/centos-home //减小8G resize2fs /dev/mapper/centos-home //执行调整 ~~~ ### 2.1 xfs文件系统 调整命令是xfs_growfs(只支持增大) ~~~ lvextend -L 120G /dev/mapper/centos-home //增大至120G lvextend -L +20G /dev/mapper/centos-home //增加20G xfs_growfs /dev/mapper/centos-home //执行调整 ~~~ ~~~ 就是说:xfs文件系统只支持增大分区空间的情况,不支持减小的情况(切记!!!!!)。 硬要减小的话,只能在减小后将逻辑分区重新通过mkfs.xfs命令重新格式化才能挂载 ~~~ 例子1: 增加ext4文件系统lvm大小 ~~~ root@ubuntu01:~# lvextend -L +200M /dev/vgroup/lvm1 Extending logical volume lvm1 to 700.00 MiB Logical volume lvm1 successfully resized root@ubuntu01:~# root@ubuntu01:~# root@ubuntu01:~# root@ubuntu01:~# df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 966M 4.0K 966M 1% /dev tmpfs tmpfs 196M 540K 195M 1% /run /dev/sda1 ext4 18G 3.0G 14G 18% / none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup none tmpfs 5.0M 0 5.0M 0% /run/lock none tmpfs 977M 0 977M 0% /run/shm none tmpfs 100M 0 100M 0% /run/user /dev/mapper/vgroup-lvm1 ext4 477M 2.3M 445M 1% /lvmdata ~~~ 如图,没有生效,因为没有执行调整: ~~~ root@ubuntu01:~# resize2fs /dev/vgroup/lvm1 resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/vgroup/lvm1 is mounted on /lvmdata; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/vgroup/lvm1 is now 716800 blocks long. root@ubuntu01:~# df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 966M 4.0K 966M 1% /dev tmpfs tmpfs 196M 540K 195M 1% /run /dev/sda1 ext4 18G 3.0G 14G 18% / none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup none tmpfs 5.0M 0 5.0M 0% /run/lock none tmpfs 977M 0 977M 0% /run/shm none tmpfs 100M 0 100M 0% /run/user /dev/mapper/vgroup-lvm1 ext4 670M 2.6M 631M 1% /lvmdata ~~~ 重启服务器依然生效 例子2: 减少ext4文件系统lvm大小 若是减小分区空间,减小前必须要先卸载这个分区。如果卸载有问题,解决如下: ~~~ [root@localhost ~]# umount /home/ umount: /home: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) 提示无法卸载,则是有进程占用/home,使用如下命令来终止占用进程: [root@localhost ~]# fuser -m -k /home /home: 1409 1519ce 1531e 1532e 1533e 1534e 1535e 1536e 1537e 1538e 1539e 1541e 1543e 1544e 1545e 1546e 1547e 1548e 1549e 1550e 1601m ~~~ 再次卸载home分区就成功了。 ### 减小分区 https://blog.csdn.net/sd4493091/article/details/66976078