[CentOS]增设物理硬盘并扩展理论分区

By | 2023-04-27

近两天,服务器突然报错:“No space left on device.”。刚开始并未放在心上。不期数日后,Apache服务器也宕掉了。无奈之下,只得着手解决。

由于报错是由于没有磁盘空间引起的,所以首先想到的是删除不要的文件。于是用以下命令对内核进行了清理。

package-cleanup --oldkernels

同时,还删除了一些临时文件。不期数周之后,再底出现相同的报错。这令人不得不思考对硬盘进行物理扩展。本打算扩展一个内置硬盘,不想没有多余的硬盘接口。一番查询之后,入手了一个东芝6T的外置硬盘。

此外,就扩展方案来说,有两个选择。一是把现有硬盘上的“/Home”进行备份,再恢复到追加的硬盘上,然后把现有硬盘的“/Home”的空间分配给“/root”。这样做的风险是可能遇到恢复出错的问题。最终选择的方案是将追加硬盘分为两个区,分别对现有硬盘的“/home”和“/root”进行扩展。

1 确认硬盘信息

接入之后,首先确认硬盘信息。

# fdisk -l
# 硬盘信息
Disk /dev/sdb: 6001.2 GB, 6001175126016 bytes, 11721045168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/recommended): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

2 对硬盘进行分区

确认硬盘为“/dev/sdb”后,接下来对硬盘进行分区。

# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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.
# 追加一个大小约为1T的分区
Command (help with m): n
Partition number (1-128, default 1):
First sector (34-11721045134, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-11721045134, default 11721045134): 2344209026
Created partition 1

# 追加一个大小约为5T的分区

Command (help with m): n
Partition number (2-128, default 2):
First sector (34-11721045134, default 2344210432):
Last sector, +sectors or +size{K,M,G,T,P} (2344210432-11721045134, default 11721045134):
Created partition 2


# 确认分区结果
Command (help with m): p

Disk /dev/sdb: 6001.2 GB, 6001175126016 bytes, 11721045168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/recommended): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX


#         Start          End    Size  Type            Name
 1         2048   2344209026    1.1T  Linux filesyste
 2   2344210432  11721045134    4.4T  Linux filesyste

# 保存分区结果
Command (help with m): w
The partition table has changed!

Call ioctl() to reload the partition table.
Synchronizing disk.

3 创建物理卷

在创建物理卷之前,首先确认分区的创建结果和当前物理卷的信息。可以看到当前物理卷的大小。

# 确认分区结果
# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2

# 确认当前物理卷的信息
# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               centos
  PV Size               <288.73 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              73914
  Free PE               0
  Allocated PE          73914
  PV UUID               XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

接下来,创建两个分区的目的是为了同时扩展“/dev/mapper/centos-root”和“/dev/mapper/centos-home”。

# 创建物理卷 /dev/sdb1
# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

# 创建物理卷 /dev/sdb2
# pvcreate /dev/sdb2
WARNING: xfs signature detected on /dev/sdb2 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb2.
  Physical volume "/dev/sdb2" successfully created.

4 扩展现有分区

在对现有逻辑卷进行扩展前,先查看一下现有逻辑卷的信息。

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:27 +0900
  LV Status              available
  # open                 2
  LV Size                7.75 GiB
  Current LE             1984
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:28 +0900
  LV Status              available
  # open                 1
  LV Size                180.00 GiB
  Current LE             46080
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:30 +0900
  LV Status              available
  # open                 1
  LV Size                1.19 TiB
  Current LE             312007
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

接下来将物理卷扩展到逻辑卷,并将扩展的逻辑卷分配给现有的逻辑卷。

# 将"/dev/sdb1"扩展到逻辑卷上
# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
# 将扩展的逻辑卷100%分配给现有的逻辑卷"/dev/mapper/centos-root"
# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from <100.98 GiB (25850 extents) to 1.19 TiB (312007 extents).
  Logical volume centos/root successfully resized.

