lvm 邏輯卷的創建,擴容,縮容[1/2]

前言的介紹:node

LVM是邏輯盤卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是創建在硬盤和分區之上的一個邏輯層,來提升磁盤分LVM區管理的靈活性。前面談到,LVM是在磁盤分區和文件系統之間添加的一個邏輯層,來爲文件系統屏蔽下層磁盤分區佈局,提供一個抽象的盤卷,在盤捲上創建文件系統。物理卷(physical volume)物理卷就是指硬盤分區或從邏輯上與磁盤分區具備一樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。 linux

LVM是在磁盤分區和文件系統之間添加的一個邏輯層,來爲文件系統屏蔽下層磁盤分區佈局,提供一個抽象的盤卷,在盤捲上創建文件系統。首先咱們討論如下幾個LVM術語:shell

   *物理存儲介質(Thephysicalmedia)app

  這裏指系統的存儲設備:硬盤,是存儲系統最低層的存儲單元。ide

  *物理卷(physicalvolume)佈局

  物理卷就是指硬盤分區或從邏輯上與磁盤分區具備一樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。測試

  *卷組(VolumeGroup)ui

  LVM卷組相似於非LVM系統中的物理硬盤,其由物理卷組成。能夠在卷組上建立一個或多個「LVM分區」(邏輯卷),LVM卷組由一個或多個物理卷組成。spa

  *邏輯卷(logicalvolume)code

  LVM的邏輯卷相似於非LVM系統中的硬盤分區,在邏輯卷之上能夠創建文件系統(好比/home或者/usr等)。

  *PE(physicalextent)

  每個物理卷被劃分爲稱爲PE(PhysicalExtents)的基本單元,具備惟一編號的PE是能夠被LVM尋址的最小單元。PE的大小是可配置的,默認爲4MB。

  *LE(logicalextent)

  邏輯卷也被劃分爲被稱爲LE(LogicalExtents)的可被尋址的基本單位。在同一個卷組中,LE的大小和PE是相同的,而且一一對應。

實驗部分: 

---------------創建邏輯卷------------------- 

1. 新建2個分區,sda5  5G,sd6 10G,完成以後以下

[root@yangcan /]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x000ed173
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2167    17091584   83  Linux
/dev/sda3            2167        2611     3571712   82  Linux swap / Solaris
/dev/sda4            2611        5221    20966162+   5  Extended
/dev/sda5            2611        3264     5246528+  83  Linux
/dev/sda6            3265        4570    10490413+  83  Linux

2. 把sda6初始化物理卷

[root@yangcan /]# pvcreate /dev/sda6
  Device vg6 not found (or ignored by filtering).
  Writing physical volume data to disk "/dev/sda6"
  Physical volume "/dev/sda6" successfully created

3. 建立卷組vg6,PE爲16M

[root@yangcan /]# vgcreate -s 16M vg6 /dev/sda6
  Volume group "vg6" successfully created
[root@yangcan /]# vgdisplay 
  --- Volume group ---
  VG Name               vg6
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               10.00 GiB
  PE Size               16.00 MiB
  Total PE              640
  Alloc PE / Size       0 / 0 
  Free  PE / Size       640 / 10.00 GiB
  VG UUID               35tmMm-Hyec-i4f1-xkd7-pMRS-RgRl-sqFa7l


4. 建立邏輯卷ftpdata,大小爲6G

[root@yangcan /]# lvcreate -L 6G -n ftpdata vg6
  Logical volume "ftpdata" created
[root@yangcan /]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg6/ftpdata
  VG Name                vg6
  LV UUID                LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                6.00 GiB
  Current LE             384
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

5. 格式化ext4,並掛載,並cp一些數據進去

[root@yangcan /]# mke2fs -T ext4 /dev/vg6/ftpdata 
[root@yangcan /]# mount /dev/vg6/ftpdata /ftpdata/
[root@yangcan /]# cd /ftpdata/
[root@yangcan ftpdata]# cp /etc/*.conf ./
[root@yangcan ftpdata]# cp /usr/share/dict/linux.words ./


------------------擴容----------------------------

1. 新增一個磁盤到卷組vg6,就用到以前剩下的sda5了

[root@yangcan ftpdata]# vgextend vg6 /dev/sda5
  Volume group "vg6" successfully extended
[root@yangcan ftpdata]# vgdisplay 
  --- Volume group ---
  VG Name               vg6
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               15.00 GiB
  PE Size               16.00 MiB
  Total PE              960
  Alloc PE / Size       384 / 6.00 GiB
  Free  PE / Size       576 / 9.00 GiB
  VG UUID               35tmMm-Hyec-i4f1-xkd7-pMRS-RgRl-sqFa7l


2. 擴容ftpdata,當咱們文件不段增長的時候,擴容就頗有必要了

[root@yangcan ftpdata]# lvextend -L +4G /dev/vg6/ftpdata

   或者使用,就是想讓你明白-L 和 -L +的區別,-L 後面直接加容量,是指直接加到容量,而-L +容量,是指在原有容量上加多少容量!

[root@yangcan ftpdata]# lvextend -L 10G /dev/vg6/ftpdata  
  Extending logical volume ftpdata to 10.00 GiB
  Logical volume ftpdata successfully resized
[root@yangcan ftpdata]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg6/ftpdata
  VG Name                vg6
  LV UUID                LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             640
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

[root@yangcan ftpdata]# df -h|grep ftpdata
/dev/mapper/vg6-ftpdata
                      6.0G  145M  5.5G   3% /ftpdata


此時查看ftpdata文件系統容量的時候,仍是以前的大小,因此須要resize2fs

