Linux系统与服务构建(一)

LVM逻辑卷的使用

思路整理:

  1. 添加硬盘
  2. 将添加的硬盘进行分区 fdisk
  3. 创建物理卷 pvcreat
  4. 将创建的物理卷添加到卷组 vgcreat
  5. 创建逻辑卷 lvcreate
  6. 扫描创建的逻辑卷 lvscan
  7. 格式化逻辑卷 mkfs.ext4
  8. 扩容逻辑卷时要先识别 resize2fs

1.在VMware Workstation中添加硬盘

Linux系统与服务构建(一)
Linux系统与服务构建(一)
Linux系统与服务构建(一)
Linux系统与服务构建(一)
用命令查看磁盘

[root@server ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.5G  0 part /
sdb      8:16   0   20G  0 disk 
sr0     11:0    1    4G  0 rom 

2.LVM逻辑卷的使用

创建物理卷
在创建物理卷之前,需要对磁盘进行分区,首先使用fdisk命令对sdb进行分区操作分出两个大小为5GB的分区,

[root@server ~]# 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 0x394e7c9d.

Command (m for help): p

Disk /dev/sdb: 21.5 	GB, 21474836480 bytes, 41943040 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: 0x394e7c9d

   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): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (10487808-41943039, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x394e7c9d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    20973567     5242880   83  Linux

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

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

命令帮助

Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition‘s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

查看分区

[root@server ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.5G  0 part /
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    5G  0 part 
└─sdb2   8:18   0    5G  0 part 
sr0     11:0    1    4G  0 rom  

创建物理卷

 [root@server opt]# pvcreate /dev/sdb1 /dev/sdb2 
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

查看物理卷的简单信息

[root@server opt]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1       lvm2 ---  5.00g 5.00g
  /dev/sdb2       lvm2 ---  5.00g 5.00g

查看物理卷的详细信息

[root@server opt]# pvdisplay
  "/dev/sdb2" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               pxvHEu-8yek-95ue-f1cw-Te6t-SrI0-PkqGVA
   
  "/dev/sdb1" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               QoT1p9-wLlk-XU6b-xVV2-PSHe-61Ch-bFpYbP

如果没有pvcreate命令

查看pvcreate在哪个包里
[root@server opt]# yum provides */pvcreate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
1:bash-completion-2.1-6.el7.noarch : Programmable completion for Bash
Repo        : centos
Matched from:
Filename    : /usr/share/bash-completion/completions/pvcreate



7:lvm2-2.02.130-5.el7.x86_64 : Userland logical volume management tools
Repo        : centos
Matched from:
Filename    : /usr/sbin/pvcreate



7:lvm2-2.02.130-5.el7.x86_64 : Userland logical volume management tools
Repo        : @centos
Matched from:
Filename    : /usr/sbin/pvcreate

安装包含pvcreate命令的包
[root@server opt]# yum -y install lvm2

创建卷组
使用刚刚创建好的物理卷创建名为myvg的卷组

[root@server opt]# vgcreate myvg /dev/sdb[1-2]
  Volume group "myvg" successfully created

查看卷组简单信息

[root@server opt]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   0   0 wz--n- 9.99g 9.99g

查看卷组详细信息

[root@server opt]# vgdisplay
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               9.99 GiB
  PE Size               4.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               gxqcgy-RGOk-RbjJ-tGLT-tHro-VyXp-QzeUPJ

PE:当多个逻辑卷组合成一个卷组后,LVM会在所有的物理卷上做类似格式化的工作,将每个物理卷切成一个一个的空间,这一个空间就叫做PE,他的默认大小是4MB

由于受到内核限制的原因,一个逻辑卷最多能包含65536个PE,如果需要逻辑卷有更大的容量就需要指定更大的PE

删除卷组,并且重新创建卷组,指定PE大小为16MB:

[root@server opt]# vgremove myvg
  Volume group "myvg" successfully removed
[root@server opt]# vgcreate -s 16m myvg /dev/sdb[1-2]
  Volume group "myvg" successfully created
[root@server opt]# vgdisplay
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               9.97 GiB
  PE Size               16.00 MiB
  Total PE              638
  Alloc PE / Size       0 / 0   
  Free  PE / Size       638 / 9.97 GiB
  VG UUID               VcaHP1-tXcZ-EaTw-hm72-Bdxu-DLme-s0GE76

在/dev/sdb上在分一个分区,并把分区添加到myvg中

M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.5G  0 part /
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    5G  0 part 
├─sdb2   8:18   0    5G  0 part 
└─sdb3   8:19   0    5G  0 part 
sr0     11:0    1    4G  0 rom  /opt/centos
[root@server opt]# pvcreate /dev/sdb3 
  Physical volume "/dev/sdb3" successfully created
[root@server opt]# vgextend myvg /dev/sdb3 
  Volume group "myvg" successfully extended
[root@server opt]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree 
  myvg   3   0   0 wz--n- 14.95g 14.95g
[root@server opt]# vgdisplay
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               14.95 GiB
  PE Size               16.00 MiB
  Total PE              957
  Alloc PE / Size       0 / 0   
  Free  PE / Size       957 / 14.95 GiB
  VG UUID               VcaHP1-tXcZ-EaTw-hm72-Bdxu-DLme-s0GE76

创建逻辑卷
创建逻辑卷,名称为mylv,大小为5GB

[root@server opt]# lvcreate -L +5G -n mylv myvg
  Logical volume "mylv" created.
[root@server opt]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 5.00g          

扫描逻辑卷

[root@server opt]# lvscan
  ACTIVE            ‘/dev/myvg/mylv‘ [5.00 GiB] inherit

使用ext4文件系统格式化逻辑卷mylv

[root@server opt]# mkfs.ext4 /dev/mapper/myvg-mylv 
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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


将逻辑卷挂载到/mnt下,并验证

[root@server opt]# mount /dev/mapper/myvg-mylv /mnt/
[root@server opt]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               48G  892M   47G   2% /
devtmpfs               984M     0  984M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.5M  985M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1              497M  103M  395M  21% /boot
tmpfs                  199M     0  199M   0% /run/user/0
/dev/sr0               4.1G  4.1G     0 100% /opt/centos
/dev/mapper/myvg-mylv  4.8G   20M  4.6G   1% /mnt

逻辑卷扩容

[root@server opt]# lvcreate -L +1G /dev/mapper/myvg-mylv 
  Volume group name expected (no slash)
  Run `lvcreate --help‘ for more information.
[root@server opt]# lvextend -L +1G /dev/mapper/myvg-mylv 
  Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
  Logical volume mylv successfully resized.
[root@server opt]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 6.00g                                                    
[root@server opt]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               48G  892M   47G   2% /
devtmpfs               984M     0  984M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.5M  985M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1              497M  103M  395M  21% /boot
tmpfs                  199M     0  199M   0% /run/user/0
/dev/sr0               4.1G  4.1G     0 100% /opt/centos
/dev/mapper/myvg-mylv  4.8G   20M  4.6G   1% /mnt
[root@server opt]# resize2fs /dev/mapper/myvg-mylv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/myvg-mylv is now 1572864 blocks long.

[root@server opt]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               48G  892M   47G   2% /
devtmpfs               984M     0  984M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.5M  985M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1              497M  103M  395M  21% /boot
tmpfs                  199M     0  199M   0% /run/user/0
/dev/sr0               4.1G  4.1G     0 100% /opt/centos
/dev/mapper/myvg-mylv  5.8G   20M  5.5G   1% /mnt

Linux系统与服务构建(一)

上一篇:python heapq 有序队列


下一篇:征途