LVM创建及管理


LVM创建及管理

安装lvm

yum install -y lvm

查看磁盘

[root@template ~]# 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: 0x000beec6

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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

初始化

 
[root@template ~]# 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.

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

Command (m for help): o
Building a new DOS disklabel with disk identifier 0x01d037f1.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): 
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.

将磁盘添加到LVM PV

[root@template ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.

列出所有PV

[root@template ~]# pvscan
  PV /dev/vda2   VG centos          lvm2 [<9.00 GiB / 0    free]
  PV /dev/vdb1                      lvm2 [<50.00 GiB]
  Total: 2 [<59.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [<50.00 GiB]

显示pv更多信息

[root@template ~]# pvdisplay /dev/vdb1
  "/dev/vdb1" is a new physical volume of "<50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               <50.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4kmSEn-kBZG-vMJc-T03x-aWfO-dEes-5prCUL

创建卷组:使用尽可能多的PV来创建VG

[root@template ~]# vgcreate share /dev/vdb1
  Volume group "share" successfully created

列出所有VG

[root@template ~]# vgscan
  Reading volume groups from cache.
  Found volume group "share" using metadata type lvm2
  Found volume group "centos" using metadata type lvm2

显示vg更多信息

[root@template ~]# vgdisplay share
  --- Volume group ---
  VG Name               share
  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               <50.00 GiB
  PE Size               4.00 MiB
  Total PE              12799
  Alloc PE / Size       0 / 0   
  Free  PE / Size       12799 / <50.00 GiB
  VG UUID               t9gVEp-02O6-IK40-oNxs-fdFC-nuA3-Y5NgNU

创建逻辑卷:使用VG创建任意数量的LV

[root@template ~]# lvcreate --size 49G --name vg_data share
  Logical volume "vg_data" created.

列出所有LV

[root@template ~]# lvscan
  ACTIVE            '/dev/share/vg_data' [49.00 GiB] inherit
  ACTIVE            '/dev/centos/swap' [1.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [<8.00 GiB] inherit

格式化

[root@template ~]# mkfs.ext4 /dev/share/vg_data
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
3211264 inodes, 12845056 blocks
642252 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
392 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, 
	4096000, 7962624, 11239424

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

创建需要挂载到的目录

[root@template ~]# mkdir -p /data

挂载

[root@template ~]# mount /dev/mapper/share-vg_data /data

查看

[root@template ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   5.7G     0  5.7G   0% /dev
tmpfs                      5.7G     0  5.7G   0% /dev/shm
tmpfs                      5.7G  8.6M  5.7G   1% /run
tmpfs                      5.7G     0  5.7G   0% /sys/fs/cgroup
/dev/mapper/centos-root    8.0G  1.8G  6.3G  22% /
/dev/vda1                 1014M  231M  784M  23% /boot
tmpfs                      1.2G     0  1.2G   0% /run/user/0
/dev/mapper/share-vg_data   49G   53M   46G   1% /data

开机自动挂载

[root@template ~]# vim /etc/fstab

/dev/mapper/share-vg_data  /data               ext4     defaults        0 0 

增加100G到/data

[root@vmhost2 ~]# lvextend --size +500G --resizefs /dev/mapper/VGsdb1T-vm_main
  Size of logical volume VGsdb1T/vm_main changed from 400.00 GiB (102400 extents) to 900.00 GiB (230400 extents).
  Logical volume VGsdb1T/vm_main successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/VGsdb1T-vm_main is mounted on /data; on-line resizing required
old_desc_blocks = 50, new_desc_blocks = 113
The filesystem on /dev/mapper/VGsdb1T-vm_main is now 235929600 blocks long.

查看

df -h
lvm