CentOS 7 扩展磁盘分区

CentOS 7 扩展磁盘分区

参考资料

  1. 链接: https://www.jianshu.com/p/273daea17b2a.
  2. 链接: https://www.cnblogs.com/morgan363/p/12103619.html.
  3. 链接: https://blog.51cto.com/qq512430/1912401?source=dra.

一、当前环境

  1. 默认 / 分区 /dev/sda1 xfs 7.9G
[root@centos7-15 tmp]# df -THhl
文件系统       类型      容量  已用  可用 已用% 挂载点
devtmpfs       devtmpfs  907M     0  907M    0% /dev
tmpfs          tmpfs     917M     0  917M    0% /dev/shm
tmpfs          tmpfs     917M  8.8M  909M    1% /run
tmpfs          tmpfs     917M     0  917M    0% /sys/fs/cgroup
/dev/sda1      xfs       7.9G  4.0G  3.9G   51% /
tmpfs          tmpfs     184M     0  184M    0% /run/user/0
  1. 当前磁盘为 sda 8:0 0 16G 0 disk
[root@centos7-15 tmp]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   16G  0 disk 
├─sda1   8:1    0  7.8G  0 part /
└─sda2   8:2    0  8.2G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom  
  1. 当前分区情况
[root@centos7-15 tmp]# fdisk -l

磁盘 /dev/sda:17.2 GB, 17179869184 字节,33554432 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    16386047     8192000   83  Linux
/dev/sda2        16386048    33554431     8584192   82  Linux swap / Solaris

二、扩展步骤

因为swap分区与默认 / 分区相连,所以先删除swap分区,然后扩展/ 分区,最后重建swap分区

(一)卸载删除已有swap分区

  1. 停止正在使用的swap分区
swapoff /dev/sda2
  1. 删除swap文件
rm -rf /dev/sda2
  1. 删除/etc/fstab文件中swap启动配置
[root@centos7-80 share]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 29 23:23:28 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=ac2b25eb-b673-4ca5-b031-fc71589ec4f9 /                  xfs     defaults        0 0
UUID=e5f84b5d-a7ab-46d5-b21b-ffbcc5553a49 swap           swap    defaults     0 0  #删除或注释
  1. fdisk命令删除swap分区
[root@centos7-15 ~]# fdisk /dev/sda

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):p

磁盘 /dev/sda:17.2 GB, 17179869184 字节,33554432 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    16386047     8192000   83  Linux
/dev/sda2        16386048    33554431     8584192   82  Linux swap / Solaris

命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

命令(输入 m 获取帮助):d
分区号 (1,2,默认 2)2
分区 2 已删除

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
  1. 检验结果
[root@centos7-15 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1833         491        1026           8         315        1195
Swap:             0           0           0
[root@centos7-15 ~]# fdisk -l

磁盘 /dev/sda:17.2 GB, 17179869184 字节,33554432 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    16386047     8192000   83  Linux
[root@centos7-15 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   16G  0 disk 
└─sda1   8:1    0  7.8G  0 part /
sr0     11:0    1 1024M  0 rom  

(二)扩展 / 分区、重建swap分区

磁盘已经先被扩充到20G,如果是物理机请参考用卷的方式扩展

  1. 删除 / 分区,并重建分区
    删除/ 分区后千万不能保存,应当马上扩容
    扩容/ 分区使用了+size方式,预留出swap所需空间
[root@centos7-18 ~]# fdisk /dev/sda

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

命令(输入 m 获取帮助):p

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    16386047     8192000   83  Linux

命令(输入 m 获取帮助):d
已选择分区 1
分区 1 已删除

命令(输入 m 获取帮助):p

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039)+18G
分区 1 已设置为 Linux 类型,大小设为 18 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    37750783    18874368   83  Linux
  1. 重新创建swap分区
命令(输入 m 获取帮助):p

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    37750783    18874368   83  Linux

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (37750784-41943039,默认为 37750784):
将使用默认值 37750784
Last 扇区, +扇区 or +size{K,M,G} (37750784-41943039,默认为 41943039):
将使用默认值 41943039
分区 2 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
  1. 刷新分区表
[root@centos7-15 ~]# partprobe /dev/sda
[root@centos7-18 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 4096 字节
I/O 大小(最小/最佳)4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0009ee74

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    37750783    18874368   83  Linux
/dev/sda2        37750784    41943039     2096128   83  Linux

[root@centos7-18 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0   18G  0 part /
└─sda2   8:2    0    2G  0 part 
sr0     11:0    1 1024M  0 rom  
  1. 检查分区文件系统
[root@centos7-18 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        907M     0  907M    0% /dev
tmpfs           917M     0  917M    0% /dev/shm
tmpfs           917M  8.8M  909M    1% /run
tmpfs           917M     0  917M    0% /sys/fs/cgroup
/dev/sda1       7.9G  4.0G  3.9G   51% /
tmpfs           184M     0  184M    0% /run/user/0
  1. 刷新分区并查看结果
[root@centos7-18 ~]# xfs_growfs /dev/sda1
meta-data=/dev/sda1              isize=512    agcount=4, agsize=512000 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2048000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2048000 to 4718592
[root@centos7-18 ~]# df -hl
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        907M     0  907M    0% /dev
tmpfs           917M     0  917M    0% /dev/shm
tmpfs           917M  8.8M  909M    1% /run
tmpfs           917M     0  917M    0% /sys/fs/cgroup
/dev/sda1        18G  4.0G   15G   22% /
tmpfs           184M     0  184M    0% /run/user/0
[root@centos7-18 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0   18G  0 part /
└─sda2   8:2    0    2G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom 
  1. 格式化swap分区
[root@centos7-18 ~]# mkswap /dev/sda2
正在设置交换空间版本 1,大小 = 2096124 KiB
无标签,UUID=3c3f8858-7230-494a-a9bd-6982e9c75f0a
  1. 挂载swap分区
[root@centos7-18 ~]# swapon /dev/sda2
[root@centos7-18 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1833         490        1033           8         309        1197
Swap:          2046           0        2046
  1. 添加启动加载
[root@centos7-18 ~]# blkid
/dev/sda1: UUID="ac2b25eb-b673-4ca5-b031-fc71589ec4f9" TYPE="xfs" 
/dev/sda2: UUID="3c3f8858-7230-494a-a9bd-6982e9c75f0a" TYPE="swap" 
[root@centos7-18 ~]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 29 23:23:28 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=ac2b25eb-b673-4ca5-b031-fc71589ec4f9 /                       xfs     defaults        0 0
UUID=3c3f8858-7230-494a-a9bd-6982e9c75f0a swap                    swap    defaults        0 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值