Linux系統與服務構建(一)
阿新 • • 發佈:2020-07-24
LVM邏輯卷的使用
思路整理:
- 新增硬碟
- 將新增的硬碟進行分割槽 fdisk
- 建立物理卷 pvcreat
- 將建立的物理卷新增到卷組 vgcreat
- 建立邏輯卷 lvcreate
- 掃描建立的邏輯卷 lvscan
- 格式化邏輯卷 mkfs.ext4
- 擴容邏輯卷時要先識別 resize2fs
1.在VMware Workstation中新增硬碟
用命令檢視磁碟
[root@server ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 50G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 2G 0 part [SWAP] └─sda3 8:3 0 47.5G 0 part / sdb 8:16 0 20G 0 disk sr0 11:0 1 4G 0 rom
2.LVM邏輯卷的使用
建立物理卷
在建立物理卷之前,需要對磁碟進行分割槽,首先使用fdisk命令對sdb進行分割槽操作分出兩個大小為5GB的分割槽,
[root@server ~]# 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 0x394e7c9d. Command (m for help): p Disk /dev/sdb: 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: 0x394e7c9d Device Boot Start End Blocks Id System Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G Partition 1 of type Linux and of size 5 GiB is set Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G Partition 2 of type Linux and of size 5 GiB is set Command (m for help): p Disk /dev/sdb: 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: 0x394e7c9d Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
命令幫助
Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)
檢視分割槽
[root@server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 47.5G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
└─sdb2 8:18 0 5G 0 part
sr0 11:0 1 4G 0 rom
建立物理卷
[root@server opt]# pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
檢視物理卷的簡單資訊
[root@server opt]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdb2 lvm2 --- 5.00g 5.00g
檢視物理卷的詳細資訊
[root@server opt]# pvdisplay
"/dev/sdb2" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID pxvHEu-8yek-95ue-f1cw-Te6t-SrI0-PkqGVA
"/dev/sdb1" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QoT1p9-wLlk-XU6b-xVV2-PSHe-61Ch-bFpYbP
如果沒有pvcreate命令
檢視pvcreate在哪個包裡
[root@server opt]# yum provides */pvcreate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
1:bash-completion-2.1-6.el7.noarch : Programmable completion for Bash
Repo : centos
Matched from:
Filename : /usr/share/bash-completion/completions/pvcreate
7:lvm2-2.02.130-5.el7.x86_64 : Userland logical volume management tools
Repo : centos
Matched from:
Filename : /usr/sbin/pvcreate
7:lvm2-2.02.130-5.el7.x86_64 : Userland logical volume management tools
Repo : @centos
Matched from:
Filename : /usr/sbin/pvcreate
安裝包含pvcreate命令的包
[root@server opt]# yum -y install lvm2
建立卷組
使用剛剛建立好的物理卷建立名為myvg的卷組
[root@server opt]# vgcreate myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
檢視卷組簡單資訊
[root@server opt]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 9.99g 9.99g
檢視卷組詳細資訊
[root@server opt]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 9.99 GiB
PE Size 4.00 MiB
Total PE 2558
Alloc PE / Size 0 / 0
Free PE / Size 2558 / 9.99 GiB
VG UUID gxqcgy-RGOk-RbjJ-tGLT-tHro-VyXp-QzeUPJ
PE:當多個邏輯卷組合成一個卷組後,LVM會在所有的物理捲上做類似格式化的工作,將每個物理卷切成一個一個的空間,這一個空間就叫做PE,他的預設大小是4MB
由於受到核心限制的原因,一個邏輯卷最多能包含65536個PE,如果需要邏輯卷有更大的容量就需要指定更大的PE
刪除卷組,並且重新建立卷組,指定PE大小為16MB:
[root@server opt]# vgremove myvg
Volume group "myvg" successfully removed
[root@server opt]# vgcreate -s 16m myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
[root@server opt]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 9.97 GiB
PE Size 16.00 MiB
Total PE 638
Alloc PE / Size 0 / 0
Free PE / Size 638 / 9.97 GiB
VG UUID VcaHP1-tXcZ-EaTw-hm72-Bdxu-DLme-s0GE76
在/dev/sdb上在分一個分割槽,並把分割槽新增到myvg中
M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 47.5G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
├─sdb2 8:18 0 5G 0 part
└─sdb3 8:19 0 5G 0 part
sr0 11:0 1 4G 0 rom /opt/centos
[root@server opt]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created
[root@server opt]# vgextend myvg /dev/sdb3
Volume group "myvg" successfully extended
[root@server opt]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 3 0 0 wz--n- 14.95g 14.95g
[root@server opt]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
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 14.95 GiB
PE Size 16.00 MiB
Total PE 957
Alloc PE / Size 0 / 0
Free PE / Size 957 / 14.95 GiB
VG UUID VcaHP1-tXcZ-EaTw-hm72-Bdxu-DLme-s0GE76
建立邏輯卷
建立邏輯卷,名稱為mylv,大小為5GB
[root@server opt]# lvcreate -L +5G -n mylv myvg
Logical volume "mylv" created.
[root@server opt]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 5.00g
掃描邏輯卷
[root@server opt]# lvscan
ACTIVE '/dev/myvg/mylv' [5.00 GiB] inherit
使用ext4檔案系統格式化邏輯卷mylv
[root@server opt]# mkfs.ext4 /dev/mapper/myvg-mylv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
將邏輯卷掛載到/mnt下,並驗證
[root@server opt]# mount /dev/mapper/myvg-mylv /mnt/
[root@server opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 48G 892M 47G 2% /
devtmpfs 984M 0 984M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.5M 985M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 497M 103M 395M 21% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /opt/centos
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt
邏輯卷擴容
[root@server opt]# lvcreate -L +1G /dev/mapper/myvg-mylv
Volume group name expected (no slash)
Run `lvcreate --help' for more information.
[root@server opt]# lvextend -L +1G /dev/mapper/myvg-mylv
Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
Logical volume mylv successfully resized.
[root@server opt]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-ao---- 6.00g
[root@server opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 48G 892M 47G 2% /
devtmpfs 984M 0 984M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.5M 985M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 497M 103M 395M 21% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /opt/centos
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt
[root@server opt]# resize2fs /dev/mapper/myvg-mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/myvg-mylv is now 1572864 blocks long.
[root@server opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 48G 892M 47G 2% /
devtmpfs 984M 0 984M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.5M 985M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 497M 103M 395M 21% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /opt/centos
/dev/mapper/myvg-mylv 5.8G 20M 5.5G 1% /mnt