将cl-home的磁盘空间压缩分配给cl-root

1 篇文章 0 订阅

转发自 -- 琦彦

磁盘情况:

    home目录:945G

root目录:70G

root分区不够用

思路:把home分区的空间划一部分到root分区

# 设置home分区大小为100G,释放300G空间 [LV Path]
$ lvreduce -L 100G /dev/cl/home
 
# 将空闲空间扩展到root分区 [LV Path]
$ lvextend -l +100%FREE /dev/cl/root
或者 lvextend -l +100G /dev/cl/root ## 指定数值
 
# 使用XFS文件系统自带的命令集增加分区空间 [Filesystem]
$ xfs_growfs /dev/mapper/cl-root

示例

挂载在根目录的分区/dev/mapper/cl-root 爆满,占用100%
# 查看文件系统类型
[root@localhost bigunion]# df -Th
文件系统            类型      容量  已用  可用 已用% 挂载点
devtmpfs            devtmpfs  7.8G     0  7.8G    0% /dev
tmpfs               tmpfs     7.8G     0  7.8G    0% /dev/shm
tmpfs               tmpfs     7.8G  9.2M  7.8G    1% /run
tmpfs               tmpfs     7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        70G   70G   12M  100% /
/dev/sda2           xfs      1014M  237M  778M   24% /boot
/dev/mapper/cl-home xfs       945G  6.9G  938G    1% /home
/dev/sda1           vfat      599M  7.3M  592M    2% /boot/efi
tmpfs               tmpfs     1.6G     0  1.6G    0% /run/user/0
overlay             overlay    70G   70G   12M  100% /var/lib/docker/overlay2/bb3e7c89d63c2647a88190874c89eb587991785738f9f6929da3aa0797f96378/merged
# 查看文件系统大小
[root@localhost bigunion]# df -hl
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             7.8G     0  7.8G    0% /dev
tmpfs                7.8G     0  7.8G    0% /dev/shm
tmpfs                7.8G  9.2M  7.8G    1% /run
tmpfs                7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/cl-root   70G   70G   14M  100% /
/dev/sda2           1014M  237M  778M   24% /boot
/dev/mapper/cl-home  945G  6.9G  938G    1% /home
/dev/sda1            599M  7.3M  592M    2% /boot/efi
tmpfs                1.6G     0  1.6G    0% /run/user/0
overlay               70G   70G   14M  100% /var/lib/docker/overlay2/bb3e7c89d63c2647a88190874c89eb587991785738f9f6929da3aa0797f96378/merged

问题分析

挂载在根目录的分区空间太小,只有70G,而服务器 home 目录为非常用目录,挂在了近945G的空间。
思路:从cl-home 分区划出300G空间到 cl-root 分区。

操作步骤

1.查看各分区信息

[root@localhost bigunion]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                NdwWIy-HWMw-rlbK-mifr-c1jF-4rrP-8YtgvJ
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:37 +0800
  LV Status              available
  # open                 2
  LV Size                7.89 GiB
  Current LE             2020
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cl/home
  LV Name                home
  VG Name                cl
  LV UUID                Tly4h6-pMT8-1LYJ-Jjkl-GGch-3taH-VVQ0zS
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:37 +0800
  LV Status              available
  # open                 1
  LV Size                <944.52 GiB
  Current LE             241797
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                Keic5y-q0fQ-6fAy-ccyc-Cirp-Kpeg-NuzYrg
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:43 +0800
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

2.减少/dev/cl/home分区空间

# 释放 /dev/cl/home 分区 844.52G 的空间
# 命令设置 /dev/cl/home 分区 100G空间
[root@localhost bigunion]# lvreduce -L 100G /dev/cl/home
  WARNING: Reducing active and open logical volume to 100.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cl/home? [y/n]: y
  Size of logical volume cl/home changed from <944.52 GiB (241797 extents) to 100.00 GiB (25600 extents).
  Logical volume cl/home successfully resized.

3.将空闲空间扩展到root分区 [LV Path]

