Linux 服务器扩盘 终极版

Posted 闭关苦炼内功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 服务器扩盘 终极版相关的知识,希望对你有一定的参考价值。

大象装冰箱,3步走

  1. 打开冰箱门
  2. 把大象放冰箱里边
  3. 关上冰箱门

对于大象装冰箱3步走,扩盘也是这样

文章目录

那本文就跟大家分享一篇 Linux 服务器扩盘终极版


1. 扩盘前

  • 查看磁盘分区
[root@pc185 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  500G  0 disk
sdc      8:32   0  500G  0 disk
sda      8:0    0  200G  0 disk
├─sda2   8:2    0 79.8G  0 part /
└─sda1   8:1    0  200M  0 part /boot/efi
[root@pc185 ~]#
  • 查看磁盘使用率、文件系统格式
[root@pc185 ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  4.0G     0  4.0G   0% /dev
tmpfs          tmpfs     4.0G     0  4.0G   0% /dev/shm
tmpfs          tmpfs     4.0G   27M  4.0G   1% /run
tmpfs          tmpfs     4.0G     0  4.0G   0% /sys/fs/cgroup
/dev/sda2      ext4       79G  4.4G   71G   6% /
/dev/sda1      vfat      200M  4.5M  196M   3% /boot/efi
tmpfs          tmpfs     809M     0  809M   0% /run/user/0
[root@pc185 ~]#

2. 扩盘进行时

  • 硬盘分区
[root@pc185 ~]# 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 0x5b5822af.

Command (m for help): p

Disk /dev/sdb: 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: 0x5b5822af

   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-1048575999, default 2048):
Using default value 2048
Last sector, +sectors or +sizeK,M,G (2048-1048575999, default 1048575999                                                                               ):
Using default value 1048575999
Partition 1 of type Linux and of size 500 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@pc185 ~]# 
  • 查看分区
