使用情况:
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