邏輯磁碟區管理LVM
#一建立邏輯磁碟區
1準備分割區或硬碟
這裡使用/dev/sdb、/dev/sdc兩塊硬碟和/dev/sda9、/dev/sda10兩個分割區,大小都為1G,磁碟有限,我也不想這麼摳的。
新增分割區/dev/sda9、/dev/sda10
[root@centos7 ~]# fdisk /dev/sda 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 All primary partitions are in use Adding logical partition 9 First sector (31885312-41943039, default 31885312): Using default value 31885312 Last sector, +sectors or +size{K,M,G} (31885312-41943039, default 41943039): +1G Partition 9 of type Linux and of size 1 GiB is set
注意,要修改分割區類型為Linux LVM
Command (m for help): t Partition number (1-9, default 9): 8e Partition number (1-9, default 9): 9 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help):
同樣的方法創建/dev/sda10。
[root@centos7 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda9 8:9 0 1G 0 part └─sda10 8:10 0 1G 0 part sdb 8:16 0 1G 0 disk sdc 8:32 0 1G 0 disk
2 建立PV(實體磁碟區)
指令格式為:pvcreate DEVICE,可以一次建立一個設備,也可以一次建立多個裝置。
查看物理卷命令:pvdisplay(查看詳細資訊)、pvs(查看簡易資訊)
[root@centos7 ~]# pvcreate /dev/sda9 Physical volume "/dev/sda9" successfully created. [root@centos7 ~]# pvcreate /dev/sda10 /dev/sdb /dev/sdc Physical volume "/dev/sda10" successfully created. Physical volume "/dev/sdb" successfully created. Physical volume "/dev/sdc" successfully created.
查看物理卷,看到已經成功創建。
[root@centos7 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda10 lvm2 --- 1.00g 1.00g /dev/sda9 lvm2 --- 1.00g 1.00g /dev/sdb lvm2 --- 1.00g 1.00g /dev/sdc lvm2 --- 1.00g 1.00g
3 建立卷宗組(VG)
指令格式為:vgcreate -s PE VGNAME(vg名) DEVICE(裝置名稱)
-s :指定實體擴充區塊大小既就是PE的大小
檢視指令:vgs、vgdisplay
[root@centos7 ~]# vgcreate -s 16M vg0 /dev/sda9 /dev/sda10 /dev/sdb /dev/sdc Volume group "vg0" successfully created
看一下[root@centos7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 4 0 0 wz--n- 3.94g 3.94g
[root@centos7 ~]# vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 4 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 4 Act PV 4 VG Size 3.94 GiB PE Size 16.00 MiB Total PE 252 Alloc PE / Size 0 / 0 Free PE / Size 252 / 3.94 GiB VG UUID qmoIMg-5wQR-GKCS-dpq9-HLrt-zVIi-JBC9ZX
一切都複合預期,大小約為4G,剛好是兩個分割區和兩個硬碟之和。 4建立邏輯磁碟區(LV)
[root@centos7 ~]# lvcreate -n lv0 -L 3G vg0 Logical volume "lv0" created.
創建成功,查看一下
[root@centos7 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-a----- 3.00g
[root@centos7 ~]# lvdisplay --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID 622i7m-uy6s-3nZI-8xSb-sxrR-cZzz-C9pwkd LV Write Access read/write LV Creation host, time centos7.3.loacl, 2017-04-25 16:25:13 +0800 LV Status available # open 0 LV Size 3.00 GiB Current LE 192 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
#可以確定已經成功創建。 現在就可以把/dev/vg0/lv0當做一塊硬碟使用了。 現在看一下磁碟訊息,已經發生了變化。
[root@centos7 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT fd0 2:0 1 4K 0 disk sda 8:0 0 20G 0 disk ├─sda9 8:9 0 1G 0 part │ └─vg0-lv0 253:0 0 3G 0 lvm └─sda10 8:10 0 1G 0 part └─vg0-lv0 253:0 0 3G 0 lvm sdb 8:16 0 1G 0 disk └─vg0-lv0 253:0 0 3G 0 lvm sdc 8:32 0 1G 0 disk └─vg0-lv0 253:0 0 3G 0 lvm
可以看到,所加入的分割區和硬碟已經屬於vg0-lv0了。 5 建立檔案系統,格式化
#
mkfs.ext4 /dev/vg0/lv0 [root@centos7 ~]# mkfs.ext4 /dev/vg0/lv0
6 掛載使用
[root@centos7 ~]# mkdir /mnt/lv0 [root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0
#二擴展LV
#擴展命令格式:
lvextend -L 100G /dev/vg0/lv0 lvextend -l +100G /dev/vg0/lv0 lvextend -l +100%FREE /dev/vg0/lv0
由於vg0還預留將近1G 空間,現在要擴充一下lv01 先來看看原有LV
##[root@centos7 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg0 4 1 0 wz--n- 3.94g 960.00m
[root@centos7 ~]# lvextend -r -l +100%FREE /dev/vg0/lv0
[root@centos7 ~]# lvextend -l +100%FREE /dev/vg0/lv0 Size of logical volume vg0/lv0 changed from 3.00 GiB (192 extents) to 3.94 GiB (252 extents). Logical volume vg0/lv0 successfully resized.
ext系列檔案系統重新定義:resize2fs /dev/vg0/lv0
xfs系列檔案系統重新定義:xfs_growfs /dev/vg0/lv0[root@centos7 ~]# resize2fs /dev/vg0/lv0 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/vg0/lv0 is now 1032192 blocks long.
#4 檢查檔案系統完成性(也可不做)
[root@centos7 ~]# fsck.ext4 /dev/vg0/lv0 e2fsck 1.42.9 (28-Dec-2013) /dev/vg0/lv0: clean, 20/262144 files, 35918/1032192 blocks
#5 確認邏輯磁碟區大小
[root@centos7 ~]# df -h /dev/vg0/lv0 Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-lv0 3.9G 12M 3.6G 1% /mnt/lv0
6 掛載使用
[root@centos7 ~]# mount -o remount /dev/vg0/lv0
##三縮減LV
雖然一般不會遺失數據,但為了保險起見。還是先備份為好。 看下/mnt/lv0目錄下現有的資料
[root@centos7 ~]# ls /mnt/lv0/ lost+found myfile2 myfile4 myfile6 myfile8 myfile1 myfile3 myfile5 myfile7 myfile9 [root@centos7 ~]# mkdir /backup [root@centos7 ~]# cp -a /mnt/lv0/* /backup/
2 卸載/dev/vg0/lv0
#
[root@centos7 ~]# umount /mnt/lv0/
[root@centos7 ~]# e2fsck -f /dev/vg0/lv0 e2fsck 1.42.9 (28-Dec-2013) 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/vg0/lv0: 20/262144 files (0.0% non-contiguous), 35918/1032192 blocks
4下来重定义文件系统大小
resize2fs /dev/vg0/lv0 2G #2G位缩减后的LV 大小。
[root@centos7 ~]# resize2fs /dev/vg0/lv0 2G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg0/lv0 to 524288 (4k) blocks. The filesystem on /dev/vg0/lv0 is now 524288 blocks long.
5 缩减LV
lvreduce -L 2G /dev/vg0/lv0 #缩减到2G
[root@centos7 ~]# lvreduce -L 2G /dev/vg0/lv0 WARNING: Reducing active logical volume to 2.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg0/lv0? [y/n]: y Size of logical volume vg0/lv0 changed from 3.94 GiB (252 extents) to 2.00 GiB (128 extents). Logical volume vg0/lv0 successfully resized.
6 挂载使用
mount /dev/vg0/lv0 /mnt/lv0 [root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0/
查看磁盘挂载情况
[root@centos7 ~]# df -h /dev/vg0/lv0 Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-lv0 2.0G 9.0M 1.8G 1% /mnt/lv0
7 确认数据是否丢失
现在再来开心目录/mnt/lv0 的内容
[root@centos7 ~]# ls /mnt/lv0/ lost+found myfile2 myfile4 myfile6 myfile8 myfile1 myfile3 myfile5 myfile7 myfile9
很完美,数据并没有丢失!
以上是邏輯磁碟區管理lvm的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!