LVM XFS縮小與擴容根目錄

本文章內容:

1. 縮小根目錄

2. 擴容根目錄

參考資料:

 

 

XFS是CentOS 7 预装的操作系统,在LVM中XFS只能扩大不能缩小,所以需要利用xfsdump / xfsrestore 工具在必须缩小lvm 的情况下,备份与还原资料。

1. 縮小根目錄

1.1 我們要將根目錄縮小至10G

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        17G  1.4G   16G   9% /
devtmpfs                devtmpfs  478M     0  478M   0% /dev
tmpfs                   tmpfs     489M     0  489M   0% /dev/shm
tmpfs                   tmpfs     489M  6.8M  482M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  125M  890M  13% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0
/dev/sdb1               xfs        20G   33M   20G   1% /data

1.2 安裝xfsdump工具

[root@localhost ~]# yum install xfsdump -y

安裝完xfsdump包後會產生xfsdump和xfsrestore指令:

[root@localhost ~]# rpm -ql xfsdump
/usr/sbin/xfsdump
/usr/sbin/xfsinvutil
/usr/sbin/xfsrestore
...省略

1.3 備份根目錄

我們將根目錄備份到/data目錄。注意,/data目錄需與根目錄是不同的分區,因為等下要縮減根目錄,根目錄會重建。

[root@localhost ~]# xfsdump -f /data/root.dump /
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> root_dump  #指定備份會話標籤
session label entered: "root_dump"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/
xfsdump: dump date: Mon Jul 22 05:24:53 2019
xfsdump: session id: 32b2d15e-827e-4707-9017-3c03eb7cc676
xfsdump: session label: "root_dump"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 1435976384 bytes
xfsdump: /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> root  #指定設備標籤,就是對要備份的設備做一個描述
media label entered: "root"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 1370257592 bytes
xfsdump: dump size (non-dir files) : 1333849032 bytes
xfsdump: dump complete: 57 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /data/root.dump OK (success)
xfsdump: Dump Status: SUCCESS
[root@localhost ~]# ll -h /data/root.dump
-rw-r--r-- 1 root root 1.3G Jul 22 05:25 /data/root.dump  #備份的大小與df -h看到的"/"大小差不多

1.4 重開機使用安裝光碟進入rescue模式

1.4.1 進入安裝光碟後進入Troubleshooting

1.4.2 再進入Rescue a CentOS system

1.4.3 出現以下畫面按ENTER鍵

1.4.4 選擇3不要掛載任何系統目錄進入Shell模式

1.5 输入lvm命令,进入lvm界面,依次输入pvscan、vgscan、lvscan三个命令扫描pv、vg、lv相关信息。然后输入lvchange -ay /dev/centos/root(根目錄分区掛載的LVM名称),此命令是激活根目錄分区所在的逻辑卷,输入 quit或exit返回到bash shell界面

提示:提示:lvchange --help可以看幫助。-ay的a是激活的意思,y是yes,    如果不要激活則使用-an。

1.6 縮小根目錄邏輯卷

1.7 執行pvs、vgs、lvs可以發現空了7G容量出來了,/dev/centos/root也變成10G,此時還沒完成,因為縮小根目錄後系統有可能已遭受破壞

1.8 將/dev/centos/root進行xfs格式化,格式化完後掛載至某一目錄

1.9 掛載/dev/sdb1至某一目錄以將剛才的根目錄備份還原至/dev/centos/root

1.10 進行根目錄還原

1.11 輸入exit重啟系統,輸入df -h即可看到根目錄已縮小了

2. 擴容根目錄

2.1 我們要將根目錄擴充

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M   14M  473M   3% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  1.8G   16G  11% /
/dev/sda1               1014M  137M  878M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0

2.2 為系統增加一顆硬碟/dev/sdb

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000ceb3f

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

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors  #增加/dev/sdb硬碟
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: 0xb4e934d0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM  #將/dev/sdb硬碟分割成/dev/sdb1,LVM格式

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

2.3 增加PV

2.3.1 執行pvs指令,看到目前只有一個PV

[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <19.00g    0

2.3.2 執行pvcreate /dev/sdb1,增加第二個PV

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <19.00g      0
  /dev/sdb1         lvm2 ---  <20.00g <20.00g  #第二個PV,目前不屬於任何VG

2.4 擴充VG

2.4.1 執行vgs指令,看到目前只有一個VG

[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0

2.4.2 執行vgextend centos /dev/sdb1,擴充現有的VG

[root@localhost ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 38.99g <20.00g  #VG擴充了,有20G還沒使用

2.5 擴充LV

2.5.1 執行lvs指令,看到目前有root、swap兩個LV

[root@localhost ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g
  swap centos -wi-ao----   2.00g

2.5.2 執行lvextend -l +100%FREE /dev/centos/root,將剩餘的VG空間擴充到根目錄

[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 36.99 GiB (9470 extents).
  Logical volume centos/root successfully resized.
[root@localhost ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 36.99g  #已擴充
  swap centos -wi-ao----  2.00g

2.6 執行df -Th,可以看到根目錄並沒有長大,需再執行xfs_growfs /dev/centos/root才會對擴充的LV進行xfs格式大小調整

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  475M     0  475M   0% /dev
tmpfs                   tmpfs     487M     0  487M   0% /dev/shm
tmpfs                   tmpfs     487M  7.7M  479M   2% /run
tmpfs                   tmpfs     487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  1.7G   16G  10% /  #雖然LV擴充了,但實際上"/"還沒長大
/dev/sda1               xfs      1014M  137M  878M  14% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0
[root@localhost ~]# xfs_growfs /dev/centos/root  #使用xfs_growfs指令對擴充的LV進行xfs格式大小調整
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 9697280
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  475M     0  475M   0% /dev
tmpfs                   tmpfs     487M     0  487M   0% /dev/shm
tmpfs                   tmpfs     487M  7.7M  479M   2% /run
tmpfs                   tmpfs     487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        37G  1.7G   36G   5% /  #擴充成功
/dev/sda1               xfs      1014M  137M  878M  14% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0