系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

怎样给centos系统扩展磁盘分区的实现方法

时间:2019-12-30来源:系统城作者:电脑系统城

问题/故障/场景/需求

eve-ng的虚拟机ova的硬盘只有38G,需要增加空间,在vmware直接扩展硬盘后,重启系统,使用fdisk -l可以看到硬盘扩大了,但文件系统并没有扩大,还需要将这些新增的空间扩展分配到某个文件系统才行。下面记录了整个扩展过程

解决方法/步骤

在VM里扩大磁盘到250G重启系统后,查看当前的文件系统的情况


 
  1. root@eve-ng:~# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. udev 7.9G 0 7.9G 0% /dev
  4. tmpfs 1.6G 19M 1.6G 2% /run
  5. /dev/mapper/eve--ng--vg-root 38G 13G 23G 36% /
  6. tmpfs 7.9G 0 7.9G 0% /dev/shm
  7. tmpfs 5.0M 0 5.0M 0% /run/lock
  8. tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
  9. /dev/sda1 472M 83M 365M 19% /boot
  10.  
  11. root@eve-ng:~# fdisk -l
  12. ...省略部分内容
  13. Disk /dev/sda: 250 GiB, 268435456000 bytes, 524288000 sectors //可以看到已经有250G了
  14. Units: sectors of 1 * 512 = 512 bytes
  15. Sector size (logical/physical): 512 bytes / 512 bytes
  16. I/O size (minimum/optimal): 512 bytes / 512 bytes
  17. Disklabel type: dos
  18. Disk identifier: 0x3e3ca055
  19.  
  20. Device Boot Start End Sectors Size Id Type
  21. /dev/sda1 * 2048 999423 997376 487M 83 Linux
  22. /dev/sda2 1001470 83884031 82882562 39.5G 5 Extended
  23. /dev/sda5 1001472 83884031 82882560 39.5G 8e Linux LVM
  24.  
  25. Disk /dev/mapper/eve--ng--vg-root: 38.6 GiB, 41406169088 bytes, 80871424 sectors
  26. Units: sectors of 1 * 512 = 512 bytes
  27. Sector size (logical/physical): 512 bytes / 512 bytes
  28. I/O size (minimum/optimal): 512 bytes / 512 bytes
  29.  
  30. Disk /dev/mapper/eve--ng--vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors
  31. Units: sectors of 1 * 512 = 512 bytes
  32. Sector size (logical/physical): 512 bytes / 512 bytes
  33. I/O size (minimum/optimal): 512 bytes / 512 bytes
  34.  
  35. //下面执行分区
  36. root@eve-ng:~# fdisk /dev/sda
  37.  
  38. Welcome to fdisk (util-linux 2.27.1).
  39. Changes will remain in memory only, until you decide to write them.
  40. Be careful before using the write command.
  41.  
  42. Command (m for help): n
  43. Partition type
  44. p primary (1 primary, 1 extended, 2 free)
  45. l logical (numbered from 5)
  46. Select (default p): p
  47. Partition number (3,4, default 3): 3
  48. First sector (999424-524287999, default 999424): 83884032 //注意,这里是之前分配的最后一块(sda5 end)加一(83884031+1)
  49. Last sector, +sectors or +size{K,M,G,T,P} (83884032-524287999, default 524287999): //注意看这是不是最后一块
  50.  
  51. Created a new partition 3 of type 'Linux' and of size 210 GiB.
  52.  
  53. Command (m for help): t
  54. Partition number (1-3,5, default 5): 3 3
  55. Partition type (type L to list all types): 8e
  56.  
  57. Changed type of partition 'Linux' to 'Linux LVM'.
  58.  
  59. Command (m for help): w
  60. The partition table has been altered.
  61. Calling ioctl() to re-read partition table.
  62. Re-reading the partition table failed.: Device or resource busy
  63.  
  64. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
  65.  
  66. root@eve-ng:~# reboot
  67.  
  68. root@eve-ng:~# pvcreate /dev/sda3
  69. Physical volume "/dev/sda3" successfully created
  70. root@eve-ng:~# vgdisplay
  71. --- Volume group ---
  72. VG Name eve-ng-vg
  73. System ID
  74. Format lvm2
  75. Metadata Areas 1
  76. Metadata Sequence No 3
  77. VG Access read/write
  78. VG Status resizable
  79. MAX LV 0
  80. Cur LV 2
  81. Open LV 2
  82. Max PV 0
  83. Cur PV 1
  84. Act PV 1
  85. VG Size 39.52 GiB
  86. PE Size 4.00 MiB
  87. Total PE 10117
  88. Alloc PE / Size 10117 / 39.52 GiB
  89. Free PE / Size 0 / 0
  90. VG UUID EvwUbc-nfsg-dHqA-znxB-U3gv-V782-Q8ZxkI
  91.  
  92. root@eve-ng:~# vgextend eve-ng-vg /dev/sda3
  93. Volume group "eve-ng-vg" successfully extended
  94. root@eve-ng:~# vgdisplay
  95. --- Volume group ---
  96. VG Name eve-ng-vg
  97. System ID
  98. Format lvm2
  99. Metadata Areas 2
  100. Metadata Sequence No 4
  101. VG Access read/write
  102. VG Status resizable
  103. MAX LV 0
  104. Cur LV 2
  105. Open LV 2
  106. Max PV 0
  107. Cur PV 2
  108. Act PV 2
  109. VG Size 249.52 GiB
  110. PE Size 4.00 MiB
  111. Total PE 63877
  112. Alloc PE / Size 10117 / 39.52 GiB
  113. Free PE / Size 53760 / 210.00 GiB
  114. VG UUID EvwUbc-nfsg-dHqA-znxB-U3gv-V782-Q8ZxkI
  115.  
  116. root@eve-ng:~# lvdisplay
  117. --- Logical volume ---
  118. LV Path /dev/eve-ng-vg/root
  119. LV Name root
  120. VG Name eve-ng-vg
  121. LV UUID ktUpR0-VpqS-rJJv-a1FC-u6Gd-U8yN-2WYQ53
  122. LV Write Access read/write
  123. LV Creation host, time eve-ng, 2018-03-21 02:25:21 +0200
  124. LV Status available
  125. # open 1
  126. LV Size 38.56 GiB
  127. Current LE 9872
  128. Segments 1
  129. Allocation inherit
  130. Read ahead sectors auto
  131. - currently set to 256
  132. Block device 253:0
  133.  
  134. //扩展逻辑卷
  135. root@eve-ng:~# lvextend -l +100%FREE /dev/eve-ng-vg/root
  136. Size of logical volume eve-ng-vg/root changed from 38.56 GiB (9872 extents) to 248.56 GiB(63632 extents).
  137. Logical volume root successfully resized.
  138.  
  139. root@eve-ng:~# df -h
  140. Filesystem Size Used Avail Use% Mounted on
  141. udev 7.9G 0 7.9G 0% /dev
  142. tmpfs 1.6G 19M 1.6G 2% /run
  143. /dev/mapper/eve--ng--vg-root 38G 13G 23G 36% /
  144. tmpfs 7.9G 0 7.9G 0% /dev/shm
  145. tmpfs 5.0M 0 5.0M 0% /run/lock
  146. tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
  147. /dev/sda1 472M 83M 365M 19% /boot
  148.  
  149. //在线扩容,要先查看当前的文件系统的类型,可以通过cat /etc/fstab来查看
  150. root@eve-ng:~# resize2fs /dev/eve-ng-vg/root
  151. resize2fs 1.42.13 (17-May-2015)
  152. Filesystem at /dev/eve-ng-vg/root is mounted on /; on-line resizing required
  153. old_desc_blocks = 3, new_desc_blocks = 16
  154. The filesystem on /dev/eve-ng-vg/root is now 65159168 (4k) blocks long.
  155.  
  156. root@eve-ng:~# df -h
  157. Filesystem Size Used Avail Use% Mounted on
  158. udev 7.9G 0 7.9G 0% /dev
  159. tmpfs 1.6G 19M 1.6G 2% /run
  160. /dev/mapper/eve--ng--vg-root 245G 13G 222G 6% /
  161. tmpfs 7.9G 0 7.9G 0% /dev/shm
  162. tmpfs 5.0M 0 5.0M 0% /run/lock
  163. tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
  164. /dev/sda1 472M 83M 365M 19% /boot
  165.  

参考

在线扩容
使用 resize2fs或xfs_growfs 对挂载目录在线扩容

  • resize2fs 针对文件系统ext2 ext3 ext4
  • xfs_growfs 针对文件系统xfs

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

分享到:

相关信息

  • linux定时关机设置教程

    当linux在运作时不能直接关闭电源容易将档案系统损毁,因此需要用shutdown以安全的方式关闭,那么这个操作该怎么实现呢?下面就为大家带来了详细教程。...

    2022-11-07

  • linux强制删除文件教程

    由于linux系统和我们常用的windows系统是不一样的,所以如果是初学者,可能会不知道linux怎么强制删除文件,其实我们只要打开终端,使用命令就可以删除了。...

    2022-11-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载