1. 程式人生 > >菜鳥學習Linux LVM及配置實戰

菜鳥學習Linux LVM及配置實戰

linux lvm shadow back segments ive lock 如果 blog swa

每當我們生產上線服務器的時候,總是無法正確的預估磁盤大小該給多少,這樣就產生了一個技術LVM,什麽是LVM?
LVM是 Logical Volume Manager(邏輯卷管理),它是Linux環境下對磁盤分區進行管理的一種機制。
這裏菜雞哥哥畫一個圖,是我自己的理解,如果有錯誤請指正!!
技術分享圖片
那麽如何使用lvm呢?

[root@jw ~]# fdisk -l

Disk /dev/sda: 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: 0x00009be7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        2611    18668544   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes——實驗演示使用此盤
255 heads, 63 sectors/track, 652 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: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000
[root@jw ~]# fdisk /dev/sdb——對需要進行lvm的磁盤進行分區

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): 
Value out of range.
Partition number (1-4): 1
First cylinder (1-652, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-652, default 133): 
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-652, default 652): +1G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         264     1060290   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks
[root@jw ~]# pvcreate /dev/sdb{1,2}——創建pv
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[root@jw ~]# vgcreate vg01 /dev/sdb{1,2}——創建vg,vg名為vg01
  Volume group "vg01" successfully created
[root@jw ~]# lvcreate -n lv01 -L 1G vg01——創建lv,lv名為lv01,大小為1G
  Logical volume "lv01" created
[root@jw ~]# 
[root@jw ~]# pvs——查看pv
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1  vg01 lvm2 a--  1.01g 8.00m
  /dev/sdb2  vg01 lvm2 a--  1.01g 1.01g
[root@jw ~]# vgs——查看vg
  VG   #PV #LV #SN Attr   VSize VFree
  vg01   2   1   0 wz--n- 2.02g 1.02g
[root@jw ~]# lvs——查看lv
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-a----- 1.00g                                             
[root@jw ~]# 
[root@jw ~]# pvscan 
  PV /dev/sdb1   VG vg01   lvm2 [1.01 GiB / 8.00 MiB free]
  PV /dev/sdb2   VG vg01   lvm2 [1.01 GiB / 1.01 GiB free]
  Total: 2 [2.02 GiB] / in use: 2 [2.02 GiB] / in no VG: 0 [0   ]
[root@jw ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg01" using metadata type lvm2
[root@jw ~]# lvscan
  ACTIVE            ‘/dev/vg01/lv01‘ [1.00 GiB] inherit
[root@jw ~]# 
[root@jw ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg01
  PV Size               1.01 GiB / not usable 3.41 MiB
  Allocatable           yes 
  PE Size               4.00 MiB——默認PE大小為4M
  Total PE              258
  Free PE               2
  Allocated PE          256
  PV UUID               CvlZqo-1Bxi-BKzg-cgGC-W7hZ-43np-SsdyEq

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg01
  PV Size               1.01 GiB / not usable 3.44 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              258
  Free PE               258
  Allocated PE          0
  PV UUID               pDa7Mj-dCs4-cY0V-4JZY-viUG-BSNI-aspjlN

[root@jw ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.02 GiB
  PE Size               4.00 MiB
  Total PE              516
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       260 / 1.02 GiB
  VG UUID               FUhTIi-lmOr-eVOi-JxMA-0pMJ-yMQq-8gSlCa

[root@jw ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                S4HQMH-jxjp-0WbT-TkiV-Ftc7-dn58-7l3nsy
  LV Write Access        read/write
  LV Creation host, time jw, 2017-12-22 23:06:19 +0800
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
[root@jw ~]# mkfs.ext4 /dev/vg01/lv01   ——格式化lvm,文件系統為ext4
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@jw ~]# mkdir /testlvm
[root@jw ~]# mount /dev/vg01/lv01 /testlvm/ 掛載lvm
[root@jw ~]# df -TH
Filesystem            Type   Size  Used Avail Use% Mounted on
/dev/sda3             ext4    19G  4.7G   14G  27% /
tmpfs                 tmpfs  515M     0  515M   0% /dev/shm
/dev/sda1             ext4   204M   31M  163M  16% /boot
/dev/mapper/vg01-lv01 ext4   **1.1G**   35M  969M   4% /testlvm
[root@jw ~]#
這裏演示lv擴容,把/testlvm加大500M
[root@jw ~]# vgs——首先查看vg是否有可用空間
  VG   #PV #LV #SN Attr   VSize VFree——VFree還有1G空閑
  vg01   2   1   0 wz--n- 2.02g 1.02g
[root@jw ~]# lvextend -L +500M /dev/vg01/lv01  ——有空間直接擴展
  Extending logical volume lv01 to 1.49 GiB
  Logical volume lv01 successfully resized
[root@jw ~]# 
---------------------------------------------------
如果vgs查看沒有空間,則需要添加vg,新格式化一個分區sdc1,這裏不演示格式化步驟;
vgextend vg01 /dev/sdc1
Lvextend -L 500M /dev/vg01/lv01
————————————————————
執行以上lv擴容後,執行df -Th 發現還是沒有擴容,
linux7對文件系統進行擴容執行以下命令
[root@xuegod61 ~]# xfs_growfs /dev/vg01/lv01
linux6對文件系統進行擴容執行以下命令
[root@xuegod61 ~]# resize2fs /dev/vg01/lv01
這裏演示的是格式化ext4,所以使用resize2fs
[root@jw ~]# resize2fs /dev/vg01/lv01 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lv01 is mounted on /testlvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg01/lv01 to 390144 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 390144 blocks long.

[root@jw ~]# df -TH
Filesystem            Type   Size  Used Avail Use% Mounted on
/dev/sda3             ext4    19G  4.7G   14G  27% /
tmpfs                 tmpfs  515M     0  515M   0% /dev/shm
/dev/sda1             ext4   204M   31M  163M  16% /boot
/dev/mapper/vg01-lv01 ext4   **1.6G**   36M  1.5G   3% /testlvm
[root@jw ~]# 

刪除lvm需要先umount
umount /dev/vg01/lv01
移除lv
lvremove /dev/vg01/lv01
移除vg
vgremove vg01
移除pv
pvremove /dev/sdb{1,2}

菜鳥學習Linux LVM及配置實戰