[关闭]
@tony-yin 2018-04-25T02:22:28.000000Z 字数 4802 阅读 512

LVM扩容

Linux


在平时的开发工作中,经常会创建磁盘不足够大的虚拟机,然后往集群里面写一些数据导致磁盘满了。手动编辑虚拟机的磁盘大小是不会文件系统识别的,大多数同学只能无奈的重新装OS,这里我介绍一种基于LVM实现动态的方式。

LVM了解

LVM是逻辑盘卷管理(Logical VolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。

查看分区

当前默认只有一个采用lvm的分区,一开始sda磁盘容量为16G,后来发现不够用了,编辑磁盘大小为50G,但是可以发现这50G并没有起到扩展分区容量的效果。

  1. [root@tony-play ~]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/mapper/vg_tonyplay-lv_root
  4. 14G 3.4G 9.6G 26% /
  5. tmpfs 1.9G 72K 1.9G 1% /dev/shm
  6. /dev/sda1 477M 42M 410M 10% /boot
  7. [root@tony-play ~]# lsblk
  8. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  9. sr0 11:0 1 1024M 0 rom
  10. sda 8:0 0 50G 0 disk
  11. ├─sda1 8:1 0 500M 0 part /boot
  12. └─sda2 8:2 0 15.5G 0 part
  13. ├─vg_tonyplay-lv_root (dm-0) 253:0 0 13.9G 0 lvm /
  14. └─vg_tonyplay-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]

分区

可以通过新增一块其他磁盘来扩容,我这边采取的是增大当前磁盘的容量实现扩容。

有时候因为系统设备处于繁忙状态,所以分区需要重启后才会生效。

  1. [root@tony-play ~]# fdisk /dev/sda
  2. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
  3. switch off the mode (command 'c') and change display units to
  4. sectors (command 'u').
  5. Command (m for help): n
  6. Command action
  7. e extended
  8. p primary partition (1-4)
  9. p
  10. Partition number (1-4): 3
  11. First cylinder (2089-6527, default 2089): // 直接回车,用默认值就可以了
  12. Using default value 2089
  13. Last cylinder, +cylinders or +size{K,M,G} (2089-6527, default 6527): // 直接回车,用默认值就可以了
  14. Using default value 6527
  15. Command (m for help): w
  16. The partition table has been altered!
  17. # 可以看到新建的分区sda3已结被创建出来了,采取默认值会将剩余所有空间都分到分区中
  18. [root@tony-play ~]# lsblk
  19. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  20. sr0 11:0 1 1024M 0 rom
  21. sda 8:0 0 50G 0 disk
  22. ├─sda1 8:1 0 500M 0 part /boot
  23. ├─sda2 8:2 0 15.5G 0 part
  24. │ ├─vg_tonyplay-lv_root (dm-0) 253:0 0 13.9G 0 lvm /
  25. │ └─vg_tonyplay-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]
  26. └─sda3 8:3 0 34G 0 part

查看当前文件系统

当前文件系统为ext4

  1. [root@tony-play ~]# mount
  2. /dev/mapper/vg_tonyplay-lv_root on / type ext4 (rw)
  3. proc on /proc type proc (rw)
  4. sysfs on /sys type sysfs (rw)
  5. devpts on /dev/pts type devpts (rw,gid=5,mode=620)
  6. tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
  7. /dev/sda1 on /boot type ext4 (rw)
  8. none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

为新分区创建文件系统

  1. [root@tony-play ~]# sudo mkfs.ext4 /dev/sda3
  2. mke2fs 1.41.12 (17-May-2010)
  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. 2228224 inodes, 8912727 blocks
  9. 445636 blocks (5.00%) reserved for the super user
  10. First data block=0
  11. Maximum filesystem blocks=4294967296
  12. 272 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, 7962624
  18. Writing inode tables: done
  19. Creating journal (32768 blocks): done
  20. Writing superblocks and filesystem accounting information: done
  21. This filesystem will be automatically checked every 39 mounts or
  22. 180 days, whichever comes first. Use tune2fs -c or -i to override.

