linux LVM分区和扩容

LVM分区流程图

在使用fdisk/parted等分区工具对硬盘分区之后,是否使用LVM由用户决定 :
1)可以直接对分区后的硬盘进行格式化操作,之后挂载分区;
2)也可以先用LVM逻辑卷管理工具对分区后的物理卷进行管理,之后再对逻辑卷进行格式化操作,之后挂载分区。( LVM的分区类型为8e)


LVM结构图

1. 手动添加物理硬盘

对硬盘进行分区

  1. 使用fdisk -l 查看添加的物理硬盘
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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: 2147 MB, 2147483648 bytes, 4194304 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/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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
  1. 使用fdisk /dev/vdb 进行分区
[root@collector13 ~]# 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 0x88cd1926.

Command (m for help): m
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)

Command (m for help): p

Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x88cd1926

   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-4194303999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999): 
Using default value 4194303999
Partition 1 of type Linux and of size 2 TiB 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.
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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: 2147 MB, 2147483648 bytes, 4194304 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/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x88cd1926

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  4194303999  2097150976   8e  Linux LVM
  1. 对分区执行LVM操作
[root@collector13 ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
[root@collector13 ~]# vgcreate vg_data /dev/vdb1
  Volume group "vg_data" successfully created
[root@collector13 ~]# lvcreate -l +100%FREE -n lv_data vg_data
  Logical volume "lv_data" created.
  
[root@collector11 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_data/lv_data
  LV Name                lv_data
  VG Name                vg_data
  LV UUID                dApUBU-pHWI-D4Ar-IvpK-GVnk-yq17-AcN6Q7
  LV Write Access        read/write
  LV Creation host, time collector11, 2018-10-16 01:53:24 -0400
  LV Status              available
  # open                 1
  LV Size                1.95 TiB
  Current LE             511999
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                32jIrc-Qdvd-7M8u-Dp3T-lEJW-HJ0j-wbBm38
  LV Write Access        read/write
  LV Creation host, time host-10-10-4-179, 2018-06-20 03:44:23 -0400
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                4AV4CF-25lk-RDt5-dnzi-KET7-wglP-fTaLWD
  LV Write Access        read/write
  LV Creation host, time host-10-10-4-179, 2018-06-20 03:44:24 -0400
  LV Status              available
  # open                 1
  LV Size                <17.47 GiB
  Current LE             4472
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  1. 格式化分区
[root@collector13 ~]# mkfs.ext4 /dev/vg_data/lv_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
131072000 inodes, 524286976 blocks
26214348 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2671771648
16000 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, 20480000, 23887872, 71663616, 78675968, 
    102400000, 214990848, 512000000

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

  1. 挂载分区到指定目录
[root@collector13 ~]# mkdir /data

[root@collector13 ~]# mount /dev/vg_data/lv_data /data
  1. 修改/etc/fstab 启动自动挂载硬盘
#
# /etc/fstab
# Created by anaconda on Wed Jun 20 03:44:25 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#

/dev/mapper/centos-swap none    swap    sw,comment=cloudconfig  0       0
/dev/vg_data/lv_data                     /data                     ext4    defaults       0 0

LVM扩容根目录

  1. 查看磁盘状态 fdisk -l
    如下linux根目录才20G,太小了,不能满足业务需求。新加了一个500G硬盘,用于扩容根目录。
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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/vdd: 536.9 GB, 536870912000 bytes, 1048576000 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
  1. 创建新的分区
    这部分参考上面有介绍
fdisk  /dev/hda

n

l        #选择逻辑分区,如果没有,则首先创建扩展分区,然后再添加逻辑分区(硬盘:最多四个分区P-P-P-P或P-P-P-E)

6        #分区号(从5开始),/dev/hda6

t      8e   #分区类型8e表示LVM分区

w        #写入分区表

partprobe   #重读分区表

mkfs.ext4 /dev/vg_data/lv_data  #格式化 ,如果是扩展根目录,则不需要格式化

partx /dev/hda #查看当前硬盘的分区表及使用情况
  1. 创建PV,扩容VG,LV
  1. 创建PV
[root@collector13 ~]# fdisk -l
Disk /dev/vdd: 536.9 GB, 536870912000 bytes, 1048576000 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: 0xaacdf342

 Device Boot      Start         End      Blocks   Id  System
/dev/vdd1            2048  1048575999   524286976   8e  Linux LVM
[root@collector13 ~]# pvcreate /dev/vdd1
Physical volume "/dev/vdd1" successfully created.

2)扩容LG
扩容前先查看先要扩容的VG。例如使用如下的VG Name:centos
把刚刚的硬盘分区挂载到要扩容的VG。

[root@collector13 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               10
  Allocated PE          4984
  PV UUID               St079E-R9o3-S5aA-FP0n-R0p3-QRxp-hgF02n

扩容VG

[root@collector13 ~]# vgextend centos /dev/vdd1
  Volume group "centos" successfully extended

3)扩容LV

[root@collector13 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from <17.47 GiB (4472 extents) to 517.50 GiB (132481 extents).
  Logical volume centos/root successfully resized.

或者可以使用如下命令
如果不希望把硬盘所有的空间全部分布可以定做分配
lvextend -L +100g /dev/centos/root

  1. 格式化 但是有报错信息
[root@collector13 ~]# resize2fs /dev/centos/root 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.

如果要扩容的分区是xfs文件系统类型,执行resize2fs时,可能会报错:
需要执行xfs_growfs命令执行扩展操作。

/dev/mapper/vg_data-lv_data  2.0T   81M  1.9T   1% /data
[root@collector13 ~]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4579328, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4579328 to 135660544
[root@collector13 ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos-root      518G  1.5G  517G   1% /
devtmpfs                      63G     0   63G   0% /dev
tmpfs                         63G     0   63G   0% /dev/shm
tmpfs                         63G  8.7M   63G   1% /run
tmpfs                         63G     0   63G   0% /sys/fs/cgroup
/dev/vda1                    497M  192M  306M  39% /boot
tmpfs                         13G     0   13G   0% /run/user/0
/dev/mapper/vg_data-lv_data  2.0T   81M  1.9T   1% /data

通过以上,根目录扩展完成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容