CentOS SWAP 空间扩容

2021/08/03 CentOS 共 5672 字,约 17 分钟
闷骚的程序员

0x01 新增加一块磁盘到SWAP空间

# 格式化新增硬盘
fdisk /dev/sdc
n
p
t
8e
w

# 关闭系统交换区
swapoff /dev/centos/swap 

# 添加 sdc1 到 VG
vgextend centos /dev/sdc1

# 扩展 VG 剩余空间到 lv
lvextend -l 100%VG /dev/centos/swap 

# 设置交换分区文件
mkswap  /dev/centos/swap

# 激活交换分区
swapon  /dev/centos/swap

# 查询交换分区信息
dfree  -h

0x02 扩容现有硬盘空间并划分到SWAP

# 格式化新增硬盘空间
fdisk /dev/sda
n
p
t
8e
w
reboot now

# 关闭系统交换区
swapoff /dev/centos/swap

# 扩展SWAP VG
vgextend centos /dev/$(lsblk  | grep sda | tail -1 | awk -F'[─| ]' '{print $2}')

# 扩展SWAP LV
lvextend -l 100%VG /dev/centos/swap

# 建立交换分区
mkswap  /dev/centos/swap

# 激活系统交换区
swapon  /dev/centos/swap

0x03 特殊情况

系统自身具备扩展过一次SWAP,现进行第二次扩展,且需要同步扩展「/」目录

3.1 查询可用块设备的信息及现有SWAP信息

[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   70G  0 disk 
sda1            8:1    0    1G  0 part /boot
sda2            8:2    0   15G  0 part 
 centos-root 253:0    0 13.4G  0 lvm  /
 centos-swap 253:1    0  9.6G  0 lvm  [SWAP]
sda3            8:3    0    8G  0 part 
  centos-swap 253:1    0  9.6G  0 lvm  [SWAP]
sdb               8:16   0  150G  0 disk 
sr0              11:0    1 1024M  0 rom  
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        245M         15G         12M        198M         14G
Swap:          9.6G          0B        9.6G

3.2 删除 sda3 分区信息

# 关闭系统交换区
[root@localhost ~]# swapoff /dev/centos/swap 

# 减小 lv 逻辑卷的大小,缩减到可以踢出 sda3 的空间
[root@localhost ~]# lvreduce -L -8G /dev/centos/swap
  WARNING: Reducing active logical volume to <1.60 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/swap? [y/n]: y
  Size of logical volume centos/swap changed from <9.60 GiB (2457 extents) to <1.60 GiB (409 extents).
  Logical volume centos/swap successfully resized.

# 从 VG 中删除 sda3 
[root@localhost ~]# vgreduce centos /dev/sda3
  Removed "/dev/sda3" from volume group "centos"

# 从 pv 中删除 sda3 
[root@localhost ~]# pvremove /dev/sda3
  Labels on physical volume "/dev/sda3" successfully wiped.

# 删除 sda3 分区
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sda: 75.2 GB, 75161927680 bytes, 146800640 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bb4bd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM
/dev/sda3        33554432    50331647     8388608   8e  Linux LVM

Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted

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.

# 重启服务器
[root@localhost ~]# rebot ot now

3.3 创建新分区

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (33554432-146800639, default 33554432): 
Using default value 33554432
Last sector, +sectors or +size{K,M,G} (33554432-146800639, default 146800639): 
Using default value 146800639
Partition 3 of type Linux and of size 54 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 75.2 GB, 75161927680 bytes, 146800640 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bb4bd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM
/dev/sda3        33554432   146800639    56623104   8e  Linux LVM

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.
[root@localhost ~]# reboot now

3.4 扩展SWAP分区到16G

# 添加 sda3 到 VG
[root@localhost ~]# vgextend centos /dev/sda3
  Physical volume "/dev/sda3" successfully created.
  Volume group "centos" successfully extended

# 扩展 lv 空间15.4G
[root@localhost ~]# lvextend -L +15.4G /dev/centos/swap
  Rounding size to boundary between physical extents: 15.40 GiB.
  Size of logical volume centos/swap changed from <1.60 GiB (409 extents) to 17.00 GiB (4352 extents).
  Logical volume centos/swap successfully resized.
  
# 设置交换分区文件
[root@localhost ~]# mkswap  /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 17825788 KiB
no label, UUID=7158b624-ecb8-4bfb-b019-f562b55ab5de

# 激活交换分区
[root@localhost ~]# swapon  /dev/centos/swap

# 查询交换分区信息
[root@localhost ~]# dfree  -h
              total        used        free      shared  buff/cache   available
Mem:            15G        248M         15G         11M        198M         14G
Swap:           16G          0B         16G

3.5 扩展剩余空间到「/」

# 扩展 VG 剩余空间到 lv
[root@localhost ~]# lvextend -l 100%VG /dev/centos/root
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 51.99 GiB (13310 extents).
  Logical volume centos/root successfully resized.

# 刷新空间
[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=877824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3511296, 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=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3511296 to 13629440

# 查询结果
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   70G  0 disk 
sda1            8:1    0    1G  0 part /boot
sda2            8:2    0   15G  0 part 
 centos-root 253:0    0   52G  0 lvm  /
 centos-swap 253:1    0   17G  0 lvm  [SWAP]
sda3            8:3    0   54G  0 part 
  centos-root 253:0    0   52G  0 lvm  /
  centos-swap 253:1    0   17G  0 lvm  [SWAP]
sdb               8:16   0  150G  0 disk 
sr0              11:0    1 1024M  0 rom  
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   52G  5.9G   47G  12% /
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G   12M  7.8G   1% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1               1014M  145M  870M  15% /boot
tmpfs                    1.6G     0  1.6G   0% /run/user/0

文档信息

Search

    Table of Contents