歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux基礎知識

LVM分區管理和擴展 格式化和擴容等

一、LVM簡介
LVM是 Logical Volume Manager(邏輯卷管理)的簡寫。LVM將一個或多個硬盤的分區在邏輯上集合,相當於一個大硬盤來使用,當硬盤的空間不夠使用的時候,可以繼續將其它的硬盤的分區加入其中,這樣可以實現磁盤空間的動態管理,相對於普通的磁盤分區有很大的靈活性。
與傳統的磁盤與分區相比,LVM為計算機提供了更高層次的磁盤存儲。它使系統管理員可以更方便的為應用與用戶分配存儲空間。在LVM管理下的存儲卷可以按需要隨時改變大小與移除(可能需對文件系統工具進行升級)。LVM也允許按用戶組對存儲卷進行管理,允許管理員用更直觀的名稱(如"sales'、 'development')代替物理磁盤名(如'sda'、'sdb')來標識存儲卷。
二、 LVM基本術語
物理存儲介質(The physical media):這裡指系統的存儲設備:硬盤,如:/dev/hda1、/dev/sda等等,是存儲系統最低層的存儲單元。
物理卷(physical volume):物理卷就是指硬盤分區或從邏輯上與磁盤分區具有同樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。
卷組(Volume Group):LVM卷組類似於非LVM系統中的物理硬盤,其由物理卷組成。可以在卷組上創建一個或多個“LVM分區”(邏輯卷),LVM卷組由一個或多個物理卷組成。
邏輯卷(logical volume):LVM的邏輯卷類似於非LVM系統中的硬盤分區,在邏輯卷之上可以建立文件系統(比如/home或者/usr等)。
PE(physical extent):每一個物理卷被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM尋址的最小單元。PE的大小是可配置的,默認為4MB。
LE(logical extent):邏輯卷也被劃分為被稱為LE(Logical Extents) 的可被尋址的基本單位。在同一個卷組中,LE的大小和PE是相同的,並且一一對應。
簡單來說就是:
PV:是物理的磁盤分區
VG:LVM中的物理的磁盤分區,也就是PV,必須加入VG,可以將VG理解為一個倉庫或者是幾個大的硬盤。
LV:也就是從VG中劃分的邏輯分區
 
三、創建和管理LVM
1、顯示硬盤及所屬分區情況
[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): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x44817895

   Device Boot      Start         End      Blocks   Id  System

2 、增加三個分區的操作(/dev/sdb1  /dev/sdb2 /dev/sdb3)

創建第一個主分區/dev/sdb1 大小為10G,/dev/sdb 為20G,/dev/sdb3 為10G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p ##分區類型
Partition number (1-4): 1 ##第一個主分區
First cylinder (1-6527, default 1):
Using default value 1     ##起始塊
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G ##指定分區的大小
 
Command (m for help): n   ##開始分第二主分區
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-6527, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-6527, default 6527): +20G

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): n  ##開始分第三個主分區
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3919-6527, default 3919):
Using default value 3919
Last cylinder, +cylinders or +size{K,M,G} (3919-6527, default 6527): +10G

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: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x44817895

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  8e  Linux LVM
/dev/sdb2            1307        3918    20980890   8e  Linux LVM
/dev/sdb3            3919        5224    10490445   8e  Linux LVM

Command (m for help): w                      ##最後記得保存,寫入分區表
The partition table has been altered!

 

3、創建物理卷PV
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3   
  Physical volume "/dev/sdb1" successfully created  #同時將三個分區創建成三個物理卷
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created

 

4、創建卷組VG

創建兩個卷組vg_server1  vg_server2 將物理卷/dev/sdb1、/dev/sdb2加入卷組vg_server1 ;將物理卷/dev/sdb3加入卷組vg_server2

[root@localhost ~]# vgcreate vg_server1 /dev/sdb1 /dev/sdb2    
  Volume group "vg_server1" successfully created
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# vgcreate vg_server2 /dev/sdb3
  Volume group "vg_server2" successfully created
[root@localhost ~]#
 
5、創建邏輯卷lV

分在卷組vg_server1和vg_server2創建邏輯卷

[root@localhost ~]# lvcreate -L 15G -n vg_server1_lvm1 vg_server1    # -L指定卷大小,-n 指定邏輯卷名
  Logical volume "vg_server1_lvm1" created.                                        #指定跟卷組名
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 5G -n vg_server2_lvm2 vg_server2
  Logical volume "vg_server2_lvm2" created.
[root@localhost ~]# lvdisplay  #查看生成的邏輯卷
  --- Logical volume ---
  LV Path                /dev/vg_server2/vg_server2_lvm2
  LV Name                vg_server2_lvm2
  VG Name                vg_server2
  LV UUID                jP8fZf-CR1l-Tpat-Q92Y-H4bZ-unJQ-dTYDra
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-04-14 00:21:11 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/vg_server1/vg_server1_lvm1
  LV Name                vg_server1_lvm1
  VG Name                vg_server1
  LV UUID                j4UX2E-LuRf-WKgW-9KGH-dBb3-RvAK-PXcORB
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-04-14 00:20:54 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3840
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

 

6、格式化
[root@localhost ~]# mk2fs -t ext3 /dev/vg_server1/vg_server1_lvm1
-bash: mk2fs: command not found
[root@localhost ~]# mke2fs -t ext3 /dev/vg_server1/vg_server1_lvm1
mke2fs 1.41.12 (17-May-2010)
文件系統標簽=
操作系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3932160 blocks
196608 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=4026531840
120 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, 1605632, 2654208

正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -t ext3 /dev/vg_server2/vg_server2_lvm2
mke2fs 1.41.12 (17-May-2010)
文件系統標簽=
操作系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=1342177280
40 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

正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

 

7 創建兩個目錄/mnt/lvm1 /mnt/lvm2,掛載生成兩個邏輯卷

[root@localhost vg_server1]# mount /dev/vg_server2/vg_server2_lvm2 /mnt/lvm2
[root@localhost vg_server1]# mount /dev/vg_server1/vg_server1_lvm1 /mnt/lvm1

 

8、寫入開機自動掛載
#
# /etc/fstab
# Created by anaconda on Mon Sep 14 21:58:13 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=5d9aa445-64de-4001-b45f-10db6bf51248 /                       ext4    defaults        1 1
UUID=c26e447c-8fde-4c82-b15f-5cd3a86a402b /boot                   ext4    defaults        1 2
UUID=ec4bd037-8b39-4148-9b07-9f15279caa92 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/mapper/vg_server1-lvm_1   /mnt/lvm1        ext3    defaults        0 0
/dev/mapper/vg_server2-lvm_2   /mnt/lvm2        ext3    defaults        0 0
~

9.LVM 擴容

LVM擴展,可以將另一個分區(沒有數據的分區)或另一塊新硬盤重新分區,然後將該分區創建物理卷,加入相應要擴容的邏輯卷的所在卷組。

在虛擬機添加一個塊硬盤。

[root@localhost dir]# fdisk /dev/sdc                  #給新加硬分區                       

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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-2610, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +5G

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): p

Disk /dev/sdc: 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: 0xe4571f5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  8e  Linux LVM  
/dev/sdc2            1307        1960     5253255   8e  Linux LVM    ##新建的分區/dev/sdc2,將用於LVM擴容
[root@localhost dir]# pvcreate /dev/sdc2                   #將分區/dev/sdc2創建為物理卷
  Physical volume "/dev/sdc2" successfully created   
[root@localhost dir]# vgextend vg_server1 /dev/sdc2 #將物理卷/dev/sdc2 加入卷組vg_server1
  Volume group "vg_server1" successfully extended
[root@localhost dir]# lvextend -L +5G /dev/vg_server1/vg_server1_lvm1 /dev/sdc2  #擴容/dev/vg_server1/vg_server1_lvm1邏輯卷,其中-L指定大小
                                                                                                                                       #+5G 容量增加5G ,如果沒有”+“ 表示擴展到多少
  Size of logical volume vg_server1/vg_server1_lvm1 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
  Logical volume vg_server1_lvm1 successfully resized
[root@localhost lvm1]# /sbin/resize2fs /dev/vg_server1/vg_server1_lvm1                                   #最後記得用命令resize 擴展邏輯大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_server1/vg_server1_lvm1 is mounted on /mnt/lvm1; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_server1/vg_server1_lvm1 to 7340032 (4k) blocks.
The filesystem on /dev/vg_server1/vg_server1_lvm1 is now 7340032 blocks long.

[root@localhost lvm1]# df -h
Filesystem                              Size  Used Avail Use% Mounted on
/dev/sda2                                18G  3.7G   13G  22% /
tmpfs                                   504M     0  504M   0% /dev/shm
/dev/sda1                               194M   26M  158M  15% /boot
/dev/mapper/vg_server2-vg_server2_lvm2  5.0G  139M  4.6G   3% /mnt/lvm2
/dev/mapper/vg_server1-vg_server1_lvm1   25G  173M   26G   1% /mnt/lvm1
[root@localhost lvm1]#
Copyright © Linux教程網 All Rights Reserved