[root@pc185 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  500G  0 disk
└─sdb1   8:17   0  500G  0 part
sda      8:0    0  200G  0 disk
├─sda2   8:2    0 79.8G  0 part /
└─sda1   8:1    0  200M  0 part /boot/efi
[root@pc185 ~]# 
  • 格式化磁盘(可能需要 Fix 修复)
[root@pc185 ~]# parted -l
Error: The backup GPT table is not at the end of the disk, as it should be                                                                               .
This might mean that another operating system believes the disk is smaller                                                                               .
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? y
parted: invalid token: y
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/sda appears to be used,
you can fix the GPT to use all of the space (an extra 251658240 blocks) or
continue with the current setting?
Fix/Ignore? Fix
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name                  Flags
 1      1049kB  211MB   210MB   fat16        EFI System Partition  boot
 2      211MB   85.9GB  85.7GB  ext4


Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  537GB  537GB  primary


[root@pc185 ~]#
  • 不重启服务器,重新读取分区表
[root@pc185 ~]# partprobe
[root@pc185 ~]#
  • 查看物理卷/卷组/逻辑卷
[root@pc185 ~]# pvs
[root@pc185 ~]# vgs
[root@pc185 ~]# lvs
[root@pc185 ~]#
  • 新建物理卷
[root@pc185 ~]# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.
[root@pc185 ~]#
  • 查看物理卷
[root@pc185 ~]# pvs
  PV         VG Fmt  Attr PSize    PFree
  /dev/sdb1     lvm2 ---  <500.00g <500.00g
  /dev/sdc1     lvm2 ---  <500.00g <500.00g
[root@pc185 ~]#
  • 新建卷组
[root@pc185 ~]# vgcreate vgdata /dev/sdb1 /dev/sdc1
  Volume group "vgdata" successfully created
[root@pc185 ~]#
  • 查看卷组
[root@pc185 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  vgdata   2   0   0 wz--n- 999.99g 999.99g
[root@pc185 ~]# 
  • 新建逻辑卷
[root@pc185 ~]# lvcreate -L 999.99g -n lvdata vgdata
  Rounding up size to full physical extent 999.99 GiB
  Logical volume "lvdata" created.
[root@pc185 ~]#
  • 查看逻辑卷
[root@pc185 ~]# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvdata vgdata -wi-ao---- 999.99g
[root@pc185 ~]#
  • 查看磁盘分区UUID
[root@pc185 ~]# blkid
/dev/sdb1: UUID="doIIWe-9p24-QK3k-Q5kY-6ZgC-X52N-OmyKcV" TYPE="LVM2_member"
/dev/sda1: SEC_TYPE="msdos" UUID="0CB8-A381" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="61519160-ec9f-4bd0-8d93-9844000caa56"
/dev/sda2: UUID="1e61014d-fcd9-4958-9f55-899ffacf7faf" TYPE="ext4" PARTUUID="ff7f7d6f-0ecf-4cd3-a8a1-b467b4a4ffa8"
/dev/sdc1: UUID="VocQNO-bfcl-fXrp-DkfS-sjji-DUAJ-OLoSqe" TYPE="LVM2_member"
[root@pc185 ~]#
  • 格式化逻辑卷为 xfs 文件系统
[root@pc185 ~]# mkfs.xfs /dev/vgdata/lvdata
meta-data=/dev/vgdata/lvdata     isize=512    agcount=4, agsize=65535488 b                                                                               lks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262141952, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=127999, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@pc185 ~]#
  • 查看磁盘分区UUID
[root@pc185 ~]# blkid
/dev/sdb1: UUID="doIIWe-9p24-QK3k-Q5kY-6ZgC-X52N-OmyKcV" TYPE="LVM2_member"
/dev/sda1: SEC_TYPE="msdos" UUID="0CB8-A381" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="61519160-ec9f-4bd0-8d93-9844000caa56"
/dev/sda2: UUID="1e61014d-fcd9-4958-9f55-899ffacf7faf" TYPE="ext4" PARTUUID="ff7f7d6f-0ecf-4cd3-a8a1-b467b4a4ffa8"
/dev/mapper/vgdata-lvdata: UUID="36e7b9b6-cb04-4ec0-8104-5d0eec38fd39" TYPE="xfs"
[root@pc185 ~]#

/dev/mapper/vgdata-lvdata: UUID="36e7b9b6-cb04-4ec0-8104-5d0eec38fd39" TYPE="xfs"
关键信息逻辑卷分区 UUID

  • 创建挂载目录 /datafs
[root@pc185 ~]# mkdir /datafs
[root@pc185 ~]#
  • 查看系统初始 /etc/fstab 文件
[root@pc185 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Jul 30 15:03:44 2020
#
# 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
#
UUID=1e61014d-fcd9-4958-9f55-899ffacf7faf /                       ext4    defaults        1 1
UUID=0CB8-A381          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
[root@pc185 ~]#
  • 配置 /etc/fstab 文件

关键一行
UUID=36e7b9b6-cb04-4ec0-8104-5d0eec38fd39 /datafs xfs defaults 1 1

[root@pc185 ~]# vim /etc/fstab
[root@pc185 ~]#
[root@pc185 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Jul 30 15:03:44 2020
#
# 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
#
UUID=1e61014d-fcd9-4958-9f55-899ffacf7faf /                       ext4    defaults        1 1
UUID=0CB8-A381          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
UUID=36e7b9b6-cb04-4ec0-8104-5d0eec38fd39 /datafs                 xfs     defaults        1 1
[root@pc185 ~]#
  • 把逻辑卷挂载到 /datafs 上
[root@pc185 ~]# mount -a
[root@pc185 ~]#

3. 扩盘后

  • 查看分区
[root@pc185 ~]# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb                 8:16   0  500G  0 disk
└─sdb1              8:17   0  500G  0 part
  └─vgdata-lvdata 253:0    0 500G  0 lvm  /datafs
sda                 8:0    0  200G  0 disk
├─sda2              8:2    0 79.8G  0 part /
└─sda1              8:1    0  200M  0 part /boot/efi
[root@pc185 ~]#
  • 查看文件系统类型、使用率
[root@pc185 ~]# df -hT
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  4.0G     0  4.0G   0% /dev
tmpfs                     tmpfs     4.0G     0  4.0G   0% /dev/shm
tmpfs                     tmpfs     4.0G   27M  4.0G   1% /run
tmpfs                     tmpfs     4.0G     0  4.0G   0% /sys/fs/cgroup
/dev/sda2                 ext4       79G  4.4G   71G   6% /
/dev/sda1                 vfat      200M  4.5M  196M   3% /boot/efi
tmpfs                     tmpfs     809M     0  809M   0% /run/user/0
/dev/mapper/vgdata-lvdata xfs      500G   33M 500G   1% /datafs
[root@pc185 ~]#

小伙伴们,你学废了么?

我们下期见,拜拜!

以上是关于Linux 服务器扩盘 终极版的主要内容,如果未能解决你的问题,请参考以下文章

linux服务器kill -9干掉服务终极版脚本

linux服务器kill -9干掉服务终极版脚本

一键获取 Linux 服务器网络 IP 地址 终极版 Shell 脚本

关于 GK盘,扩盘在storage和os两侧的联系

CentOS扩盘

面向对象 初级版 (Preview) 未完