【CentOS】物理ハードディスクを追加して理論パーティションを拡張

投稿者: | 2023-04-28

過去 2 日間、サーバーから突然エラーが報告されました:「デバイスに空き容量がありません。」 最初は心に留めていませんでした。 数日後、Apache サーバーもダウンしました。 必死になって、私はそれを解決し始めなければなりませんでした。

エラーはディスク容量不足が原因なので、まず頭に浮かぶのは不要なファイルを削除することです。 次に、次のコマンドを使用して古いカーネルをクリーンアップします。

package-cleanup --oldkernels

同時に、いくつかの一時ファイルも削除されます。 予想外に、数週間後、最度に同じエラーが報告されました。 これにより、物理ハードディスクの増設について考える必要があります。 内蔵ハードディスクを拡張する予定でしたが、余分なハードディスク拡張スロットがありませんでした。 いろいろ調べた後、東芝 6T 外付けハードドライブを購入しました。

また、増設案について、2 つの選択肢があります。 1 つは、既存のハードディスクの「/Home」をバックアップしてから増設のハードディスクに復元し、既存のハードディスクの「/Home」の領域を「/root」に割り当てる方法です。 これを行うと、回復エラーの問題が発生する可能性があるというリスクがあります。 最終的な選択肢は、増設用ハードディスクを 2 つの領域に分割し、既存のハードディスクの「/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

次に2つのパーティションを作成する目的は、「/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/root"上に反映
# 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

参考文献