因为linux使用了lvm进行磁盘空间的组织与管理,有些类似于raid0。
磁盘空间在使用上直接面对的是逻辑卷,而逻辑卷则又使用卷组管理物理卷,物理卷就是直接创建在物理磁盘上的空间。
所以除了系统文件所在的根目录之外,所有挂载的数据目录都是可以动态调整的,只要加入新的磁盘空间,就可以对逻辑卷进行缩放。
磁盘扩容 - 图1
而系统所在的根目录,理论上只能扩容,无法进行缩容,因为磁盘空间一旦加入进去就被完全占用了,无法释放。

Step.1 物理磁盘分区

先查看系统上安装的磁盘情况。

  1. [root@0001 server1]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/mapper/centos-root 50G 46G 4.5G 92% /
  4. devtmpfs 16G 0 16G 0% /dev
  5. tmpfs 16G 0 16G 0% /dev/shm
  6. tmpfs 16G 1.6G 15G 11% /run
  7. tmpfs 16G 0 16G 0% /sys/fs/cgroup
  8. /dev/vda1 1014M 169M 846M 17% /boot
  9. /dev/mapper/centos-home 42G 324M 41G 1% /home
  10. tmpfs 3.2G 0 3.2G 0% /run/user/1001
  11. tmpfs 3.2G 0 3.2G 0% /run/user/1003
  12. tmpfs 3.2G 0 3.2G 0% /run/user/0
  13. [root@0001 server1]# fdisk -l
  14. Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectors
  15. Units = sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes
  18. Disk label type: dos
  19. Disk identifier: 0x000d5b31
  20. Device Boot Start End Blocks Id System
  21. /dev/vda1 * 2048 2099199 1048576 83 Linux
  22. /dev/vda2 2099200 209715199 103808000 8e Linux LVM
  23. Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
  24. Units = sectors of 1 * 512 = 512 bytes
  25. Sector size (logical/physical): 512 bytes / 512 bytes
  26. I/O size (minimum/optimal): 512 bytes / 512 bytes
  27. Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
  28. Units = sectors of 1 * 512 = 512 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes
  31. Disk /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 sectors
  32. Units = sectors of 1 * 512 = 512 bytes
  33. Sector size (logical/physical): 512 bytes / 512 bytes
  34. I/O size (minimum/optimal): 512 bytes / 512 bytes
  35. Disk /dev/mapper/centos-home: 44.1 GB, 44149243904 bytes, 86228992 sectors
  36. Units = sectors of 1 * 512 = 512 bytes
  37. Sector size (logical/physical): 512 bytes / 512 bytes
  38. I/O size (minimum/optimal): 512 bytes / 512 bytes

发现有一块磁盘 /dev/vdb 没有使用。接下来对物理磁盘进行格式化与分区。

  1. [root@0001 server1]# fdisk /dev/vdb
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Device does not contain a recognized partition table
  6. Building a new DOS disklabel with disk identifier 0xea2ef8f6.
  7. Command (m for help): n
  8. Partition type:
  9. p primary (0 primary, 0 extended, 4 free)
  10. e extended
  11. Select (default p):
  12. Using default response p
  13. Partition number (1-4, default 1): First sector (2048-209715199, default 2048):
  14. Using default value 2048
  15. Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): Using default value 209715199
  16. Partition 1 of type Linux and of size 100 GiB is set
  17. Command (m for help): t
  18. Selected partition 1
  19. Hex code (type L to list all codes): 8e
  20. Changed type of partition 'Linux' to 'Linux LVM'
  21. Command (m for help): w
  22. The partition table has been altered!
  23. Calling ioctl() to re-read partition table.
  24. Syncing disks.

