1. 程式人生 > 實用技巧 >20,lvm 使用簡介

20,lvm 使用簡介

1,新建立一塊硬碟
[root@centos7 ~]# 
[root@centos7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   10G  0 disk 
├─sda1            8:1    0  200M  0 part /boot
└─sda2            8:2    0  9.8G  0 part 
  ├─centos-root 253:0    0  9.3G  0 lvm  /
  └─centos-swap 253:1    0  512M  0 lvm  [SWAP]
sdb               8:16   0    3G  0 disk 
sr0              11:0    1 1024M  0 rom  
[root@centos7 ~]# 
[root@centos7 ~]# 

2,分割槽硬碟,建立一個擴充套件分割槽,然後分出3個邏輯分割槽
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x1f2362c8.

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f2362c8

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): e
Partition number (1-4, default 1): 
First sector (2048-6291455, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-6291455, default 6291455): 
Using default value 6291455
Partition 1 of type Extended and of size 3 GiB is set

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f2362c8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6291455     3144704    5  Extended

Command (m for help):n

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x9929aef0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6291455     3144704    5  Extended
/dev/sdb5            4096      106495       51200   83  Linux
/dev/sdb6          108544      210943       51200   83  Linux
/dev/sdb7          212992      315391       51200   83  Linux

Command (m for help): t
Partition number (1,5-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): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x9929aef0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6291455     3144704    5  Extended
/dev/sdb5            4096      106495       51200   83  Linux
/dev/sdb6          108544      210943       51200   83  Linux
/dev/sdb7          212992      315391       51200   8e  Linux LVM

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x9929aef0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6291455     3144704    5  Extended
/dev/sdb5            4096      106495       51200   8e  Linux LVM
/dev/sdb6          108544      210943       51200   8e  Linux LVM
/dev/sdb7          212992      315391       51200   8e  Linux LVM

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

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

---------------------------------------------------------------------上面操作是做好物理分割槽--------------------------

3,接下來做PV
[root@centos7 ~]# pvcreate /dev/sdb5 /dev/sdb6 /dev/sdb7
  Physical volume "/dev/sdb5" successfully created.
  Physical volume "/dev/sdb6" successfully created.
  Physical volume "/dev/sdb7" successfully created.
[root@centos7 ~]# 

4,接下來建立VG,vg繫結PV
[root@centos7 ~]# vgcreate vg1_demo  /dev/sdb5 /dev/sdb6
  Volume group "vg1_demo" successfully created
[root@centos7 ~]# 
vg建立好了後就可以把他當成一塊硬碟。

5,vg 建立好了,我們就把他當作成一塊硬碟,然後我們用lvcreate 對他分割槽
檢視vg的簡易資訊
[root@centos7 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  centos     1   2   0 wz--n-  9.80g     0 
  vg1_demo   2   0   0 wz--n- 96.00m 96.00m
[root@centos7 ~]#
[root@centos7 ~]# lvcreate -n lv1 -L +30M vg1_demo
  Rounding up size to full physical extent 32.00 MiB
  Logical volume "lv1" created. 
[root@centos7 ~]# 
[root@centos7 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  centos     1   2   0 wz--n-  9.80g     0 
  vg1_demo   2   1   0 wz--n- 96.00m 64.00m
[root@centos7 ~]#
6,接著對LV格式化
[root@centos7 ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos   -wi-ao----   9.30g                                                    
  swap centos   -wi-ao---- 512.00m                                                    
  lv1  vg1_demo -wi-a-----  32.00m                                                    
[root@centos7 ~]# 
[root@centos7 ~]# mkfs.ext4 /dev/vg1_demo/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577

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

[root@centos7 ~]# 
[root@centos7 ~]# 

7,掛載
[root@centos7 ~]# mkdir /app
[root@centos7 ~]# 
[root@centos7 ~]# mount /dev/vg1_demo/lv1 /app
[root@centos7 ~]# 
[root@centos7 ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   9.3G  1.1G  8.3G  12% /
devtmpfs                  982M     0  982M   0% /dev
tmpfs                     993M     0  993M   0% /dev/shm
tmpfs                     993M  8.7M  984M   1% /run
tmpfs                     993M     0  993M   0% /sys/fs/cgroup
/dev/sda1                 197M  103M   95M  53% /boot
tmpfs                     199M     0  199M   0% /run/user/0
/dev/mapper/vg1_demo-lv1   27M  779K   24M   4% /app
[root@centos7 ~]# 

8,重啟主機,掛載會丟失,需要寫入掛載資訊到/etc/fstab 檔案。


檢視pv資訊:pvs
檢視vg資訊:vgs
檢視lv資訊:lvs