文章目錄
- 前言
- 一、創建一個分區
- 二、創建PV
- 三、創建VG
- 四、創建LV
- 五、格式化并掛載目錄
前言
在數據庫管理中,常有比較頭疼的問題,就是一段時間發展后我的磁盤空間不夠了,想要擴容原有的目錄很是頭疼,那么LVM管理的優勢就體現出來了:邏輯卷的大小可以動態調整,而且不會丟失現在的數據,從而達到一個不動舊數據的情況下進行擴容。
一、創建一個分區
使用fdisk分區工具創建LVM分區,區別在與分區類型選擇為8e
[root@XXJSB-OEM-01 /]# fdisk -lDisk /dev/sda: 536.9 GB, 536870912000 bytes, 1048576000 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: 0x000adc79Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 1048575999 523238400 8e Linux LVMDisk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel-root: 527.3 GB, 527337259008 bytes, 1029955584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel-swap: 8455 MB, 8455716864 bytes, 16515072 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[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# 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 0xc7d7fbc4.Command (m for help): m
Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)Command (m for help):
Command (m for help): n
Partition type:p primary (0 primary, 0 extended, 4 free)e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-2147483647, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2147483647, default 2147483647):
Using default value 2147483647
Partition 1 of type Linux and of size 1024 GiB is setCommand (m for help):
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L0 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
Hex code (type L to list all codes):
#選擇8e:一定要指定分區的格式為8e,這是LVM的分區格式
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command (m for help):
Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 499G 0 part ├─rhel-root 253:0 0 491.1G 0 lvm /└─rhel-swap 253:1 0 7.9G 0 lvm [SWAP]
sdb 8:16 0 1T 0 disk
└─sdb1 8:17 0 1024G 0 part
sr0 11:0 1 1024M 0 rom
[root@XXJSB-OEM-01 /]#
二、創建PV
[root@XXJSB-OEM-01 /]# pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created.
[root@XXJSB-OEM-01 /]#
三、創建VG
[root@XXJSB-OEM-01 /]# vgdisplay--- Volume group ---VG Name rhelSystem ID Format lvm2Metadata Areas 2Metadata Sequence No 7VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 2Max PV 0Cur PV 2Act PV 2VG Size <1.49 TiBPE Size 4.00 MiBTotal PE 389886Alloc PE / Size 127743 / <499.00 GiBFree PE / Size 262143 / <1024.00 GiBVG UUID 7L2R89-R1ep-1It5-pg4h-Xx4i-DHGR-fuMunL[root@XXJSB-OEM-01 /]#
#新增已經存在的VG
[root@XXJSB-OEM-01 /]# vgextend rhel /dev/sdb1Volume group "rhel" successfully extended
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# vgsVG #PV #LV #SN Attr VSize VFree rhel 2 2 0 wz--n- <1.49t <1024.00g
四、創建LV
#新建256G
[root@XXJSB-OEM-01 /]# lvcreate -L 256G -n u01 rhelRounding up size to full physical extent 256.00 GiBLogical volume "u01" created.
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# vgsVG #PV #LV #SN Attr VSize VFree rhel 2 3 0 wz--n- <1.49t <768.00g#擴容668G
[root@XXJSB-OEM-01 /]# lvextend -L +668G /dev/rhel/u01Size of logical volume rhel/u01 changed from 256.00 GiB (65536 extents) to 924.00 GiB (236544 extents).Logical volume rhel/u01 successfully resized.
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# vgsVG #PV #LV #SN Attr VSize VFree rhel 2 3 0 wz--n- <1.49t <100.00g
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# lvdisplay--- Logical volume ---LV Path /dev/rhel/swapLV Name swapVG Name rhelLV UUID hR1pPu-I1hw-cjql-tPdk-tWDF-58ok-XVk4RVLV Write Access read/writeLV Creation host, time localhost, 2021-05-21 13:14:00 +0800LV Status available# open 2LV Size <7.88 GiBCurrent LE 2016Segments 1Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:1--- Logical volume ---LV Path /dev/rhel/rootLV Name rootVG Name rhelLV UUID PR5wZY-1aUU-jVKr-4Dpe-gi0U-B0Cp-nnzMGhLV Write Access read/writeLV Creation host, time localhost, 2021-05-21 13:14:02 +0800LV Status available# open 1LV Size 491.12 GiBCurrent LE 125727Segments 2Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:0--- Logical volume ---LV Path /dev/rhel/u01LV Name u01VG Name rhelLV UUID O9vP6V-28jH-WbpF-sMLn-1SQn-EuqE-iUSY13LV Write Access read/writeLV Creation host, time XXJSB-OEM-01, 2024-05-10 17:44:05 +0800LV Status available# open 0LV Size 924.00 GiBCurrent LE 236544Segments 1Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:2[root@XXJSB-OEM-01 /]#
五、格式化并掛載目錄
[root@XXJSB-OEM-01 /]# mkfs -t xfs /dev/rhel/u01
meta-data=/dev/rhel/u01 isize=512 agcount=4, agsize=60555264 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=242221056, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=118272, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# mount /dev/rhel/u01 /u01
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 12K 32G 1% /dev/shm
tmpfs 32G 21M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 492G 6.0G 486G 2% /
/dev/sda1 1014M 183M 832M 19% /boot
tmpfs 6.3G 12K 6.3G 1% /run/user/42
tmpfs 6.3G 0 6.3G 0% /run/user/0
tmpfs 6.3G 0 6.3G 0% /run/user/1000
/dev/mapper/rhel-u01 924G 33M 924G 1% /u01
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# cat /etc/fstab#
# /etc/fstab
# Created by anaconda on Fri May 21 13:14:02 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=7b18c326-0c8d-43b7-ba7b-ca3a73e77a0d /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# vi /etc/fstab
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# cat /etc/fstab #
# /etc/fstab
# Created by anaconda on Fri May 21 13:14:02 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=7b18c326-0c8d-43b7-ba7b-ca3a73e77a0d /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/rhel/u01 /u01 xfs defaults 0 0
配置好了fstab后要進行測試哦!(防止)
[root@XXJSB-OEM-01 /]# umount /u01
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 12K 32G 1% /dev/shm
tmpfs 32G 21M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 492G 6.0G 486G 2% /
/dev/sda1 1014M 183M 832M 19% /boot
tmpfs 6.3G 12K 6.3G 1% /run/user/42
tmpfs 6.3G 0 6.3G 0% /run/user/0
tmpfs 6.3G 0 6.3G 0% /run/user/1000
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# mount -a
[root@XXJSB-OEM-01 /]#
[root@XXJSB-OEM-01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 12K 32G 1% /dev/shm
tmpfs 32G 21M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 492G 6.0G 486G 2% /
/dev/sda1 1014M 183M 832M 19% /boot
tmpfs 6.3G 12K 6.3G 1% /run/user/42
tmpfs 6.3G 0 6.3G 0% /run/user/0
tmpfs 6.3G 0 6.3G 0% /run/user/1000
/dev/mapper/rhel-u01 924G 33M 924G 1% /u01
[root@XXJSB-OEM-01 /]#