初始环境

  • 系统:Centos7.6,最小化安装
  • 分区:默认使用lvm,分了/home和/两个分区
#从下面的输出可以看到:/分配了50G;/home分配了65G,空间有些浪费
[test@192 ~]$ df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos_192-root   50G  1.6G   49G   4% /
devtmpfs                     2.5G     0  2.5G   0% /dev
tmpfs                        2.5G     0  2.5G   0% /dev/shm
tmpfs                        2.5G   12M  2.5G   1% /run
tmpfs                        2.5G     0  2.5G   0% /sys/fs/cgroup
/dev/sda1                   1014M  132M  883M  13% /boot
/dev/mapper/centos_192-home   65G   33M   65G   1% /home
tmpfs                        495M     0  495M   0% /run/user/1000
tmpfs                        495M     0  495M   0% /run/user/0
#lvs显示逻辑卷信息,需要root权限运行
[test@192 ~]$ sudo lvs
  LV   VG         Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos_192 -wi-ao---- <64.12g                                                    
  root centos_192 -wi-ao----  50.00g                                                    
  swap centos_192 -wi-ao----  <4.88g 

目标

  • 缩小/home空间为5G
  • 创建新分区/data并分配50G空间

步骤

/home缩容

  • 首先备份 /home下面的目录,因为xfs默认不允许缩容,缩容需要重新格式化
#/home目录只有用户test的家目录,所以只要备份这个目录就行
[test@192 /]$ sudo cp /home/test /opt/ -rp
  • 卸载/home
[test@192 /]$ sudo umount /home
  • 对home卷缩容
#lvreduce用于调整逻辑卷大小,-L指定逻辑卷的空间大小
[test@192 /]$ sudo lvreduce -L 5G /dev/mapper/centos_192-home 
  WARNING: Reducing active logical volume to 5.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos_192/home? [y/n]: y
  Size of logical volume centos_192/home changed from <64.12 GiB (16414 extents) to 5.00 GiB (1280 extents).
  Logical volume centos_192/home successfully resized.
  • 格式化新的/home分区
[test@192 /]$ sudo mkfs.xfs -f /dev/mapper/centos_192-home 
meta-data=/dev/mapper/centos_192-home isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • 挂载新的/home分区,可以看到/home分区已经变为5G
[test@192 /]$ sudo mount /home
#可以看到/home已经变为5G
[test@192 /]$ df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos_192-root   50G  1.6G   49G   4% /
devtmpfs                     2.5G     0  2.5G   0% /dev
tmpfs                        2.5G     0  2.5G   0% /dev/shm
tmpfs                        2.5G   12M  2.5G   1% /run
tmpfs                        2.5G     0  2.5G   0% /sys/fs/cgroup
/dev/sda1                   1014M  132M  883M  13% /boot
tmpfs                        495M     0  495M   0% /run/user/1000
/dev/mapper/centos_192-home  5.0G   33M  5.0G   1% /home
  • 恢复原/home分区下的目录
[test@192 /]$ sudo cp /opt/test /home/ -rp

创建/data分区

  • 创建/data
[test@192 /]$ sudo mkdir /data
  • 查看VG名字
#vgdisplay显示卷组信息
[test@192 /]$ sudo vgdisplay 
  --- Volume group ---
  VG Name               centos_192
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  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               <119.00 GiB
  PE Size               4.00 MiB
  Total PE              30463
  Alloc PE / Size       15328 / <59.88 GiB
  Free  PE / Size       15135 / 59.12 GiB
  VG UUID               puZvUi-cXGH-FPFj-lmBN-6tM3-cbKT-Eb6k6Y
  • 创建名字为data的LV
#lvcreate创建逻辑卷,-L空间大小,-n逻辑卷名称,最后参数为卷组名
[test@192 /]$ sudo lvcreate -L 50G -n data centos_192
  Logical volume "data" created.
[test@192 /]$ sudo lvs
  LV   VG         Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data centos_192 -wi-a----- 50.00g                                                    
  home centos_192 -wi-ao----  5.00g                                                    
  root centos_192 -wi-ao---- 50.00g                                                    
  swap centos_192 -wi-ao---- <4.88g  
  • 格式化data
[test@192 /]$ sudo mkfs.xfs /dev/mapper/centos_192-data 
meta-data=/dev/mapper/centos_192-data isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • 挂载到/data目录
[test@192 /]$ sudo mount /dev/mapper/centos_192-data /data
[test@192 /]$ sudo mount |grep mapper
/dev/mapper/centos_192-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/mapper/centos_192-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/mapper/centos_192-data on /data type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
  • 修改/etc/fstab文件,实现开机自动挂载
[test@192 /]$ sudo vi /etc/fstab 
[test@192 /]$ cat /etc/fstab |grep -Ev "^#|^$"
/dev/mapper/centos_192-root /                       xfs     defaults        0 0
UUID=90c04914-92d7-47bc-956f-e1a4bb0fe848 /boot                   xfs     defaults        0 0
/dev/mapper/centos_192-home /home                   xfs     defaults        0 0
/dev/mapper/centos_192-data /data                   xfs     defaults        0 0
/dev/mapper/centos_192-swap swap                    swap    defaults        0 0
  • 检查最终效果
[test@192 /]$ df -hT
Filesystem                  Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos_192-root xfs        50G  1.6G   49G   4% /
devtmpfs                    devtmpfs  2.5G     0  2.5G   0% /dev
tmpfs                       tmpfs     2.5G     0  2.5G   0% /dev/shm
tmpfs                       tmpfs     2.5G   12M  2.5G   1% /run
tmpfs                       tmpfs     2.5G     0  2.5G   0% /sys/fs/cgroup
/dev/sda1                   xfs      1014M  132M  883M  13% /boot
tmpfs                       tmpfs     495M     0  495M   0% /run/user/1000
/dev/mapper/centos_192-home xfs       5.0G   33M  5.0G   1% /home
/dev/mapper/centos_192-data xfs        50G   33M   50G   1% /data

名词解释

LVM架构

发表评论

电子邮件地址不会被公开。 必填项已用*标注