linux LVM 常用总结
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原先分区
[[email protected] ~]# 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)
[[email protected] ~]# 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.
[[email protected] ~]# partprobe --重新扫描分区
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[[email protected] ~]# 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
[[email protected] ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
2)查看当前物理卷信息:pvs或pvscan
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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...}:需要的物理卷可以写多个;
[[email protected] ~]# vgcreate -s 16M testvg /dev/sda3
Volume group "testvg" successfully created
2)查看当前系统信息
[[email protected] ~]# 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)查看当前卷组详细信息
[[email protected] ~]# 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扩展
[[email protected] ~]# 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.
[[email protected] ~]# 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
[[email protected] ~]# partprobe --重新扫描分区
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[[email protected] ~]# pvcreate /dev/sda5 --创建物理卷
Physical volume "/dev/sda5" successfully created.
[[email protected] ~]# vgextend testvg /dev/sda5 --扩展vg
Volume group "testvg" successfully extended
5)vg缩小
[[email protected] ~]# vgreduce testvg /dev/sda5
Removed "/dev/sda5" from volume group "testvg"
6)vg删除
[[email protected] ~]# 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:设置是有那个卷组生成的逻辑卷。
[[email protected] ~]# 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 :设置是有那个卷组生成的逻辑卷。 [[email protected] ~]# lvcreate -l 10 -n data testvg
Logical volume "data" created.
2) 查看逻辑卷:lvs或者lvscan
[[email protected] ~]# 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)查看逻辑卷详细信息
[[email protected] ~]# 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)格式化逻辑卷
[[email protected] ~]# mkfs.ext3 /dev/testvg/data
5)增大逻辑卷:lvextend
格式:lvextend -L +160M /dev/testvg/data
需要额外的命令使立即生效:resize2fs
resize2fs /dev/testvg/data
6)减少逻辑卷大小:lvreduce
[[email protected] dev]# lvreduce -L 160M /dev/testvg/data
resize2fs /dev/testvg/data
7)删除逻辑卷:lvremove/卷组名/逻辑卷名
[[email protected] dev]# lvremove /dev/testvg/date
6挂载文件系统
[[email protected] ~]# mkfs.ext3 /dev/testvg/data
[[email protected] dev]# mkdir /data
[[email protected] 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:源逻辑卷名字。
[[email protected] dev]# lvcreate -L 160M -s -n databackup /dev/testvg/data
Using default stripesize 64.00 KiB
2)查看
[[email protected] 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)快照详细信息查看
[[email protected] dev]# lvdisplay /dev/testvg/data
4)快照的删除:lvremove snapshotname
[[email protected] dev]# lvremove /dev/testvg/databackup
8vg架构图: