centos7 压缩回收swap分区,增加/home分区

author:石鲁坤

因为部署项目过程中服务器改变使用用途,将数据库服务器改用为应用服务器,之前的分区规划swap占用了15G,home目录20G,考虑应用服务器对swap应用有限,造成资源浪费,所以将swap分区减小到5G,将home增加10G

磁盘应用了逻辑分区,记录操作过程如下

[root@shiyou ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  185G  2.0G  183G   2% /
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G   11M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/sda2               1014M  135M  880M  14% /boot
/dev/sda1               1022M  9.8M 1013M   1% /boot/efi
/dev/sdb1                1.1T  265M  1.1T   1% /app
/dev/mapper/centos-home   20G   33M   20G   1% /home
tmpfs                    3.1G     0  3.1G   0% /run/user/0

[root@shiyou ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:             30           1          28           0           1          29
Swap:            15           0          15
 

1,首先将swap分区解挂

[root@shiyou ~]# swapoff /dev/mapper/centos-swap

[root@shiyou ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:             30           1          28           0           1          29
Swap:             0           0           0

此时swap分区已经显示为0

2,压缩 /dev/mapper/centos-swap 分区

[root@shiyou ~]# lvreduce -L -10240M /dev/mapper/centos-swap

WARNING: Reducing active logical volume to 6.00 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 16.00 GiB (4096 extents) to 6.00 GiB (1536 extents).
  Logical volume centos/swap successfully resized.
[root@shiyou ~]# mkswap /dev/mapper/centos-swap
mkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 11534332 KiB
no label, UUID=35749ea4-428c-47fb-9384-5fdbd35d4977

3,重新挂载swap分区

[root@shiyou ~]# swapon /dev/mapper/centos-swap
[root@shiyou ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:             30           1          28           0           1          29
Swap:             5           0           5

此时swap分区已经变成了5G,接下来我们将10G空间增加到home下

4,检查free的PE,将空闲出的空间增加到home分区

[root@shiyou ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               220.58 GiB
  PE Size               4.00 MiB
  Total PE              56469
  Alloc PE / Size       53908 / <210.58 GiB
  Free  PE / Size       2561 / 10.00 GiB
  VG UUID               8l07mB-oXcz-WwDx-Rl3i-tRXX-D7gM-tIokDr
此时看到Free  PE / Size       2561 / 10.00 GiB

[root@shiyou ~]# lvresize -L +10G  /dev/mapper/centos-home

[root@shiyou ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  16
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               220.58 GiB
  PE Size               4.00 MiB
  Total PE              56469
  Alloc PE / Size       56469 / 220.58 GiB
  Free  PE / Size       0 / 0   
  VG UUID               8l07mB-oXcz-WwDx-Rl3i-tRXX-D7gM-tIokDr
此时所有的空闲空间到/dev/mapper/centos-home

[root@shiyou ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                xXZju5-tSek-qsTG-sInL-gd2C-L8z8-rcZCcw
  LV Write Access        read/write
  LV Creation host, time bogon, 2019-06-17 09:23:33 +0800
  LV Status              available
  # open                 1
  LV Size                30.00 GiB
  Current LE             7681
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
5,增加home文件系统的容量,这有有个报错,一并记录

[root@shiyou ~]# resize2fs /dev/centos/home
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/home
Couldn't find valid filesystem superblock.

报错了,打不开分区

[root@shiyou ~]# mount|grep home  或者使用(df -T)
/dev/mapper/centos-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,sunit=128,swidth=128,noquota)
发现/dev/mapper/centos-home 是xfs文件系统,不是ext

[root@shiyou ~]# xfs_growfs /dev/mapper/centos-home

meta-data=/dev/mapper/centos-home isize=512    agcount=16, agsize=327680 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=16     swidth=16 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 5242880 to 7669760

[root@shiyou ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  185G  2.6G  182G   2% /
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G   11M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/sda2               1014M  135M  880M  14% /boot
/dev/sda1               1022M  9.8M 1013M   1% /boot/efi
/dev/sdb1                1.1T  265M  1.1T   1% /app
/dev/mapper/centos-home   30G   55M   30G   1% /home
tmpfs                    3.1G     0  3.1G   0% /run/user/0
检查可以看到home目录已经成功增加到30G

整个拓展空间过程因为磁盘使用了lvm,无需系统重启,即可动态解决



 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章