LVM的工作原理
- LVM( Logical Volume Manager)邏輯卷管理,是在磁盤分區和文件系統之間添加的一個邏輯層,來為文件系統屏蔽下層磁盤分區布局,提供一個抽象的盤卷,在盤卷上建立文件系統。管理員利用LVM可以在磁盤不用重新分區的情況下動態調整文件系統的大小,並且利用LVM管理的文件系統可以跨越磁盤,當服務器添加了新的磁盤后,管理員不必將原有的文件移動到新的磁盤上,而是通過LVM可以直接擴展文件系統跨越磁盤。
- 它就是通過將底層的物理硬盤封裝起來,然后以邏輯卷的方式呈現給上層應用。在LVM中,其通過對底層的硬盤進行封裝,當我們對底層的物理硬盤進行操作時,其不再是針對於分區進行操作,而是通過一個叫做邏輯卷的東西來對其進行底層的磁盤管理操作。
LVM常用的術語
- 物理存儲介質(The physical media):LVM存儲介質可以是磁盤分區,整個磁盤,RAID陣列或SAN磁盤,設備必須初始化為LVM物理卷,才能與LVM結合使用
- 物理卷PV(physical volume) :物理卷就是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數,創建物理卷它可以用硬盤分區,也可以用硬盤本身;
- 卷組VG(Volume Group) :一個LVM卷組由一個或多個物理卷組成
- 邏輯卷LV(logical volume) :LV建立在VG之上,可以在LV之上建立文件系統
- PE(physical extents) :PV物理卷中可以分配的最小存儲單元,PE的大小是可以指定的,默認為4MB
- LE(logical extent) : LV邏輯卷中可以分配的最小存儲單元,在同一個卷組中,LE的大小和PE是相同的,並且一一對應
最小存儲單位總結:
- 名稱 最小存儲單位 示例
- 硬盤 扇區(512字節)
- 文件系統 block(1K或4K ) # mkfs.ext4 -b 2048 /dev/sdb1 ,最大支持到4096
- raid chunk (512K) #mdadm -C -v /dev/md5 -l 5 -n 3 -c 512 -x 1 /dev/sde{1,2,3,5}
- LVM PE (4M) # vgcreate -s 4M vg1 /dev/sdb{1,2}
LVM主要元素構成:
LVM優點
- 使用卷組,使多個硬盤空間看起來像是一個大的硬盤
- 使用邏輯卷,可以跨多個硬盤空間的分區 sdb1 sdb2 sdc1 sdd2 sdf
- 在使用邏輯卷時,它可以在空間不足時動態調整它的大小
- 在調整邏輯卷大小時,不需要考慮邏輯卷在硬盤上的位置,不用擔心沒有可用的連續空間
- 可以在線對LV,VG 進行創建,刪除,調整大小等操作。LVM上的文件系統也需要重新調整大小。
- 允許創建快照,可以用來保存文件系統的備份。
RAID+LVM一起用:LVM是軟件的卷管理方式,而RAID是磁盤管理的方法。對於重要的數據,使用RAID用來保護物理的磁盤不會因為故障而中斷業務,再用LVM用來實現對卷的良性的管理,更好的利用磁盤資源。
創建LVM的基本步驟:
- (1) 物理磁盤被格式化為PV,(空間被划分為一個個的PE) #PV包含PE
- (2) 不同的PV加入到同一個VG中,(不同PV的PE全部進入到了VG的PE池內) #VG包含PV
- (3) 在VG中創建LV邏輯卷,基於PE創建,(組成LV的PE可能來自不同的物理磁盤) #LV基於PE創建
- (4) LV直接可以格式化后掛載使用 #格式化掛載使用
- (5) LV的擴充縮減實際上就是增加或減少組成該LV的PE數量,其過程不會丟失原始數據
lvm常用的命令
下面的操作會用的一些查看命令:
准備環境
[root@wencheng ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
[root@wencheng ~]# rpm -qa | grep lvm lvm2-libs-2.02.187-6.el7_9.4.x86_64 lvm2-2.02.187-6.el7_9.4.x86_64
創建並使用LVM邏輯卷
VMware Workstation添加硬盤
- 在"我的計算機"中選擇該主機--右鍵--設置--添加--硬盤--下一步--下一步--下一步--完成 添加硬盤步驟。
- 命令:echo "- - -" > /sys/class/scsi_host/host0/scan
- 1.新增添磁盤,還不能直接顯示出來,要想出現要對磁盤進行掃描(實際生產環境中不需要,都是熱插拔技術):
- 2.假如 fdisk -l 還是未發現新硬盤,則將上面命令中的host0,替換為host1,host2,....看看;
- 3.我們再查看系統日志/var/log/messages,發現對SCSI設備進行了一次重新掃描,用fdisk -l也看到了新增加的磁盤了。
查看新增的硬盤。
創建分區並設置LVM(8e)類型。
[root@wencheng ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x81cc782c. Command (m for help): n //新建 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p //p為主分區,e為擴展分區 Partition number (1-4, default 1): 1 //分區號,回車(默認) First sector (2048-20971519, default 2048): //回車(默認) Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): //回車(默認) Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): p //顯示信息 Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x81cc782c Device Boot Start End Blocks Id System /dev/sdb1 2048 20971519 10484736 83 Linux Command (m for help): l //列出所有類型 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Command (m for help): t //修改類型 Selected partition 1 Hex code (type L to list all codes): 8e //改為LVM類型 Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): p //顯示信息 Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x81cc782c Device Boot Start End Blocks Id System /dev/sdb1 2048 20971519 10484736 8e Linux LVM //類型修改成功 Command (m for help): w //保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
注: 其他新增硬盤分區類似,不再贅述,如下查看最后分區結果。
[root@wencheng ~]# ls -l /dev/sd*1
brw-rw----. 1 root disk 8, 1 Mar 25 10:54 /dev/sda1 //系統分區
brw-rw----. 1 root disk 8, 17 Apr 19 16:48 /dev/sdb1
brw-rw----. 1 root disk 8, 33 Apr 19 16:53 /dev/sdc1
brw-rw----. 1 root disk 8, 49 Apr 19 16:53 /dev/sdd1
brw-rw----. 1 root disk 8, 65 Apr 19 16:53 /dev/sde1
[root@wencheng ~]#
[root@localhost ~]# fdisk -l | grep -e "/dev/sd[b-e]1"
/dev/sdb1 2048 41943039 20970496 8e Linux LVM
/dev/sdc1 2048 41943039 20970496 8e Linux LVM
/dev/sdd1 2048 41943039 20970496 8e Linux LVM
/dev/sde1 2048 20971519 10484736 83 Linux //備用磁盤
創建pv,並查看結果。
[root@wencheng ~]# pvcreate /dev/sd[b,c,d]1 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created. Physical volume "/dev/sdd1" successfully created. [root@wencheng ~]# [root@wencheng ~]# pvdisplay /dev/sd[b,c,d]1 "/dev/sdb1" is a new physical volume of "<10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID kZxjyM-Ora3-io5L-J14w-ljlM-TArl-gYQhqm "/dev/sdd1" is a new physical volume of "<10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdd1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID NN1HSf-JQAA-PiMm-MEmm-WocM-9QrD-zcStUf "/dev/sdc1" is a new physical volume of "<10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID I92jzU-WN1f-zcrW-Mzm7-HuoZ-3hBP-BjJFkG
創建名為'vg01'的卷組,使用/dev/sdb1, /dev/sdc1和/dev/sdb1創建,並查看結果。
[root@wencheng ~]# vgcreate vg01 /dev/sd[b,c,d]1 //指定PE大小用的參數:-s 16M, 如果存儲的數據都是大文件,那么PE盡量調大,讀取速度快 Volume group "vg01" successfully created [root@wencheng ~]# [root@localhost ~]# vgdisplay --- Volume group --- VG Name vg01 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size <59.99 GiB ///dev/sd[b,c]1硬盤總和容量 PE Size 4.00 MiB //默認4MB Total PE 15357 Alloc PE / Size 0 / 0 Free PE / Size 15357 / <59.99 GiB VG UUID 0HRkjd-8ccf-Alpe-vhfJ-eOLi-vtnX-0Vl7sR
創建一個名為'lv01'、大小為30G的邏輯卷。
[root@wencheng ~]# lvcreate -n lv01 -L 30G vg01 //-L指定lv大小的SIZE(M,G) (-l:小l 指定LE的數量) Logical volume "lv01" created. [root@wencheng ~]# [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg01/lv01 LV Name lv01 VG Name vg01 LV UUID Iwvoxi-Ekrb-8nQ7-QfDt-1jIx-FiXy-srYbbZ LV Write Access read/write LV Creation host, time localhost.localdomain, 2021-04-20 11:17:57 +0800 LV Status available # open 0 LV Size 30.00 GiB Current LE 4 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
創建文件系統,並掛載。
[root@wencheng ~]# mkdir /mnt/lv01 [root@wencheng ~]# ls -l /dev/vg01/ total 0 lrwxrwxrwx. 1 root root 7 Apr 20 11:17 lv01 -> ../dm-0 [root@localhost ~]# mkfs.xfs /dev/vg01/lv01 meta-data=/dev/vg01/lv01 isize=512 agcount=1, agsize=4096 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=4096, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@wencheng ~]# mount /dev/vg01/lv01 /mnt/lv01/ [root@wencheng ~]# echo "/dev/vg01/lv01 /mnt/lv01 xfs defaults 0 0" >> /etc/fstab //添加開機自動掛載 [root@wencheng ~]# df -Th /mnt/lv01/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 31G 61M 30G 1% /mnt/lv01
動態調整邏輯卷大小的功能是LVM最有用的功能。
擴展LV首先,確定一下是否有可用的擴容空間,因為空間是從VG里面創建的,並且LV不能跨VG擴容。
- 1.如果VG無剩余空間,則添加新硬盤,這里不再贅述(添加硬盤,分區LVM),再如下操作。
[root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 3 1 0 wz--n- <30.99g 1012.00m [root@wencheng ~]# pvcreate /dev/sde1 Physical volume "/dev/sde1" successfully created. [[root@wencheng ~]# vgextend vg01 /dev/sde1 //新增硬盤/dev/sde1為10G Volume group "vg01" successfully extended [root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 4 1 0 wz--n- 30.99g 10.98g
- 2.如果VG有預留空間,則如下操作。
[root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 3 1 0 wz--n- <59.99g 29.97g //空閑約29G
擴容邏輯卷,用的如下命令。
[root@wencheng ~]# lvextend -L 59G /dev/vg01/lv01 Size of logical volume vg01/lv01 changed from <30.02 GiB (7684 extents) to 59.00 GiB (15104 extents). Logical volume vg01/lv01 successfully resized. [root@wencheng ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv01 vg01 -wi-ao---- 59.00g //LV已擴容成功 [root@wencheng ~]# lvdisplay --- Logical volume --- LV Path /dev/vg01/lv01 LV Name lv01 VG Name vg01 LV UUID Iwvoxi-Ekrb-8nQ7-QfDt-1jIx-FiXy-srYbbZ LV Write Access read/write LV Creation host, time localhost.localdomain, 2021-04-20 11:17:57 +0800 LV Status available # open 1 LV Size 59.00 GiB Current LE 15104 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 說明: 在指定大小的時候,擴容30m和擴容到30m是不一樣的寫法 擴容59G ====> -L +59G 擴容到59G =====> -L 59G [root@wencheng ~]# df -Th /mnt/lv01/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 31G 61M 30G 1% /mnt/lv01 //但仍然看不到擴展的容量 注:可以看到LV雖然擴展了,但是文件系統大小還是原來的,下面開始擴容文件系統 ext4文件系統擴容使用命令語法: resize2fs 邏輯卷名稱 xfs文件系統擴容使用命令語法: xfs_growfs 掛載點 resize2fs和xfs_growfs 兩者的區別是傳遞的參數不一樣的,xfs_growfs是采用的掛載點;resize2fs是邏輯卷名稱,而且resize2fs命令不能對xfs類型文件系統使用。 [root@wencheng ~]# xfs_growfs /mnt/lv01/ meta-data=/dev/mapper/vg01-lv01 isize=512 agcount=1921, agsize=4096 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=7868416, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 7868416 to 15466496 [root@wencheng ~]# [root@wencheng ~]# df -Th /mnt/lv01/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 59G 119M 59G 1% /mnt/lv01 //擴展完成
注:先擴lv邏輯卷,再擴文件系統。 掌握另一方法:使用lvextend命令鍾加參數-r,它是支持多種不同的文件系統,比如ext3,ext4,xfs,… [root@wencheng ~]# lvextend -L 59G -r /dev/vg01/lv01 #直接擴容到59G空間,一步到位,不用再擴文件系統了
縮減LVM卷,注意事項
- 減少邏輯卷的大小值若小於儲存的數據大小,存儲在后面的數據會丟失,縮減 LVM 之前,建議先做一個備份。
- 在 GFS2 或者 XFS 文件系統上不支持在線縮小;btrfs文件系統支持在線縮小。
萬不得已需要縮減xfs文件系統的lvm分區空間時候,一定要注意順序。
- 第一步,確認現有環境,並目標數據備份;以/mnt/lv01為例。
[root@wencheng ~]# ls -l /mnt/lv01* /mnt/lv01: total 1060 -rw-r--r--. 1 root root 16 Apr 21 10:44 adjtime -rw-r--r--. 1 root root 1518 Apr 21 10:44 aliases -rw-r--r--. 1 root root 12288 Apr 21 10:44 aliases.db -rw-------. 1 root root 541 Apr 21 10:44 anacrontab -rw-r--r--. 1 root root 55 Apr 21 10:44 asound.conf -rw-r--r--. 1 root root 2853 Apr 21 10:44 bashrc -rw-r--r--. 1 root root 38 Apr 21 10:44 centos-release [root@wencheng ~]# df -Th /mnt/lv01 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 50G 170M 50G 1% /mnt/lv01 [root@wencheng ~]# rpm -qa | grep xfsdump xfsdump-3.1.7-1.el7.x86_64 [root@wencheng ~]# xfsdump -f /mnt/lv01.xfsdump /mnt/lv01 //xfsdump -f "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) -> lv01 session label entered: "lv01" --------------------------------- end dialog --------------------------------- xfsdump: level 0 dump of wencheng:/mnt/lv01 xfsdump: dump date: Wed Apr 21 10:59:05 2021 xfsdump: session id: 619c7222-752a-44af-8b7f-411d33fbef15 xfsdump: session label: "lv01" 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: 143172224 bytes xfsdump: /var/lib/xfsdump/inventory created ============================= media label dialog ============================= please enter label for media in drive 0 (timeout in 300 sec) -> lv01 media label entered: "lv01" --------------------------------- 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 140934528 bytes xfsdump: dump size (non-dir files) : 139858712 bytes xfsdump: dump complete: 32 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /mnt/lv01.xfsdump OK (success) xfsdump: Dump Status: SUCCESS [root@wencheng ~]# ls -l /mnt/lv01* -rw-r--r--. 1 root root 140934528 Apr 21 10:59 /mnt/lv01.xfsdump //生成備份 /mnt/lv01: total 1060 -rw-r--r--. 1 root root 16 Apr 21 10:44 adjtime -rw-r--r--. 1 root root 1518 Apr 21 10:44 aliases -rw-r--r--. 1 root root 12288 Apr 21 10:44 aliases.db -rw-------. 1 root root 541 Apr 21 10:44 anacrontab -rw-r--r--. 1 root root 55 Apr 21 10:44 asound.conf -rw-r--r--. 1 root root 2853 Apr 21 10:44 bashrc -rw-r--r--. 1 root root 38 Apr 21 10:44 centos-release
- 第二步,卸載掛載的目錄。
[root@wencheng ~]# umount /mnt/lv01/
- 第三步,把你原來的邏輯卷變成5G是擴大還是縮小它自己會判斷,並格式化以更新到最新大小。
[root@wencheng ~]# lvreduce -L 5G /dev/vg01/lv01 WARNING: Reducing active logical volume to 5.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg01/lv01? [y/n]: y Size of logical volume vg01/lv01 changed from 50.00 GiB (12800 extents) to 5.00 GiB (1280 extents). Logical volume vg01/lv01 successfully resized. [root@wencheng ~]# mkfs.xfs -f /dev/vg01/lv01 //格式化以更新到最新大小(清除所有數據) meta-data=/dev/vg01/lv01 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 [root@wencheng ~]# lvs //lv縮小成功 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv01 vg01 -wi-a----- 5.00g [root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 4 1 0 wz--n- 69.98g 64.98g
- 第四步,掛載出來空分區 。
[root@wencheng ~]# mount /dev/vg01/lv01 /mnt/lv01 [root@wencheng ~]# df -Th /mnt/lv01 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 5.0G 33M 5.0G 1% /mnt/lv01 [root@wencheng ~]# ls -l /mnt/lv01 total 0
- 第五步,把dump的數據恢復。
[root@wencheng ~]# xfsrestore -f /mnt/lv01.xfsdump /mnt/lv01 xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control xfsrestore: searching media for dump xfsrestore: examining media file 0 xfsrestore: dump description: xfsrestore: hostname: wencheng xfsrestore: mount point: /mnt/lv01 xfsrestore: volume: /dev/mapper/vg01-lv01 xfsrestore: session time: Wed Apr 21 10:59:05 2021 xfsrestore: level: 0 xfsrestore: session label: "lv01" xfsrestore: media label: "lv01" xfsrestore: file system id: ded6656d-b154-4c48-bda8-2935c5fa8dd8 xfsrestore: session id: 619c7222-752a-44af-8b7f-411d33fbef15 xfsrestore: media id: e4f6c078-14b3-43c3-9ff7-5c847977ca32 xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 533 directories and 3574 entries processed xfsrestore: directory post-processing xfsrestore: restoring non-directory files xfsrestore: restore complete: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /mnt/lv01.xfsdump OK (success) xfsrestore: Restore Status: SUCCESS [root@wencheng ~]# [root@wencheng ~]# ls -l /mnt/lv01 //查看數據是否恢復完成 total 1060 -rw-r--r--. 1 root root 16 Apr 21 10:44 adjtime -rw-r--r--. 1 root root 1518 Apr 21 10:44 aliases -rw-r--r--. 1 root root 12288 Apr 21 10:44 aliases.db -rw-------. 1 root root 541 Apr 21 10:44 anacrontab -rw-r--r--. 1 root root 55 Apr 21 10:44 asound.conf -rw-r--r--. 1 root root 2853 Apr 21 10:44 bashrc -rw-r--r--. 1 root root 38 Apr 21 10:44 centos-release
LVM刪除操作
創建LVM流程:pvcreate創建pv -> vgcreate創建卷組 -> lvcreate創建邏輯卷 -> mkfs.xfs lv 格式化-> mount掛載
刪除LVM流程:umount卸載 -> lvremove lv移出卷組中所有邏輯卷-> vgremove vg移出卷組-> pvremove 移出pv
- 查看現有lvm狀態。
[root@wencheng ~]# df -Th /mnt/lv01 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 xfs 5.0G 170M 4.9G 4% /mnt/lv01 [root@wencheng ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv01 vg01 -wi-ao---- 5.00g [root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 4 1 0 wz--n- 69.98g 64.98g [root@wencheng ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 vg01 lvm2 a-- <20.00g <15.00g /dev/sdc1 vg01 lvm2 a-- <20.00g <20.00g /dev/sdd1 vg01 lvm2 a-- <20.00g <20.00g /dev/sde1 vg01 lvm2 a-- <10.00g <10.00g
- 卸載lvm掛載目錄。
[root@wencheng ~]# umount /mnt/lv01 [root@wencheng ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 30G 1.2G 29G 4% / devtmpfs devtmpfs 981M 0 981M 0% /dev tmpfs tmpfs 992M 0 992M 0% /dev/shm tmpfs tmpfs 992M 9.5M 982M 1% /run tmpfs tmpfs 992M 0 992M 0% /sys/fs/cgroup /dev/sda1 xfs 297M 107M 191M 36% /boot tmpfs tmpfs 199M 0 199M 0% /run/user/0
- 刪除邏輯卷lv。
[root@wencheng ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv01 vg01 -wi-a----- 5.00g [root@wencheng ~]# [root@wencheng ~]# lvremove /dev/vg01/lv01 Do you really want to remove active logical volume vg01/lv01? [y/n]: y Logical volume "lv01" successfully removed [root@wencheng ~]# lvs
- 刪除卷組vg。
[root@wencheng ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 4 0 0 wz--n- 69.98g 69.98g [root@wencheng ~]# [root@wencheng ~]# vgremove vg01 Volume group "vg01" successfully removed [root@wencheng ~]# [root@wencheng ~]# vgs
- 刪除物理機pv。
[root@wencheng ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- <20.00g <20.00g /dev/sdc1 lvm2 --- <20.00g <20.00g /dev/sdd1 lvm2 --- <20.00g <20.00g /dev/sde1 lvm2 --- <10.00g <10.00g [root@wencheng ~]# [root@wencheng ~]# pvremove /dev/sde1 Labels on physical volume "/dev/sde1" successfully wiped. [root@wencheng ~]# pvremove /dev/sdd1 Labels on physical volume "/dev/sdd1" successfully wiped. [root@wencheng ~]# [root@wencheng ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- <20.00g <20.00g /dev/sdc1 lvm2 --- <20.00g <20.00g
- 編輯/etc/fstab,刪除對應自動掛載信息 。
UUID=57c4afb5-1953-4695-b173-965f1b12500f / xfs defaults 0 0 UUID=a04da5a6-ddd1-41b8-a9d6-ee5f6b62596e /boot xfs defaults 0 0 UUID=77e1f34d-8161-4b9c-ba1f-240898a695ef swap swap defaults 0 0 #/dev/vg01/lv01 /mnt/lv01 xfs defaults 0 0
- 格式化磁盤。
[root@wencheng ~]# mkfs.xfs /dev/sde1 meta-data=/dev/sde1 isize=512 agcount=4, agsize=655296 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=2621184, 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 [root@wencheng ~]# mkfs.xfs /dev/sdd1 meta-data=/dev/sdd1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5242624, 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
附:其他LVM 邏輯卷管理器使用及配置詳情見官方文檔:https://access.redhat.com/documentation