[root@localhost bigunion]# lvextend -l +100%FREE /dev/cl/root
  Size of logical volume cl/root changed from 70.00 GiB (17920 extents) to <914.52 GiB (234117 extents).
  Logical volume cl/root successfully resized.

4.扩展XFS文件空间大小

[root@localhost bigunion]# xfs_growfs /dev/mapper/cl-root  ##通过fdisk -l查看磁盘分区名称为/dev/mapper/cl-root
meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=4587520 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=18350080, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=8960, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 18350080 to 239735808

验证

[root@localhost bigunion]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                NdwWIy-HWMw-rlbK-mifr-c1jF-4rrP-8YtgvJ
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:37 +0800
  LV Status              available
  # open                 2
  LV Size                7.89 GiB
  Current LE             2020
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cl/home
  LV Name                home
  VG Name                cl
  LV UUID                Tly4h6-pMT8-1LYJ-Jjkl-GGch-3taH-VVQ0zS
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:37 +0800
  LV Status              available
  # open                 1
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                Keic5y-q0fQ-6fAy-ccyc-Cirp-Kpeg-NuzYrg
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-21 15:47:43 +0800
  LV Status              available
  # open                 1
  LV Size                <914.52 GiB
  Current LE             234117
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@localhost bigunion]# fdisk -l
Disk /dev/sda:1 TiB,1099511627776 字节,2147483648 个扇区
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
磁盘标识符:FCD2E4E9-95FA-4E25-9B07-FD489CBDF52A
设备          起点       末尾       扇区    大小 类型
/dev/sda1     2048    1230847    1228800    600M EFI 系统
/dev/sda2  1230848    3327999    2097152      1G Linux 文件系统
/dev/sda3  3328000 2147481599 2144153600 1022.4G Linux LVM
Disk /dev/mapper/cl-root:914.5 GiB,981957869568 字节,1917886464 个扇区
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
Disk /dev/mapper/cl-swap:7.9 GiB,8472494080 字节,16547840 个扇区
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
Disk /dev/mapper/cl-home:100 GiB,107374182400 字节,209715200 个扇区
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

xfs_growfs

XFS是一个开源的(GPL)日志文件系统,最初由硅谷图形(SGI)开发,现在大多数的Linux发行版都支持。
事实上,XFS已被最新的CentOS/RHEL 7采用,成为其默认的文件系统。在其众多的特性中,包含了“在线调整大小”这一特性,使得现存的XFS文件系统在已经挂载的情况下可以进行扩展。
然而,对于XFS文件系统的缩减却还没有支持。
要扩展一个现存的XFS文件系统,你可以使用命令行工具xfs_growfs,这在大多数Linux发行版上都默认可用。由于XFS支持在线调整大小,目标文件系统可以挂载,也可以不挂载。
[root@localhost bigunion]# xfs_growfs --help
xfs_growfs: 不适用的选项 -- -
Usage: xfs_growfs [options] mountpoint
Options:
-d          grow data/metadata section
-l          grow log section
-r          grow realtime section
-n          don't change anything, just show geometry
-i          convert log from external to internal format
-t          alternate location for mount table (/etc/mtab)
-x          convert log from internal to external format
-D size     grow data/metadata section to size blks
-L size     grow/shrink log section to size blks
-R size     grow realtime section to size blks
-e size     set realtime extent size to size blks
-m imaxpct  set inode max percent to imaxpct
-V          print version information

疑问点

df -hl  显示的 /dev/mapper/cl-home空间和实际不一致

磁盘空间分配分配前

# 查看文件系统大小
[root@localhost bigunion]# df -hl
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             7.8G     0  7.8G    0% /dev
tmpfs                7.8G     0  7.8G    0% /dev/shm
tmpfs                7.8G  9.2M  7.8G    1% /run
tmpfs                7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/cl-root   70G   70G   14M  100% /
/dev/sda2           1014M  237M  778M   24% /boot
/dev/mapper/cl-home  945G  6.9G  938G    1% /home
/dev/sda1            599M  7.3M  592M    2% /boot/efi
tmpfs                1.6G     0  1.6G    0% /run/user/0
overlay               70G   70G   14M  100% /var/lib/docker/overlay2/bb3e7c89d63c2647a88190874c89eb587991785738f9f6929da3aa0797f96378/merged

