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)parted 进入[root@rac1 ~]# partedGNU Parted 2.1Using /dev/sdaWelcome to GNU Parted! Type 'help' to view a list of commands.(parted)
3)帮助(parted) help align-check TYPE N check partition N for TYPE(min|opt) alignmentcheck NUMBER do a simple check on the file systemcp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partitionhelp [COMMAND] print general help, or help on COMMANDmklabel,mktable LABEL-TYPE create a new disklabel (partition table)mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBERmkpart PART-TYPE [FS-TYPE] START END make a partitionmkpartfs PART-TYPE FS-TYPE START END make a partition with a file systemmove NUMBER START END move partition NUMBERname NUMBER NAME name partition NUMBER as NAMEprint [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partitionquit exit programrescue START END rescue a lost partition near START and ENDresize NUMBER START END resize partition NUMBER and its file systemrm NUMBER delete partition NUMBERselect DEVICE choose the device to editset NUMBER FLAG STATE change the FLAG on partition NUMBERtoggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBERunit UNIT set the default unit to UNITversion display the version number and copyright information of GNU Parted
3)选择要创建分区的盘,并选择创建磁盘格式[root@rac1 ~]# parted /dev/sdeGNU Parted 2.1Using /dev/sdeWelcome to GNU Parted! Type 'help' to view a list of commands.(parted) mklabel mbr ----parted不支持创建MBR磁盘格式parted: invalid token: mbrNew disk label type? gpt Warning: The existing disk label on /dev/sde will be destroyed and all data on this disk will be lost. Do you want to continue?Yes/No? yes ----选择yes格式化磁盘(parted) mkpart ----输入mkpart开始创建分区 Partition name? []? /dev/sde1 ----创建的分区名字File system type? [ext2]? ext4--输入文件系统格式Start? 0 End? 8G ----创建分区大小Warning: The resulting partition is not properly aligned for best performance.Ignore/Cancel? ignore ----选择忽略(parted) mkpart ----第二次进行分区Partition name? []? /dev/sde2 File system type? [ext2]? ext3 ----支持一块盘存在不同格式文件系统Start? 8G ----分区必须连续 End? 16G (parted) print ----查看硬盘的分区信息Model: ATA VBOX HARDDISK (scsi)Disk /dev/sde: 16.1GBSector size (logical/physical): 512B/512BPartition Table: gpt Number Start End Size File system Name Flags1 17.4kB 8000MB 8000MB /dev/sde12 8001MB 16.1GB 8104MB /dev/sde2(parted) quit ----退出,注意parted分区是立即生效的,而fdisk需要保存才会生效 root@rac1 ~]# fdisk -l /dev/sde ----fdisk无法查看parted分区 WARNING: GPT (GUID Partition Table) detected on '/dev/sde'! The util fdisk doesn't support GPT. Use GNU Parted. Disk /dev/sde: 16.1 GB, 16106127360 bytes255 heads, 63 sectors/track, 1958 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000 Device Boot Start End Blocks Id System/dev/sde1 1 1959 15728639+ ee GPT
3 物理卷的管理
1)创建物理卷:pvcreate[root@rac1 ~]# pvcreate /dev/sde1Physical volume "/dev/sde1" successfully created
2) 查看当前物理卷信息:pvs或pvscan[root@rac1 ~]# pvsPV VG Fmt Attr PSize PFree/dev/sde1 lvm2 --- 7.45g 7.45g
3)查看物理卷详细信息:pvdiskplay pvname[root@rac1 ~]# pvdisplay /dev/sde1"/dev/sde1" is a new physical volume of "7.45 GiB"--- NEW Physical volume ---PV Name /dev/sde1VG Name PV Size 7.45 GiBAllocatable NOPE Size 0 Total PE 0Free PE 0Allocated PE 0PV UUID Jcmv5C-wi2x-bn4g-OgTq-w0oV-zDOH-Bofml6
4)删除物理卷:pvremove[root@rac1 ~]# pvremove /dev/sde1Labels on physical volume "/dev/sde1" successfully wiped[root@rac1 ~]# pvs[root@rac1 ~]# pvscanNo matching physical volumes found
卷组的管理
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@rac1 ~]# vgcreate -s 8M myvg /dev/sde1Volume group "myvg" successfully created
2)查看当前系统信息[root@rac1 ~]# vgsVG #PV #LV #SN Attr VSize VFreemyvg 1 0 0 wz--n- 7.45g 7.45g
3)查看当前卷组详细信息[root@rac1 ~]# vgdisplay myvg--- Volume group ---VG Name myvgSystem ID Format lvm2Metadata Areas 1Metadata Sequence No 1VG Access read/writeVG Status resizableMAX LV 0Cur LV 0Open LV 0Max PV 0Cur PV 1Act PV 1VG Size 7.45 GiBPE Size 8.00 MiBTotal PE 953Alloc PE / Size 0 / 0 Free PE / Size 953 / 7.45 GiBVG UUID 17u2dn-FolV-uBEX-0HLj-bEZZ-GZDz-JwqdOt
4)vg扩展[root@rac1 ~]# vgextend myvg /dev/sde2Physical volume "/dev/sde2" successfully createdVolume group "myvg" successfully extended
5)vg缩小[root@rac1 ~]# vgreduce myvg /dev/sde2Removed "/dev/sde2" from volume group "myvg"
6)vg删除[root@rac1 ~]# vgremove myvgVolume group "myvg" successfully removed
4 逻辑卷的管理
1)逻辑卷的创建格式1:lvcreate -L 512M -n data myvg -L 512M:设置逻辑卷的大小为512M。 -n data:设置逻辑卷的名字为data ;表示法:/dev/卷组名/datamyvg:设置是有那个卷组生成的逻辑卷。 [root@rac1 ~]# lvcreate -L 4G -n redo myvgLogical volume "redo" created 格式2:lvcreate -l 32 -n data myvg -l 32:生成的逻辑卷使用32个扩展块,逻辑卷大小=32*PE(默认4M)则为128M -n data:逻辑卷的名字; vg0 :设置是有那个卷组生成的逻辑卷。 [root@rac1 ~]# lvcreate -l 50 -n vote myvgLogical volume "vote" created
2) 查看逻辑卷:lvs或者lvscan[root@rac1 ~]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertredo myvg -wi-a----- 4.00g
3)查看逻辑卷详细信息[root@rac1 ~]# lvdisplay myvg --- Logical volume ---LV Path /dev/myvg/redoLV Name redoVG Name myvgLV UUID sPYFkQ-KM8q-oTYY-5AXu-D0oE-TdWS-IBx03fLV Write Access read/writeLV Creation host, time rac1, 2015-09-24 10:30:41 +0800LV Status available# open 0LV Size 4.00 GiBCurrent LE 256Segments 1Allocation inheritRead ahead sectors autocurrently set to 256Block device 253:0
4)格式化逻辑卷格式:mkfs.ext3 /dev/vg0/data mke2fs -j /dev/vg0/data [root@rac1 ~]# mkfs.ext2 /dev/myvg/redomke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks262144 inodes, 1048576 blocks52428 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=107374182432 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.
5)增大逻辑卷:lvextend格式:lvextend -L +100M /dev/vg0/data [root@rac1 ~]# lvextend -L 1000M /dev/myvg/voteRounding size to boundary between physical extents: 1008.00 MiBSize of logical volume myvg/vote changed from 800.00 MiB (50 extents) to 1008.00 MiB (63 extents).Logical volume vote successfully resized
如果使用下面的方式增加lv,需要额外的命令使立即生效:resize2fs[root@rac1 ~]# lvextend -L +200M /dev/myvg/redoRounding size to boundary between physical extents: 208.00 MiBSize of logical volume myvg/redo changed from 4.00 GiB (256 extents) to 4.20 GiB (269 extents).Logical volume redo successfully resized[root@rac1 ~]# resize2fs /dev/myvg/redoresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/myvg/redo to 1101824 (4k) blocks.The filesystem on /dev/myvg/redo is now 1101824 blocks long.
6)减少逻辑卷大小:lvreduce[root@rac1 ~]# lvreduce -L -100M /dev/myvg/redoRounding size to boundary between physical extents: 96.00 MiBWARNING: Reducing active logical volume to 4.11 GiBTHIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce redo? [y/n]: ySize of logical volume myvg/redo changed from 4.20 GiB (269 extents) to 4.11 GiB (263 extents).Logical volume redo successfully resized
使上面的配置生效:[root@rac1 ~]# resize2fs /dev/myvg/redoresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/myvg/redo to 1077248 (4k) blocks.resize2fs: Can't read an block bitmap while trying to resize /dev/myvg/redoPlease run 'e2fsck -fy /dev/myvg/redo' to fix the filesystemafter the aborted resize operation.
如果使用lvreduce -L 500M /dev/myvg/redo,即直接指定缩小后的大小不需要生效
7)删除逻辑卷:lvremove/卷组名/逻辑卷名[root@rac1 ~]# lvremove /dev/myvg/redoDo you really want to remove active logical volume redo? [y/n]: yLogical volume "redo" successfully removed
5 lvm快照
1)快照的创建格式:Lvcreate -l 64 -s -n databackup /dev/vg0/data -l 64:之前我们已经说过了这里是 64个扩展块,总容量=64*单扩展块大小 -n databackup:生成的快照的名字。 /dev/vg0/data:源逻辑卷名字。
[root@rac1 ~]# lvcreate -L 1200M -s -n databackup /dev/myvg/voteReducing COW size 1.17 GiB down to maximum usable size 1.00 GiB.Logical volume "databackup" created
2)查看
[root@rac1 ~]# lvscanACTIVE Original '/dev/myvg/vote' [1008.00 MiB] inheritACTIVE Snapshot '/dev/myvg/databackup' [1.00 GiB] inherit
3)快照详细信息查看
[root@rac1 ~]# lvdisplay /dev/myvg/databackup --- Logical volume ---LV Path /dev/myvg/databackupLV Name databackupVG Name myvgLV UUID aY17jL-H3uz-hTqK-Oevv-pfgR-CYhd-k3znPrLV Write Access read/writeLV Creation host, time rac1, 2015-09-24 11:00:36 +0800LV snapshot status active destination for voteLV Status available# open 0LV Size 1008.00 MiBCurrent LE 63COW-table size 1.00 GiBCOW-table LE 64Allocated to snapshot 0.00%Snapshot chunk size 4.00 KiBSegments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:0
4)快照的删除:
lvremove snapshotname[root@rac1 ~]# lvremove /dev/myvg/databackup Do you really want to remove active logical volume databackup? [y/n]: yLogical volume "databackup" successfully removed