xfs ext4 combined with lvm to expand and shrink capacity - the road to dream

Ext4 file system expansion and shrinkage operations

Expand the system root partition

The root file system is on the /dev/VolGroup/lv_root logical volume, the file system type is ext4, and the size is 10G. Now it needs to be expanded to 20G.

给空闲空间分区

# 调整分区类型为LVM,也就是8e类型
fdisk /dev/sdb
    # 选定分区后使用 t 选项,然后选择8e类型
 
# 通知内核重读分区表
partx -a /dev/sdb

# 查看
cat /proc/partitions

lsblk
扩容操作

# 1. 创建pv
pvcreate /dev/sdb7

# 2. 对根 / 所在的vg进行扩容,将/dev/sdb7这个pv添加到VolGroup这个vg中去
vgextend VolGroup /dev/sdb7

# 3. 对根 / 所在的lv进行扩容
# 先通过vgdisplay命令查看对应vg中空闲的pe数量,然后再将这些空闲的pe添加到lv中去
lvextend -l +2194 /dev/VolGroup/lv_root 

# 4. 扩容文件系统
resize2fs /dev/VolGroup/lv_root  # 自动扩容,将可分配的空间全部分配完
# 或者扩容成指定的大小 resize2fs /dev/VolGroup/lv_root  15G

# 5. 查看是否扩容成功
df -hT
  • Note: Before LV expansion, it is best to uninstall the mounted device first, and then use lvextend to expand the capacity. Here, because the root file system is expanded, it is not uninstalled.

Shrinkage

The total partition size of /dev/sdb6 and /dev/sdb4 is 5G, and the partition type is 8e. They are added to the volume group VolGroup and mounted as lv_mnt logical volumes in the /mnt directory.

Now shrink lv_mnt to 3G, and its data cannot be damaged

# 1. 卸载设备
umount /mnt

# 2. 进行磁盘检查
e2fsck -f /dev/VolGroup/lv_mnt

# 3. 收缩文件系统
resize2fs /dev/VolGroup/lv_mnt 3G

# 4. 收缩LV
lvreduce -L -2G /dev/VolGroup/lv_mnt 

# 5. 将PE移动到空闲的地方
### 查看哪些PV上有空闲的PE
pvdisplay | grep 'PV Name| Free'
### 将PE移动到有空闲PE的PV上
pvmove  /dev/sdb6:0-99  /dev/sdb4

# 6. 从VG中移除PV
vgreduce VolGroup /dev/sdb6

# 7. 移除该PV
pvremove /dev/sdb6

xfs file system expansion and shrinkage operations

expansion

# 1. 与CentOS6一样,先要创建一个8e格式磁盘分区
fdisk /dev/sdb

# 2. 创建PV
pvcreate /dev/sdb5

# 3. 将pv添加到根所在的VG中
vgextend centos /dev/sdb5

# 4. 扩容LV# 特别注意:在扩容LV之前最好要先卸载设备,这里没有卸载是因为扩容的根文件系统lvextend -l +2048 /dev/centos/root

# 5. 扩容xfs文件系统
xfs_growfs /dev/centos/root

# 6. 查看是否扩容成功
df -hT

Shrinkage

The xfs file system can only be expanded but not shrunk. If you want to shrink, you can back it up first, then rebuild it with reduced capacity, and then restore the data.

---------------备份

# xfsdump的方式备份
xfsdump -f /data/home.img /home
    -f 备份保存的位置

# cp直接复制文件的方式备份
cp -aR /home /data/home

----------------缩容重建

# 1. 卸载home目录
umount /home/

# 2. 删除 lv_home
### 删除之前要备份或者将PE移动到空闲的PV中
lvremove /dev/centos/home

# 3. 扩容根所在的的 lv
lvresize -L 20G /dev/centos/root

# 4. 扩容根文件系统
xfs_growfs /dev/centos/root

# 5. 重建home的LV
lvcreate -L 1G -n home centos

# 6. 格式化并挂载
mkfs.xfs /dev/centos/home
mount /dev/centos/home /home

# 7. 还原数据
xfsrestore -f /data/home.img /home

  • Note: Try to avoid shrinking the file system where important data is located

lvm logical volume management

 

 

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/132497523