# 将"/dev/sdb2"扩展到逻辑卷上
# vgextend centos /dev/sdb2
  Volume group "centos" successfully extended
# 将扩展的逻辑卷100%分配给现有的逻辑卷"/dev/mapper/centos-home"
# lvextend -l +100%FREE /dev/mapper/centos-home
  Size of logical volume centos/home changed from 180.00 GiB (46080 extents) to 4.54 TiB (1190712 extents).
  Logical volume centos/home successfully resized.

查看处理结果。从结果中可以看到,刚才创建的物理卷分别被分配给了逻辑卷“/dev/mapper/centos-root”和“/dev/mapper/centos-home”。

# lvdisplay

  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:27 +0900
  LV Status              available
  # open                 2
  LV Size                7.75 GiB
  Current LE             1984
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:28 +0900
  LV Status              available
  # open                 1
  LV Size                4.54 TiB
  Current LE             1190712
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXX
  LV Write Access        read/write
  LV Creation host, time localhost, XXXX-04-20 22:33:30 +0900
  LV Status              available
  # open                 1
  LV Size                1.19 TiB
  Current LE             312007
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

5 将处理结果反映到文件系统

# 将扩展结果反映到文件系统"/dev/centos/root"上
# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=9, agsize=3276800 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=26470400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 26470400 to 319495168

# 将扩展结果反映到文件系统"/dev/centos/home"上
# xfs_growfs /dev/centos/home
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=11796480 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=47185920, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=23040, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 47185920 to 1219289088

以下是用”df”命令的确认结果。从中可以看到,”/root”和”/home”均被扩展到指定的大小。

# df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
devtmpfs                  7.7G     0  7.7G    0% /dev
tmpfs                     7.7G  572K  7.7G    1% /dev/shm
tmpfs                     7.7G   12M  7.7G    1% /run
tmpfs                     7.7G     0  7.7G    0% /sys/fs/cgroup
/dev/mapper/centos-root   1.2T   99G  1.1T    9% /
/dev/loop2                 45M   45M     0  100% /var/lib/snapd/snap/certbot/2913
/dev/loop0                 54M   54M     0  100% /var/lib/snapd/snap/snapd/18933
/dev/loop3                 64M   64M     0  100% /var/lib/snapd/snap/core20/1852
/dev/loop7                128K  128K     0  100% /var/lib/snapd/snap/bare/5
/dev/loop5                 90M   90M     0  100% /var/lib/snapd/snap/lumi/199
/dev/loop6                165M  165M     0  100% /var/lib/snapd/snap/gnome-3-28-1804/198
/dev/loop1                 92M   92M     0  100% /var/lib/snapd/snap/gtk-common-themes/1535
/dev/loop4                165M  165M     0  100% /var/lib/snapd/snap/gnome-3-28-1804/194
/dev/loop8                 50M   50M     0  100% /var/lib/snapd/snap/snapd/18596
/dev/loop10                44M   44M     0  100% /var/lib/snapd/snap/certbot/2836
/dev/loop11                64M   64M     0  100% /var/lib/snapd/snap/core20/1828
/dev/loop12                56M   56M     0  100% /var/lib/snapd/snap/core18/2714
/dev/loop9                 56M   56M     0  100% /var/lib/snapd/snap/core18/2721
/dev/sda5                1014M  206M  809M   21% /boot
/dev/sda2                  96M   36M   61M   38% /boot/efi
/dev/mapper/centos-home   4.6T   13G  4.6T    1% /home
tmpfs                     1.6G   72K  1.6G    1% /run/user/1000
tmpfs                     1.6G  8.0K  1.6G    1% /run/user/0
tmpfs                     1.6G   12K  1.6G    1% /run/user/42

6 检查文件系统类型

# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Apr 20 22:33:31 XXXX
#
# 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-root /                       xfs     defaults        0 0
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX /boot                   xfs     defaults        0 0
UUID=XXXX-XXXX          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

参考文献