LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁盤分區進行管理的一種機制,Linux用戶安裝Linux操做系統時遇到的一個常見的難以決定的問題就是如何正確地評估各分區大小,以分配合適的硬盤空間。普通的磁盤分區管理方式在邏輯分區劃分好以後就沒法改變其大小,當一個邏輯分區存放不下某個文件時,這個文件由於受上層文件系統的限制,也不能跨越多個分區來存放,因此也不能同時放到別的磁盤上。而遇到出現某個分區空間耗盡時,解決的方法一般是使用符號連接,或者使用調整分區大小的工具,但這只是暫時解決辦法,沒有從根本上解決問題。隨着Linux的邏輯卷管理功能的出現,這些問題都迎刃而解,用戶在無需停機的狀況下能夠方便地調整各個分區大小。node
[root@localhost ~]# fdisk -l 磁盤 /dev/sda:21.5 GB, 21474836480 字節,41943040 個扇區 Units = 扇區 of 1 * 512 = 512 bytes 扇區大小(邏輯/物理):512 字節 / 512 字節 I/O 大小(最小/最佳):512 字節 / 512 字節 磁盤標籤類型:dos 磁盤標識符:0x000a5fed 設備 Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 4507647 2048000 82 Linux swap / Solaris /dev/sda3 4507648 41943039 18717696 83 Linux 磁盤 /dev/sdb:10.7 GB, 10737418240 字節,20971520 個扇區 Units = 扇區 of 1 * 512 = 512 bytes 扇區大小(邏輯/物理):512 字節 / 512 字節 I/O 大小(最小/最佳):512 字節 / 512 字節
[root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created. [root@localhost ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created.
建立完物理卷以後可使用pvdisplay 或 dvs命令來查看一下當前的物理卷,若是查看不到,能夠先運行一下partprobe命令。bash
[root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 1.00g 1.00g /dev/sdb2 lvm2 --- 1.00g 1.00g /dev/sdb3 lvm2 --- 1.00g 1.00g
[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 Volume group "vg1" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 1.99g 1.99g
[root@localhost ~]# lvcreate -L 100M -n lv1 vg1 Logical volume "lv1" created. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID h418at-aYwA-m8Ah-lAji-ICmF-uVW0-eDjAdQ LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-02-28 08:44:57 +0800 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) 文件系統標籤= OS type: Linux 塊大小=1024 (log=0) 分塊大小=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user 第一個數據塊=1 Maximum filesystem blocks=33685504 13 block groups 8192 blocks per group, 8192 fragments per group 1976 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Allocating group tables: 完成 正在寫入inode表: 完成 Creating journal (4096 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 [root@localhost ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# ls /mnt lost+found
到這裏,LVM就算完成了,當邏輯卷空間不足時怎樣擴容呢?服務器
[root@localhost ~]# umount /mnt umount: /mnt:未掛載 [root@localhost ~]# lvresize -L 200M /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents). Logical volume vg1/lv1 successfully resized. [root@localhost ~]# e2fsck -f /dev/vg1/lv1 e2fsck 1.42.9 (28-Dec-2013) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄鏈接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks [root@localhost ~]# resize2fs /dev/vg1/lv1 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks. The filesystem on /dev/vg1/lv1 is now 204800 blocks long. [root@localhost ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# df -h /mnt 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt
[root@localhost ~]# e2fsck -f /dev/vg1/lv1 e2fsck 1.42.9 (28-Dec-2013) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄鏈接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11887/204800 blocks [root@localhost ~]# lvresize -L 100M /dev/vg1/lv1 WARNING: Reducing active logical volume to 100.00 MiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg1/lv1? [y/n]: y Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents). Logical volume vg1/lv1 successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m [root@localhost ~]# mount /dev/vg1/lv1 /mnt mount: 文件系統類型錯誤、選項錯誤、/dev/mapper/vg1-lv1 上有壞超級塊、 缺乏代碼頁或助手程序,或其餘錯誤 有些狀況下在 syslog 中能夠找到一些有用信息- 請嘗試 dmesg | tail 這樣的命令看看。
爲了符合實驗要求,首先將/dev/vg1/lv1 格式化爲xfs類型 ,mkfs.xfs -f /dev/vg1/lv1app
[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1 meta-data=/dev/vg1/lv1 isize=256 agcount=2, agsize=6656 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=13312, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# ls /mnt [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 52.00m [root@localhost ~]# lvresize -L 300M /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 52.00 MiB (13 extents) to 300.00 MiB (75 extents). Logical volume vg1/lv1 successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 300.00m [root@localhost ~]# df -h /mnt 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/vg1-lv1 49M 2.7M 46M 6% /mnt [root@localhost ~]# xfs_grows /dev/vg1/lv1 -bash: xfs_grows: 未找到命令 [root@localhost ~]# xfs_growfs /dev/vg1/lv1 meta-data=/dev/mapper/vg1-lv1 isize=256 agcount=2, agsize=6656 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=13312, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 13312 to 76800 [root@localhost ~]# df -h /mnt 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/vg1-lv1 297M 3.1M 294M 2% /mnt
能夠看到,容量從50M成功的擴容到了300Mdom
從上面的內容能夠看出,邏輯卷擴容的上限就是卷組的容量,若是卷組的容量不夠用了該怎麼擴容呢?在實驗之初咱們是建立了三個物理卷,而建立卷組的時候只用了兩個物理卷,當卷組容量不夠時 ,咱們能夠把空閒的第三個物理卷加入到卷組當中ide
[root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 1 0 wz--n- 1.99g <1.70g [root@localhost ~]# vgextent vg1 /dev/sdb3 -bash: vgextent: 未找到命令 [root@localhost ~]# vgex vgexport vgextend [root@localhost ~]# vgextend vg1 /dev/sdb3 Volume group "vg1" successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 3 Act PV 3 VG Size <2.99 GiB PE Size 4.00 MiB Total PE 765 Alloc PE / Size 75 / 300.00 MiB Free PE / Size 690 / <2.70 GiB VG UUID gT48qq-JCCo-I17e-H82v-gQoV-d5f8-9IoYeR