1. 程式人生 > >CentOS使用fdisk擴展磁盤空間

CentOS使用fdisk擴展磁盤空間

否則 ram ima ransac 正在 less directory spa call

使用情況:

1. 虛擬機配置150G硬盤,初始化為/dev/sdb1,後因為磁盤空間不夠使用,動態擴容至300G,擴容完成後,想要動態擴容/dev/sdb1分區。

2. 磁盤空間300G,之前分區只劃分150G的/data分區,現在/data不夠使用,想要把剩余150G容量動態增加到之前/data分區,並且保證/data數據不損壞

操作(以/data為例):

1. 關閉/data分區使用的服務

2. 卸載磁盤

使用umount命令卸載正在使用的/data磁盤

umount -l /data
若遇到磁盤繁忙,可以使用fuser查看正在使用磁盤的程序。
fuser -mv /data
手工退出或關閉占用文件的用戶或程序。或者采用如下交互式命令強制kill掉使用/data進程
fuser -mvik /data

fuser命令參數含義:

-m     name specifies a file on a mounted file system or a block device that is mounted. All processes accessing files  on  that  file system  are  listed.  If a directory file is specified, it is automatically changed to name/. to use any file system that might be mounted on that directory.
-c Same as -m option, used for POSIX compatibility. -V Display version information. -k Kill processes accessing the file. Unless changed with -signal, SIGKILL is sent. An fuser process never kills itself, but may kill other fuser processes. The effective user ID of the process executing fuser is
set to its real user ID before attempting to kill. -i Ask the user for confirmation before killing a process. This option is silently ignored if -k is not present too.

3. 磁盤分區

使用fdisk命令重新調整磁盤分區大小。刪除之前的分區,然後建立新分區。開始的磁柱號要和原來一致(否則會導致數據丟失),結束的磁柱號默認使用全部空間。

技術分享圖片


fdisk -cu /dev/sdb p #查看磁柱號 ,記住,後面要用到 d #刪除之前的分區 n #建立新分區 p #主分區 1 #第一個主分區 wq #保存退出

4. 調整分區文件系統

e2fsck -f /dev/sdb1                  #檢查分區信息
resize2fs /dev/sdb1                  #調整分區文件系統

命令含義:
e2fsck - check a Linux ext2/ext3/ext4 file system
e2fsck is used to check the ext2/ext3/ext4 family of file systems. For ext3 and ext4 filesystems that use a journal, if the system
has been shut down uncleanly without any errors, normally, after replaying the committed transactions in the journal, the file system
should be marked as clean. Hence, for filesystems that use journalling, e2fsck will normally replay the journal and exit, unless its
superblock indicates that further checking is required.
resize2fs -   ext2/ext3/ext4 file system resizer
The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system
located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel
supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and
ext4.).

如果采用虛擬機動態增加磁盤方式,需要重新通知內核分區表變化或者重啟設備
partprobe /dev/sdb

partprobe is a program that informs the operating system kernel of partition table changes, by requesting that the operating system
re-read the partition table.

5. 重新掛載文件系統

手工掛載/data目錄
mount /dev/sdb1 /data

如果想要重啟後自動掛載目錄,需要在/etc/fstab增加下邊的配置
/dev/sdb1    /data    ext4    defaults    0    0

CentOS使用fdisk擴展磁盤空間