通过卷组方式实现存储扩容

前提:

  • 物理上需要先将扩容磁盘挂载在对应的机器上
  • 第一块盘挂载至/data/ 目录的挂载方式为卷组

一.创建LVM(第一块盘已卷组方式挂载至 /data)

1.首先通过fdisk -l 或 lsblk 查看磁盘的属性,找到要添加的磁盘名称

vdc是手动挂载到虚机上的一块,执行命令如下:

root@newsqltest:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   40G  0 disk 
|-vda1 252:1    0  571M  0 part /boot
`-vda2 252:2    0 39.5G  0 part /
vdb    252:16   0   64M  0 disk 
root@newsqltest:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   40G  0 disk 
|-vda1 252:1    0  571M  0 part /boot
`-vda2 252:2    0 39.5G  0 part /
vdb    252:16   0   64M  0 disk 
vdc    252:32   0   10G  0 disk 
root@newsqltest:~# fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x9b600047

Device     Boot   Start      End  Sectors  Size Id Type
/dev/vda1  *       2048  1171455  1169408  571M 83 Linux
/dev/vda2       1171456 83886046 82714591 39.5G 83 Linux


Disk /dev/vdb: 64 MiB, 67108864 bytes, 131072 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
Disklabel type: dos
Disk identifier: 0x00000000


Disk /dev/vdc: 10 GiB, 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

2.使用fdisk将磁盘进行逻辑分区

fdisk /dev/vdc

m来查看命令帮助

n添加一张新的partition,划分分区,以及分区的大小,这里我都使用默认值,创建一个分区

w命令保存并退出

root@newsqltest:~# fdisk /dev/vdc

Welcome to fdisk (util-linux 2.31.1).
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.
Created a new DOS disklabel with disk identifier 0xbb7719f5.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@newsqltest:~# fdisk -l
......
Device     Boot Start      End  Sectors Size Id Type
/dev/vdc1        2048 20971519 20969472  10G 83 Linux


3.使用lvm来管理这个分区

1).首先需要安装lvm

  apt-get install lvm2

2).创建 Physical Volume(PV)

  pvcreate /dev/vdc1

  成功之后可以通过pvdisplay查看信息

root@newsqltest:~# pvcreate /dev/vdc1
  Physical volume "/dev/vdc1" successfully created.
root@newsqltest:~# pvdisplay
  "/dev/vdc1" is a new physical volume of "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdc1
  VG Name               
  PV Size               <10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               5FxevL-aneV-Xhe6-pFEv-b3pH-l4v4-Gb6TeA

3).创建 Volume Group (VG).

  vgcreate vg-newsql /dev/vdc1

root@newsqltest:~# vgcreate vg-newsql /dev/vdc1
  Volume group "vg-newsql" successfully created
root@newsqltest:~# vgscan
  Reading volume groups from cache.
  Found volume group "vg-newsql" using metadata type lvm2

4).创建Logical Volumes (LV).

lvcreate -n lv-newsql -l 100%VG vg-newsql(将所有的vg-newsql的内存都给这个lv-newsql逻辑卷)

root@newsqltest:~# lvcreate -n lv-newsql -l 100%VG vg-newsql
  Logical volume "lv-newsql" created.
root@newsqltest:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg-newsql/lv-newsql
  LV Name                lv-newsql
  VG Name                vg-newsql
  LV UUID                bMsyAX-xatH-cdTY-JbzE-4ao5-QR6h-j4Z7ON
  LV Write Access        read/write
  LV Creation host, time newsqltest, 2021-01-27 16:09:54 +0800
  LV Status              available
  # open                 0
  LV Size                <10.00 GiB
  Current LE             2559
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


4.格式化并挂载逻辑卷至/data

1).第一步格式化逻辑卷

mkfs.ext4 /dev/vg-newsql/lv-newsql

2).将逻辑卷挂载到home文件夹下面

mount /dev/vg-newsql/lv-newsql /data

root@newsqltest:~# mkfs.ext4 /dev/vg-newsql/lv-newsql
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2620416 4k blocks and 655360 inodes
Filesystem UUID: a667f950-9b91-45ce-9931-8fb147173054
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

