Centos-扩容已有分区的磁盘空间

117 阅读1分钟

在原博客中仅有的存量不多的好文,曾经陪伴笔者多次解决Linux磁盘空间的问题。

针对:磁盘已在用盘,再扩容空间用于增加新的MBR分区。

分区挂载准备

$ fdisk -lu /dev/vda  #运行以下命令确认数据盘的分区表格式,,本例是MBR分区
  #如果"System"="Linux",说明数据盘使用的是MBR分区表格式。
  #如果"System"="GPT",说明数据盘使用的是GPT分区表格式。
$ blkid /dev/vda1  #确认已有分区的文件系统类型。此文件系统类型为ext4。
  # (如未分区,无信息显示)
  /dev/vdb1: UUID="e97bf1e2-fc84-4c11-9652-73********24" TYPE="ext4"
$ e2fsck -n /dev/vda1	#ext4,查看文件系统状态 clean代表是正常的
  #xfs_repair -n 		#xfs文件系统用这个命令

新增并格式化MBR分区

场景:新增空间用于增加新的MBR分区

$ fdisk -u /dev/vda  #命令新建分区
  Command (m for help): p  #打印分区情况
  Command (m for help): n  #创建分我
  Partition type:
     p   primary (1 primary, 0 extended, 3 free)
     e   extended
  Select (default p): p #主分区
  Partition number (2-4, default 2): 2
  First sector (41943040-83886079, default 41943040):  回车
  Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079): 回车

  Command (m for help): w  #
  The partition table has been altered!
  Calling ioctl() to re-read partition table.
      WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
      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)
      Syncing disks.

$  fdisk -lu /dev/vda	      #查看分区

等待重启连接

[root@*** ~]#   reboot
  1. mkfs.ext4 /dev/vda2 #创建文件系统
[root@***~]#  fdisk -lu /dev/vda	      #查看分区
[root@***~]# mkfs.ext4 /dev/vda2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

备份数据,清空/data用户分区

[root@***~]#  cp /etc/fstab /etc/fstab.bak
[root@*** ~]#  mkdir /bak
[root@*** ~]#  tar -zcvf /bak/data.tar.gz /data/
[root@*** ~]#  cd /data & cd /data
[root@*** ~]#  rm -rf * &&  cd ..

向/etc/fstab写入新分区信息,启动开机自动挂载分区。

#向/etc/fstab里写入新分区信息。如只是挂载到 /mnt,将/data 改为/mnt 
[root@*** ~]#  echo `blkid /dev/vda2 | awk '{print $2}' | sed 's/\"//g'` /data ext4 defaults 0 0 >> /etc/fstab
[root@*** ~]# cat /etc/fstab #查看信息
[root@*** ~]# mount /dev/vda2 /data   #立即生效的mount

df -h #查看结果情况

”/data“挂载成功

[root@*** /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  460K  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda1        20G   15G  3.7G  81% /
tmpfs           379M     0  379M   0% /run/user/0
/dev/vda2        20G   45M   19G   1% /data

恢复数据

$ tar -zxvf /bak/data.tar.gz -C /data/
$ mv /data/data/* /data/
$ rmdir /data/data
$ reboot