在 Linux 下使用 fdisk 扩展分区容量

摘要: 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务;又或者我们在使用linux的过程中, 有时会因为安装系统时分区不当导致有的分区空间不足,而有的分区空间过剩的情况,都可以是使用fdisk分区工具来动态调整分区的大小。

我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务;又或者我们在使用linux的过程中, 有时会因为安装系统时分区不当导致有的分区空间不足,而有的分区空间过剩的情况,都可以是使用fdisk分区工具来动态调整分区的大小。

扩展磁盘空间

硬盘空间为20G,使用vSphere Client增加磁盘大小,需要再增加10G空间;

fdisk_l-20G

fdisk_l-20G

vshere_add

vshere_add

扩展完后,重启系统,再次使用fdisk -l查看,会发现硬盘空间变大了;

fdisk_l-30

fdisk_l-30

重新创建分区,调整分区信息

本次实验主要对/dev/sda4这个分区扩展,如果是生产环境,请提前做好备份保存到其他分区,虽然扩展分区大小不会导致数据丢失,安全起见,请提前做好备份;
首先模拟出一些数据:


  1. [root@linuxprobe data]# mkdir test
  2. [root@linuxprobe data]# echo "we are Linuxer" > linuxprobe
  3. [root@linuxprobe data]# ll
  4. total 24
  5. -rw-r--r--. 1 root root 15 May 23 21:59 linuxprobe
  6. drwx------. 2 root root 16384 May 23 15:07 lost+found
  7. drwxr-xr-x. 2 root root 4096 May 23 21:51 test
  8. [root@linuxprobe ~]# umount /dev/sda4 ### 卸载磁盘分区

若提示磁盘忙,使用fuser找出将正在使用磁盘的程序并结束掉;


  1. fuser --/data
  2. fuser ----/data

使用fdisk工具先删除/dev/sda4分区,然后创建新分区,注意开始的磁柱号要和原来的一致(是保证数据不丢失的关键步骤),结束的磁柱号默认回车使用全部磁盘。


  1. [root@linuxprobe ~]# fdisk /dev/sda
  2. WARNING: DOS-compatible mode is deprecated. It is strongly recommended to
  3.  switch off the mode (command 'c') and change display units to
  4. sectors (command 'u').
  5. Command (for help): p ### 查看分区表信息
  6. Disk /dev/sda: 32.2 GB, 32212254720 bytes
  7. 255 heads, 63 sectors/track, 3916 cylinders
  8. Units = cylinders of 16065 * 512 = 8225280 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk identifier: 0x0005210c
  12.  Device Boot Start End Blocks Id System
  13. /dev/sda1 * 1 26 204800 83 Linux
  14. Partition 1 does not end on cylinder boundary.
  15. /dev/sda2 26 1301 10240000 83 Linux
  16. /dev/sda3 1301 1497 1572864 82 Linux swap / Solaris
  17. /dev/sda4 1497 2611 8952832 83 Linux
  18. Command (for help): d ### 删除分区
  19. Partition number (1-4): 4 ### 删除第四个
  20. Command (for help): p ### 再次查看分区信息,/dev/sda4已被删除
  21. Disk /dev/sda: 32.2 GB, 32212254720 bytes
  22. 255 heads, 63 sectors/track, 3916 cylinders
  23. Units = cylinders of 16065 * 512 = 8225280 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/size (minimum/optimal): 512 bytes / 512 bytes
  26. Disk identifier: 0x0005210c
  27.  Device Boot Start End Blocks Id System
  28. /dev/sda1 * 1 26 204800 83 Linux
  29. Partition 1 does not end on cylinder boundary.
  30. /dev/sda2 26 1301 10240000 83 Linux
  31. /dev/sda3 1301 1497 1572864 82 Linux swap / Solaris
  32. Command (for help): n ### 创建新的分区
  33. Command action
  34. e extended
  35. p primary partition (1-4)
  36. #创建为主分区
  37. Selected partition 4
  38. First cylinder (1497-3916, default 1497): ### 经对比,正好和上一个磁盘柱一致,默认即可
  39. Using default value 1497
  40. Last cylinder, +cylinders or +size{K,M,G} (1497-3916, default 3916):
  41. Using default value 3916 ### 直接默认就可以
  42. Command (for help): p ### 查看分区表信息
  43. Disk /dev/sda: 32.2 GB, 32212254720 bytes
  44. 255 heads, 63 sectors/track, 3916 cylinders
  45. Units = cylinders of 16065 * 512 = 8225280 bytes
  46. Sector size (logical/physical): 512 bytes / 512 bytes
  47. I/size (minimum/optimal): 512 bytes / 512 bytes
  48. Disk identifier: 0x0005210c
  49.  Device Boot Start End Blocks Id System
  50. /dev/sda1 * 1 26 204800 83 Linux
  51. Partition 1 does not end on cylinder boundary.
  52. /dev/sda2 26 1301 10240000 83 Linux
  53. /dev/sda3 1301 1497 1572864 82 Linux swap / Solaris
  54. /dev/sda4 1497 3916 19436582 83 Linux
  55. Command (for help): wp ### 保存并退出,如果创建有误,直接退出不要保存即可
  56. The partition table has been altered!
  57. Calling ioctl() to re-read partition table.
  58. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  59. The kernel still uses the old table. The new table will be used at
  60. the next reboot or after you run partprobe(8) or kpartx(8)
  61. Syncing disks.

fdisk-d

fdisk-d

fdisk-p
重新创建分区后,需要重启一下;

fdisk-p


  1. [root@linuxprobe ~]# init 6
  2. [root@linuxprobe ~]# e2fsck -/dev/sda4 ### 检查分区信息
  3. [root@linuxprobe ~]# resize2fs -/dev/sda4 ### 调整分区大小

重新挂载、查看分区大小、数据:

df-hT_19G

df-hT_19G

可见/dev/sda4分区的空间大小已经增加了。

原文发布时间为:2016-05-26

本文来自云栖社区合作伙伴“Linux中国”

原文链接

原文地址:https://www.cnblogs.com/jzy996492849/p/7048451.html