Step.2 操作LVM

  1. [root@0001 server1]# fdisk -l
  2. Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectors
  3. Units = sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk label type: dos
  7. Disk identifier: 0x000d5b31
  8. Device Boot Start End Blocks Id System
  9. /dev/vda1 * 2048 2099199 1048576 83 Linux
  10. /dev/vda2 2099200 209715199 103808000 8e Linux LVM
  11. Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
  12. Units = sectors of 1 * 512 = 512 bytes
  13. Sector size (logical/physical): 512 bytes / 512 bytes
  14. I/O size (minimum/optimal): 512 bytes / 512 bytes
  15. Disk label type: dos
  16. Disk identifier: 0xea2ef8f6
  17. Device Boot Start End Blocks Id System
  18. /dev/vdb1 2048 209715199 104856576 8e Linux LVM
  19. Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
  20. Units = sectors of 1 * 512 = 512 bytes
  21. Sector size (logical/physical): 512 bytes / 512 bytes
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes
  23. Disk /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 sectors
  24. Units = sectors of 1 * 512 = 512 bytes
  25. Sector size (logical/physical): 512 bytes / 512 bytes
  26. I/O size (minimum/optimal): 512 bytes / 512 bytes
  27. Disk /dev/mapper/centos-home: 44.1 GB, 44149243904 bytes, 86228992 sectors
  28. Units = sectors of 1 * 512 = 512 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes
  31. [root@0001 server1]# pvcreate /dev/vdb1
  32. Physical volume "/dev/vdb1" successfully created.
  33. [root@0001 server1]# pvdisplay
  34. --- Physical volume ---
  35. PV Name /dev/vda2
  36. VG Name centos
  37. PV Size <99.00 GiB / not usable 3.00 MiB
  38. Allocatable yes
  39. PE Size 4.00 MiB
  40. Total PE 25343
  41. Free PE 1
  42. Allocated PE 25342
  43. PV UUID D3atmG-vYwp-asY9-UCMy-L5LZ-JgH6-CrYXdl
  44. "/dev/vdb1" is a new physical volume of "<100.00 GiB"
  45. --- NEW Physical volume ---
  46. PV Name /dev/vdb1
  47. VG Name
  48. PV Size <100.00 GiB
  49. Allocatable NO
  50. PE Size 0
  51. Total PE 0
  52. Free PE 0
  53. Allocated PE 0
  54. PV UUID BSpZvK-jM8r-fYDo-XsuS-huoa-NMsd-EdnSSw
  55. [root@0001 server1]# vgdisplay
  56. --- Volume group ---
  57. VG Name centos
  58. System ID
  59. Format lvm2
  60. Metadata Areas 1
  61. Metadata Sequence No 4
  62. VG Access read/write
  63. VG Status resizable
  64. MAX LV 0
  65. Cur LV 3
  66. Open LV 3
  67. Max PV 0
  68. Cur PV 1
  69. Act PV 1
  70. VG Size <99.00 GiB
  71. PE Size 4.00 MiB
  72. Total PE 25343
  73. Alloc PE / Size 25342 / 98.99 GiB
  74. Free PE / Size 1 / 4.00 MiB
  75. VG UUID kZrifW-oQKr-6ezp-J1fz-l704-BHCs-QZhIRq
  76. [root@0001 server1]# vgextend centos /dev/vdb1
  77. Volume group "centos" successfully extended
  78. [root@0001 server1]# vgdisplay
  79. --- Volume group ---
  80. VG Name centos
  81. System ID
  82. Format lvm2
  83. Metadata Areas 2
  84. Metadata Sequence No 5
  85. VG Access read/write
  86. VG Status resizable
  87. MAX LV 0
  88. Cur LV 3
  89. Open LV 3
  90. Max PV 0
  91. Cur PV 2
  92. Act PV 2
  93. VG Size 198.99 GiB
  94. PE Size 4.00 MiB
  95. Total PE 50942
  96. Alloc PE / Size 25342 / 98.99 GiB
  97. Free PE / Size 25600 / 100.00 GiB
  98. VG UUID kZrifW-oQKr-6ezp-J1fz-l704-BHCs-QZhIRq
  99. [root@0001 server1]# lvextend -l +100%FREE /dev/mapper/centos-root /dev/vdb1
  100. Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to <150.00 GiB (38399 extents).
  101. Logical volume centos/root successfully resized.
  102. [root@0001 server1]# xfs_growfs /dev/mapper/centos-root
  103. meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
  104. = sectsz=512 attr=2, projid32bit=1
  105. = crc=1 finobt=0 spinodes=0
  106. data = bsize=4096 blocks=13107200, imaxpct=25
  107. = sunit=0 swidth=0 blks
  108. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  109. log =internal bsize=4096 blocks=6400, version=2
  110. = sectsz=512 sunit=0 blks, lazy-count=1
  111. realtime =none extsz=4096 blocks=0, rtextents=0
  112. data blocks changed from 13107200 to 39320576
  113. [root@0001 server1]# df -h
  114. Filesystem Size Used Avail Use% Mounted on
  115. /dev/mapper/centos-root 150G 46G 105G 31% /
  116. devtmpfs 16G 0 16G 0% /dev
  117. tmpfs 16G 0 16G 0% /dev/shm
  118. tmpfs 16G 1.6G 15G 11% /run
  119. tmpfs 16G 0 16G 0% /sys/fs/cgroup
  120. /dev/vda1 1014M 169M 846M 17% /boot
  121. /dev/mapper/centos-home 42G 324M 41G 1% /home
  122. tmpfs 3.2G 0 3.2G 0% /run/user/1001
  123. tmpfs 3.2G 0 3.2G 0% /run/user/1003
  124. tmpfs 3.2G 0 3.2G 0% /run/user/0