磁盘空间分配分配后

[root@localhost ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             7.8G     0  7.8G    0% /dev
tmpfs                7.8G     0  7.8G    0% /dev/shm
tmpfs                7.8G  9.2M  7.8G    1% /run
tmpfs                7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/cl-root  915G   77G  839G    9% /
/dev/sda2           1014M  237M  778M   24% /boot
/dev/mapper/cl-home  945G  6.9G  938G    1% /home
/dev/sda1            599M  7.3M  592M    2% /boot/efi
tmpfs                1.6G     0  1.6G    0% /run/user/0

df和lvdisplay的巨大区别

lvdisplay报告磁盘上的实际大小,但df显示系统可用的磁盘空间,即实际磁盘大小减去所使用的文件系统空间。

参考链接:

https://www.cnblogs.com/ct20150811/p/11010955.html

https://blog.csdn.net/lxw983520/article/details/115132507

仅作为笔记记录,以下为个人实操过程

思路:将/home分区去掉,将其空间扩容至root分区

df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   50G   45G  5.8G  89% /
devtmpfs             1.9G     0  1.9G   0% /dev
tmpfs                1.9G     0  1.9G   0% /dev/shm
tmpfs                1.9G  193M  1.7G  11% /run
tmpfs                1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1           1014M  139M  876M  14% /boot
/dev/mapper/cl-home   46G   33M   46G   1% /home

先卸载/home

umount /home ## 卸载

lsblk ## 查看/home的挂载路径
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0           2:0    1    4K  0 disk
sda           8:0    0  100G  0 disk
├─sda1        8:1    0    1G  0 part /boot   
└─sda2        8:2    0   99G  0 part
  ├─cl-root 253:0    0   50G  0 lvm  /
  ├─cl-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─cl-home 253:2    0 45.1G  0 lvm
fdisk -l ## 查看磁盘分区情况

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00013a1c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   209715199   103808000   8e  Linux LVM

Disk /dev/mapper/cl-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/cl-home: 48.4 GB, 48444211200 bytes, 94617600 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


ll /dev/cl/home ## 查看/home路径

lrwxrwxrwx 1 root root 7 Aug 19 15:31 /dev/cl/home -> ../dm-2
lvreduce -L 0G /dev/cl/home  ## 将home分区设置为0G,报错不能为0
  --size may not be zero.
  Run `lvreduce --help' for more information.
lvreduce -L 1G /dev/cl/home   ## 保险做法:将home分区改为1G
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cl/home? [y/n]: y
  Size of logical volume cl/home changed from 5.00 GiB (1280 extents) to 1.00 GiB (256 extents).
  Logical volume cl/home successfully resized.
lvextend -l +100%FREE /dev/cl/root  ## 将/home分区多出来的所有空闲空间都分配给root
  Size of logical volume cl/root changed from 90.12 GiB (23071 extents) to 94.12 GiB (24095 extents).
  Logical volume cl/root successfully resized.
xfs_growfs /dev/mapper/cl-root ## 使用 xfs_growfs 命令热扩容至 /root,此命令只使用xfs类型的分区
meta-data=/dev/mapper/cl-root    isize=512    agcount=8, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=23624704, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 23624704 to 24673280
df -h ## 检查查看磁盘扩容是否成功
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   95G   45G   50G  48% /
devtmpfs             1.9G     0  1.9G   0% /dev
tmpfs                1.9G     0  1.9G   0% /dev/shm
tmpfs                1.9G  193M  1.7G  11% /run
tmpfs                1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1           1014M  139M  876M  14% /boot
,,

 lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0           2:0    1    4K  0 disk
sda           8:0    0  100G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part
  ├─cl-root 253:0    0 94.1G  0 lvm  /
  ├─cl-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─cl-home 253:2    0    1G  0 lvm

## 最后,将/etc/fstab里的 /home挂载项取消掉

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值