[root@yangcan ftpdata]# resize2fs /dev/vg6/ftpdata 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg6/ftpdata is mounted on /ftpdata; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg6/ftpdata to 2621440 (4k) blocks.
The filesystem on /dev/vg6/ftpdata is now 2621440 blocks long.
[root@yangcan ftpdata]# df -h|grep ftpdata
/dev/mapper/vg6-ftpdata
                      9.9G  147M  9.3G   2% /ftpdata
[root@yangcan ftpdata]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg6/ftpdata
  VG Name                vg6
  LV UUID                LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             640
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

如今就文件系統和邏輯卷空間一致了

  

------------------------縮容---------------------------

擴容實驗作完了,接下來作縮容,縮容比擴容複雜,平時工做也不能輕易的指定縮容的大小.

[root@yangcan ftpdata]# cd ..
[root@yangcan /]# umount /ftpdata/
[root@yangcan /]# e2fsck -f /dev/vg6/ftpdata 
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/vg6/ftpdata: 41/655360 files (0.0% non-contiguous), 78604/2621440 blocks

注意:咱們大概要縮到6G左右,那咱們就不能直接resize2fs 到6G,這樣確定是錯誤的,由於PE是能夠被LVM尋址的最小單元,本次實驗PE大小爲16M.咱們去作縮容的時候,不多是0.6個PE.全部尤爲注意,網上不少資料就是直接指定大小,而沒有作測試環節!!!會形成數據的丟失,因此指定大小時,先用lvreduce -t 測試一下.用bc算出6G的位置,到底那個值合適

[root@yangcan /]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
--6000恰好整除16
6000%16
0
--6001除16爲1
6001%16
1
5999%16
15

--恰好6000M,取餘爲0,咱們比較一下6001和6000的區別

[root@yangcan /]# lvreduce -L 6001M /dev/vg6/ftpdata -t
  Test mode: Metadata will NOT be updated and volumes will not be (de)activated.
  Rounding up size to full physical extent 5.88 GiB
  WARNING: Reducing active logical volume to 5.88 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]: n
  Logical volume ftpdata NOT reduced
顯示了警告信息!

[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata -t
  Test mode: Metadata will NOT be updated and volumes will not be (de)activated.
  WARNING: Reducing active logical volume to 5.86 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]: 
提示爲6000M是,沒問題

當-t 測試時,沒問題了,那咱們就resize2fs了

[root@yangcan /]# resize2fs /dev/vg6/ftpdata 6000M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg6/ftpdata to 1536000 (4k) blocks.
The filesystem on /dev/vg6/ftpdata is now 1536000 blocks long.

掛載,查看文件系統的大小,爲最新的6000M,數據也沒有丟失!!

[root@yangcan /]# mount -a
[root@yangcan /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              17G  2.1G   14G  14% /
tmpfs                 504M  264K  504M   1% /dev/shm
/dev/sda1             291M   31M  246M  11% /boot
/dev/sr0              2.9G  2.9G     0 100% /media/cdrom
/dev/mapper/vg6-ftpdata  5.8G  145M  5.4G   3% /ftpdata

咱們用lvdisplay查看一下邏輯卷的大小仍是爲10G

[root@yangcan /]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg6/ftpdata
  VG Name                vg6
  LV UUID                LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             640
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

因此咱們用lvreduce進行調整!

[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata 
  WARNING: Reducing active and open logical volume to 5.86 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]: n
  Logical volume ftpdata NOT reduced
--提示邏輯卷正在打開,全部咱們須要中止邏輯卷,再去調整大小

[root@yangcan /]# lvchange -an /dev/vg6/ftpdata 
[root@yangcan /]# lvscan 
  inactive          '/dev/vg6/ftpdata' [10.00 GiB] inherit
[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata 
  Reducing logical volume ftpdata to 5.86 GiB
  Logical volume ftpdata successfully resized

[root@yangcan /]# mount -a
mount: special device /dev/vg6/ftpdata does not exist
--當咱們去掛載的時候,提示沒有這個設備.因此須要啓用

[root@yangcan /]# lvchange -ay /dev/vg6/ftpdata 
[root@yangcan /]# lvscan 
  ACTIVE            '/dev/vg6/ftpdata' [5.86 GiB] inherit


從新掛載,裏面的文件還在!

[root@yangcan /]# mount -a
[root@yangcan /]# cd /ftpdata/
[root@yangcan ftpdata]# ll
total 4996
-rw-r--r--. 1 root root     148 Jul 30 19:37 asound.conf
-rw-r--r--. 1 root root   21214 Jul 30 19:37 dnsmasq.conf
-rw-r--r--. 1 root root     559 Jul 30 19:37 dracut.conf
-rw-r--r--. 1 root root      20 Jul 30 19:37 fprintd.conf
-rw-r--r--. 1 root root       0 Jul 30 19:37 gai.conf
-rw-------. 1 root root     781 Jul 30 19:37 grub.conf
-rw-r--r--. 1 root root       9 Jul 30 19:37 host.conf
-rw-r--r--. 1 root root    5309 Jul 30 19:37 kdump.conf

ps: 你也能夠不作-t測試,直接指定大小去縮容,看數據是否還在?


--------移除物理卷--------

1. 移除以前,物理卷裏面必須沒有數據,用下如下命令查看各物理卷的空間大小.計劃是否有足夠的空間能夠被用來移動數據!

[root@yangcan Desktop]# pvscan  

2. 搬移要移除物理卷的數據到其餘空閒且知足大小的物理卷

[root@yangcan Desktop]# pvmove /dev/sda6 /dev/sda7

完成以後

3. 從卷組移除物理卷

[root@yangcan Desktop]#vgchange -an vg5
[root@yangcan Desktop]# vgreduce vg5 /dev/sda6



------------------------END-----------------------------
相關文章
相關標籤/搜索