LVM是一個多才多藝的硬盤系統工具。在Linux上非常的好用,傳統分區使用固定大小分區,重新調整大小十分麻煩。但是,LVM可以創建和管理“邏輯”卷,而不是直接使用物理硬盤??梢宰尮芾韱T彈性的管理邏輯卷的擴大縮小,操作簡單,而不損壞已存儲的數據。可以隨意將新的硬盤添加到LVM,以直接擴展已經存在的邏輯卷。
首先是實際的物理磁盤及其劃分的分區和其上的物理卷(PV)。一個或多個物理卷可以用來創建卷組(VG)。然后基于卷組可以創建邏輯卷(LV)。只要在卷組中有可用空間,就可以隨心所欲的創建邏輯卷。文件系統就是在邏輯卷上創建的,然后可以在操作系統掛載和訪問。
本文將介紹怎么在linux中創建和管理LVM卷。我們將會分成兩個部分。第一個部分,我們首先要在一個硬盤上創建多個邏輯卷,然后將它們掛載在/lvm-mount目錄。然后我們將要對創建好的卷調整大小。而第二部分,我們將會擴充所創建的LVM的大小。
1.準備磁盤分區
通過使用fdisk,創建磁盤分區。我們需要創建3個2G分區,注意,并不要求分區的大小一致。同樣,分區需要使用‘8e’類型來使他們可用于LVM。
#新建3個分區,大小都為2G,并轉換類型為"8e" [root@localhost ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): m #查看幫助 Command action d delete a partition #刪除一個分區 l list known partition types #列出所有的分區類型 m print this menu #打印幫助菜單 n add a new partition #新增分區 p print the partition table #打印分區表 q quit without saving changes #不保存退出 t change a partition's system id #修改分區系統ID w write table to disk and exit #保存退出 Command (m for help): n #新建分區 Command action e extended p primary partition (1-4) p #新建一個主分區 Partition number (1-4): 1 #指定分區號為1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G #指定分區大小為2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (525-2610, default 525): Using default value 525 Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G Command (m for help): p #打印分區表 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x5f0f2678 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux /dev/sdb2 263 524 2104515 83 Linux /dev/sdb3 525 786 2104515 83 Linux Command (m for help): t #修改分區的系統ID Partition number (1-4): 1 Hex code (type L to list codes): 8e #LVM的系統ID為8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x5f0f2678 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 8e Linux LVM /dev/sdb2 263 524 2104515 8e Linux LVM /dev/sdb3 525 786 2104515 8e Linux LVM Command (m for help): w #保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2.準備物理卷(PV)
剛創建的分區是用來儲存物理卷的。LVM可以使用不同大小的物理卷。
#把/dev/sdb1分區創建成物理卷 [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created #把/dev/sdb2分區創建成物理卷 [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created #把/dev/sdb3分區創建成物理卷 [root@localhost ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created [root@localhost ~]# pvs #查看系統上的物理卷 PV VG Fmt Attr PSize PFree /dev/sdb2 lvm2 ---- 2.01g 2.01g /dev/sdb3 lvm2 ---- 2.01g 2.01g [root@localhost ~]# pvdisplay #查看系統上的物理卷 "/dev/sdb2" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Q4cbqD-Y3RV-pvMH-gk4t-Hlbh-31uH-tyDE54 "/dev/sdb3" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb3 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID waK6Gz-fo3F-MjzQ-e33P-tbsY-eP46-PYGrC8
3.準備卷組(VG)
下列命令用來創建名為’vg1′的卷組,使用/dev/sdb1, /dev/sdb2 和 /dev/sdb3創建。
#把/dev/sdb1創建成卷組 [root@localhost ~]# vgcreate /dev/vg1 /dev/sdb1 Volume group "vg1" successfully created #查看卷組 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 1 0 0 wz--n- 2.00g 2.00g #擴充卷組 [root@localhost ~]# vgextend /dev/vg1 /dev/sdb2 /dev/sdb3 Volume group "vg1" successfully extended #查看卷組 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 3 0 0 wz--n- 6.01g 6.01g #查看卷組的詳細信息 [root@localhost ~]# vgdisplay --- Volume group --- VG Name vg1 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 6.01 GiB PE Size 4.00 MiB Total PE 1539 Alloc PE / Size 0 / 0 Free PE / Size 1539 / 6.01 GiB
從輸出中,我們可以看見卷組的使用量/總量。物理卷給卷組提供空間。只要在這個卷組中還有可用空間,我們就可以隨意創建邏輯卷。
4.創建邏輯卷(LV)
下列命令創建一個名為’lv0′、大小為4G的邏輯卷。這個邏輯卷使用之前創建的卷組的空間。
#在vg1的上創建邏輯卷lv0,指定大小為4G [root@localhost ~]# lvcreate -n lv0 -L 4G vg1 Logical volume "lv0" created. #查看所創建的邏輯卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 4.00g #邏輯卷可使用lvdisplay命令查看。 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv0 LV Name lv0 VG Name vg1 LV UUID KCLYti-wEGU-CupG-ucsk-UlF0-Pgpb-ij71xJ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-04-24 23:24:09 +0800 LV Status available # open 0 LV Size 4.00 GiB Current LE 1024 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
現在邏輯卷已經準備好了,我們可以格式化和掛載邏輯卷,就像其它ext2/3/4分區一樣!
5.為邏輯卷創建文件系統,并掛載
#把所創建的邏輯卷格式化為ext分區 [root@localhost ~]# mkfs.ext4 /dev/vg1/lv0 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 262144 inodes, 1048576 blocks 52428 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 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 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. #創建掛載點/mnt/lv0 [root@localhost ~]# mkdir /mnt/lvo #把邏輯卷掛載到剛才所創建的掛載點上 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
一旦邏輯卷掛載,我們就可以到掛載點 /lvm-mount/ 上讀寫了。要創建和掛載其它的邏輯卷,我們重復這個過程。
6.擴展一個LVM卷
調整邏輯卷大小的功能是LVM最有用的功能。這個部分會討論我們怎么樣擴展一個存在的邏輯卷。下面,我們將會擴展先前創建的邏輯卷‘lv0’擴大到6G。
注意,調整邏輯卷大小之后,也需要對文件系統調整大小進行匹配。這個額外的步驟各不相同,取決于創建文件系統的類型。在本文中,我們使用’lv0′創建了ext4類型的文件系統,所以這里的操作是針對ext4文件系統的。(ext2/3文件系統也類同)。
#卸載掉lv0卷 [root@localhost ~]# umount /mnt/lv0 #可以看到已經安全卸載了 [root@localhost ~]# mount | grep lv0 #擴展前先檢查文件系統(必須的步驟) [root@localhost ~]# e2fsck -f /dev/vg1/lv0 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vg1/lv0: 11/262144 files (0.0% non-contiguous), 51278/1048576 blocks #把邏輯卷擴展到6G的大小 [root@localhost ~]# lvresize -L 6G /dev/vg1/lv0 Size of logical volume vg1/lv0 changed from 4.00 GiB (1024 extents) to 6.00 GiB (1536 extents). Logical volume lv0 successfully resized. #查看系統上的邏輯卷 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv0 LV Name lv0 VG Name vg1 LV UUID KCLYti-wEGU-CupG-ucsk-UlF0-Pgpb-ij71xJ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-04-24 23:24:09 +0800 LV Status available # open 0 LV Size 6.00 GiB Current LE 1536 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 #再次掛載邏輯卷 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
7.縮減一個LVM卷
現在介紹縮減LVM卷大小的方法。
注意減少邏輯卷的大小值若小于儲存的數據大小,存儲在后面的數據會丟失。
#卸載掉卷。 [root@localhost ~]# umount /mnt/lv0 #檢查邏輯卷上的文件系統(必須的步驟) [root@localhost ~]# e2fsck -f /dev/vg1/lv0 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vg1/lv0: 11/262144 files (0.0% non-contiguous), 51278/1048576 blocks [root@localhost ~]# resize2fs /dev/vg1/lv0 4G resize2fs 1.41.12 (17-May-2010) The filesystem is already 1048576 blocks long. Nothing to do! #查看邏輯卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 6.00g #縮減邏輯卷到4G [root@localhost ~]# lvreduce -L 4G /dev/vg1/lv0 WARNING: Reducing active logical volume to 4.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg1/lv0? [y/n]: y Size of logical volume vg1/lv0 changed from 6.00 GiB (1536 extents) to 4.00 GiB (1024 extents). #查看縮減后的邏輯卷 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg1 -wi-a----- 4.00g Logical volume lv0 successfully resized. #掛載到掛載點 [root@localhost ~]# mount /dev/vg1/lv0 /mnt/lv0
現在可以使用所創建好的邏輯卷了
祝大家工作生活愉快??!
原創文章,作者:renpingsheng,如若轉載,請注明出處:http://www.www58058.com/74662