linux LVM 常用總結
1 創建 、刪除思路
創建:Linux分區---物理卷---卷組---邏輯卷--文件系統
刪除:卸載文件系統---邏輯卷---卷組----物理卷---Linux分區
2 linux分區
linux中我們通常使用fdisk對磁盤做分區操作,但它只能劃分2T以下的磁盤,而現在大於2T的磁盤很常見。這個時候我們使用parted命令對磁盤做分區操作,以下用於學習parted命令
1)GPT 磁盤和 MBR磁盤
利用parted命令可以對GPT磁盤分區,GPT格式的磁盤相當於原來MBR磁盤中原來保留4個partition table的4*16個字節,只留第一個16個字節,類似於擴展分區,GPT磁盤沒有四個主分區的限制,可以設置多達128個分區
2)操作
--查看sda原先分區
[root@mytest07 ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000cc025
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 100681727 48242688 8e Linux LVM
/dev/sda3 100681728 117187500 8252886+ 8e Linux LVM
/dev/sda4 117187501 126953125 4882812+ f W95 Ext'd (LBA)
[root@mytest07 ~]# parted /dev/sda --對sda進行分區
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found
partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted) p --打印當前分區情況
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 2149MB 2147MB primary xfs boot
2 2149MB 51.5GB 49.4GB primary lvm
3 51.5GB 60.0GB 8451MB primary lvm
4 60.0GB 65.0GB 5000MB extended lba
(parted) rm 4 --刪除分區3、4
(parted) rm 3
(parted) mkpart primary 51500 60000 --創建新分區從 51.5G 到60G
Warning: You requested a partition from 51.5GB to 60.0GB (sectors 100585937..117187500).
The closest location we can manage is 51.5GB to 60.0GB (sectors 100681728..117187500).
Is this still acceptable to you?
Yes/No? y
(parted) p
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 2149MB 2147MB primary xfs boot
2 2149MB 51.5GB 49.4GB primary lvm
3 51.5GB 60.0GB 8451MB primary
(parted) set 3 lvm on --設置分區爲lvm類型
(parted) q
Information: You may need to update /etc/fstab.
[root@mytest07 ~]# partprobe --重新掃描分區
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@mytest07 ~]# fdisk -l --查看分區後
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000cc025
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 100681727 48242688 8e Linux LVM
/dev/sda3 100681728 117187500 8252886+ 8e Linux LVM
3 物理卷的管理
1)創建物理卷:pvcreate
[root@mytest07 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
2)查看當前物理卷信息:pvs或pvscan
[root@mytest07 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 46.00g 4.00m
/dev/sda3 lvm2 --- 7.87g 7.87g
3)查看物理卷詳細信息:pvdiskplay pvname
[root@mytest07 ~]# pvdisplay /dev/sda3
"/dev/sda3" is a new physical volume of "7.87 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 7.87 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID EpUIK3-k9Tj-6laD-fDGI-LGRu-VlAi-MBjq6d
4)刪除物理卷:pvremove
[root@mytest07 ~]# pvremove /dev/sda3
Labels on physical volume "/dev/sda3" successfully wiped.
4卷組的管理
1) 創建卷組
創建格式:vgcreate [-s <8M|16M|...>] vg0 /dev/sdb{1,2,3...} -s:指定擴展塊(PE)的大小,默認爲4MB;(相當與磁盤上的簇,擴展邏輯卷的基本單位。 後面的值可以是8M 16M 32M 64M .....最多65532個擴展塊。vg0:新創建的卷組的名字。/dev/sdb{1,2,3...}:需要的物理卷可以寫多個;
[root@mytest07 ~]# vgcreate -s 16M testvg /dev/sda3
Volume group "testvg" successfully created
2)查看當前系統信息
[root@mytest07 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 3 0 wz--n- 46.00g 4.00m
testvg 1 0 0 wz--n- 7.86g 7.86g
3)查看當前卷組詳細信息
[root@mytest07 ~]# vgdisplay testvg
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 7.86 GiB
PE Size 16.00 MiB
Total PE 503
Alloc PE / Size 0 / 0
Free PE / Size 503 / 7.86 GiB
VG UUID ywd9ae-AVf5-LWl1-WuYM-xk95-KzsS-aVCanx
4)vg擴展
[root@mytest07 ~]# fdisk /dev/sda --此處用fdisk劃分區
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.
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
Selected partition 4
First sector (117187501-209715199, default 117188608):
Using default value 117188608
Last sector, +sectors or +size{K,M,G} (117188608-209715199, default 209715199):
Using default value 209715199
Partition 4 of type Extended and of size 44.1 GiB is set
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000cc025
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 100681727 48242688 8e Linux LVM
/dev/sda3 100681728 117187500 8252886+ 8e Linux LVM
/dev/sda4 117188608 209715199 46263296 5 Extended
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (117190656-209715199, default 117190656):
Using default value 117190656
Last sector, +sectors or +size{K,M,G} (117190656-209715199, default 209715199): +20G
Partition 5 of type Linux and of size 20 GiB is set
Command (m for help): t --改分區類型爲lvm
Partition number (1-5, default 5):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@mytest07 ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000cc025
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 100681727 48242688 8e Linux LVM
/dev/sda3 100681728 117187500 8252886+ 8e Linux LVM
/dev/sda4 117188608 209715199 46263296 5 Extended
/dev/sda5 117190656 159133695 20971520 8e Linux LVM
Disk /dev/mapper/rhel-root: 42.9 GB, 42949672960 bytes, 83886080 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/rhel-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
Disk /dev/mapper/rhel-var: 4294 MB, 4294967296 bytes, 8388608 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@mytest07 ~]# partprobe --重新掃描分區
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@mytest07 ~]# pvcreate /dev/sda5 --創建物理卷
Physical volume "/dev/sda5" successfully created.
[root@mytest07 ~]# vgextend testvg /dev/sda5 --擴展vg
Volume group "testvg" successfully extended
5)vg縮小
[root@mytest07 ~]# vgreduce testvg /dev/sda5
Removed "/dev/sda5" from volume group "testvg"
6)vg刪除
[root@mytest07 ~]# vgremove testvg
Volume group "testvg" successfully removed
5 邏輯卷的管理
1)邏輯卷的創建
格式1:lvcreate -L 512M -n data testvg -L 512M:設置邏輯卷的大小爲512M。 -n data:設置邏輯卷的名字爲data ;表示法:/dev/卷組名/data/testvg:設置是有那個卷組生成的邏輯卷。
[root@mytest07 ~]# lvcreate -L 510M -n date testvg
Rounding up size to full physical extent 512.00 MiB
Logical volume "date" created.
格式2:lvcreate -l 32 -n data testvg -l 32:生成的邏輯卷使用32個擴展塊,邏輯卷大小=32*PE(默認4M)則爲128M -n data:邏輯卷的名字; vg0 :設置是有那個卷組生成的邏輯卷。 [root@mytest07 ~]# lvcreate -l 10 -n data testvg
Logical volume "data" created.
2) 查看邏輯卷:lvs或者lvscan
[root@mytest07 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 40.00g
swap rhel -wi-ao---- 2.00g
var rhel -wi-ao---- 4.00g
data testvg -wi-a----- 160.00m
date testvg -wi-a----- 512.00m
3)查看邏輯卷詳細信息
[root@mytest07 ~]# lvdisplay testvg
--- Logical volume ---
LV Path /dev/testvg/date
LV Name date
VG Name testvg
LV UUID dGmhX5-AYbF-QVo4-iNPF-Br82-eBvJ-h4Asep
LV Write Access read/write
LV Creation host, time mytest07, 2018-04-19 13:47:55 +0800
LV Status available
# open 0
LV Size 512.00 MiB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
4)格式化邏輯卷
[root@mytest07 ~]# mkfs.ext3 /dev/testvg/data
5)增大邏輯卷:lvextend
格式:lvextend -L +160M /dev/testvg/data
需要額外的命令使立即生效:resize2fs
resize2fs /dev/testvg/data
6)減少邏輯卷大小:lvreduce
[root@mytest07 dev]# lvreduce -L 160M /dev/testvg/data
resize2fs /dev/testvg/data
7)刪除邏輯卷:lvremove/卷組名/邏輯卷名
[root@mytest07 dev]# lvremove /dev/testvg/date
6掛載文件系統
[root@mytest07 ~]# mkfs.ext3 /dev/testvg/data
[root@mytest07 dev]# mkdir /data
[root@mytest07 dev]# mount /dev/testvg/data /data
7 lvm快照
1)快照的創建
格式:Lvcreate -l 64 -s -n databackup /dev/testvg/data
-l 64:之前我們已經說過了這裏是 64個擴展塊,總容量=64*單擴展塊大小 -n databackup:生成的快照的名字。 /dev/testvg/data:源邏輯卷名字。
[root@mytest07 dev]# lvcreate -L 160M -s -n databackup /dev/testvg/data
Using default stripesize 64.00 KiB
2)查看
[root@mytest07 dev]# lvscan
ACTIVE '/dev/rhel/root' [40.00 GiB] inherit
ACTIVE '/dev/rhel/var' [4.00 GiB] inherit
ACTIVE '/dev/rhel/swap' [2.00 GiB] inherit
ACTIVE Original '/dev/testvg/data' [160.00 MiB] inherit
ACTIVE Snapshot '/dev/testvg/databackup' [160.00 MiB] inherit
3)快照詳細信息查看
[root@mytest07 dev]# lvdisplay /dev/testvg/data
4)快照的刪除:lvremove snapshotname
[root@mytest07 dev]# lvremove /dev/testvg/databackup
8vg架構圖: