18.Linux下LVM邏輯卷管理器(LV創建、LV擴展與縮減、LV快照、LV移除)

1.LVM簡介:

LVM(Logical Volume Manager) 邏輯卷管理器:

可以動態調整磁盤容量,提高磁盤管理靈活性。

注意:絕大多數分區可以基於LVM創建,但是 /boot 掛載分區不能基於LVM創建。

LVM管理組成成份:

PV(物理卷):物理卷可以由整個硬盤也可以是獨立分區轉化而成。物理捲包括了許多默認大小爲4MB的PE(Physical Extent)基本單元。

PE(物理拓展):lvm設備的最小存儲單元。

VG(物理卷組):卷組由一個或多個物理卷組成的整體

LV( 邏輯卷):從卷組中抽出一部分空間,可以建立文件系統;直接使用的設備,可以增大縮減並保持原有的數據不變

2.創建LV邏輯卷

fdisk -l
fdisk /dev/vdb

[root@server ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe4cfbfc1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848     1230847      512000   83  Linux
/dev/vdb3         1230848     2254847      512000   83  Linux
/dev/vdb4         2254848    20971519     9358336    5  Extended
/dev/vdb5         2256896     2461695      102400   83  Linux
/dev/vdb6         2463744     2668543      102400   83  Linux
/dev/vdb7         2670592     3694591      512000   83  Linux
[root@server ~]# fdisk /dev/vdb
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): t
Partition number (1-7, default 7): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe4cfbfc1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848     1230847      512000   83  Linux
/dev/vdb3         1230848     2254847      512000   83  Linux
/dev/vdb4         2254848    20971519     9358336    5  Extended
/dev/vdb5         2256896     2461695      102400   83  Linux
/dev/vdb6         2463744     2668543      102400   8e  Linux LVM
/dev/vdb7         2670592     3694591      512000   8e  Linux LVM

[root@server ~]# pvcreate  /dev/vdb6              ##創建PV            
[root@server ~]# vgcreate  vg0   /dev/vdb6        ##創建VG
[root@server ~]# lvcreate -L 20M -n lv0 vg0       ##創建LV
[root@server ~]# mkfs.xfs   /dev/vg0/lv0          ##格式化建立文件系統

[root@server ~]# pvcreate  /dev/vdb6
  Physical volume "/dev/vdb6" successfully created
[root@server ~]# vgcreate  vg0   /dev/vdb6
  Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 20M -n lv0 vg0
  Logical volume "lv0" created
[root@server ~]# mkfs.xfs   /dev/vg0/lv0
meta-data=/dev/vg0/lv0           isize=256    agcount=1, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5120, 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@server ~]# rm -rf /mnt/*
[root@server ~]# mount /dev/vg0/lv0    /mnt/
[root@server ~]# df -H /mnt/
Filesystem                  Size    Used    Avail   Use%      Mounted on
/dev/mapper/vg0-lv0   18M   1.2M   17M      7%            /mnt

 

爲了更方便的觀察LVM的管理情況,我們採用監控命令:

watch -n 1 'pvs;echo =======;vgs;echo =======;lvs;echo =======;df -h /mnt/'

2.LV邏輯卷擴展

[root@server ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd5528270

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      821247      307200   83  Linux
/dev/vdb3          821248     1845247      512000   83  Linux
/dev/vdb4         1845248    20971519     9563136    5  Extended
/dev/vdb5         1847296     2871295      512000   83  Linux
/dev/vdb6         2873344     3078143      102400   8e  Linux LVM
/dev/vdb7         3080192     3489791      204800   8e  Linux LVM
[root@server ~]# pvcreate /dev/vdb6                                                       ##新建pv物理卷/dev/vdb6
  Physical volume "/dev/vdb6" successfully created
[root@server ~]# pvcreate /dev/vdb7                                                       ##新建pv物理卷/dev/vdb7
  Physical volume "/dev/vdb7" successfully created
[root@server ~]# vgcreate vg0 /dev/vdb6                                               ##新建物理卷組vg0
  Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 20M -n lv0 vg0                                          ##在物理卷組vg0中新建20M的邏輯卷lv0
  Logical volume "lv0" created                         
[root@server ~]# mkfs.xfs /dev/vg0/lv0                                                  ##格式化,建立文件系統
meta-data=/dev/vg0/lv0           isize=256    agcount=1, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5120, 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@server ~]# mount /dev/vg0/lv0  /mnt                                             ##掛載邏輯卷lv0到/mnt/
[root@server ~]# df                                                                                  ##查看掛載情況
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3154804   7319096  31% /
devtmpfs               469332       0    469332   0% /dev
tmpfs                  484920     140    484780   1% /dev/shm
tmpfs                  484920   12844    472076   3% /run
tmpfs                  484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-lv0     17068    1088     15980   7% /mnt