root@newsqltest:~# mount /dev/vg-newsql/lv-newsql /data
mount: /data: mount point does not exist.
root@newsqltest:~# mkdir /data
root@newsqltest:~# mount /dev/vg-newsql/lv-newsql /data
root@newsqltest:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               3.9G     0  3.9G   0% /dev
tmpfs                              798M  3.2M  795M   1% /run
/dev/vda2                           39G  2.8G   35G   8% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                          547M   77M  430M  16% /boot
tmpfs                              798M     0  798M   0% /run/user/0
/dev/mapper/vg--newsql-lv--newsql  9.8G   37M  9.3G   1% /data


至此,10G的盘已卷组的方式挂载至/data目录

二.扩容

1.添加磁盘
root@newsqltest:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                       252:0    0   40G  0 disk 
|-vda1                    252:1    0  571M  0 part /boot
`-vda2                    252:2    0 39.5G  0 part /
vdb                       252:16   0   64M  0 disk 
vdc                       252:32   0   10G  0 disk 
`-vdc1                    252:33   0   10G  0 part 
  `-vg--newsql-lv--newsql 253:0    0   10G  0 lvm  /data
vdd                       252:48   0   10G  0 disk
2. 格式化磁盘
1).当磁盘大小小于2TB的时候,同上面的步骤 一.2

fdisk /dev/vdd

m来查看命令帮助

n添加一张新的partition,划分分区,以及分区的大小,这里我都使用默认值,创建一个分区

w命令保存并退出

2)当大于2TB的时候,使用如下命令:
a.编辑/dev/sda磁盘

parted /dev/vde

b.设立磁盘分区类型

mklabel gpt

c.设置默认单位为TB、GB

unit TB

d.创建分区大小

mkpart primary 0 0 or mkpart primary 0.00TB 2.10TB

e.quit
f.格式化磁盘

mkfs.ext4 /dev/vde

root@newsqltest:~# parted /dev/vde
GNU Parted 3.2
Using /dev/vde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) mklabel gpt                                                      
(parted) unit TB                                                          
(parted) mkpart primary 0 0 or mkpart primary 0.00TB 2.10TB               
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
Warning: You requested a partition from 0.00TB to 2.10TB (sectors 0..4101562500).
The closest location we can manage is 0.00TB to 0.00TB (sectors 34..2047).
Is this still acceptable to you?
Yes/No? yes                                                               
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore                                                     
(parted) quit                                                             
Information: You may need to update /etc/fstab.

root@newsqltest:~# mkfs.ext4 /dev/vde                                     
mke2fs 1.44.1 (24-Mar-2018)
Found a gpt partition table in /dev/vde
Proceed anyway? (y,N) y
Creating filesystem with 550502400 4k blocks and 137625600 inodes
Filesystem UUID: a08b03f2-05b7-4029-b317-0e07f93fb6d2
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 (262144 blocks): done
Writing superblocks and filesystem accounting information: done 
2.创建pv卷,同上面的步骤一.3.2)

pvcreate /dev/vdd1

3.扩展物理硬盘到同一个卷组里面,每个组管理不同物理磁盘:

vgextend vg-newsql /dev/vdd1

root@newsqltest:~# vgextend vg-newsql /dev/vdd1
  Volume group "vg-newsql" successfully extended
4.从卷组里面向逻辑卷里面分配空间,

lvextend -l +100%FREE /dev/mapper/vg–newsql-lv–newsql(向逻辑卷里面分配空间)

root@newsqltest:~# lvextend -l +100%FREE /dev/mapper/vg--newsql-lv--newsql
  Size of logical volume vg-newsql/lv-newsql changed from <10.00 GiB (2559 extents) to 19.99 GiB (5118 extents).
  Logical volume vg-newsql/lv-newsql successfully resized.
5.重启逻辑卷使其生效

resize2fs /dev/mapper/vg–newsql-lv–newsql

root@newsqltest:~# resize2fs /dev/mapper/vg--newsql-lv--newsql
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/vg--newsql-lv--newsql is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/vg--newsql-lv--newsql is now 5240832 (4k) blocks long.

6.查看:

df -h
  当发现该逻辑卷(resize2fs /dev/mapper/vg–newsql-lv–newsql)的空间增大时,表示添加成功。

root@newsqltest:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               3.9G     0  3.9G   0% /dev
tmpfs                              798M  3.2M  795M   1% /run
/dev/vda2                           39G  2.8G   35G   8% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                          547M   77M  430M  16% /boot
/dev/mapper/vg--newsql-lv--newsql   20G   44M   19G   1% /data
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值