查看卷组信息

  1. [root@tony-play ~]# vgdisplay
  2. --- Volume group ---
  3. VG Name vg_tonyplay // 卷组名在下面扩展中会用到
  4. System ID
  5. Format lvm2
  6. Metadata Areas 1
  7. Metadata Sequence No 3
  8. VG Access read/write
  9. VG Status resizable
  10. MAX LV 0
  11. Cur LV 2
  12. Open LV 2
  13. Max PV 0
  14. Cur PV 1
  15. Act PV 1
  16. VG Size 15.51 GiB
  17. PE Size 4.00 MiB
  18. Total PE 3970
  19. Alloc PE / Size 3970 / 15.51 GiB
  20. Free PE / Size 0 / 0
  21. VG UUID Y9usSM-nDU5-ZAUd-Y3Te-u5Pd-uFBr-gcYHf0

创建新物理卷

  1. [root@tony-play ~]# pvcreate /dev/sda3
  2. Physical volume "/dev/sda3" successfully created

扩展到卷组

  1. vgextend vg_tonyplay /dev/sda3 // 卷组名在查看卷组信息中
  2. Volume group "vg_tonyplay" successfully extended

查看逻辑分区

/dev/vg_tonyplay/lv_root就是根分区,也是我们要扩展的分区。

  1. [root@tony-play ~]# lvdisplay
  2. --- Logical volume ---
  3. LV Path /dev/vg_tonyplay/lv_root // 根分区
  4. LV Name lv_root
  5. VG Name vg_tonyplay
  6. LV UUID IPd7lm-Sx8g-pe7k-llNL-j1wc-mbA2-2cAdsy
  7. LV Write Access read/write
  8. LV Creation host, time tony-play, 2017-04-10 17:58:53 -0400
  9. LV Status available
  10. # open 1
  11. LV Size 13.91 GiB
  12. Current LE 3561
  13. Segments 1
  14. Allocation inherit
  15. Read ahead sectors auto
  16. - currently set to 256
  17. Block device 253:0
  18. --- Logical volume ---
  19. LV Path /dev/vg_tonyplay/lv_swap
  20. LV Name lv_swap
  21. VG Name vg_tonyplay
  22. LV UUID qX637q-iD6i-8blp-hmmS-MvLy-xZ0y-b4D0BF
  23. LV Write Access read/write
  24. LV Creation host, time tony-play, 2017-04-10 17:59:07 -0400
  25. LV Status available
  26. # open 1
  27. LV Size 1.60 GiB
  28. Current LE 409
  29. Segments 1
  30. Allocation inherit
  31. Read ahead sectors auto
  32. - currently set to 256
  33. Block device 253:1

扩展容量到逻辑分区

  1. [root@tony-play ~]# lvextend /dev/vg_tonyplay/lv_root /dev/sda3
  2. Size of logical volume vg_tonyplay/lv_root changed from 13.91 GiB (3561 extents) to 47.91 GiB (12264 extents).
  3. Logical volume lv_root successfully resized

刷新逻辑分区容量使扩展生效

ext4resize2fsxfsxfs_growfs

  1. [root@tony-play ~]# resize2fs /dev/vg_tonyplay/lv_root
  2. resize2fs 1.41.12 (17-May-2010)
  3. Filesystem at /dev/vg_tonyplay/lv_root is mounted on /; on-line resizing required
  4. old desc_blocks = 1, new_desc_blocks = 3
  5. Performing an on-line resize of /dev/vg_tonyplay/lv_root to 12558336 (4k) blocks.
  6. The filesystem on /dev/vg_tonyplay/lv_root is now 12558336 blocks long.

查看逻辑分区容量

可以发现/dev/mapper/vg_tonyplay-lv_root已经从开始的14G扩展到了48G。ok,这就说明大功告成了,再也不用通过重装系统这种蹩脚的方式扩容了

  1. [root@tony-play ~]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/mapper/vg_tonyplay-lv_root
  4. 48G 3.4G 42G 8% /
  5. tmpfs 1.9G 72K 1.9G 1% /dev/shm
  6. /dev/sda1 477M 42M 410M 10% /boot

小结

至此,lvm扩容工作的过程应该是比较清楚了,之后有机会的话我会再补充一下LVM的压缩、删除等操作过程。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注