##從現有的邏輯分區中擴展

[root@server ~]# vgextend vg0 /dev/vdb7                                             ##擴展vg0
  Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 290M /dev/vg0/lv0                                   ##擴展lv
  Rounding size to boundary between physical extents: 292.00 MiB
  Extending logical volume lv0 to 292.00 MiB
  Logical volume lv0 successfully resized

[root@server ~]# xfs_growfs /dev/vg0/lv0                                              ##擴展文件系統
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=1, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5120, 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 5120 to 74752
[root@server ~]# fdisk /dev/vdb
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 8
First sector (3491840-20971519, default 3491840):  
Using default value 3491840
Last sector, +sectors or +size{K,M,G} (3491840-20971519, default 20971519): +1G
Partition 8 of type Linux and of size 1 GiB is set

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@server ~]# partprobe                                                 ##同步分區表

##新建分區並擴展

[root@server ~]# fdisk /dev/vdb                                           ##新建分區
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): t
Partition number (1-8, default 8): 8
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)                        ##有這個提示說明需要同步分區表
Syncing disks.
[root@server ~]# partprobe                                                                        ##同步分區表
[root@server ~]# pvcreate /dev/vdb8                                                        ##新建pv物理卷/vdb8
  Physical volume "/dev/vdb8" successfully created
[root@server ~]# vgextend vg0 /dev/vdb8                                                ##將/vdb8添加到物理卷組vg0中
  Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 1200M /dev/vg0/lv0                                   ##擴展邏輯卷lv0
  Extending logical volume lv0 to 1.17 GiB
  Logical volume lv0 successfully resized
[root@server ~]# xfs_growfs /dev/vg0/lv0                                                ##擴展xfs文件系統
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=15, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=74752, 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 74752 to 307200

 

[root@server ~]# umount  /mnt/                                              ##卸載/mnt
[root@server ~]# mkfs.ext4  /dev/vg0/lv0                               ##更改邏輯卷文件類型(
xfs不可縮減,ext4可縮減
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
76800 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7680 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912

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

[root@server ~]# mount /dev/vg0/lv0   /mnt/                                            ##再次掛載lv0邏輯卷
[root@server ~]# df                                                                                   ##查看掛載情況
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3159240   7314660  31% /
devtmpfs               469332       0    469332   0% /dev
tmpfs                  484920     140    484780   1% /dev/shm
tmpfs                  484920   12848    472072   3% /run
tmpfs                  484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-lv0   1176704    3600   1095280   1% /mnt
[root@server ~]# lvextend -L 1250M /dev/vg0/lv0                               ##擴展lv0邏輯卷
  Rounding size to boundary between physical extents: 1.22 GiB
  Extending logical volume lv0 to 1.22 GiB
  Logical volume lv0 successfully resized
[root@server ~]# resize2fs  /dev/vg0/lv0                            
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 320512 blocks long.
[root@server ~]# mkfs.ext4  /dev/vg0/lv0                                                ##重建文件系統
mke2fs 1.42.9 (28-Dec-2013)
/dev/vg0/lv0 is mounted; will not make a filesystem here!
[root@server ~]# lvextend -L 1300M /dev/vg0/lv0                                  ## 擴展lv0邏輯卷
  Extending logical volume lv0 to 1.27 GiB
  Logical volume lv0 successfully resized
[root@server ~]# resize2fs  /dev/vg0/lv0
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 332800 blocks long.

[root@server ~]# umount /mnt/                                                                        ##卸載/mnt下掛載的邏輯卷

3.LV邏輯捲縮減

[root@server ~]# 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: 11/84480 files (0.0% non-contiguous), 14406/332800 blocks
[root@server ~]# resize2fs  /dev/vg0/lv0  1000M                                                         ##縮減文件系統
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 256000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 256000 blocks long.
[root@server ~]# mount /dev/vg0/lv0   /mnt/                                                                  ##掛載邏輯卷
[root@server ~]# lvreduce -L 1000 /dev/vg0/lv0                                                           ##縮減邏輯卷至1000M

  WARNING: Reducing active and open logical volume to 1000.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 1000.00 MiB
  Logical volume lv0 successfully resized
[root@server ~]# pvmove /dev/vdb7 /dev/vdb8                                                     ##將/vdb7上的數據轉移到vdb8上,以使vdb7空閒
  /dev/vdb7: Moved: 12.2%
  /dev/vdb7: Moved: 100.0%
[root@server ~]# vgreduce  vg0 /dev/vdb7                                                            ##從物理卷組中移除物理卷/vdb7
  Removed "/dev/vdb7" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb7                                                                  ##移除/vdb7物理卷
  Labels on physical volume "/dev/vdb7" successfully wiped

4.LV邏輯卷快照

[root@server ~]# cd /mnt/
[root@server mnt]# rm -rf *                                                                    ##清空/mnt                                                     
[root@server mnt]# ls
[root@server mnt]# touch file{1..9}                                                      ##新建文件
[root@server mnt]# ls
file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@server mnt]# cd ~
[root@server ~]# umount /mnt/                                                             ##卸載/mnt
[root@server ~]# cd /mnt/
[root@server mnt]# ls                                                                             ##查看/mnt
[root@server mnt]# lvcreate  -L 50M -n /dev/vg0/lv0_snap -s  /dev/vg0/lv0                ##創建50M的lv0的快照
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "lv0_snap" created
[root@server mnt]# mount /dev/vg0/lv0_snap /mnt/                                                     ##將lv0的快照掛載到/mnt下
[root@server mnt]# ls
[root@server mnt]# cd -
/root
[root@server ~]# cd -                                                                                   ##重新進入文件目錄
/mnt
[root@server mnt]# ls                                                                                   ##可以查看到文件存在
file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@server mnt]# rm -rf *
[root@server mnt]# ls
[root@server mnt]# cd -
/root
[root@server ~]# umount /mnt/
[root@server ~]# lvremove /dev/vg0/lv0_snap                                             ##移除
Do you really want to remove active logical volume lv0_snap? [y/n]: y
  Logical volume "lv0_snap" successfully removed
[root@server ~]# lvcreate  -L 50M -n /dev/vg0/lv0_snap  -s  /dev/vg0/lv0      ##重建快照
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "lv0_snap" created
[root@server ~]# mount /dev/vg0/lv0_snap /mnt/
[root@server ~]# cd -
/mnt
[root@server mnt]# ls
file1  file2  file3  file4  file5  file6  file7  file8  file9

5.移除LV邏輯卷

[root@server ~]# umount /mnt                                                        ##卸載
[root@server ~]# lvremove /dev/vg0/lv0                                         ##移除邏輯卷lv0
Do you really want to remove active logical volume lv0? [y/n]: y
  Logical volume "lv0" successfully removed
[root@server ~]# vgremove vg0                                                      ##移除物理卷組vg0
  Volume group "vg0" successfully removed
[root@server ~]# pvremove /dev/vdb6                                            ##移除物理卷vdb6
  Labels on physical volume "/dev/vdb6" successfully wiped
[root@server ~]# pvremove /dev/vdb8                                             ##移除物理卷vdb8
  Labels on physical volume "/dev/vdb8" successfully wiped
[root@server ~]# fdisk /dev/vdb                                                         ##刪除分區
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): d
Partition number (1-8, default 8): 8
Partition 8 is deleted
Command (m for help): d
Partition number (1-7, default 7): 7
Partition 7 is deleted
Command (m for help): d
Partition number (1-6, default 6): 6
Partition 6 is deleted
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# fdisk -l                                                                            ##查看分區情況
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd5528270
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      821247      307200   83  Linux
/dev/vdb3          821248     1845247      512000   83  Linux
/dev/vdb4         1845248    20971519     9563136    5  Extended
/dev/vdb5         1847296     2871295      512000   83  Linux
[root@server ~]# partprobe                                                                ##同步分區表

完成以上操作就可以移除LV邏輯卷

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章